Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom interface serializers #104

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions types/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func deserializeType(d *Deserializer) reflect.Type {

func serializeAny(s *Serializer, t reflect.Type, p unsafe.Pointer) {
if serde, ok := types.serdeOf(t); ok {
serde.ser(s, p)
serde.ser(s, t, p)
return
}

Expand Down Expand Up @@ -93,7 +93,7 @@ func serializeAny(s *Serializer, t reflect.Type, p unsafe.Pointer) {

func deserializeAny(d *Deserializer, t reflect.Type, p unsafe.Pointer) {
if serde, ok := types.serdeOf(t); ok {
serde.des(d, p)
serde.des(d, t, p)
return
}

Expand Down Expand Up @@ -653,7 +653,7 @@ func deserializeFunc(d *Deserializer, t reflect.Type, p unsafe.Pointer) {
if fn.Type == nil {
panic(name + ": function type is missing")
}
if fn.Type != t {
if !t.AssignableTo(fn.Type) {
panic(name + ": function type mismatch: " + fn.Type.String() + " != " + t.String())
}

Expand Down
6 changes: 6 additions & 0 deletions types/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ func scan(s *Serializer, t reflect.Type, p unsafe.Pointer) {
return
}

// Don't scan types where custom serialization routines
// have been registered.
if _, ok := types.serdeOf(t); ok {
return
}

r := reflect.NewAt(t, p)
if _, ok := s.scanptrs[r]; ok {
return
Expand Down
53 changes: 43 additions & 10 deletions types/typemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,53 @@ func registerSerde[T any](tm *typemap,

t := reflect.TypeOf((*T)(nil)).Elem()

s := func(s *Serializer, p unsafe.Pointer) {
s := func(s *Serializer, actualType reflect.Type, p unsafe.Pointer) {
if t != actualType {
v := reflect.NewAt(actualType, p).Elem()
box := reflect.New(t)
box.Elem().Set(v.Convert(t))
p = box.UnsafePointer()
}
if err := serializer(s, (*T)(p)); err != nil {
panic(fmt.Errorf("serializing %s: %w", t, err))
}
}

d := func(d *Deserializer, p unsafe.Pointer) {
if err := deserializer(d, (*T)(p)); err != nil {
panic(fmt.Errorf("deserializing %s: %w", t, err))
d := func(d *Deserializer, actualType reflect.Type, p unsafe.Pointer) {
if t != actualType {
box := reflect.New(t)
boxp := box.UnsafePointer()
if err := deserializer(d, (*T)(boxp)); err != nil {
panic(fmt.Errorf("deserializing %s: %w", t, err))
}
v := reflect.NewAt(actualType, p)
reinterpreted := reflect.ValueOf(box.Elem().Interface())
v.Elem().Set(reinterpreted)
} else {
if err := deserializer(d, (*T)(p)); err != nil {
panic(fmt.Errorf("deserializing %s: %w", t, err))
}
}
}

tm.attach(t, s, d)
}

type serializerFunc func(*Serializer, unsafe.Pointer)
type deserializerFunc func(d *Deserializer, p unsafe.Pointer)
type serializerFunc func(*Serializer, reflect.Type, unsafe.Pointer)
type deserializerFunc func(*Deserializer, reflect.Type, unsafe.Pointer)

type serde struct {
id int
t reflect.Type
ser serializerFunc
des deserializerFunc
}

type typemap struct {
custom []reflect.Type
cache doublemap[reflect.Type, *typeinfo]
serdes map[reflect.Type]serde
custom []reflect.Type
cache doublemap[reflect.Type, *typeinfo]
serdes map[reflect.Type]serde
interfaces []serde
}

func newTypemap() *typemap {
Expand All @@ -99,15 +118,29 @@ func (m *typemap) attach(t reflect.Type, ser serializerFunc, des deserializerFun
s.id = len(m.custom)
m.custom = append(m.custom, t)
}
s.t = t
s.ser = ser
s.des = des

m.serdes[t] = s

if t.Kind() == reflect.Interface {
m.interfaces = append(m.interfaces, s)
}
}

func (m *typemap) serdeOf(x reflect.Type) (serde, bool) {
s, ok := m.serdes[x]
return s, ok
if ok {
return s, true
}
for i := range m.interfaces {
s := m.interfaces[i]
if x.Implements(s.t) {
return s, true
}
}
return serde{}, false
}

type doublemap[K, V comparable] struct {
Expand Down
Loading