Skip to content

Commit

Permalink
remove types from callback
Browse files Browse the repository at this point in the history
remove types from callback
  • Loading branch information
parinpan committed Jan 7, 2024
1 parent 1c95b5b commit 2c54103
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func Marshal(anything any) (payload []byte, err error) {
return json.Marshal(anything)
}

err = walk(anything, func(t reflect.Type, v reflect.Value, marshaller bool, path string) error {
bytes, err := parse(t, v, marshaller)
err = walk(anything, func(v reflect.Value, marshaller bool, path string) error {
bytes, err := parse(v, marshaller)
if err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"strconv"
)

func parse(t reflect.Type, v reflect.Value, marshaller bool) ([]byte, error) {
func parse(v reflect.Value, marshaller bool) ([]byte, error) {
if !marshaller {
return defaultParser(t, v)
return defaultParser(v)
}
return marshalJSON(t, v)
return marshalJSON(v)
}

func marshalJSON(t reflect.Type, v reflect.Value) ([]byte, error) {
vPtr := reflect.NewAt(t.Elem(), v.UnsafePointer())
func marshalJSON(v reflect.Value) ([]byte, error) {
vPtr := reflect.NewAt(v.Elem().Type(), v.UnsafePointer())
results := vPtr.Elem().MethodByName("MarshalJSON").Call([]reflect.Value{})

if len(results) != 2 {
Expand All @@ -39,8 +39,8 @@ func marshalJSON(t reflect.Type, v reflect.Value) ([]byte, error) {
return b, err
}

func defaultParser(t reflect.Type, v reflect.Value) ([]byte, error) {
result, err := resolve(t, v)
func defaultParser(v reflect.Value) ([]byte, error) {
result, err := resolve(v)
if err != nil {
return nil, err
}
Expand All @@ -53,11 +53,11 @@ func defaultParser(t reflect.Type, v reflect.Value) ([]byte, error) {
return b, nil
}

func resolve(t reflect.Type, v reflect.Value) (any, error) {
func resolve(v reflect.Value) (any, error) {
vKind := v.Kind()
s := fmt.Sprint(reflect.Indirect(v))

if isBytes(t) {
if isBytes(v.Type()) {
return json.Marshal(s)
}

Expand Down
16 changes: 10 additions & 6 deletions walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"reflect"
)

type callbackFn func(t reflect.Type, v reflect.Value, marshaller bool, path string) error
type callbackFn func(v reflect.Value, marshaller bool, path string) error

func walk(anything any, cb callbackFn) error {
return walker(reflect.TypeOf(anything), reflect.ValueOf(anything), "", cb)
Expand All @@ -16,12 +16,12 @@ func walker(t reflect.Type, v reflect.Value, path string, cb callbackFn) error {
// check if the value is the type of marshaler
if isMarshaler(v) {
ref := toRef(v)
return cb(ref.Type(), ref, true, path)
return cb(ref, true, path)
}

// de-reference the value when it's a pointer - a value can be a type of marshaler
if t.Kind() == reflect.Ptr && isMarshaler(v.Elem()) {
return cb(t, v, true, path)
return cb(v, true, path)
}

switch t.Kind() {
Expand All @@ -35,20 +35,24 @@ func walker(t reflect.Type, v reflect.Value, path string, cb callbackFn) error {
}
case reflect.Slice, reflect.Array:
for idx := 0; idx < v.Len(); idx++ {
if err := walker(v.Index(idx).Type(), v.Index(idx), addPath(path, fmt.Sprint(idx)), cb); err != nil {
item := v.Index(idx)

if err := walker(item.Type(), item, addPath(path, fmt.Sprint(idx)), cb); err != nil {
return err
}
}
case reflect.Map:
for _, key := range v.MapKeys() {
if err := walker(v.MapIndex(key).Type(), v.MapIndex(key), addPath(path, key.String()), cb); err != nil {
item := v.MapIndex(key)

if err := walker(item.Type(), item, addPath(path, key.String()), cb); err != nil {
return err
}
}
case reflect.Ptr:
return walker(t.Elem(), v.Elem(), path, cb)
default:
return cb(t, v, false, path)
return cb(v, false, path)
}

return nil
Expand Down

0 comments on commit 2c54103

Please sign in to comment.