From 7c3dd2d1b7185ca12bfa149942187cfb8face4e1 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 23 Aug 2019 16:35:36 -0400 Subject: [PATCH] Cache field encoders and setter --- decode.go | 4 ++-- encode.go | 2 +- tags.go | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/decode.go b/decode.go index d9e0d34..cd490b1 100644 --- a/decode.go +++ b/decode.go @@ -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} } } diff --git a/encode.go b/encode.go index 64911f2..6f4902f 100644 --- a/encode.go +++ b/encode.go @@ -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 } diff --git a/tags.go b/tags.go index 71a2a04..8377daf 100644 --- a/tags.go +++ b/tags.go @@ -37,6 +37,8 @@ type structSpec struct { type fieldSpec struct { startPos, endPos int + encoder valueEncoder + setter valueSetter ok bool } @@ -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 }