-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper_type.go
88 lines (75 loc) · 1.82 KB
/
helper_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"strings"
)
type TypeTemplate struct {
Name string
Description string
Implements string
Fields []TypeField
ContainsInterface bool
ContainsInputFile bool
}
type TypeField struct {
Name string
FieldName string
Type string
Tag string
Description string
IsInterface bool
IsInputFile bool
}
func NewTypeFields(fields []Field, sections []Section) (newFields []TypeField, containsInterface, containsInputFile bool) {
for _, f := range fields {
var optionalField string
if f.IsOptional {
optionalField = ",omitempty"
}
tf := TypeField{
Name: snakeToPascal(f.Name),
FieldName: f.Name,
Type: getType(f.Name, f.Type, f.IsOptional, sections),
Tag: fmt.Sprintf("`json:\"%s%s\"`", f.Name, optionalField),
Description: f.Description,
}
tf.IsInputFile = tf.Type == "*InputFile"
if !tf.IsInputFile {
for _, s := range sections {
if s.Name == tf.Type && s.IsInterface {
tf.IsInterface = true
break
}
}
}
newFields = append(newFields, tf)
}
for _, f := range newFields {
if f.IsInterface {
containsInterface = true
break
}
}
for _, f := range newFields {
if f.IsInputFile {
containsInputFile = true
break
}
}
return
}
func getTypeTemplate(section Section, sections []Section, implementers map[string]string) TypeTemplate {
fields, containsInterface, containsInputFile := NewTypeFields(section.Fields, sections)
desc := strings.Join(section.Description, "\n// ")
if desc != "" {
desc = "// " + desc
}
return TypeTemplate{
Name: section.Name,
Description: desc,
Implements: implementers[section.Name],
Fields: fields,
ContainsInterface: containsInterface,
ContainsInputFile: containsInputFile,
}
}