Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
})
})
})