-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderingoptions.go
206 lines (172 loc) · 5.44 KB
/
renderingoptions.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @begin 2020-03-18
* @author <a href="mailto:[email protected]">Giuseppe Greco</a>
* @copyright 2020 <a href="skeeterhealth.com">Skeeter</a>
*/
package espressopp
import "github.com/pkg/errors"
// FieldProps is the set of properties associated with a field.
type FieldProps struct {
// Filterable specifies whether or not the field can be used in a query. If
// not and an expression actually contains it, the underlying CodeGenerator
// implementation shall raise an error.
Filterable bool
// NativeName is used to map those fields in an input expression that do
// not match the field names of the underlying database.
NativeName string
}
// namedParams lets code generators render named parameters and set aside their
// values for client code.
type namedParams struct {
// enabled specifies whether or not named parameters are enabled.
enabled bool
// prefix specifies the string that is prepended to parameter names.
prefix string
// values contains the values of the named parameters present in rendered
// code.
values map[string]string
}
// RenderingOptions is the set of options used by CodeGenerator implementations
// to control the way target code is generated.
type RenderingOptions struct {
fields map[string]*FieldProps
namedParams *namedParams
}
const (
defaultPrefix = "P"
)
// NewRenderingOptions creates a new instance of RenderingOptions.
func NewRenderingOptions() *RenderingOptions {
return &RenderingOptions{
fields: make(map[string]*FieldProps),
namedParams: &namedParams{
prefix: defaultPrefix,
},
}
}
// Clone performs a shallow copy of read-only data and a deep copy of
// read-write data. Read-only data includes field properties whereas
// read-write data includes named parameters.
func (ro *RenderingOptions) Clone() *RenderingOptions {
var m map[string]string
if ro.namedParams.values != nil {
m = make(map[string]string)
for k, v := range ro.namedParams.values {
m[k] = v
}
}
return &RenderingOptions{
fields: ro.fields,
namedParams: &namedParams{
enabled: ro.namedParams.enabled,
prefix: ro.namedParams.prefix,
values: m,
},
}
}
// Fields initializes the fields rendering options with the specified map of
// fieldName:fieldProps items. Previous fields rendering options are lost. If
// m is nil then all fields rendering options are removed.
func (ro *RenderingOptions) Fields(m map[string]*FieldProps) *RenderingOptions {
for k := range ro.fields {
delete(ro.fields, k)
}
if m != nil {
for k, v := range m {
if len(v.NativeName) == 0 {
v.NativeName = k
}
ro.fields[k] = &FieldProps{
Filterable: v.Filterable,
NativeName: v.NativeName,
}
}
}
return ro
}
// FieldsWithDefault initializes the fields rendering options with the specified
// map of fieldName:nativeFieldName items. Previous fields rendering options are
// lost and Filterable is default to true for each field. If m is nil then all
// fields rendering options are removed.
func (ro *RenderingOptions) FieldsWithDefault(m map[string]string) *RenderingOptions {
for k := range ro.fields {
delete(ro.fields, k)
}
if m != nil {
for k, v := range m {
if len(v) == 0 {
v = k
}
ro.fields[k] = &FieldProps{
Filterable: true,
NativeName: v,
}
}
}
return ro
}
// AddFieldProps adds the specified field properties to the rendering options.
func (ro *RenderingOptions) AddFieldProps(fieldName string, fp *FieldProps) error {
if len(fieldName) == 0 {
return errors.New("field name not specified")
}
if fp == nil {
return errors.Errorf("properties for field %v not specified", fieldName)
}
if len(fp.NativeName) == 0 {
fp.NativeName = fieldName
}
ro.fields[fieldName] = fp
return nil
}
// RemoveFieldProps removes the properties of the specified field from the
// rendering options.
func (ro *RenderingOptions) RemoveFieldProps(fieldName string) *FieldProps {
fp := ro.fields[fieldName]
if fp != nil {
delete(ro.fields, fieldName)
}
return fp
}
// GetFieldProps retrieves the properties of the specified field from the
// rendering options.
func (ro *RenderingOptions) GetFieldProps(fieldName string) *FieldProps {
return ro.fields[fieldName]
}
// EnableNamedParams enables named parameters in rendered code.
func (ro *RenderingOptions) EnableNamedParams() {
if !ro.namedParams.enabled {
ro.namedParams.enabled = true
ro.namedParams.values = make(map[string]string)
}
}
// DisableNamedParams disables named parameters in rendered code.
func (ro *RenderingOptions) DisableNamedParams() {
ro.namedParams.enabled = false
ro.namedParams.values = nil
}
// NamedParamsEnabled returns a Boolean value indicating whether or not
// named parameters are enabled.
func (ro *RenderingOptions) NamedParamsEnabled() bool {
return ro.namedParams.enabled
}
// GetNamedParamValues returns a map containing the values of the named
// parameters present in rendered code.
func (ro *RenderingOptions) GetNamedParamValues() (map[string]string, error) {
if !ro.namedParams.enabled {
return nil, errors.New("named parameters not enabled")
}
return ro.namedParams.values, nil
}
// SetNamedParamsPrefix sets the string that is prepended to parameter names.
func (ro *RenderingOptions) SetNamedParamsPrefix(p string) {
prefix := p
if prefix == "" {
prefix = defaultPrefix
}
ro.namedParams.prefix = prefix
}
// getNamedParamsPrefix gets the string that is prepended to parameter names.
func (ro *RenderingOptions) GetNamedParamsPrefix() string {
return ro.namedParams.prefix
}