diff --git a/implementation.go b/implementation.go index 129e9af..84e9c7f 100644 --- a/implementation.go +++ b/implementation.go @@ -43,6 +43,11 @@ func (f implementation) unmarshal(prefix Key, dest interface{}) error { for i := 0; i < vt.NumField(); i++ { field := vt.Field(i) value := v.Field(i) + tag := field.Tag.Get("flatpack") + // Skip processing the field. + if tag == "-" { + continue + } name[len(name)-1] = field.Name err := f.read(name, value) @@ -149,7 +154,7 @@ func (f implementation) read(name Key, value reflect.Value) error { } } case reflect.Struct: - f.unmarshal(name, value.Addr().Interface()) + err = f.unmarshal(name, value.Addr().Interface()) case reflect.Ptr: // Handle pointers by allocating if necessary, then recursively calling // ourselves. diff --git a/unmarshal_test.go b/unmarshal_test.go index 68ed2e1..391af15 100644 --- a/unmarshal_test.go +++ b/unmarshal_test.go @@ -34,6 +34,17 @@ type exoticPerson struct { Age chan bool } +type exoticTaggedPerson struct { + Email map[int]bool `flatpack:"-"` + Age chan bool `flatpack:"-"` + Mass float32 +} +type nestedExoticPerson struct { + Family struct { + Father map[string]bool + } +} + // For testing Validater callbacks type dysfunctionalPerson struct { person @@ -105,6 +116,14 @@ var _ = Describe("Unmarshal()", func() { Expect(Unmarshal(&got2)).To(MatchError("invalid value: cannot parse string as float (value=carol@example.com,error=invalid syntax)")) got3 := exoticPerson{} Expect(Unmarshal(&got3)).To(MatchError("invalid type: unsupported data type (key=Email,type=map[int]bool)")) + got4 := nestedExoticPerson{} + Expect(Unmarshal(&got4)).To(MatchError("invalid type: unsupported data type (key=Family.Father,type=map[string]bool)")) + }) + + It("skips the fields marked with flatpack tags", func() { + got := exoticTaggedPerson{} + Expect(Unmarshal(&got)).To(BeNil()) + Expect(got.Mass).To(Equal(float32(16.84))) }) }) })