Skip to content

Commit

Permalink
fixed decoding for "required" struct fields (#126)
Browse files Browse the repository at this point in the history
* fixed decoding for "required" struct fields

* added optimization if key is not in dot notation
  • Loading branch information
kucherenkovova authored and elithrar committed Nov 1, 2019
1 parent 6f159e3 commit 61751c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,15 @@ type fieldWithPrefix struct {
func isEmptyFields(fields []fieldWithPrefix, src map[string][]string) bool {
for _, f := range fields {
for _, path := range f.paths(f.prefix) {
if !isEmpty(f.typ, src[path]) {
v, ok := src[path]
if ok && !isEmpty(f.typ, v) {
return false
}
for key := range src {
if !isEmpty(f.typ, src[key]) && strings.HasPrefix(key, path) {
return false
}
}
}
}
return true
Expand Down
17 changes: 17 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,23 @@ func TestRequiredField(t *testing.T) {
}
}

type R2 struct {
A struct {
B int `schema:"b"`
} `schema:"a,required"`
}

func TestRequiredStructFiled(t *testing.T) {
v := map[string][]string{
"a.b": []string{"3"},
}
var a R2
err := NewDecoder().Decode(&a, v)
if err != nil {
t.Errorf("error: %v", err)
}
}

func TestRequiredFieldIsMissingCorrectError(t *testing.T) {
type RM1S struct {
A string `schema:"rm1aa,required"`
Expand Down

0 comments on commit 61751c9

Please sign in to comment.