Skip to content

Commit

Permalink
DRY: remove end() function by reusing traverse function for the last …
Browse files Browse the repository at this point in the history
…field to traverse
  • Loading branch information
emilgpa committed May 27, 2020
1 parent 1b566c0 commit 6f3cce7
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions formam.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,13 @@ func (dec *Decoder) analyzePath() (err error) {
continue
}
}
// last field of path
dec.field = dec.path[lastPos:]

return dec.end()
// last field of path, traverse it and decode the value in it
dec.field = dec.path[lastPos:]
if err := dec.traverse(); err != nil {
return err
}
return dec.decode()
}

// walk traverses the current path until to the last field
Expand All @@ -265,6 +268,7 @@ func (dec *Decoder) traverse() error {
return err
}
case reflect.Map:
// leave backward compatibility for access to maps by .
dec.traverseInMap(true)
}
dec.field = ""
Expand Down Expand Up @@ -300,6 +304,7 @@ func (dec *Decoder) traverse() error {
}
dec.curr = dec.curr.Index(index)
case reflect.Map:
// leave backward compatibility for access to maps by .
dec.traverseInMap(false)
default:
return newError(ErrCodeArrayIndex, dec.field, dec.path, "has an array index but it is a %v", dec.curr.Kind())
Expand Down Expand Up @@ -345,20 +350,6 @@ func (dec *Decoder) traverseInMap(byField bool) {
}
}

// end finds the last field for decode its value correspondent
func (dec *Decoder) end() error {
switch dec.curr.Kind() {
case reflect.Struct:
if err := dec.findStructField(); err != nil {
return err
}
case reflect.Map:
// leave backward compatibility for access to maps by .
dec.traverseInMap(true)
}
return dec.decode()
}

// decode sets the value in the field
func (dec *Decoder) decode() error {
// check if has UnmarshalText method or a custom function to decode it
Expand Down

0 comments on commit 6f3cce7

Please sign in to comment.