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

Replace MethodByName with type assertions #510

Merged
merged 2 commits into from
Sep 25, 2024
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
54 changes: 18 additions & 36 deletions apps/internal/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
)

const addField = "AdditionalFields"
const (
marshalJSON = "MarshalJSON"
unmarshalJSON = "UnmarshalJSON"
)

var (
leftBrace = []byte("{")[0]
Expand Down Expand Up @@ -106,48 +102,38 @@ func delimIs(got json.Token, want rune) bool {
// hasMarshalJSON will determine if the value or a pointer to this value has
// the MarshalJSON method.
func hasMarshalJSON(v reflect.Value) bool {
if method := v.MethodByName(marshalJSON); method.Kind() != reflect.Invalid {
_, ok := v.Interface().(json.Marshaler)
return ok
}

if v.Kind() == reflect.Ptr {
v = v.Elem()
} else {
if !v.CanAddr() {
return false
ok := false
if _, ok = v.Interface().(json.Marshaler); !ok {
var i any
if v.Kind() == reflect.Ptr {
i = v.Elem().Interface()
} else if v.CanAddr() {
i = v.Addr().Interface()
}
v = v.Addr()
}

if method := v.MethodByName(marshalJSON); method.Kind() != reflect.Invalid {
_, ok := v.Interface().(json.Marshaler)
return ok
_, ok = i.(json.Marshaler)
}
return false
return ok
}

// callMarshalJSON will call MarshalJSON() method on the value or a pointer to this value.
// This will panic if the method is not defined.
func callMarshalJSON(v reflect.Value) ([]byte, error) {
if method := v.MethodByName(marshalJSON); method.Kind() != reflect.Invalid {
marsh := v.Interface().(json.Marshaler)
if marsh, ok := v.Interface().(json.Marshaler); ok {
return marsh.MarshalJSON()
}

if v.Kind() == reflect.Ptr {
v = v.Elem()
if marsh, ok := v.Elem().Interface().(json.Marshaler); ok {
return marsh.MarshalJSON()
}
} else {
if v.CanAddr() {
v = v.Addr()
if marsh, ok := v.Addr().Interface().(json.Marshaler); ok {
return marsh.MarshalJSON()
}
}
}

if method := v.MethodByName(unmarshalJSON); method.Kind() != reflect.Invalid {
marsh := v.Interface().(json.Marshaler)
return marsh.MarshalJSON()
}

panic(fmt.Sprintf("callMarshalJSON called on type %T that does not have MarshalJSON defined", v.Interface()))
}

Expand All @@ -162,12 +148,8 @@ func hasUnmarshalJSON(v reflect.Value) bool {
v = v.Addr()
}

if method := v.MethodByName(unmarshalJSON); method.Kind() != reflect.Invalid {
_, ok := v.Interface().(json.Unmarshaler)
return ok
}

return false
_, ok := v.Interface().(json.Unmarshaler)
return ok
}

// hasOmitEmpty indicates if the field has instructed us to not output
Expand Down
17 changes: 16 additions & 1 deletion apps/internal/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type StructE struct {
AdditionalFields map[string]interface{}
}

func TestUnmarshal(t *testing.T) {
func TestUnmarshalRoundTrip(t *testing.T) {
now := time.Now()
nowJSON, err := now.MarshalJSON()
if err != nil {
Expand Down Expand Up @@ -147,6 +147,21 @@ func TestUnmarshal(t *testing.T) {
}
if diff := (&pretty.Config{IncludeUnexported: false}).Compare(test.want, test.got); diff != "" {
t.Errorf("TestUnmarshal(%s): -want/+got:\n%s", test.desc, diff)
continue
}
b, err := Marshal(test.got)
if err != nil {
t.Errorf("TestUnmarshal(%s): Marshal failed: %s", test.desc, err)
continue
}
err = Unmarshal(b, test.got)
if err != nil {
t.Errorf("TestUnmarshal(%s): Unmarshal round trip failed: %s", test.desc, err)
continue
}
if diff := (&pretty.Config{IncludeUnexported: false}).Compare(test.want, test.got); diff != "" {
t.Errorf("TestUnmarshal(%s): Round trip failed. -want/+got:\n%s", test.desc, diff)
continue
}
}
}
Expand Down