replace zxq.co/ripple/hanayo
This commit is contained in:
6
vendor/github.com/thehowl/conf/.travis.yml
generated
vendored
Normal file
6
vendor/github.com/thehowl/conf/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.5
|
||||
- 1.6
|
||||
- tip
|
18
vendor/github.com/thehowl/conf/LICENSE
generated
vendored
Normal file
18
vendor/github.com/thehowl/conf/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
Copyright (c) 2016 Morgan Bazalgette
|
||||
|
||||
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.
|
83
vendor/github.com/thehowl/conf/README.md
generated
vendored
Normal file
83
vendor/github.com/thehowl/conf/README.md
generated
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
# conf [](https://travis-ci.org/thehowl/conf) [](https://godoc.org/github.com/thehowl/conf)
|
||||
|
||||
(yes I am that creative with names)
|
||||
|
||||
I have been using [ini](http://gopkg.in/ini.v1) for managing configuration files in go for quite some time. One of the things that had bothered me though, was that it really was a pain to set up for small projects, as it's just boilerplate code over and over. So I decided to write my own configuration file system, and now I'm here.
|
||||
|
||||
## Quick start
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/thehowl/conf"
|
||||
)
|
||||
|
||||
type myConf struct {
|
||||
Port string `description:"The port from which the application will take HTTP requests"`
|
||||
Password string
|
||||
MaxUsers int
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := myConf{}
|
||||
err := conf.Load(&c, "myapp.conf")
|
||||
if err == conf.ErrNoFile {
|
||||
// You can export your conf to a file, so you can write default values.
|
||||
conf.Export(myConf{
|
||||
Port: ":8080",
|
||||
Password: "hunter2",
|
||||
MaxUsers: 9001,
|
||||
}, "myapp.conf")
|
||||
fmt.Println("Please compile the configuration file (myapp.conf.)")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// You can now use the values in `c` without thinking about the actual configuration ever again!
|
||||
fmt.Printf("%#v\n", c)
|
||||
}
|
||||
```
|
||||
|
||||
## Configuration file format
|
||||
|
||||
```
|
||||
; This is an example configuration file generated with `conf`. Comments are done using semicolons.
|
||||
;
|
||||
; This is a simple string value in the configuration:
|
||||
String=Hello world!
|
||||
|
||||
; Note that there are no spaces between the field (key) name and its value. Conf does not trim strings.
|
||||
; int, float, uint values are done just as easily. You just need to write that they're of that type in
|
||||
; the struct, and conf will do all the magic!
|
||||
Int=481
|
||||
|
||||
; There are also bools.
|
||||
Bool=1
|
||||
; Bools are retrieved through [ParseBool](https://golang.org/pkg/strconv/#ParseBool), as such they
|
||||
; need to be one of 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
|
||||
|
||||
; But, what about strings with newlines?
|
||||
AreTheyPossible=Yes\
|
||||
They\
|
||||
Are!
|
||||
; If you need to export a flag with a multiline string, conf will automatically escape it.
|
||||
;
|
||||
; By the way, conf automatically ignores lines without a valid field=value combination, including
|
||||
; empty lines, so you can use them as comments, although discouraged.
|
||||
So yes, this line will be silently ignored!
|
||||
=This one, too!
|
||||
And this one, too!=
|
||||
|
||||
; Escaping can not only be done with newlines. Here's what you can possibly escape!
|
||||
Fields\=With\=Equal\=Signs=can be escaped!
|
||||
Comments=Can \; be escaped!
|
||||
Oh yeah, fields=can also have spaces and what not in them.; You can also write comments straight after a value!
|
||||
|
||||
; And that's all you need to know for using `conf`!
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
165
vendor/github.com/thehowl/conf/conf.go
generated
vendored
Normal file
165
vendor/github.com/thehowl/conf/conf.go
generated
vendored
Normal file
@@ -0,0 +1,165 @@
|
||||
// Package conf lets you manage configuration files in the easiest way possible, without the unnecessary pain.
|
||||
package conf
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The only custom errors this package will return.
|
||||
var (
|
||||
ErrNoFile = errors.New("conf: the configuration file doesn't exist")
|
||||
ErrNotAStruct = errors.New("conf: the passed into/from variable is not a pointer to a struct")
|
||||
)
|
||||
|
||||
// Load unmarshals a file into the struct passed as the argument "into".
|
||||
func Load(into interface{}, filename string) error {
|
||||
intoValue := reflect.ValueOf(into)
|
||||
if intoValue.Kind() != reflect.Ptr || intoValue.Elem().Kind() != reflect.Struct {
|
||||
return ErrNotAStruct
|
||||
}
|
||||
intoValue = intoValue.Elem()
|
||||
f, err := ioutil.ReadFile(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return ErrNoFile
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return loadRaw(intoValue, f)
|
||||
}
|
||||
|
||||
// LoadRaw allows you to load into a struct some raw data bytes.
|
||||
func LoadRaw(into interface{}, data []byte) error {
|
||||
intoValue := reflect.ValueOf(into)
|
||||
if intoValue.Kind() != reflect.Ptr || intoValue.Elem().Kind() != reflect.Struct {
|
||||
return ErrNotAStruct
|
||||
}
|
||||
intoValue = intoValue.Elem()
|
||||
return loadRaw(intoValue, data)
|
||||
}
|
||||
|
||||
func loadRaw(intoValue reflect.Value, data []byte) error {
|
||||
fvs := Parse(data)
|
||||
for _, v := range fvs {
|
||||
for i := 0; i < intoValue.Type().NumField(); i++ {
|
||||
field := intoValue.Type().Field(i)
|
||||
if !intoValue.Field(i).CanSet() {
|
||||
continue
|
||||
}
|
||||
if field.Name == v.Field {
|
||||
switch field.Type.Kind() {
|
||||
case reflect.String:
|
||||
intoValue.Field(i).SetString(v.Value)
|
||||
case reflect.Bool:
|
||||
boolVal, err := strconv.ParseBool(v.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
intoValue.Field(i).SetBool(boolVal)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
intVal, err := strconv.ParseInt(v.Value, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
intoValue.Field(i).SetInt(intVal)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
uintVal, err := strconv.ParseUint(v.Value, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
intoValue.Field(i).SetUint(uintVal)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
floatVal, err := strconv.ParseFloat(v.Value, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
intoValue.Field(i).SetFloat(floatVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MustLoad has the same behaviour as Load, but panics if it returns an error.
|
||||
func MustLoad(into interface{}, filename string) {
|
||||
if err := Load(into, filename); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// MustLoadRaw has the same behaviour as LoadRaw, but panics if it returns an error.
|
||||
func MustLoadRaw(into interface{}, data []byte) {
|
||||
if err := LoadRaw(into, data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Export uses ExportRaw to put the data into a file, specified with its name.
|
||||
func Export(from interface{}, filename string) error {
|
||||
data, err := ExportRaw(from)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(filename, data, 0644)
|
||||
}
|
||||
|
||||
// ExportRaw can create a []byte that can then be loaded back by LoadRaw to get a struct's original form back.
|
||||
// I suck at explaining stuff.
|
||||
func ExportRaw(from interface{}) ([]byte, error) {
|
||||
fromValue := reflect.ValueOf(from)
|
||||
if fromValue.Kind() == reflect.Ptr {
|
||||
return ExportRaw(fromValue.Elem().Interface())
|
||||
}
|
||||
if fromValue.Kind() != reflect.Struct {
|
||||
return []byte{}, ErrNotAStruct
|
||||
}
|
||||
return exportRaw(fromValue), nil
|
||||
}
|
||||
|
||||
func exportRaw(fromValue reflect.Value) []byte {
|
||||
var ret []byte
|
||||
for i := 0; i < fromValue.Type().NumField(); i++ {
|
||||
curfield := fromValue.Field(i)
|
||||
curfieldType := fromValue.Type().Field(i)
|
||||
|
||||
// Dirty hack to ignore that field if we don't support that type.
|
||||
if !((curfield.Kind() >= reflect.Bool && curfield.Kind() <= reflect.Uint64) ||
|
||||
curfield.Kind() == reflect.String || curfield.Kind() == reflect.Float32 ||
|
||||
curfield.Kind() == reflect.Float64) {
|
||||
continue
|
||||
}
|
||||
|
||||
/* guten */ tag := curfieldType.Tag.Get("description")
|
||||
if tag != "" {
|
||||
tag = strings.Replace(tag, "\n", "\n; ", -1)
|
||||
ret = append(ret, []byte("; "+tag+"\n")...)
|
||||
}
|
||||
ret = append(ret, []byte(Escape(curfieldType.Name)+"="+Escape(fmt.Sprint(curfield.Interface()))+"\n")...)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// MustExport panics if Export returns an error, removing error checking from your code. For the lazy.
|
||||
func MustExport(from interface{}, filename string) {
|
||||
if err := Export(from, filename); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// MustExportRaw panics if ExportRaw returns an error, removing error checking from your code. For the lazy.
|
||||
func MustExportRaw(from interface{}) []byte {
|
||||
data, err := ExportRaw(from)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return data
|
||||
}
|
377
vendor/github.com/thehowl/conf/conf_test.go
generated
vendored
Normal file
377
vendor/github.com/thehowl/conf/conf_test.go
generated
vendored
Normal file
@@ -0,0 +1,377 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const sampleForParsing = `Key1=Value
|
||||
Test=Xd
|
||||
What=1294; This be a comment.
|
||||
; This be another comment.
|
||||
|
||||
|
||||
Multi=Line\
|
||||
String!
|
||||
Key\=With\=Equal=Indeed!
|
||||
ShouldNotOutput=
|
||||
=ShouldNotOutput
|
||||
ShouldNotOutput
|
||||
|
||||
Can=Contain\;Semicolons!
|
||||
EqualSign=Can also = not = be = escaped!
|
||||
|
||||
ThisShouldComeUp=Yup`
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
kvs := Parse([]byte(sampleForParsing))
|
||||
t.Log("Result:")
|
||||
for _, v := range kvs {
|
||||
t.Logf(" %s - %s\n", v.Field, v.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParseWithSample(b *testing.B) {
|
||||
sp := []byte(sampleForParsing)
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
Parse(sp)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
escapeOriginal = `;aqsdwe103====aa\a\\aa!a;a
|
||||
|
||||
xd|`
|
||||
escapeExpected = `\;aqsdwe103\=\=\=\=aa\\a\\\\aa!a\;a\
|
||||
\
|
||||
xd|`
|
||||
)
|
||||
|
||||
func TestEscape(t *testing.T) {
|
||||
escaped := Escape(escapeOriginal)
|
||||
if escaped != escapeExpected {
|
||||
t.Fatalf("Expected Escape to return '%s', got '%s' instead.", escapeExpected, escaped)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkEscape(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
Escape(escapeOriginal)
|
||||
}
|
||||
}
|
||||
|
||||
const sampleForUnmarshaling = `
|
||||
; Welcome to hell.
|
||||
; As we have to try every single possible value a struct can have, all of this is required.
|
||||
; Fuck.
|
||||
TestInt=1510591
|
||||
TestInt8=-100
|
||||
TestInt16=-12844
|
||||
TestInt32=-3211
|
||||
TestInt64=410491
|
||||
|
||||
TestUint=284129419
|
||||
TestUint8=255
|
||||
TestUint16=65535
|
||||
TestUint32=151529;Can't remember the max value. Won't bother googling.
|
||||
TestUint64=24148999
|
||||
|
||||
TestFloat32=24.1123
|
||||
TestFloat64=110.134141223
|
||||
|
||||
TestBool=1
|
||||
|
||||
TestString=How much wood would a woodchuck chuck if a woodchuck could chuck wood?
|
||||
TestEmpty=
|
||||
`
|
||||
|
||||
type sampleTest struct {
|
||||
TestInt int
|
||||
TestInt8 int8
|
||||
TestInt16 int16
|
||||
TestInt32 int32
|
||||
TestInt64 int64
|
||||
|
||||
TestUint uint
|
||||
TestUint8 uint8
|
||||
TestUint16 uint16
|
||||
TestUint32 uint32
|
||||
TestUint64 uint64
|
||||
|
||||
TestFloat32 float32
|
||||
TestFloat64 float64
|
||||
|
||||
TestBool bool
|
||||
|
||||
TestString string
|
||||
TestEmpty string
|
||||
}
|
||||
|
||||
func (si sampleTest) check(t *testing.T) {
|
||||
if true &&
|
||||
si.TestInt == 1510591 &&
|
||||
si.TestInt8 == -100 &&
|
||||
si.TestInt16 == -12844 &&
|
||||
si.TestInt32 == -3211 &&
|
||||
si.TestInt64 == 410491 &&
|
||||
|
||||
si.TestUint == 284129419 &&
|
||||
si.TestUint8 == 255 &&
|
||||
si.TestUint16 == 65535 &&
|
||||
si.TestUint32 == 151529 &&
|
||||
si.TestUint64 == 24148999 &&
|
||||
|
||||
si.TestFloat32 == 24.1123 &&
|
||||
si.TestFloat64 == 110.134141223 &&
|
||||
|
||||
si.TestBool == true &&
|
||||
|
||||
si.TestString == "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" &&
|
||||
si.TestEmpty == "" {
|
||||
t.Log("It surprisingly worked.")
|
||||
} else {
|
||||
t.Log("Nope.")
|
||||
t.Fatalf("%#v", si)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
// Prepare file
|
||||
err := ioutil.WriteFile("test.conf", []byte(sampleForUnmarshaling), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove("test.conf")
|
||||
|
||||
// Prepare struct
|
||||
si := sampleTest{}
|
||||
|
||||
// Load
|
||||
err = Load(&si, "test.conf")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Check for all values to be valid.
|
||||
si.check(t)
|
||||
}
|
||||
|
||||
func TestLoadShouldErrNotAStruct(t *testing.T) {
|
||||
if err := Load(2, "test.conf"); err != ErrNotAStruct {
|
||||
t.Fatalf("Should have panicked with ErrNotAStruct, got error '%s' instead.", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadShouldErrNoFile(t *testing.T) {
|
||||
si := sampleTest{}
|
||||
if err := Load(&si, "test.conf"); err != ErrNoFile {
|
||||
t.Fatalf("Should have panicked with ErrNoFile, got error '%s' instead.", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMustLoadShouldPanic(t *testing.T) {
|
||||
defer func() {
|
||||
c := recover()
|
||||
if c == nil {
|
||||
t.Fatal("MustLoad with wrong values didn't panic!")
|
||||
}
|
||||
}()
|
||||
MustLoad(2, "test.conf")
|
||||
}
|
||||
|
||||
func TestFailBool(t *testing.T) {
|
||||
// Prepare file
|
||||
err := ioutil.WriteFile("test.conf", []byte(`TestBool=Lolno.`), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove("test.conf")
|
||||
|
||||
// Prepare struct
|
||||
si := sampleTest{}
|
||||
|
||||
// Load
|
||||
err = Load(&si, "test.conf")
|
||||
if err == nil {
|
||||
t.Fatal("Should have returned an error, didn't")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailInt(t *testing.T) {
|
||||
// Prepare file
|
||||
err := ioutil.WriteFile("test.conf", []byte(`TestInt=10934104912049120491204912031301293102`), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove("test.conf")
|
||||
|
||||
// Prepare struct
|
||||
si := sampleTest{}
|
||||
|
||||
// Load
|
||||
err = Load(&si, "test.conf")
|
||||
if err == nil {
|
||||
t.Fatal("Should have returned an error, didn't")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailFloat(t *testing.T) {
|
||||
// Prepare file
|
||||
err := ioutil.WriteFile("test.conf", []byte(`TestFloat32=2931.23111aaddfffeep`), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove("test.conf")
|
||||
|
||||
// Prepare struct
|
||||
si := sampleTest{}
|
||||
|
||||
// Load
|
||||
err = Load(&si, "test.conf")
|
||||
if err == nil {
|
||||
t.Fatal("Should have returned an error, didn't")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailUint(t *testing.T) {
|
||||
// Prepare file
|
||||
err := ioutil.WriteFile("test.conf", []byte(`TestUint8=asd`), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove("test.conf")
|
||||
|
||||
// Prepare struct
|
||||
si := sampleTest{}
|
||||
|
||||
// Load
|
||||
err = Load(&si, "test.conf")
|
||||
if err == nil {
|
||||
t.Fatal("Should have returned an error, didn't")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRaw(t *testing.T) {
|
||||
si := sampleTest{}
|
||||
|
||||
LoadRaw(&si, []byte(sampleForUnmarshaling))
|
||||
si.check(t)
|
||||
}
|
||||
|
||||
func TestMustLoadRawShouldPanic(t *testing.T) {
|
||||
defer func() {
|
||||
c := recover()
|
||||
if c == nil {
|
||||
t.Fatal("MustLoad with wrong values didn't panic!")
|
||||
}
|
||||
}()
|
||||
MustLoadRaw(2, []byte("a"))
|
||||
}
|
||||
|
||||
func BenchmarkLoadRaw(b *testing.B) {
|
||||
si := &sampleTest{}
|
||||
data := []byte(sampleForUnmarshaling)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
LoadRaw(si, data)
|
||||
}
|
||||
}
|
||||
|
||||
type exportTypeTest struct {
|
||||
Name string
|
||||
Age int `description:"The age of the patient.\nDescriptions can be multi-line."`
|
||||
}
|
||||
|
||||
const expectedExportType = `Name=Jack
|
||||
; The age of the patient.
|
||||
; Descriptions can be multi-line.
|
||||
Age=19
|
||||
`
|
||||
|
||||
func TestMustExportRaw(t *testing.T) {
|
||||
defer func() {
|
||||
c := recover()
|
||||
if c != nil {
|
||||
t.Fatal(c)
|
||||
}
|
||||
}()
|
||||
e := &exportTypeTest{
|
||||
Name: "Jack",
|
||||
Age: 19,
|
||||
}
|
||||
data := MustExportRaw(e)
|
||||
if string(data) != expectedExportType {
|
||||
t.Fatalf("Expected '%s', got '%s'", expectedExportType, string(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMustExportShouldPanic(t *testing.T) {
|
||||
defer func() {
|
||||
c := recover()
|
||||
if c == nil {
|
||||
t.Fatal(c)
|
||||
}
|
||||
}()
|
||||
MustExport(2, "a.conf")
|
||||
}
|
||||
|
||||
func TestMustExportRawShouldPanic(t *testing.T) {
|
||||
defer func() {
|
||||
c := recover()
|
||||
if c == nil {
|
||||
t.Fatal(c)
|
||||
}
|
||||
}()
|
||||
MustExportRaw(2)
|
||||
}
|
||||
|
||||
type exportWithInvalidType struct {
|
||||
InvalidType []byte
|
||||
Name string
|
||||
}
|
||||
|
||||
func TestExport(t *testing.T) {
|
||||
err := Export(exportWithInvalidType{
|
||||
InvalidType: []byte("well"),
|
||||
Name: "xd",
|
||||
}, "test.conf")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
os.Remove("test.conf")
|
||||
}
|
||||
|
||||
func TestFullChain(t *testing.T) {
|
||||
initial := exportTypeTest{
|
||||
Name: "Pisellone",
|
||||
Age: 133337,
|
||||
}
|
||||
c := MustExportRaw(initial)
|
||||
secondary := exportTypeTest{}
|
||||
MustLoadRaw(&secondary, c)
|
||||
if !reflect.DeepEqual(initial, secondary) {
|
||||
t.Fatalf("Initial struct %#v is not the same as the derivate %#v.", initial, secondary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCRLF(t *testing.T) {
|
||||
const w = "Key1=Nice\r\nKey2=Meme"
|
||||
vals := Parse([]byte(w))
|
||||
for _, i := range vals {
|
||||
switch i.Field {
|
||||
case "Key1":
|
||||
if i.Value != "Nice" {
|
||||
t.Fatalf("Expected '%s', got '%s'", "Nice", i.Value)
|
||||
}
|
||||
case "Key2":
|
||||
if i.Value != "Meme" {
|
||||
t.Fatalf("Expected '%s', got '%s'", "Meme", i.Value)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("Unexpected key '%s'", i.Field)
|
||||
}
|
||||
}
|
||||
}
|
15
vendor/github.com/thehowl/conf/escape.go
generated
vendored
Normal file
15
vendor/github.com/thehowl/conf/escape.go
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Escape escapes characters for then putting it into conf field/values without issues.
|
||||
func Escape(s string) string {
|
||||
return strings.NewReplacer(
|
||||
"\n", "\\\n",
|
||||
`\`, `\\`,
|
||||
`;`, `\;`,
|
||||
`=`, `\=`,
|
||||
).Replace(s)
|
||||
}
|
21
vendor/github.com/thehowl/conf/example_test.go
generated
vendored
Normal file
21
vendor/github.com/thehowl/conf/example_test.go
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
package conf_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thehowl/conf"
|
||||
)
|
||||
|
||||
type myConf struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
const myConfString = `Name=Jack
|
||||
Age=19`
|
||||
|
||||
func Example() {
|
||||
c := myConf{}
|
||||
conf.LoadRaw(&c, []byte(myConfString))
|
||||
fmt.Printf("%#v\n", c)
|
||||
}
|
89
vendor/github.com/thehowl/conf/parse.go
generated
vendored
Normal file
89
vendor/github.com/thehowl/conf/parse.go
generated
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
package conf
|
||||
|
||||
// FieldValue is a field=value pair in the configuration.
|
||||
type FieldValue struct {
|
||||
Field string
|
||||
Value string
|
||||
}
|
||||
|
||||
const (
|
||||
currentName = iota
|
||||
currentValue
|
||||
currentComment
|
||||
)
|
||||
|
||||
// Parse converts some bytes into various FieldValue pairs.
|
||||
func Parse(data []byte) []FieldValue {
|
||||
fvs := []FieldValue{}
|
||||
var (
|
||||
fieldName string
|
||||
fieldValue string
|
||||
nextNormal bool
|
||||
current byte
|
||||
)
|
||||
|
||||
for _, c := range data {
|
||||
if current == currentComment && c != '\n' {
|
||||
continue
|
||||
}
|
||||
if nextNormal {
|
||||
switch current {
|
||||
case currentName:
|
||||
fieldName += string(c)
|
||||
case currentValue:
|
||||
fieldValue += string(c)
|
||||
}
|
||||
nextNormal = false
|
||||
continue
|
||||
}
|
||||
switch c {
|
||||
case '=':
|
||||
switch current {
|
||||
// if we are still at the name, let's switch to a value.
|
||||
case currentName:
|
||||
current = currentValue
|
||||
// if we are already at the value, treat the = character like any other sign
|
||||
case currentValue:
|
||||
fieldValue += string(c)
|
||||
}
|
||||
case ';':
|
||||
current = currentComment
|
||||
case '\n':
|
||||
if fieldName != "" && fieldValue != "" {
|
||||
fvs = append(fvs, FieldValue{
|
||||
Field: fieldName,
|
||||
Value: removeTrailingCR(fieldValue),
|
||||
})
|
||||
}
|
||||
fieldName = ""
|
||||
fieldValue = ""
|
||||
current = currentName
|
||||
case '\\':
|
||||
nextNormal = true
|
||||
default:
|
||||
switch current {
|
||||
case currentName:
|
||||
fieldName += string(c)
|
||||
case currentValue:
|
||||
fieldValue += string(c)
|
||||
}
|
||||
nextNormal = false
|
||||
}
|
||||
}
|
||||
if fieldName != "" && fieldValue != "" {
|
||||
fvs = append(fvs, FieldValue{
|
||||
Field: fieldName,
|
||||
Value: removeTrailingCR(fieldValue),
|
||||
})
|
||||
}
|
||||
return fvs
|
||||
}
|
||||
func removeTrailingCR(s string) string {
|
||||
if len(s) == 0 {
|
||||
return s
|
||||
}
|
||||
if s[len(s)-1] == '\r' {
|
||||
return s[:len(s)-1]
|
||||
}
|
||||
return s
|
||||
}
|
Reference in New Issue
Block a user