/
conditionsGenerator.go
151 lines (126 loc) · 3.76 KB
/
conditionsGenerator.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cmd
import (
"fmt"
"go/types"
"github.com/dave/jennifer/jen"
"github.com/ettle/strcase"
"github.com/FrancoLiberali/cql/cql-gen/cmd/log"
)
type ConditionsGenerator struct {
object types.Object
objectType Type
}
func NewConditionsGenerator(object types.Object) *ConditionsGenerator {
return &ConditionsGenerator{
object: object,
objectType: Type{Type: object.Type()},
}
}
// Add conditions for an object in the file
func (cg ConditionsGenerator) Into(file *File) error {
fields, err := getFields(cg.objectType)
if err != nil {
return err
}
log.Logger.Infof("Generating conditions for type %q in %s", cg.object.Name(), file.name)
// Add one condition for each field of the object
conditions := cg.ForEachField(file, fields)
objectName := cg.object.Name()
objectQual := jen.Qual(
getRelativePackagePath(file.destPkg, cg.objectType),
cg.objectType.Name(),
)
fieldIdentifiers := []jen.Code{}
conditionsModelType := getConditionsModelType(objectName)
conditionsModelAttributesDef := []jen.Code{}
conditionsModelAttributesIns := jen.Dict{}
for _, condition := range conditions {
if condition.ConditionMethod != nil {
file.Add(condition.ConditionMethod)
}
// add all field names to the list of fields of the preload condition
if condition.FieldDefinition != nil {
conditionsModelAttributesDef = append(conditionsModelAttributesDef, condition.FieldType)
conditionsModelAttributesIns[jen.Id(condition.FieldName)] = condition.FieldDefinition
if !condition.FieldIsCollection {
fieldIdentifiers = append(
fieldIdentifiers,
jen.Id(conditionsModelType).Dot(condition.FieldName),
)
}
}
// add the preload to the list of all possible preloads
if condition.PreloadRelationMethod != nil {
file.Add(condition.PreloadRelationMethod)
}
}
addConditionsModelDefinition(file, conditionsModelType, conditionsModelAttributesDef)
addConditionsModelInstantiation(file, objectName, conditionsModelType, conditionsModelAttributesIns)
addPreloadMethod(file, objectName, objectQual, conditionsModelType, fieldIdentifiers)
return nil
}
func addPreloadMethod(file *File, objectName string, objectQual *jen.Statement, conditionsModelType string, fieldIdentifiers []jen.Code) {
file.Add(
jen.Comment(fmt.Sprintf("Preload allows preloading the %s when doing a query", objectName)),
createMethod(conditionsModelType, preloadMethod).Params().Add(
jen.Qual(
conditionPath, cqlCondition,
).Types(
objectQual,
),
).Block(
jen.Return(
jen.Qual(
conditionPath, cqlNewPreloadCondition,
).Types(
objectQual,
).Call(fieldIdentifiers...),
),
),
)
}
func addConditionsModelInstantiation(file *File, objectName, conditionsModelType string, conditionsModelAttributes jen.Dict) {
file.Add(
jen.Var().Id(
objectName,
).Op("=").Add(
jen.Id(conditionsModelType).Values(
conditionsModelAttributes,
),
),
)
}
func addConditionsModelDefinition(file *File, conditionsModelType string, conditionsModelAttributes []jen.Code) {
file.Add(
jen.Type().Id(
conditionsModelType,
).Struct(
conditionsModelAttributes...,
),
)
}
func getConditionsModelType(objectName string) string {
return strcase.ToCamel(objectName) + "Conditions"
}
// Generate the conditions for each of the object's fields
func (cg ConditionsGenerator) ForEachField(file *File, fields []Field) []Condition {
conditions := []Condition{}
for _, field := range fields {
log.Logger.Debugf("Generating condition for field %q", field.Name)
if field.Embedded {
conditions = append(
conditions,
generateForEmbeddedField[Condition](
file,
field,
cg,
)...,
)
} else {
conditions = append(conditions, *NewCondition(
file.destPkg, cg.objectType, field,
))
}
}
return conditions
}