Skip to content

Commit ada547b

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent 5ac806c commit ada547b

File tree

3 files changed

+21
-43
lines changed

3 files changed

+21
-43
lines changed

gen/generator/golang/type.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,17 @@ const {{$c.Name}} {{$c.Type}} = {{$c.Value}}
147147
}
148148
{{- end}}
149149
150-
// Validate checks field values using generated validation expressions
151-
func (x *{{$s.Name}}) Validate() error {
152-
{{- range $f := $s.Fields}}
153-
{{- if $f.Validate}}
154-
{{$f.Validate}}
150+
{{- if $s.ValidateCount}}
151+
// Validate checks field values using generated validation expressions
152+
func (x *{{$s.Name}}) Validate() error {
153+
{{- range $f := $s.Fields}}
154+
{{- if $f.Validate}}
155+
{{$f.Validate}}
156+
{{- end}}
155157
{{- end}}
156-
{{- end}}
157-
return nil
158-
}
158+
return nil
159+
}
160+
{{- end}}
159161
160162
func (x *{{$s.Name}}) String() string {
161163
if x == nil {
@@ -344,6 +346,17 @@ func (t *Type) BindingCount() int {
344346
return count
345347
}
346348

349+
// ValidateCount returns the number of fields in the struct that have validation info
350+
func (t *Type) ValidateCount() int {
351+
var count int
352+
for _, f := range t.Fields {
353+
if f.Validate != nil {
354+
count++
355+
}
356+
}
357+
return count
358+
}
359+
347360
// convertTypes converts IDL struct types to Go struct types
348361
func convertTypes(ctx Context, doc tidl.Document) (_ []Type, err error) {
349362
var ret []Type

gen/testdata/manager/go/proto/manager.go

Lines changed: 0 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/testdata/manager/go/proto/stream.go

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)