#60 minor fix and update docs

This commit is contained in:
Unknwon
2016-08-05 11:03:21 -07:00
parent 8e15fc6d72
commit 088ac83a7d
5 changed files with 14 additions and 31 deletions
+3 -4
View File
@@ -512,8 +512,8 @@ Why not?
```go
type Embeded struct {
Dates []time.Time `delim:"|"`
Places []string
None []int
Places []string `ini:"places,omitempty"`
None []int `ini:",omitempty"`
}
type Author struct {
@@ -548,8 +548,7 @@ GPA = 2.8
[Embeded]
Dates = 2015-08-07T22:14:22+08:00|2015-08-07T22:14:22+08:00
Places = HangZhou,Boston
None =
places = HangZhou,Boston
```
#### Name Mapper
+3 -4
View File
@@ -503,8 +503,8 @@ p := &Person{
```go
type Embeded struct {
Dates []time.Time `delim:"|"`
Places []string
None []int
Places []string `ini:"places,omitempty"`
None []int `ini:",omitempty"`
}
type Author struct {
@@ -539,8 +539,7 @@ GPA = 2.8
[Embeded]
Dates = 2015-08-07T22:14:22+08:00|2015-08-07T22:14:22+08:00
Places = HangZhou,Boston
None =
places = HangZhou,Boston
```
#### 名称映射器(Name Mapper)
+1 -1
View File
@@ -36,7 +36,7 @@ const (
// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 99
_VERSION = "1.18.0"
_VERSION = "1.19.0"
)
// Version returns current package version literal.
+7 -12
View File
@@ -324,6 +324,8 @@ func reflectWithProperType(t reflect.Type, key *Key, field reflect.Value, delim
return nil
}
// CR: copied from encoding/json/encode.go with modifications of time.Time support.
// TODO: add more test coverage.
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
@@ -359,16 +361,9 @@ func (s *Section) reflectFrom(val reflect.Value) error {
continue
}
opts := strings.Split(tag, ",")
if len(opts) > 2 {
return fmt.Errorf("Expected max. 2 comma-separated values in a tag, %d given", len(opts))
}
if len(opts) == 2 {
v := opts[1]
if v == "omitempty" && isEmptyValue(field) {
continue
}
opts := strings.SplitN(tag, ",", 2)
if len(opts) == 2 && opts[1] == "omitempty" && isEmptyValue(field) {
continue
}
fieldName := s.parseFieldName(tpField.Name, opts[0])
@@ -385,7 +380,7 @@ func (s *Section) reflectFrom(val reflect.Value) error {
sec, _ = s.f.NewSection(fieldName)
}
if err = sec.reflectFrom(field); err != nil {
return fmt.Errorf("error reflecting field(%s): %v", fieldName, err)
return fmt.Errorf("error reflecting field (%s): %v", fieldName, err)
}
continue
}
@@ -396,7 +391,7 @@ func (s *Section) reflectFrom(val reflect.Value) error {
key, _ = s.NewKey(fieldName, "")
}
if err = reflectWithProperType(tpField.Type, key, field, parseDelim(tpField.Tag.Get("delim"))); err != nil {
return fmt.Errorf("error reflecting field(%s): %v", fieldName, err)
return fmt.Errorf("error reflecting field (%s): %v", fieldName, err)
}
}
-10
View File
@@ -291,16 +291,6 @@ omitempty = 9
`)
})
Convey("Reflect from struct with too many opts in a tag", func() {
cfg := Empty()
type SpecialStruct struct {
TooManyOpts string `ini:"first_name,second,third"`
LastName string `ini:"last_name"`
}
So(ReflectFrom(cfg, &SpecialStruct{TooManyOpts: "John", LastName: "Doe"}), ShouldNotBeNil)
})
})
}