Skip to content

Commit

Permalink
Merge pull request #17 from alex/cache-more
Browse files Browse the repository at this point in the history
Cache field encoders and setter
  • Loading branch information
ianlopshire authored Aug 23, 2019
2 parents f5b7a36 + 7c3dd2d commit 918824e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ func structSetter(v reflect.Value, raw rawValue) error {
if !fieldSpec.ok {
continue
}
sf := t.Field(i)
rawValue := rawValueFromLine(raw, fieldSpec.startPos, fieldSpec.endPos)
err := newValueSetter(sf.Type)(v.Field(i), rawValue)
err := fieldSpec.setter(v.Field(i), rawValue)
if err != nil {
sf := t.Field(i)
return &UnmarshalTypeError{string(rawValue.bytes), sf.Type, t.Name(), sf.Name, err}
}
}
Expand Down
2 changes: 1 addition & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func structEncoder(v reflect.Value) ([]byte, error) {
continue
}

val, err := newValueEncoder(v.Field(i).Type())(v.Field(i))
val, err := spec.encoder(v.Field(i))
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type structSpec struct {

type fieldSpec struct {
startPos, endPos int
encoder valueEncoder
setter valueSetter
ok bool
}

Expand All @@ -50,6 +52,8 @@ func buildStructSpec(t reflect.Type) structSpec {
if ss.fieldSpecs[i].endPos > ss.ll {
ss.ll = ss.fieldSpecs[i].endPos
}
ss.fieldSpecs[i].encoder = newValueEncoder(f.Type)
ss.fieldSpecs[i].setter = newValueSetter(f.Type)
}
return ss
}
Expand Down

0 comments on commit 918824e

Please sign in to comment.