Skip to content

Commit

Permalink
feat: allow use of strict field names
Browse files Browse the repository at this point in the history
This change allows configuring a Decoder to not match folded field names by using the UseStrictNames function. It should be noted this is not exhaustively tested at this stage.
  • Loading branch information
james-d-elliott committed Nov 29, 2022
1 parent 7ad7038 commit 5d7d407
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type decodeState struct {
savedError error
useNumber bool
disallowUnknownFields bool
strict bool
}

// readIndex returns the position of the last byte read.
Expand Down Expand Up @@ -703,7 +704,7 @@ func (d *decodeState) object(v reflect.Value) error {
if i, ok := fields.nameIndex[string(key)]; ok {
// Found an exact name match.
f = &fields.list[i]
} else {
} else if !d.strict {
// Fall back to the expensive case-insensitive
// linear search.
for i := range fields.list {
Expand Down
3 changes: 3 additions & 0 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func NewDecoder(r io.Reader) *Decoder {
// Number instead of as a float64.
func (dec *Decoder) UseNumber() { dec.d.useNumber = true }

// UseStrictNames causes the Decoder to unmarshal only to exact field names.
func (dec *Decoder) UseStrictNames() { dec.d.strict = true }

// DisallowUnknownFields causes the Decoder to return an error when the destination
// is a struct and the input contains object keys which do not match any
// non-ignored, exported fields in the destination.
Expand Down

0 comments on commit 5d7d407

Please sign in to comment.