convert return types in go template

This commit is contained in:
Egor Aristov 2025-11-16 07:31:42 +03:00
parent 2c6429065b
commit a83446019d
Signed by: egor3f
GPG Key ID: 40482A264AAEC85F
2 changed files with 24 additions and 14 deletions

View File

@ -50,6 +50,17 @@ func (g *GoApiGenerator) Generate(apis *api.Api, destFile string) error {
} }
return td, nil return td, nil
}, },
"convtype": func(valDef string, valType api.ValType) (string, error) {
td, ok := map[api.ValType]string{
api.TInt: fmt.Sprintf("int(%s.(float64))", valDef),
api.TString: fmt.Sprintf("%s.(string)", valDef),
api.TBool: fmt.Sprintf("%s.(bool)", valDef),
}[valType]
if !ok {
return "", fmt.Errorf("cannot convert type %v for val %s", valType, valDef)
}
return td, nil
},
"zerovalue": func(t api.ValType) (string, error) { "zerovalue": func(t api.ValType) (string, error) {
v, ok := map[api.ValType]string{ v, ok := map[api.ValType]string{
api.TInt: "0", api.TInt: "0",

View File

@ -1,5 +1,4 @@
{{- /*gotype: efprojects.com/kitten-ipc/kitcom/internal/golang.goGenData*/ -}}
{{- /*gotype: efprojects.com/kitten-ipc/kitcom.goGenData*/ -}}
// Code generated by kitcom. DO NOT EDIT. // Code generated by kitcom. DO NOT EDIT.
@ -27,7 +26,7 @@ func ({{$e.Name | receiver}} *{{$e.Name}}) {{$mtd.Name}}(
return {{ range $mtd.Ret }}{{ .Type | zerovalue }}, {{ end }} fmt.Errorf("call to {{ $e.Name }}.{{ $mtd.Name }} failed: %w", err) return {{ range $mtd.Ret }}{{ .Type | zerovalue }}, {{ end }} fmt.Errorf("call to {{ $e.Name }}.{{ $mtd.Name }} failed: %w", err)
} }
_ = results _ = results
return {{range $idx, $ret := $mtd.Ret}}results[{{$idx}}].({{$ret.Type | typedef}}), {{end}}nil return {{ range $idx, $ret := $mtd.Ret }}{{ convtype ( printf "results[%d]" $idx ) $ret.Type }}, {{ end }}nil
} }
{{ end }} {{ end }}