Skip to content

Commit

Permalink
Fix serde to support context.Context serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletier committed Sep 25, 2023
1 parent dbb0561 commit 306d8c1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 1 addition & 2 deletions internal/serde/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,7 @@ func serializeInterface(s *Serializer, t reflect.Type, p unsafe.Pointer) {
return
}

x := *(*interface{})(p)
et := reflect.TypeOf(x)
et := reflect.TypeOf(reflect.NewAt(t, p).Elem().Interface())
serializeType(s, et)

eptr := i.ptr
Expand Down
4 changes: 2 additions & 2 deletions internal/serde/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ func scan(s *Serializer, t reflect.Type, p unsafe.Pointer) {
scan(s, et, ep)
}
case reflect.Interface:
x := *(*interface{})(p)
et := reflect.TypeOf(x)
et := reflect.TypeOf(r.Elem().Interface())
eptr := (*iface)(p).ptr

if eptr == nil {
return
}
Expand Down
6 changes: 6 additions & 0 deletions serde/serde_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package serde_test

import (
"bytes"
"context"
"encoding/binary"
"fmt"
"net/http"
Expand All @@ -26,6 +27,7 @@ func TestReflect(t *testing.T) {
intv := int(100)
intp := &intv
intpp := &intp
type ctxKey1 struct{}

cases := []any{
"foo",
Expand Down Expand Up @@ -63,6 +65,10 @@ func TestReflect(t *testing.T) {
func(int) int { return 42 },

[1]*int{intp},

context.Background(),
context.TODO(),
context.WithValue(context.Background(), ctxKey1{}, "hello"),
}

for _, x := range cases {
Expand Down

0 comments on commit 306d8c1

Please sign in to comment.