Skip to content

Commit 14d1ee3

Browse files
authored
feat(core): add scw.JSONObject marshaler (#2721)
1 parent 2260b3d commit 14d1ee3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

internal/human/marshal_func.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package human
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"net"
67
"reflect"
@@ -117,6 +118,20 @@ func init() {
117118
}
118119
return strings.Join(res, " "), nil
119120
})
121+
registerMarshaler(func(i scw.JSONObject, opt *MarshalOpt) (string, error) {
122+
data, err := json.Marshal(i)
123+
if err != nil {
124+
return "", err
125+
}
126+
return string(data), nil
127+
})
128+
}
129+
130+
func registerMarshaler[T any](marshalFunc func(i T, opt *MarshalOpt) (string, error)) {
131+
var val T
132+
marshalerFuncs.Store(reflect.TypeOf(val), func(i interface{}, opt *MarshalOpt) (string, error) {
133+
return marshalFunc(i.(T), opt)
134+
})
120135
}
121136

122137
// TODO: implement the same logic as args.RegisterMarshalFunc(), where i must be a pointer

0 commit comments

Comments
 (0)