Skip to content

Commit

Permalink
Add Support To Pack Pointer and Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokangwang committed Oct 31, 2023
1 parent 784aaeb commit 0e38117
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (f *Field) Size(val reflect.Value, options *Options) int {
size = length * typ.Size()
} else if typ == CustomType {
return val.Addr().Interface().(Custom).Size(options)
} else if typ == CustomTypePointer {
return reflect.Indirect(val.Addr()).Interface().(Custom).Size(options)
} else if typ == CustomTypeInterface {
return val.Interface().(Custom).Size(options)
} else {
size = typ.Size()
}
Expand Down Expand Up @@ -140,6 +144,10 @@ func (f *Field) packVal(buf []byte, val reflect.Value, length int, options *Opti
}
case CustomType:
return val.Addr().Interface().(Custom).Pack(buf, options)
case CustomTypePointer:
return val.Addr().Interface().(Custom).Pack(buf, options)
case CustomTypeInterface:
return val.Interface().(Custom).Pack(buf, options)
default:
panic(fmt.Sprintf("no pack handler for type: %s", typ))
}
Expand Down
8 changes: 8 additions & 0 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ func (f Fields) Unpack(r io.Reader, val reflect.Value, options *Options) error {
if err := v.Addr().Interface().(Custom).Unpack(r, length, options); err != nil {
return err
}
} else if typ == CustomTypePointer {
if err := reflect.Indirect(v.Addr()).Interface().(Custom).Unpack(r, length, options); err != nil {
return err
}
} else if typ == CustomTypeInterface {
if err := v.Interface().(Custom).Unpack(r, length, options); err != nil {
return err
}
} else {
size := length * field.Type.Resolve(options).Size()
if size < 8 {
Expand Down
9 changes: 9 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ func parseField(f reflect.StructField) (fd *Field, tag *strucTag, err error) {
if _, ok := tmp.Interface().(Custom); ok {
fd.Type = CustomType
return
} else {
if _, ok := reflect.Indirect(tmp).Interface().(Custom); ok {
fd.Type = CustomTypePointer
return
}
if tmp.Elem().Type().Implements(reflect.TypeOf((*Custom)(nil)).Elem()) {
fd.Type = CustomTypeInterface
return
}
}
var defTypeOk bool
fd.defType, defTypeOk = reflectTypeMap[fd.kind]
Expand Down
5 changes: 4 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
SizeType
OffType
CustomType
CustomTypePointer
CustomTypeInterface
)

func (t Type) Resolve(options *Options) Type {
Expand Down Expand Up @@ -104,7 +106,8 @@ var typeLookup = map[string]Type{
}

var typeNames = map[Type]string{
CustomType: "Custom",
CustomType: "Custom",
CustomTypePointer: "CustomPointer",
}

func init() {
Expand Down

0 comments on commit 0e38117

Please sign in to comment.