Skip to content

Commit e92f303

Browse files
committed
Fix more optional field regressions
1 parent 393237f commit e92f303

5 files changed

+10
-9
lines changed

client.go.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ func New{{.Name | firstLetterToUpper }}Client(addr string, client HTTPClient) {{
3434
{{- $inputs := $method.Inputs -}}
3535
{{- $outputs := $method.Outputs }}
3636

37-
func (c *{{$serviceName}}) {{.Name}}(ctx context.Context{{range $_, $input := $inputs}}, {{$input.Name}} {{template "type" dict "Type" $input.Type "TypeMap" $typeMap}}{{end}}) {{if len .Outputs}}({{end}}{{range $i, $output := .Outputs}}{{template "type" dict "Type" $output.Type "TypeMap" $typeMap}}{{if lt $i (len $method.Outputs)}}, {{end}}{{end}}error{{if len .Outputs}}){{end}} {
37+
func (c *{{$serviceName}}) {{.Name}}(ctx context.Context{{range $_, $input := $inputs}}, {{$input.Name}} {{template "type" dict "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap}}{{end}}) {{if len .Outputs}}({{end}}{{range $i, $output := .Outputs}}{{template "type" dict "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap}}{{if lt $i (len $method.Outputs)}}, {{end}}{{end}}error{{if len .Outputs}}){{end}} {
3838
{{- $inputVar := "nil" -}}
3939
{{- $outputVar := "nil" -}}
4040
{{- if $inputs | len}}
4141
{{- $inputVar = "in"}}
4242
in := struct {
4343
{{- range $i, $input := $inputs}}
44-
Arg{{$i}} {{template "type" dict "Type" $input.Type "TypeMap" $typeMap}} `json:"{{firstLetterToLower $input.Name}}"`
44+
Arg{{$i}} {{template "type" dict "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap}} `json:"{{firstLetterToLower $input.Name}}"`
4545
{{- end}}
4646
}{ {{- range $i, $input := $inputs}}{{if gt $i 0}}, {{end}}{{$input.Name}}{{end}}}
4747
{{- end}}
4848
{{- if $outputs | len}}
4949
{{- $outputVar = "&out"}}
5050
out := struct {
5151
{{- range $i, $output := .Outputs}}
52-
Ret{{$i}} {{template "type" dict "Type" $output.Type "TypeMap" $typeMap}} `json:"{{firstLetterToLower $output.Name}}"`
52+
Ret{{$i}} {{template "type" dict "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap}} `json:"{{firstLetterToLower $output.Name}}"`
5353
{{- end}}
5454
}{}
5555
{{- end}}

server.go.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context
7171
{{- if .Inputs|len}}
7272
reqContent := struct {
7373
{{- range $i, $input := .Inputs}}
74-
Arg{{$i}} {{template "type" dict "Type" $input.Type "TypeMap" $typeMap}} `json:"{{firstLetterToLower $input.Name}}"`
74+
Arg{{$i}} {{template "type" dict "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap}} `json:"{{firstLetterToLower $input.Name}}"`
7575
{{- end}}
7676
}{}
7777

@@ -93,7 +93,7 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context
9393

9494
// Call service method
9595
{{- range $i, $output := .Outputs}}
96-
var ret{{$i}} {{template "type" dict "Type" $output.Type "TypeMap" $typeMap}}
96+
var ret{{$i}} {{template "type" dict "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap}}
9797
{{- end}}
9898
func() {
9999
defer func() {
@@ -108,7 +108,7 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context
108108
{{- if .Outputs | len}}
109109
respContent := struct {
110110
{{- range $i, $output := .Outputs}}
111-
Ret{{$i}} {{template "type" dict "Type" $output.Type "TypeMap" $typeMap}} `json:"{{firstLetterToLower $output.Name}}"`
111+
Ret{{$i}} {{template "type" dict "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap}} `json:"{{firstLetterToLower $output.Name}}"`
112112
{{- end}}
113113
}{ {{- range $i, $_ := .Outputs}}{{if gt $i 0}}, {{end}}ret{{$i}}{{end}}}
114114
{{- end}}

struct.go.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type {{$struct.Name}} struct {
2525
{{- $dbTags = printf " db:%q" (get $meta "go.tag.db") -}}
2626
{{- end -}}
2727
{{- end }}
28-
{{$fieldName}} {{if ne $customType ""}}{{$customType}}{{else}}{{if and $field.Optional (not (isStructType $field.Type))}}*{{end}}{{template "type" dict "Type" $field.Type "TypeMap" $typeMap}}{{end}} `{{$jsonTags}}{{$dbTags}}`
28+
{{$fieldName}} {{if ne $customType ""}}{{$customType}}{{else}}{{template "type" dict "Type" $field.Type "Optional" $field.Optional "TypeMap" $typeMap}}{{end}} `{{$jsonTags}}{{$dbTags}}`
2929
{{- end}}
3030
}
3131
{{- end }}

type.go.tmpl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{- define "type" -}}
22

33
{{- $type := .Type -}}
4+
{{- $optional := .Optional -}}
45
{{- $typeMap := .TypeMap -}}
56

67
{{- if isMapType $type -}}
@@ -17,7 +18,7 @@
1718

1819
{{- else -}}
1920

20-
{{ get $typeMap $type }}
21+
{{if and $optional (not (isStructType $type))}}*{{end}}{{ get $typeMap $type }}
2122

2223
{{- end -}}
2324
{{- end -}}

types.go.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{{range .Services}}
2323
type {{.Name}} interface {
2424
{{- range $_, $method := .Methods}}
25-
{{.Name}}(ctx context.Context{{range $_, $input := .Inputs}}, {{$input.Name}} {{template "type" dict "Type" $input.Type "TypeMap" $typeMap}}{{end}}) {{if len .Outputs}}({{end}}{{range $i, $output := .Outputs}}{{template "type" dict "Type" $output.Type "TypeMap" $typeMap}}{{if lt $i (len $method.Outputs)}}, {{end}}{{end}}error{{if len .Outputs}}){{end}}
25+
{{.Name}}(ctx context.Context{{range $_, $input := .Inputs}}, {{$input.Name}} {{template "type" dict "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap}}{{end}}) {{if len .Outputs}}({{end}}{{range $i, $output := .Outputs}}{{template "type" dict "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap}}{{if lt $i (len $method.Outputs)}}, {{end}}{{end}}error{{if len .Outputs}}){{end}}
2626
{{- end}}
2727
}
2828
{{end}}

0 commit comments

Comments
 (0)