46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package golang
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"reflect"
|
|
)
|
|
|
|
func (ipc *ipcCommon) serialize(arg any) any {
|
|
t := reflect.TypeOf(arg)
|
|
switch t.Kind() {
|
|
case reflect.Slice:
|
|
switch t.Elem().Name() {
|
|
case "uint8":
|
|
return map[string]any{
|
|
"t": "blob",
|
|
"d": base64.StdEncoding.EncodeToString(arg.([]byte)),
|
|
}
|
|
}
|
|
}
|
|
return arg
|
|
}
|
|
|
|
func (ipc *ipcCommon) ConvType(needType reflect.Type, gotType reflect.Type, arg any) any {
|
|
switch needType.Kind() {
|
|
case reflect.Int:
|
|
// JSON decodes any number to float64. If we need int, we should check and convert
|
|
if gotType.Kind() == reflect.Float64 {
|
|
floatArg := arg.(float64)
|
|
if float64(int64(floatArg)) == floatArg && !needType.OverflowInt(int64(floatArg)) {
|
|
arg = int(floatArg)
|
|
}
|
|
}
|
|
case reflect.Slice:
|
|
if needType.Elem().Kind() == reflect.Uint8 {
|
|
// Need []byte — incoming blob is a base64 string (from TS serialize)
|
|
if s, ok := arg.(string); ok {
|
|
decoded, err := base64.StdEncoding.DecodeString(s)
|
|
if err == nil {
|
|
arg = decoded
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return arg
|
|
}
|