Skip to content

add support for type meta to service method arguments #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 55 additions & 57 deletions _examples/golang-basics/example.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion _examples/golang-basics/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -out=./example.gen.go -fmt=false -legacyErrors=true
//go:generate webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -out=./example.gen.go -legacyErrors=true
package main

import (
Expand Down
6 changes: 3 additions & 3 deletions client.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ func New{{.Name | firstLetterToUpper }}Client(addr string, client HTTPClient) {{
{{- $inputs := $method.Inputs -}}
{{- $outputs := $method.Outputs }}

func (c *{{$serviceName}}) {{.Name}}(ctx context.Context{{range $_, $input := $inputs}}, {{$input.Name}} {{template "type" dict "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix}}{{end}}) {{if len .Outputs}}({{end}}{{range $i, $output := .Outputs}}{{template "type" dict "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix}}{{if lt $i (len $method.Outputs)}}, {{end}}{{end}}error{{if len .Outputs}}){{end}} {
func (c *{{$serviceName}}) {{.Name}}(ctx context.Context{{range $_, $input := $inputs}}, {{$input.Name}} {{template "field" dict "Name" $input.Name "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $input.Meta}}{{end}}) {{if len .Outputs}}({{end}}{{range $i, $output := .Outputs}}{{template "field" dict "Name" $output.Name "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $output.Meta}}{{if lt $i (len $method.Outputs)}}, {{end}}{{end}}error{{if len .Outputs}}){{end}} {
{{- $inputVar := "nil" -}}
{{- $outputVar := "nil" -}}
{{- if $inputs | len}}
{{- $inputVar = "in"}}
in := struct {
{{- range $i, $input := $inputs}}
Arg{{$i}} {{template "type" dict "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix}} `json:"{{$input.Name}}"`
Arg{{$i}} {{template "field" dict "Name" $input.Name "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $input.Meta "JsonTags" true}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{- end}}
}{ {{- range $i, $input := $inputs}}{{if gt $i 0}}, {{end}}{{$input.Name}}{{end}}}
{{- end}}
{{- if $outputs | len}}
{{- $outputVar = "&out"}}
out := struct {
{{- range $i, $output := .Outputs}}
Ret{{$i}} {{template "type" dict "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix}} `json:"{{$output.Name}}"`
Ret{{$i}} {{template "field" dict "Name" $output.Name "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $output.Meta "JsonTags" true}}
{{- end}}
}{}
{{- end}}
Expand Down
66 changes: 66 additions & 0 deletions field.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{{- define "field" -}}

{{- $type := .Type -}}
{{- $name := (coalesce .Name "") -}}
{{- $fieldName := $name | firstLetterToUpper -}}
{{- $typePrefix := .TypePrefix -}}
{{- $typeMeta := .TypeMeta -}}
{{- $typeMap := .TypeMap -}}
{{- $optional := .Optional -}}
{{- $printName := .PrintName -}}
{{- $includeJsonTags := .JsonTags -}}
{{- $includeStructTags := .StructTags -}}

{{- $customType := "" -}}
{{- $jsonTag := printf "json:%q" $name }}
{{- $structTags := array -}}
{{- range $meta := $typeMeta -}}
{{- if exists $meta "json" -}}
{{- $jsonTag = printf "json:%q" (get $meta "json") -}}
{{- end -}}
{{- if exists $meta "go.field.name" -}}
{{- $fieldName = get $meta "go.field.name" -}}
{{- end -}}
{{- if exists $meta "go.field.type" -}}
{{- $customType = get $meta "go.field.type" -}}
{{- end -}}
{{- if exists $meta "go.tag.json" -}}
{{- $jsonTag = printf "json:%q" (get $meta "go.tag.json") -}}
{{- end -}}
{{- range $metaKey, $metaValue := $meta -}}
{{- if and (hasPrefix $metaKey "go.tag.") (ne $metaKey "go.tag.json") -}}
{{- $structTags = append $structTags (printf "%s:%q" (trimPrefix $metaKey "go.tag.") $metaValue) -}}
{{- end -}}
{{- end -}}
{{- end }}

{{- if $printName -}}
{{$fieldName}}{{" "}}
{{- end -}}
{{- if (ne $customType "") -}}

{{$customType}}

{{- else if isMapType $type -}}

map[{{mapKeyType $type}}]{{template "field" dict "Name" $name "Type" (mapValueType $type) "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $typeMeta}}

{{- else if isListType $type -}}

[]{{template "field" dict "Name" $name "Type" (listElemType $type) "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $typeMeta}}

{{- else if isCoreType $type -}}

{{if $optional}}*{{end}}{{ get $typeMap $type }}

{{- else -}}{{- /* structs */ -}}

*{{$typePrefix}}{{$type}}

{{- end -}}

{{- if $includeJsonTags -}}
`{{$jsonTag}}{{if and $includeStructTags (len $structTags)}} {{join (sort $structTags) " "}}{{end}}`
{{- end -}}

{{- end -}}
Loading