Skip to content

Commit 97a2a75

Browse files
authored
Teach struct types to render themselves to go code. (#348)
Rather than including all go rendering logic in a large writeTypes function, this patch teaches go structs that represent types and fields to render themselves with Render() methods. Then we can write and test each Render method independently, and combine them in writeTypes. Part of #350.
1 parent 24ae859 commit 97a2a75

14 files changed

+267
-93
lines changed

internal/generate/paths.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,27 +315,27 @@ func writeTpl(f *os.File, config methodTemplate) error {
315315
var err error
316316

317317
if config.IsListAll {
318-
t, err = template.ParseFiles("./templates/listall_method.tpl", "./templates/description.tpl")
318+
t, err = template.ParseFiles("./templates/listall_method.go.tpl", "./templates/description.go.tpl")
319319
if err != nil {
320320
return err
321321
}
322322
} else if config.ResponseType == "" && config.HasBody {
323-
t, err = template.ParseFiles("./templates/no_resptype_body_method.tpl", "./templates/description.tpl")
323+
t, err = template.ParseFiles("./templates/no_resptype_body_method.go.tpl", "./templates/description.go.tpl")
324324
if err != nil {
325325
return err
326326
}
327327
} else if config.ResponseType == "" {
328-
t, err = template.ParseFiles("./templates/no_resptype_method.tpl", "./templates/description.tpl")
328+
t, err = template.ParseFiles("./templates/no_resptype_method.go.tpl", "./templates/description.go.tpl")
329329
if err != nil {
330330
return err
331331
}
332332
} else if config.HasBody {
333-
t, err = template.ParseFiles("./templates/resptype_body_method.tpl", "./templates/description.tpl")
333+
t, err = template.ParseFiles("./templates/resptype_body_method.go.tpl", "./templates/description.go.tpl")
334334
if err != nil {
335335
return err
336336
}
337337
} else {
338-
t, err = template.ParseFiles("./templates/resptype_method.tpl", "./templates/description.tpl")
338+
t, err = template.ParseFiles("./templates/resptype_method.go.tpl", "./templates/description.go.tpl")
339339
if err != nil {
340340
return err
341341
}
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{splitDocString .Description}}
2+
{{.ValueType}} {{.Name}} {{.Value}}
3+
File renamed without changes.

internal/generate/templates/no_resptype_body_method.tpl renamed to internal/generate/templates/no_resptype_body_method.go.tpl

File renamed without changes.
File renamed without changes.

internal/generate/templates/resptype_body_method.tpl renamed to internal/generate/templates/resptype_body_method.go.tpl

File renamed without changes.
File renamed without changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{splitDocString .Description}}
2+
{{- if .Fields}}
3+
type {{.Name}} {{.Type}} {
4+
{{- range .Fields}}
5+
{{- if .Description}}
6+
{{splitDocString .Description}}
7+
{{- end}}
8+
{{.Name}} {{.Type}} {{.SerializationInfo}}
9+
{{- end}}
10+
}
11+
12+
{{else}}
13+
type {{.Name}} {{.Type}}
14+
{{end -}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Validate verifies all required fields for {{.AssociatedType}} are set
2+
func (p *{{.AssociatedType}}) Validate() error {
3+
v := new(Validator)
4+
{{- range .RequiredObjects}}
5+
v.HasRequiredObj(p.{{.}}, "{{.}}")
6+
{{- end}}
7+
{{- range .RequiredStrings}}
8+
v.HasRequiredStr(string(p.{{.}}), "{{.}}")
9+
{{- end}}
10+
{{- range .RequiredNums}}
11+
v.HasRequiredNum(p.{{.}}, "{{.}}")
12+
{{- end}}
13+
if !v.IsValid() {
14+
return fmt.Errorf("validation error:\n%v", v.Error())
15+
}
16+
return nil
17+
}

0 commit comments

Comments
 (0)