Skip to content

Commit

Permalink
diff: reduce (ethereum#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
tynes authored Mar 9, 2021
1 parent a74a334 commit b6475d7
Showing 1 changed file with 0 additions and 39 deletions.
39 changes: 0 additions & 39 deletions rlp/typecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ type tags struct {

// rlp:"-" ignores fields.
ignored bool

// rlp:"?" means that this field is not present in the RLP-encoded object if it is nil.
// Note: fields like this must be at the end of the struct.
omittedIfNil bool
}

// typekey is the key of a type in typeCache. It includes the struct tags because
Expand Down Expand Up @@ -178,8 +174,6 @@ func parseStructTag(typ reflect.Type, fi, lastPublic int) (tags, error) {
if f.Type.Kind() != reflect.Slice {
return ts, structTagError{typ, f.Name, t, "field type is not slice"}
}
case "?":
ts.omittedIfNil = true
default:
return ts, fmt.Errorf("rlp: unknown struct tag %q on %v.%s", t, typ, f.Name)
}
Expand All @@ -199,40 +193,7 @@ func lastPublicField(typ reflect.Type) int {

func (i *typeinfo) generate(typ reflect.Type, tags tags) {
i.decoder, i.decoderErr = makeDecoder(typ, tags)
omitIfEmpty := tags.omittedIfNil && typ.Kind() == reflect.Ptr
if i.decoderErr == nil && omitIfEmpty {
i.decoder, i.decoderErr = wrapDecoderToAccountForOmitted(typ, i.decoder)
}

i.writer, i.writerErr = makeWriter(typ, tags)
if i.writerErr == nil && omitIfEmpty {
i.writer, i.writerErr = wrapWriterToAccountForOmitted(i.writer)
}
}

// wrapDecoderToAccountForOmitted handles decoding nullable types that are omitted when empty.
// If omitted, this decoder will set the value to nil.
func wrapDecoderToAccountForOmitted(typ reflect.Type, decoder decoder) (decoder, error) {
nilPtr := reflect.Zero(typ)
return func(s *Stream, val reflect.Value) (err error) {
if err = decoder(s, val); err != nil && err == EOL {
val.Set(nilPtr)
return nil
}
return err
}, nil
}

// wrapWriterToAccountForOmitted handles writing nullable types that are omitted when empty.
// If omitted, this writer omit it from the encoded buffer.
func wrapWriterToAccountForOmitted(writer writer) (writer, error) {
return func(val reflect.Value, w *encbuf) (err error) {
if val.IsNil() {
// Omit if nil
return nil
}
return writer(val, w)
}, nil
}

// defaultNilKind determines whether a nil pointer to typ encodes/decodes
Expand Down

0 comments on commit b6475d7

Please sign in to comment.