diff options
| author | Shulhan <ms@kilabit.info> | 2022-02-06 14:19:15 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-02-06 14:19:15 +0700 |
| commit | f6326eebcc38daf25636bc4f32ab9dfc76ba6d79 (patch) | |
| tree | 428614517a87256d08de31e33a2189db8f815225 /lib/ini | |
| parent | bc9beab883833279580319eb7670cbbc21cd628f (diff) | |
| download | pakakeh.go-f6326eebcc38daf25636bc4f32ab9dfc76ba6d79.tar.xz | |
lib/ini: realign all structs
Changes,
* reader: from 184 to 168 bytes (-16 bytes)
* Section: from 104 to 88 bytes (-16 bytes)
* structField: from 120 to 112 bytes (-8 bytes)
* variable: from 120 to 104 bytes (-16 bytes)
Diffstat (limited to 'lib/ini')
| -rw-r--r-- | lib/ini/ini_example_test.go | 171 | ||||
| -rw-r--r-- | lib/ini/ini_test.go | 2 | ||||
| -rw-r--r-- | lib/ini/ini_unmarshal_test.go | 44 | ||||
| -rw-r--r-- | lib/ini/reader.go | 17 | ||||
| -rw-r--r-- | lib/ini/reader_test.go | 27 | ||||
| -rw-r--r-- | lib/ini/section.go | 8 | ||||
| -rw-r--r-- | lib/ini/section_test.go | 46 | ||||
| -rw-r--r-- | lib/ini/struct_field.go | 8 | ||||
| -rw-r--r-- | lib/ini/tag_struct_field_test.go | 37 | ||||
| -rw-r--r-- | lib/ini/variable.go | 6 |
10 files changed, 215 insertions, 151 deletions
diff --git a/lib/ini/ini_example_test.go b/lib/ini/ini_example_test.go index 9a6c10e6..0b7f11b5 100644 --- a/lib/ini/ini_example_test.go +++ b/lib/ini/ini_example_test.go @@ -168,34 +168,54 @@ func ExampleMarshal() { Int int `ini:"::int"` } - t := struct { - String string `ini:"section::string"` - Int int `ini:"section::int"` - Bool bool `ini:"section::bool"` - Duration time.Duration `ini:"section::duration"` - Time time.Time `ini:"section::time" layout:"2006-01-02 15:04:05"` - Struct U `ini:"section:struct"` - SliceString []string `ini:"section:slice:string"` - SliceInt []int `ini:"section:slice:int"` - SliceUint []uint `ini:"section:slice:uint"` - SliceBool []bool `ini:"section:slice:bool"` - SliceStruct []U `ini:"slice:OfStruct"` - MapString map[string]string `ini:"map:string"` - MapInt map[string]int `ini:"map:int"` - PtrString *string `ini:"section:pointer:string"` - PtrInt *int `ini:"section:pointer:int"` - PtrTime *time.Time `ini:"section:pointer:time" layout:"2006-01-02 15:04:05"` - PtrStruct *U `ini:"pointer:struct"` - }{ - String: "a", - Int: 1, - Bool: true, - Duration: time.Minute, - Time: time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), - Struct: U{ - String: "b", - Int: 2, + type ADT struct { + Time time.Time `ini:"section::time" layout:"2006-01-02 15:04:05"` + + PtrString *string `ini:"section:pointer:string"` + PtrInt *int `ini:"section:pointer:int"` + PtrTime *time.Time `ini:"section:pointer:time" layout:"2006-01-02 15:04:05"` + PtrStruct *U `ini:"pointer:struct"` + + MapString map[string]string `ini:"map:string"` + MapInt map[string]int `ini:"map:int"` + + String string `ini:"section::string"` + + SliceString []string `ini:"section:slice:string"` + SliceInt []int `ini:"section:slice:int"` + SliceUint []uint `ini:"section:slice:uint"` + SliceBool []bool `ini:"section:slice:bool"` + SliceStruct []U `ini:"slice:OfStruct"` + + Struct U `ini:"section:struct"` + + Duration time.Duration `ini:"section::duration"` + + Int int `ini:"section::int"` + + Bool bool `ini:"section::bool"` + } + + t := ADT{ + Time: time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), + + PtrString: &ptrString, + PtrInt: &ptrInt, + PtrTime: &ptrTime, + PtrStruct: &U{ + String: "PtrStruct.String", + Int: 3, }, + + MapString: map[string]string{ + "k": "v", + }, + MapInt: map[string]int{ + "keyInt": 6, + }, + + String: "a", + SliceString: []string{"c", "d"}, SliceInt: []int{2, 3}, SliceUint: []uint{4, 5}, @@ -207,19 +227,17 @@ func ExampleMarshal() { String: "U.string 2", Int: 2, }}, - MapString: map[string]string{ - "k": "v", - }, - MapInt: map[string]int{ - "keyInt": 6, - }, - PtrString: &ptrString, - PtrInt: &ptrInt, - PtrTime: &ptrTime, - PtrStruct: &U{ - String: "PtrStruct.String", - Int: 3, + + Struct: U{ + String: "b", + Int: 2, }, + + Duration: time.Minute, + + Int: 1, + + Bool: true, } iniText, err := Marshal(&t) @@ -230,15 +248,26 @@ func ExampleMarshal() { fmt.Printf("%s\n", iniText) // Output: // [section] + // time = 2006-01-02 15:04:05 // string = a + // duration = 1m0s // int = 1 // bool = true - // duration = 1m0s - // time = 2006-01-02 15:04:05 // - // [section "struct"] + // [section "pointer"] // string = b // int = 2 + // time = 2021-02-28 18:44:01 + // + // [pointer "struct"] + // string = PtrStruct.String + // int = 3 + // + // [map "string"] + // k = v + // + // [map "int"] + // keyint = 6 // // [section "slice"] // string = c @@ -258,20 +287,9 @@ func ExampleMarshal() { // string = U.string 2 // int = 2 // - // [map "string"] - // k = v - // - // [map "int"] - // keyint = 6 - // - // [section "pointer"] + // [section "struct"] // string = b // int = 2 - // time = 2021-02-28 18:44:01 - // - // [pointer "struct"] - // string = PtrStruct.String - // int = 3 } func ExampleUnmarshal() { @@ -317,22 +335,35 @@ int = 2 Int int `ini:"::int"` } - t := struct { - String string `ini:"section::string"` - Int int `ini:"section::int"` - Bool bool `ini:"section::bool"` - Duration time.Duration `ini:"section::duration"` - Time time.Time `ini:"section::time" layout:"2006-01-02 15:04:05"` - SliceString []string `ini:"section:slice:string"` - SliceInt []int `ini:"section:slice:int"` - SliceUint []uint `ini:"section:slice:uint"` - SliceBool []bool `ini:"section:slice:bool"` - SliceStruct []U `ini:"slice:OfStruct"` - MapString map[string]string `ini:"map:string"` - MapInt map[string]int `ini:"map:int"` - PtrString *string `ini:"section:pointer:string"` - PtrInt *int `ini:"section:pointer:int"` - }{} + type ADT struct { + Time time.Time `ini:"section::time" layout:"2006-01-02 15:04:05"` + + PtrString *string `ini:"section:pointer:string"` + PtrInt *int `ini:"section:pointer:int"` + PtrTime *time.Time `ini:"section:pointer:time" layout:"2006-01-02 15:04:05"` + PtrStruct *U `ini:"pointer:struct"` + + MapString map[string]string `ini:"map:string"` + MapInt map[string]int `ini:"map:int"` + + String string `ini:"section::string"` + + SliceString []string `ini:"section:slice:string"` + SliceInt []int `ini:"section:slice:int"` + SliceUint []uint `ini:"section:slice:uint"` + SliceBool []bool `ini:"section:slice:bool"` + SliceStruct []U `ini:"slice:OfStruct"` + + Struct U `ini:"section:struct"` + + Duration time.Duration `ini:"section::duration"` + + Int int `ini:"section::int"` + + Bool bool `ini:"section::bool"` + } + + t := ADT{} err := Unmarshal([]byte(iniText), &t) if err != nil { diff --git a/lib/ini/ini_test.go b/lib/ini/ini_test.go index 45f807c6..e7f41b6c 100644 --- a/lib/ini/ini_test.go +++ b/lib/ini/ini_test.go @@ -105,9 +105,9 @@ func TestAddSection(t *testing.T) { in := &Ini{} cases := []struct { - desc string sec *Section expIni *Ini + desc string }{{ desc: "With nil section", expIni: &Ini{}, diff --git a/lib/ini/ini_unmarshal_test.go b/lib/ini/ini_unmarshal_test.go index abfdaecd..aa5f57c1 100644 --- a/lib/ini/ini_unmarshal_test.go +++ b/lib/ini/ini_unmarshal_test.go @@ -17,35 +17,39 @@ type Y struct { } type X struct { - Struct Y `ini:"section:struct"` - PtrStruct *Y `ini:"section:ptr_struct"` - PtrStructNil *Y `ini:"section:ptr_struct_nil"` + Time time.Time `ini:"section::time" layout:"2006-01-02 15:04:05"` - String string `ini:"section::string"` - Int int `ini:"section::int"` - Bool bool `ini:"section::bool"` - Duration time.Duration `ini:"section::duration"` - Time time.Time `ini:"section::time" layout:"2006-01-02 15:04:05"` - - PtrString *string `ini:"section:pointer:string"` - PtrInt *int `ini:"section:pointer:int"` PtrBool *bool `ini:"section:pointer:bool"` PtrDuration *time.Duration `ini:"section:pointer:duration"` + PtrInt *int `ini:"section:pointer:int"` + PtrString *string `ini:"section:pointer:string"` PtrTime *time.Time `ini:"section:pointer:time" layout:"2006-01-02 15:04:05"` - SliceStruct []Y `ini:"slice:struct"` - SliceString []string `ini:"slice::string"` - SliceInt []int `ini:"slice::int"` - SliceBool []bool `ini:"slice::bool"` - SliceDuration []time.Duration `ini:"slice::duration"` - SliceTime []time.Time `ini:"slice::time" layout:"2006-01-02 15:04:05"` + PtrStruct *Y `ini:"section:ptr_struct"` + PtrStructNil *Y `ini:"section:ptr_struct_nil"` + + Struct Y `ini:"section:struct"` + + String string `ini:"section::string"` + + SliceStruct []Y `ini:"slice:struct"` - SlicePtrStruct []*Y `ini:"slice:ptr_struct"` - SlicePtrString []*string `ini:"slice:ptr:string"` - SlicePtrInt []*int `ini:"slice:ptr:int"` SlicePtrBool []*bool `ini:"slice:ptr:bool"` SlicePtrDuration []*time.Duration `ini:"slice:ptr:duration"` + SlicePtrInt []*int `ini:"slice:ptr:int"` + SlicePtrString []*string `ini:"slice:ptr:string"` + SlicePtrStruct []*Y `ini:"slice:ptr_struct"` SlicePtrTime []*time.Time `ini:"slice:ptr:time" layout:"2006-01-02 15:04:05"` + + SliceBool []bool `ini:"slice::bool"` + SliceDuration []time.Duration `ini:"slice::duration"` + SliceInt []int `ini:"slice::int"` + SliceString []string `ini:"slice::string"` + SliceTime []time.Time `ini:"slice::time" layout:"2006-01-02 15:04:05"` + + Duration time.Duration `ini:"section::duration"` + Int int `ini:"section::int"` + Bool bool `ini:"section::bool"` } func TestIni_Unmarshal(t *testing.T) { diff --git a/lib/ini/reader.go b/lib/ini/reader.go index 6c5e76f7..eb6ddbfb 100644 --- a/lib/ini/reader.go +++ b/lib/ini/reader.go @@ -43,17 +43,20 @@ var ( // reader define the INI file reader. // type reader struct { - br *bytes.Reader - b byte - r rune - lineNum int - filename string - _var *variable - sec *Section + br *bytes.Reader + _var *variable + sec *Section + + filename string + buf bytes.Buffer bufComment bytes.Buffer bufFormat bytes.Buffer bufSpaces bytes.Buffer + + lineNum int + r rune + b byte } // diff --git a/lib/ini/reader_test.go b/lib/ini/reader_test.go index e1d3a4e4..5e71b0d5 100644 --- a/lib/ini/reader_test.go +++ b/lib/ini/reader_test.go @@ -13,14 +13,15 @@ import ( func TestParseSectionHeader(t *testing.T) { cases := []struct { - desc string - in string expErr error - expMode lineMode expFormat string expSecName string expSubName string expComment string + + desc string + in string + expMode lineMode }{{ desc: "With empty input", expErr: errBadConfig, @@ -124,13 +125,15 @@ func TestParseSectionHeader(t *testing.T) { func TestParseSubsection(t *testing.T) { cases := []struct { - desc string - in string expErr error - expMode lineMode expFormat string expSub string expComment string + + desc string + in string + + expMode lineMode }{{ desc: "With empty input", expErr: errBadConfig, @@ -178,14 +181,15 @@ func TestParseSubsection(t *testing.T) { func TestParseVariable(t *testing.T) { cases := []struct { - desc string - in []byte expErr error - expMode lineMode expFormat string expComment string expKey string expValue string + + desc string + in []byte + expMode lineMode }{{ desc: "Empty", expErr: errVarNameInvalid, @@ -346,12 +350,13 @@ func TestParseVariable(t *testing.T) { func TestParseVarValue(t *testing.T) { cases := []struct { - desc string - in []byte expErr error expFormat string expValue string expComment string + + desc string + in []byte }{{ desc: `Empty input`, expErr: io.EOF, diff --git a/lib/ini/section.go b/lib/ini/section.go index 604d2218..20ddb984 100644 --- a/lib/ini/section.go +++ b/lib/ini/section.go @@ -16,14 +16,16 @@ import ( // Remember that section's name is case insensitive. // type Section struct { - mode lineMode - lineNum int name string sub string format string nameLower string others string - vars []*variable + + vars []*variable + + mode lineMode + lineNum int } // diff --git a/lib/ini/section_test.go b/lib/ini/section_test.go index c0de6534..4e3c891e 100644 --- a/lib/ini/section_test.go +++ b/lib/ini/section_test.go @@ -12,10 +12,11 @@ import ( func TestNewSection(t *testing.T) { cases := []struct { - desc string - name string - sub string expSec *Section + + desc string + name string + sub string }{{ desc: "With empty name", }, { @@ -69,11 +70,13 @@ func TestSectionSet(t *testing.T) { } cases := []struct { - desc string - k string - v string - expOK bool expSec *Section + + desc string + k string + v string + + expOK bool }{{ desc: "With empty value", k: "k", @@ -145,10 +148,11 @@ func TestSection_add(t *testing.T) { } cases := []struct { - desc string - k string - v string expSec *Section + + desc string + k string + v string }{{ desc: "With empty key", expSec: &Section{ @@ -244,10 +248,11 @@ func TestSectionUnset(t *testing.T) { } cases := []struct { - desc string - k string - expOK bool expSec *Section + + desc string + k string + expOK bool }{{ desc: "With empty key", expOK: false, @@ -337,9 +342,10 @@ func TestSectionUnsetAll(t *testing.T) { } cases := []struct { - desc string - k string expSec *Section + + desc string + k string }{{ desc: "With empty key", expSec: &Section{ @@ -417,10 +423,11 @@ func TestSection_replaceAll(t *testing.T) { sec.add("key-3", "3333") cases := []struct { - desc string - k string - v string expSec *Section + + desc string + k string + v string }{{ desc: "With empty key", expSec: &Section{ @@ -537,8 +544,9 @@ func TestSectionGet(t *testing.T) { desc string k string def string - expOK bool expVal string + + expOK bool }{{ desc: "With invalid key and default", k: "key-1", diff --git a/lib/ini/struct_field.go b/lib/ini/struct_field.go index 6ed85ff1..95f17247 100644 --- a/lib/ini/struct_field.go +++ b/lib/ini/struct_field.go @@ -11,14 +11,16 @@ import ( ) type structField struct { + ftype reflect.Type + fval reflect.Value + sec string sub string key string layout string fname string - fkind reflect.Kind - ftype reflect.Type - fval reflect.Value + + fkind reflect.Kind } func (sfield *structField) set(val string) bool { diff --git a/lib/ini/tag_struct_field_test.go b/lib/ini/tag_struct_field_test.go index 40c3a2fd..d9b90c78 100644 --- a/lib/ini/tag_struct_field_test.go +++ b/lib/ini/tag_struct_field_test.go @@ -19,23 +19,30 @@ func TestUnpackStruct(t *testing.T) { } type T struct { - String string `ini:"section::string"` - Int int `ini:"section::int"` - Bool bool `ini:"section::bool"` - Duration time.Duration `ini:"section::duration"` - Time time.Time `ini:"section::time" layout:"2006-01-02 15:04:05"` - SliceString []string `ini:"section:slice:string"` - SliceInt []int `ini:"section:slice:int"` - SliceUint []uint `ini:"section:slice:uint"` - SliceBool []bool `ini:"section:slice:bool"` - SliceStruct []U `ini:"slice:OfStruct"` - MapString map[string]string `ini:"section:mapstring"` - MapInt map[string]int `ini:"section:mapint"` - PtrString *string `ini:"section:pointer"` - PtrInt *int `ini:"section:pointer"` - Struct U + Time time.Time `ini:"section::time" layout:"2006-01-02 15:04:05"` + PtrStruct *U PtrStructNil *U + + PtrString *string `ini:"section:pointer"` + PtrInt *int `ini:"section:pointer"` + + MapString map[string]string `ini:"section:mapstring"` + MapInt map[string]int `ini:"section:mapint"` + + String string `ini:"section::string"` + + SliceString []string `ini:"section:slice:string"` + SliceInt []int `ini:"section:slice:int"` + SliceUint []uint `ini:"section:slice:uint"` + SliceStruct []U `ini:"slice:OfStruct"` + SliceBool []bool `ini:"section:slice:bool"` + + Struct U + + Duration time.Duration `ini:"section::duration"` + Int int `ini:"section::int"` + Bool bool `ini:"section::bool"` } var v interface{} = &T{ diff --git a/lib/ini/variable.go b/lib/ini/variable.go index cbe6bddf..f86df053 100644 --- a/lib/ini/variable.go +++ b/lib/ini/variable.go @@ -18,8 +18,6 @@ import ( // want to compare variable, use the KeyLower value. // type variable struct { - mode lineMode - lineNum int format string secName string subName string @@ -27,6 +25,10 @@ type variable struct { keyLower string value string others string + + mode lineMode + lineNum int + isQuoted bool } |
