mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-07 22:20:24 +00:00
Conver to regular Go vendor + dep tool
This commit is contained in:
+4
@@ -0,0 +1,4 @@
|
||||
This is the official list of JsonConfigReader authors for copyright purposes.
|
||||
|
||||
* DisposaBoy `https://github.com/DisposaBoy`
|
||||
* Steven Osborn `https://github.com/steve918`
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
Copyright (c) 2012 The JsonConfigReader Authors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
JsonConfigReader is a proxy for [golang's io.Reader](http://golang.org/pkg/io/#Reader) that strips line comments and trailing commas, allowing you to use json as a *reasonable* config format.
|
||||
|
||||
Comments start with `//` and continue to the end of the line.
|
||||
|
||||
If a trailing comma is in front of `]` or `}` it will be stripped as well.
|
||||
|
||||
|
||||
Given `settings.json`
|
||||
|
||||
{
|
||||
"key": "value", // k:v
|
||||
|
||||
// a list of numbers
|
||||
"list": [1, 2, 3],
|
||||
}
|
||||
|
||||
|
||||
You can read it in as a *normal* json file:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/DisposaBoy/JsonConfigReader"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var v interface{}
|
||||
f, _ := os.Open("settings.json")
|
||||
// wrap our reader before passing it to the json decoder
|
||||
r := JsonConfigReader.New(f)
|
||||
json.NewDecoder(r).Decode(&v)
|
||||
fmt.Println(v)
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package JsonConfigReader
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
type state struct {
|
||||
r io.Reader
|
||||
br *bytes.Reader
|
||||
}
|
||||
|
||||
func isNL(c byte) bool {
|
||||
return c == '\n' || c == '\r'
|
||||
}
|
||||
|
||||
func isWS(c byte) bool {
|
||||
return c == ' ' || c == '\t' || isNL(c)
|
||||
}
|
||||
|
||||
func consumeComment(s []byte, i int) int {
|
||||
if i < len(s) && s[i] == '/' {
|
||||
s[i-1] = ' '
|
||||
for ; i < len(s) && !isNL(s[i]); i += 1 {
|
||||
s[i] = ' '
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func prep(r io.Reader) (s []byte, err error) {
|
||||
buf := &bytes.Buffer{}
|
||||
_, err = io.Copy(buf, r)
|
||||
s = buf.Bytes()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
i := 0
|
||||
for i < len(s) {
|
||||
switch s[i] {
|
||||
case '"':
|
||||
i += 1
|
||||
for i < len(s) {
|
||||
if s[i] == '"' {
|
||||
i += 1
|
||||
break
|
||||
} else if s[i] == '\\' {
|
||||
i += 1
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
case '/':
|
||||
i = consumeComment(s, i+1)
|
||||
case ',':
|
||||
j := i
|
||||
for {
|
||||
i += 1
|
||||
if i >= len(s) {
|
||||
break
|
||||
} else if s[i] == '}' || s[i] == ']' {
|
||||
s[j] = ' '
|
||||
break
|
||||
} else if s[i] == '/' {
|
||||
i = consumeComment(s, i+1)
|
||||
} else if !isWS(s[i]) {
|
||||
break
|
||||
}
|
||||
}
|
||||
default:
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Read acts as a proxy for the underlying reader and cleans p
|
||||
// of comments and trailing commas preceeding ] and }
|
||||
// comments are delimitted by // up until the end the line
|
||||
func (st *state) Read(p []byte) (n int, err error) {
|
||||
if st.br == nil {
|
||||
var s []byte
|
||||
if s, err = prep(st.r); err != nil {
|
||||
return
|
||||
}
|
||||
st.br = bytes.NewReader(s)
|
||||
}
|
||||
return st.br.Read(p)
|
||||
}
|
||||
|
||||
// New returns an io.Reader acting as proxy to r
|
||||
func New(r io.Reader) io.Reader {
|
||||
return &state{r: r}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package JsonConfigReader
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var tests = map[string]string{
|
||||
`{
|
||||
// a
|
||||
"x": "y", // b
|
||||
"x": "y", // c
|
||||
}`: `{
|
||||
|
||||
"x": "y",
|
||||
"x": "y"
|
||||
}`,
|
||||
|
||||
`// serve a directory
|
||||
"l/test": [
|
||||
{
|
||||
"handler": "fs",
|
||||
"dir": "../",
|
||||
// "strip_prefix": "",
|
||||
},
|
||||
],`: `
|
||||
"l/test": [
|
||||
{
|
||||
"handler": "fs",
|
||||
"dir": "../"
|
||||
|
||||
}
|
||||
],`,
|
||||
|
||||
`[1, 2, 3]`: `[1, 2, 3]`,
|
||||
`[1, 2, 3, 4,]`: `[1, 2, 3, 4 ]`,
|
||||
`{"x":1}//[1, 2, 3, 4,]`: `{"x":1} `,
|
||||
`//////`: ` `,
|
||||
`{}/ /..`: `{}/ /..`,
|
||||
`{,}/ /..`: `{ }/ /..`,
|
||||
`{,}//..`: `{ } `,
|
||||
`{[],}`: `{[] }`,
|
||||
`{[,}`: `{[ }`,
|
||||
`[[",",],]`: `[["," ] ]`,
|
||||
`[",\"",]`: `[",\"" ]`,
|
||||
`[",\"\\\",]`: `[",\"\\\",]`,
|
||||
`[",//"]`: `[",//"]`,
|
||||
`[",//\"
|
||||
"],`: `[",//\"
|
||||
"],`,
|
||||
}
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
for a, b := range tests {
|
||||
buf := &bytes.Buffer{}
|
||||
io.Copy(buf, New(strings.NewReader(a)))
|
||||
a = buf.String()
|
||||
if a != b {
|
||||
a = strings.Replace(a, " ", ".", -1)
|
||||
b = strings.Replace(b, " ", ".", -1)
|
||||
t.Errorf("reader failed to clean json: expected: `%s`, got `%s`", b, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user