-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping_properties_builder.go
268 lines (242 loc) · 7.11 KB
/
mapping_properties_builder.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package opensearchutil
import (
"fmt"
"reflect"
"time"
"github.com/pkg/errors"
)
type MappingPropertiesBuilder struct {
optionContainer mappingPropertiesBuilderOptionContainer
}
type fieldWrapper struct {
field reflect.StructField
kind reflect.Kind
value reflect.Value
isPrimitive bool
}
func NewMappingPropertiesBuilder(options ...MappingPropertiesBuilderOption) *MappingPropertiesBuilder {
optContainer := mappingPropertiesBuilderOptionContainer{
maxDepth: DefaultMaxDepth,
}
for _, o := range options {
o.apply(&optContainer)
}
if optContainer.fieldNameTransformer == nil {
optContainer.fieldNameTransformer = NewSnakeCaser()
}
if optContainer.jsonFormatter == nil {
optContainer.jsonFormatter = NewMarshalIndentJsonFormatter()
}
return &MappingPropertiesBuilder{optionContainer: optContainer}
}
func (b *MappingPropertiesBuilder) BuildMappingProperties(obj interface{}) ([]MappingProperty, error) {
mps, err := b.doBuildMappingProperties(obj, 1)
if err != nil {
return nil, errors.Wrapf(err, "b.doBuildMappingProperties")
}
return mps, nil
}
func (b *MappingPropertiesBuilder) doBuildMappingProperties(
obj interface{},
nthLevel uint8,
) ([]MappingProperty, error) {
var mappingProperties []MappingProperty
v := reflect.ValueOf(obj)
t := v.Type()
for i := 0; i < t.NumField(); i++ {
tField := t.Field(i)
fieldName := tField.Name
resolvedField := b.resolveField(tField, v.Field(i))
resolvedField = b.unslice(resolvedField)
if err := validateField(resolvedField); err != nil {
return nil, errors.Wrapf(err, "validateField")
}
fieldType, err := b.resolveFieldType(resolvedField)
if err != nil {
return nil, errors.Wrapf(err, "resolveFieldType")
}
fieldFormat, err := b.resolveFieldFormat(resolvedField)
if err != nil {
return nil, errors.Wrapf(err, "resolveFieldFormat")
}
transformedFieldName, err := b.optionContainer.fieldNameTransformer.TransformFieldName(fieldName)
if err != nil {
return nil, errors.Wrapf(err, "TransformFieldName")
}
if fieldType != "" {
mappingProperty := MappingProperty{
FieldName: transformedFieldName,
FieldType: fieldType,
FieldFormat: fieldFormat,
}
if err := b.addProperties(resolvedField, &mappingProperty); err != nil {
return nil, errors.Wrapf(err, "addProperties")
}
mappingProperties = append(mappingProperties, mappingProperty)
continue
} else if resolvedField.kind == reflect.Struct {
if nthLevel+1 > b.optionContainer.maxDepth {
continue
}
children, err := b.doBuildMappingProperties(resolvedField.value.Interface(), nthLevel+1)
if err != nil {
return nil, errors.Wrapf(err, "nested b.doBuildMappingProperties")
}
mappingProperties = append(mappingProperties, MappingProperty{
FieldName: transformedFieldName,
Children: children,
FieldFormat: fieldFormat,
})
continue
} else if !b.optionContainer.omitUnsupportedTypes {
return nil, fmt.Errorf(
"field not supported: %s, please use opensearchutil.OmitUnsupportedTypes to skip"+
" fields of unsupported types",
resolvedField.field.Name)
}
}
return mappingProperties, nil
}
func (b *MappingPropertiesBuilder) addProperties(resolvedField *fieldWrapper, mappingProperty *MappingProperty) error {
indexPrefixes := getTagOptionValue(resolvedField.field, tagKey, tagOptionIndexPrefixes)
if indexPrefixes != "" {
opts := parseCustomPropertyValue(indexPrefixes)
mappingProperty.IndexPrefixes = MakePtr(make(map[string]string, len(opts)))
for k, v := range opts {
(*mappingProperty.IndexPrefixes)[k] = v
}
}
analyzer := getTagOptionValue(resolvedField.field, tagKey, tagOptionAnalyzer)
if analyzer != "" {
mappingProperty.Analyzer = MakePtr(analyzer)
}
return nil
}
func validateField(field *fieldWrapper) error {
if field.kind == reflect.Struct {
switch field.value.Interface().(type) {
case time.Time:
return ErrGotBuiltInTimeField
}
}
return nil
}
func (b *MappingPropertiesBuilder) resolveFieldType(field *fieldWrapper) (string, error) {
fieldTypeOverride := getTagOptionValue(field.field, tagKey, tagOptionType)
if fieldTypeOverride != "" {
return fieldTypeOverride, nil
}
if field.isPrimitive {
return b.getDefaultOSTypeFromPrimitiveKind(field.kind), nil
}
if field.kind == reflect.Struct {
if x, ok := field.value.Interface().(OpenSearchDateType); ok {
if x.GetOpenSearchFieldType() != "" {
return "date", nil
}
}
}
return "", nil
}
func (b *MappingPropertiesBuilder) resolveFieldFormat(field *fieldWrapper) (*string, error) {
fieldFormatOverride := getTagOptionValue(field.field, tagKey, tagOptionFormat)
if fieldFormatOverride != "" {
return &fieldFormatOverride, nil
}
if field.kind == reflect.Struct {
if x, ok := field.value.Interface().(OpenSearchDateType); ok {
if x.GetOpenSearchFieldType() != "" {
return MakePtr(x.GetOpenSearchFieldType()), nil
}
}
}
return nil, nil
}
// resolveField returns a wrapper object for the given field. If the field is a pointer, it returns a wrapper
// for the dereferenced field, since we treat both pointer and value fields the same.
func (b *MappingPropertiesBuilder) resolveField(structField reflect.StructField, value reflect.Value) *fieldWrapper {
var kind reflect.Kind
var val reflect.Value
if structField.Type.Kind() == reflect.Ptr {
kind = structField.Type.Elem().Kind()
val = reflect.New(structField.Type.Elem()).Elem()
} else {
kind = structField.Type.Kind()
val = value
}
return &fieldWrapper{
field: structField,
kind: kind,
value: val,
isPrimitive: b.isPrimitive(kind),
}
}
// unslice "un-slices" the field by resolving the underlying element type. I.e. if it's a slice of struct Foo
// the returned object contains reflection objects for just Foo (not its slice).
func (b *MappingPropertiesBuilder) unslice(wrapper *fieldWrapper) *fieldWrapper {
if wrapper.kind != reflect.Slice {
return wrapper
}
elemType := wrapper.field.Type.Elem()
var (
newKind reflect.Kind
newVal reflect.Value
)
if elemType.Kind() == reflect.Ptr {
newKind = elemType.Elem().Kind()
newVal = reflect.New(elemType.Elem()).Elem()
} else {
newKind = elemType.Kind()
newVal = reflect.New(elemType).Elem()
}
return &fieldWrapper{
field: wrapper.field,
kind: newKind,
value: newVal,
isPrimitive: b.isPrimitive(newKind),
}
}
func (b *MappingPropertiesBuilder) isPrimitive(kind reflect.Kind) bool {
switch kind {
case reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64,
reflect.String:
return true
default:
return false
}
}
func (b *MappingPropertiesBuilder) getDefaultOSTypeFromPrimitiveKind(kind reflect.Kind) string {
switch kind {
case reflect.Bool:
return "boolean"
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
return "integer"
case reflect.Float32, reflect.Float64:
return "float"
case reflect.String:
return "text"
default:
return ""
}
}