convert blobs in codegen code; avoid panics
This commit is contained in:
parent
4aac076caa
commit
4f8e3adc6d
@ -4,8 +4,8 @@ package main
|
||||
|
||||
import (
|
||||
kittenipc "efprojects.com/kitten-ipc"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type TsIpcApi struct {
|
||||
@ -21,7 +21,7 @@ func (self *TsIpcApi) Div(
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("call to TsIpcApi.Div failed: %w", err)
|
||||
}
|
||||
_ = results
|
||||
|
||||
return int(results[0].(float64)), nil
|
||||
}
|
||||
|
||||
@ -34,6 +34,11 @@ func (self *TsIpcApi) XorData(
|
||||
if err != nil {
|
||||
return []byte{}, fmt.Errorf("call to TsIpcApi.XorData failed: %w", err)
|
||||
}
|
||||
_ = results
|
||||
return self.Ipc.ConvType(reflect.TypeOf([]byte{}), reflect.TypeOf(""), results[0]).([]byte), nil
|
||||
|
||||
results[0], err = base64.StdEncoding.DecodeString(results[0].(string))
|
||||
if err != nil {
|
||||
return []byte{}, fmt.Errorf("call to TsIpcApi.XorData: error decoding blob: %w", err)
|
||||
}
|
||||
|
||||
return results[0].([]byte), nil
|
||||
}
|
||||
|
||||
@ -58,11 +58,7 @@ func (g *GoApiGenerator) Generate(apis *api.Api, destFile string) error {
|
||||
types.TInt: fmt.Sprintf("int(%s.(float64))", valDef),
|
||||
types.TString: fmt.Sprintf("%s.(string)", valDef),
|
||||
types.TBool: fmt.Sprintf("%s.(bool)", valDef),
|
||||
types.TBlob: fmt.Sprintf(
|
||||
"%s.Ipc.ConvType(reflect.TypeOf([]byte{}), reflect.TypeOf(\"\"), %s).([]byte)",
|
||||
defaultReceiver,
|
||||
valDef,
|
||||
),
|
||||
types.TBlob: fmt.Sprintf("%s.([]byte)", valDef),
|
||||
}[t]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("cannot convert type %v for val %s", t, valDef)
|
||||
|
||||
@ -6,8 +6,8 @@ package {{ .PkgName }}
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
kittenipc "efprojects.com/kitten-ipc"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
{{ range $e := .Api.Endpoints }}
|
||||
@ -26,7 +26,14 @@ func ({{ $e.Name | receiver }} *{{ $e.Name }}) {{ $mtd.Name }}(
|
||||
if err != nil {
|
||||
return {{ range $mtd.Ret }}{{ .Type | zerovalue }}, {{ end }} fmt.Errorf("call to {{ $e.Name }}.{{ $mtd.Name }} failed: %w", err)
|
||||
}
|
||||
_ = results
|
||||
{{ range $i, $ret := $mtd.Ret }}
|
||||
{{ if eq $ret.Type "blob" }}
|
||||
results[{{ $i }}], err = base64.StdEncoding.DecodeString(results[{{ $i }}].(string))
|
||||
if err != nil {
|
||||
return {{ range $mtd.Ret }}{{ .Type | zerovalue }}, {{ end }} fmt.Errorf("call to {{ $e.Name }}.{{ $mtd.Name }}: error decoding blob: %w", err)
|
||||
}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
return {{ range $idx, $ret := $mtd.Ret }}{{ convtype ( printf "results[%d]" $idx ) $ret.Type }}, {{ end }}nil
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
@ -283,19 +283,6 @@ func (ipc *ipcCommon) ConvType(needType reflect.Type, gotType reflect.Type, arg
|
||||
arg = int(floatArg)
|
||||
}
|
||||
}
|
||||
case reflect.Slice:
|
||||
switch needType.Elem().Kind() {
|
||||
case reflect.Uint8:
|
||||
if gotType.Kind() == reflect.String {
|
||||
// need []byte, got (base64) string
|
||||
var err error
|
||||
arg, err = base64.StdEncoding.DecodeString(arg.(string))
|
||||
if err != nil {
|
||||
// todo: avoid panicking. but this complicates codegen, so need to refactor somehow
|
||||
panic(fmt.Sprintf("decode base64: %s", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return arg
|
||||
}
|
||||
@ -307,7 +294,7 @@ func (ipc *ipcCommon) serialize(arg any) any {
|
||||
switch t.Elem().Name() {
|
||||
case "uint8":
|
||||
return map[string]any{
|
||||
"t": types.TBlob.String(),
|
||||
"t": types.TBlob,
|
||||
"d": base64.StdEncoding.EncodeToString(arg.([]byte)),
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user