-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapper.go
732 lines (658 loc) · 23.8 KB
/
mapper.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
package common_datalayer
import (
"fmt"
"math"
"reflect"
"regexp"
"strconv"
"strings"
"time"
egdm "github.com/mimiro-io/entity-graph-data-model"
)
type Mapper struct {
logger Logger
incomingMappingConfig *IncomingMappingConfig
outgoingMappingConfig *OutgoingMappingConfig
itemToEntityCustomTransform []func(item Item, entity *egdm.Entity) error
entityToItemCustomTransform []func(entity *egdm.Entity, item Item) error
}
func NewMapper(logger Logger, incomingMappingConfig *IncomingMappingConfig, outgoingMappingConfig *OutgoingMappingConfig) *Mapper {
mapper := &Mapper{
logger: logger,
incomingMappingConfig: incomingMappingConfig,
outgoingMappingConfig: outgoingMappingConfig,
itemToEntityCustomTransform: make([]func(item Item, entity *egdm.Entity) error, 0),
entityToItemCustomTransform: make([]func(entity *egdm.Entity, item Item) error, 0),
}
// ensure base URI ends with /
mapper.verifyBaseUri()
return mapper
}
func (mapper *Mapper) verifyBaseUri() {
if mapper.incomingMappingConfig != nil && mapper.incomingMappingConfig.BaseURI != "" {
if !strings.HasSuffix(mapper.incomingMappingConfig.BaseURI, "/") && !strings.HasSuffix(mapper.incomingMappingConfig.BaseURI, "#") {
mapper.incomingMappingConfig.BaseURI = mapper.incomingMappingConfig.BaseURI + "/"
}
}
if mapper.outgoingMappingConfig != nil && mapper.outgoingMappingConfig.BaseURI != "" {
if !strings.HasSuffix(mapper.outgoingMappingConfig.BaseURI, "/") && !strings.HasSuffix(mapper.outgoingMappingConfig.BaseURI, "#") {
mapper.outgoingMappingConfig.BaseURI = mapper.outgoingMappingConfig.BaseURI + "/"
}
}
}
func (mapper *Mapper) WithEntityToItemTransform(transform func(entity *egdm.Entity, item Item) error) *Mapper {
mapper.entityToItemCustomTransform = append(mapper.entityToItemCustomTransform, transform)
return mapper
}
func (mapper *Mapper) WithItemToEntityTransform(transform func(item Item, entity *egdm.Entity) error) *Mapper {
mapper.itemToEntityCustomTransform = append(mapper.itemToEntityCustomTransform, transform)
return mapper
}
type mutableItem struct {
item Item
constructedProperties map[string]any
}
func (m *mutableItem) GetValue(name string) any {
val, err := getValueFromItemOrConstruct(m.item, name, m.constructedProperties)
if err != nil {
return nil
}
return val
}
func (m *mutableItem) SetValue(name string, value any) { m.item.SetValue(name, value) }
func (m *mutableItem) NativeItem() any { return m.item.NativeItem() }
func (m *mutableItem) GetPropertyNames() []string { return m.item.GetPropertyNames() }
func (mapper *Mapper) MapItemToEntity(item Item, entity *egdm.Entity) error {
// ensure props and refs are not nil
if entity.Properties == nil {
entity.Properties = make(map[string]any)
}
if entity.References == nil {
entity.References = make(map[string]any)
}
// apply constructions
constructedProperties := make(map[string]any)
item = &mutableItem{item, constructedProperties}
if mapper.outgoingMappingConfig == nil {
return fmt.Errorf("outgoing mapping config is nil")
}
if mapper.outgoingMappingConfig.Constructions != nil {
for _, construction := range mapper.outgoingMappingConfig.Constructions {
switch construction.Operation {
case "concat":
if len(construction.Arguments) != 2 {
return fmt.Errorf("concat operation requires two arguments")
}
concatedValue, err := concat(item, construction.Arguments[0], construction.Arguments[1])
if err != nil {
return err
}
constructedProperties[construction.PropertyName] = concatedValue
case "split":
if len(construction.Arguments) != 2 {
return fmt.Errorf("split operation requires two arguments")
}
splitValue, err := split(item, construction.Arguments[0], construction.Arguments[1])
if err != nil {
return err
}
constructedProperties[construction.PropertyName] = splitValue
case "replace":
if len(construction.Arguments) != 3 {
return fmt.Errorf("replace operation requires three arguments")
}
replacedValue, err := replace(item, construction.Arguments[0], construction.Arguments[1], construction.Arguments[2])
if err != nil {
return err
}
constructedProperties[construction.PropertyName] = replacedValue
case "trim":
if len(construction.Arguments) != 1 {
return fmt.Errorf("trim operation requires one argument")
}
trimmedValue, err := trim(item, construction.Arguments[0])
if err != nil {
return err
}
constructedProperties[construction.PropertyName] = trimmedValue
case "tolower":
if len(construction.Arguments) != 1 {
return fmt.Errorf("tolower operation requires one argument")
}
tolowerValue, err := tolower(item, construction.Arguments[0])
if err != nil {
return err
}
constructedProperties[construction.PropertyName] = tolowerValue
case "toupper":
if len(construction.Arguments) != 1 {
return fmt.Errorf("toupper operation requires one argument")
}
toupperValue, err := toupper(item, construction.Arguments[0])
if err != nil {
return err
}
constructedProperties[construction.PropertyName] = toupperValue
case "regex":
if len(construction.Arguments) != 2 {
return fmt.Errorf("regex operation requires two arguments")
}
regexValue, err := regex(item, construction.Arguments[0], construction.Arguments[1])
if err != nil {
return err
}
constructedProperties[construction.PropertyName] = regexValue
case "slice":
if len(construction.Arguments) != 3 {
return fmt.Errorf("slice operation requires three arguments")
}
slicedValue, err := slice(item, construction.Arguments[0], construction.Arguments[1], construction.Arguments[2])
if err != nil {
return err
}
constructedProperties[construction.PropertyName] = slicedValue
case "literal":
if len(construction.Arguments) != 1 {
return fmt.Errorf("literal operation requires one argument")
}
constructedProperties[construction.PropertyName] = construction.Arguments[0]
}
}
}
if mapper.outgoingMappingConfig.MapAll {
// iterate over unmapped properties and add them to the entity
for _, propertyName := range item.GetPropertyNames() {
propertyValue := item.GetValue(propertyName)
entityPropertyName := mapper.outgoingMappingConfig.BaseURI + propertyName
value, err := mapper.mapSubEntities(propertyValue)
if err != nil {
return fmt.Errorf("failed to map sub entities. item: %+v, error: %w", item.NativeItem(), err)
}
entity.Properties[entityPropertyName] = value
}
}
// apply mappings
for _, mapping := range mapper.outgoingMappingConfig.PropertyMappings {
if mapping.Property == "" {
return fmt.Errorf("property name is required, mapping: %+v", mapping)
}
propertyName := mapping.Property
entityPropertyName := mapping.EntityProperty
if !strings.HasPrefix(entityPropertyName, "http") && entityPropertyName != "" {
if mapper.outgoingMappingConfig.BaseURI == "" {
return fmt.Errorf("base uri is required for mapping and entity_property isnt full URI. mapping: %+v", mapping)
}
entityPropertyName = mapper.outgoingMappingConfig.BaseURI + entityPropertyName
}
propertyValue, err := getValueFromItemOrConstruct(item, propertyName, constructedProperties)
if err != nil {
return fmt.Errorf("failed to get value from item or construct. item: %+v, error: %w", item.NativeItem(), err)
}
if propertyValue == nil {
if mapping.DefaultValue != nil {
propertyValue = mapping.DefaultValue
} else {
if mapping.Required || mapping.IsIdentity {
return fmt.Errorf("property %s is required. item: %+v", propertyName, item.NativeItem())
}
continue
}
}
if mapping.Datatype != "" {
var err error
switch mapping.Datatype {
case "integer", "int":
propertyValue, err = int32OfValue(propertyValue)
case "long":
propertyValue, err = int64OfValue(propertyValue)
case "float":
propertyValue, err = float32OfValue(propertyValue)
case "double":
propertyValue, err = float64OfValue(propertyValue)
case "bool":
propertyValue, err = boolOfValue(propertyValue)
case "string":
propertyValue, err = stringOfValue(propertyValue)
default:
return fmt.Errorf("unsupported datatype %s", mapping.Datatype)
}
if err != nil {
return fmt.Errorf("failed to convert value to datatype. item: %+v, error: %w", item.NativeItem(), err)
}
}
if mapping.IsIdentity {
idValue, err := stringOfValue(propertyValue)
if err != nil {
return fmt.Errorf("failed to convert identity value to string. item: %+v, error: %w", item.NativeItem(), err)
}
if mapping.URIValuePattern == "" {
return fmt.Errorf("url value pattern is required for identity property. mapping: %+v", mapping)
}
entity.ID = makeURL(mapping.URIValuePattern, idValue)
} else if mapping.IsReference {
if entityPropertyName == "" {
return fmt.Errorf("entity property name is required for mapping. mapping: %+v", mapping)
}
// reference property
var entityPropertyValue any
switch v := propertyValue.(type) {
case []string:
entityPropertyValue = make([]string, len(v))
for i, val := range v {
s, err := stringOfValue(val)
if err != nil {
return fmt.Errorf("failed to convert reference value to string value: %+v, item: %+v,error: %w", val, item.NativeItem(), err)
}
entityPropertyValue.([]string)[i] = makeURL(mapping.URIValuePattern, s)
}
default:
s, err := stringOfValue(propertyValue)
if err != nil {
return fmt.Errorf("failed to convert reference value to string value: %+v, item: %+v,error: %w", propertyValue, item.NativeItem(), err)
}
entityPropertyValue = makeURL(mapping.URIValuePattern, s)
}
entity.References[entityPropertyName] = entityPropertyValue
} else if mapping.IsDeleted {
if boolVal, ok := propertyValue.(bool); ok {
entity.IsDeleted = boolVal
} else {
return fmt.Errorf("IsDeleted property '%v' must be a bool. item: %+v", propertyName, item.NativeItem())
}
} else if mapping.IsRecorded {
intVal, err := int64OfValue(propertyValue)
if err != nil {
return fmt.Errorf("IsRecorded property '%v' must be a uint64 (unix timestamp), item: %+v, error: %w", propertyName, item.NativeItem(), err)
}
entity.Recorded = uint64(intVal)
} else {
if entityPropertyName == "" {
return fmt.Errorf("entity property name is required for mapping: %+v", mapping)
}
// check if property is a sub item
value, err := mapper.mapSubEntities(propertyValue)
if err != nil {
return fmt.Errorf("failed to map sub entities. item: %+v, error: %w", item.NativeItem(), err)
}
entity.Properties[entityPropertyName] = value
}
}
// apply custom transforms
if len(mapper.itemToEntityCustomTransform) > 0 {
for _, transform := range mapper.itemToEntityCustomTransform {
err := transform(item, entity)
if err != nil {
return fmt.Errorf("custom transform failed. mapper: %+v, item: %+v, error: %w", mapper, item.NativeItem(), err)
}
}
}
// apply default type if missing
if entity.References["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"] == nil && mapper.outgoingMappingConfig.DefaultType != "" {
entity.References["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"] = mapper.outgoingMappingConfig.DefaultType
}
return nil
}
// mapSubEntities takes a property value and maps it to a sub entity or array of sub entities if it is an Item or a slice of Items
// if the property value is not an Item or a slice of Items, it returns the property value as is
func (mapper *Mapper) mapSubEntities(propertyValue any) (any, error) {
var result any
switch propertyValue.(type) {
case []Item:
var subEntities []*egdm.Entity
for _, i := range propertyValue.([]Item) {
e := &egdm.Entity{Properties: make(map[string]any)}
err := mapper.MapItemToEntity(i, e)
if err != nil {
return propertyValue, fmt.Errorf("failed to map sub item to entity. item: %+v, error: %w", i.NativeItem(), err)
}
subEntities = append(subEntities, e)
}
result = subEntities
case Item:
e := &egdm.Entity{Properties: make(map[string]any)}
err := mapper.MapItemToEntity(propertyValue.(Item), e)
if err != nil {
return propertyValue, fmt.Errorf("failed to map sub item to entity. item: %+v, error: %w", propertyValue.(Item).NativeItem(), err)
}
result = e
default:
result = propertyValue
}
return result, nil
}
func getValueFromItemOrConstruct(item Item, propertyName string, constructedProperties map[string]any) (any, error) {
if val, ok := constructedProperties[propertyName]; ok {
return val, nil
} else {
return item.GetValue(propertyName), nil
}
}
func makeURL(urlPattern string, value string) string {
return strings.ReplaceAll(urlPattern, "{value}", value)
}
func regex(item Item, p1 string, pattern string) (string, error) {
s1, err := stringOfValue(item.GetValue(p1))
if err != nil {
return "", fmt.Errorf("regex: property '%s' could not be accessed. item: %+v, error: %w", p1, item.NativeItem(), err)
}
// Compile the regular expression pattern
re, err := regexp.Compile(pattern)
if err != nil {
return "", fmt.Errorf("regex: invalid pattern: '%s'. item: %+v, error: %w", pattern, item.NativeItem(), err)
}
// Find the first match in the string
match := re.FindString(s1)
if match == "" {
return "", fmt.Errorf("regex: no match. pattern: '%s', property: '%s' item: %+v, error: %w", pattern, p1, item.NativeItem(), err)
}
return match, nil
}
func slice(item Item, p1, p2, p3 string) (string, error) {
s1, err := stringOfValue(item.GetValue(p1))
if err != nil {
return "", fmt.Errorf("slice: property '%s' could not be accessed. item: %+v, error: %w", p1, item.NativeItem(), err)
}
start, err := int32OfValue(p2)
if err != nil {
return "", fmt.Errorf("slice: start index '%s' could not be parsed. item: %+v, error: %w", p2, item.NativeItem(), err)
}
end, err := int32OfValue(p3)
if err != nil {
return "", fmt.Errorf("slice: end index '%s' could not be parsed. item: %+v, error: %w", p3, item.NativeItem(), err)
}
return s1[start:end], nil
}
func tolower(item Item, p1 string) (string, error) {
s1, err := stringOfValue(item.GetValue(p1))
if err != nil {
return "", fmt.Errorf("tolower: property '%s' could not be accessed. item: %+v, error: %w", p1, item.NativeItem(), err)
}
return strings.ToLower(s1), nil
}
func toupper(item Item, p1 string) (string, error) {
s1, err := stringOfValue(item.GetValue(p1))
if err != nil {
return "", fmt.Errorf("toupper: property '%s' could not be accessed. item: %+v, error: %w", p1, item.NativeItem(), err)
}
return strings.ToUpper(s1), nil
}
func trim(item Item, p1 string) (string, error) {
s1, err := stringOfValue(item.GetValue(p1))
if err != nil {
return "", fmt.Errorf("trim: property '%s' could not be accessed. item: %+v, error: %w", p1, item.NativeItem(), err)
}
return strings.TrimSpace(s1), nil
}
func replace(item Item, p1 string, s2, s3 string) (string, error) {
s1, err := stringOfValue(item.GetValue(p1))
if err != nil {
return "", fmt.Errorf("replace: property '%s' could not be accessed. item: %+v, error: %w", p1, item.NativeItem(), err)
}
return strings.ReplaceAll(s1, s2, s3), nil
}
func split(item Item, valueProp string, splitProp string) ([]string, error) {
s1, err := stringOfValue(item.GetValue(valueProp))
if err != nil {
return nil, fmt.Errorf("split: property '%s' could not be accessed. item: %+v, error: %w", splitProp, item.NativeItem(), err)
}
s2, err := stringOfValue(item.GetValue(splitProp))
if err != nil {
return nil, fmt.Errorf("split: property '%s' could not be accessed. item: %+v, error: %w", splitProp, item.NativeItem(), err)
}
return strings.Split(s1, s2), nil
}
func concat(item Item, propName1, propName2 string) (string, error) {
s1, err := stringOfValue(item.GetValue(propName1))
if err != nil {
return "", fmt.Errorf("concat: property '%s' could not be accessed. item: %+v, error: %w", propName1, item.NativeItem(), err)
}
s2, err := stringOfValue(item.GetValue(propName2))
if err != nil {
return "", fmt.Errorf("concat: property '%s' could not be accessed. item: %+v, error: %w", propName2, item.NativeItem(), err)
}
return s1 + s2, nil
}
func int32OfValue(val any) (int, error) {
if val == nil {
return 0, fmt.Errorf("value is nil")
}
v := reflect.ValueOf(val)
t := v.Type()
var value int64
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value = v.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value = int64(v.Uint())
case reflect.Float32, reflect.Float64:
value = int64(v.Float())
case reflect.String:
var err error
value, err = strconv.ParseInt(v.String(), 10, strconv.IntSize)
if err != nil {
return 0, err
}
default:
return 0, fmt.Errorf("unsupported type %s", t.Kind())
}
if value < math.MinInt32 || value > math.MaxInt32 {
return 0, fmt.Errorf("value out of range for int type. Maybe try long(int64) instead")
}
return int(value), nil
}
func int64OfValue(val any) (int64, error) {
if val == nil {
return 0, fmt.Errorf("value is nil")
}
v := reflect.ValueOf(val)
t := v.Type()
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int64(v.Uint()), nil
case reflect.Float32, reflect.Float64:
return int64(v.Float()), nil
case reflect.String:
value, err := strconv.ParseInt(v.String(), 10, strconv.IntSize)
if err != nil {
return 0, err
}
if value < math.MinInt || value > math.MaxInt {
return 0, fmt.Errorf("value out of range for int type")
}
return value, nil
default:
return 0, fmt.Errorf("unsupported type %s", t.Kind())
}
}
func float64OfValue(val any) (float64, error) {
if val == nil {
return 0.0, fmt.Errorf("value is nil")
}
v := reflect.ValueOf(val)
t := v.Type()
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(v.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(v.Uint()), nil
case reflect.Float32, reflect.Float64:
return v.Float(), nil
case reflect.String:
return strconv.ParseFloat(v.String(), 64)
default:
return 0.0, fmt.Errorf("unsupported type %s", t.Kind())
}
}
func float32OfValue(val any) (float32, error) {
if val == nil {
return 0.0, fmt.Errorf("value is nil")
}
v := reflect.ValueOf(val)
t := v.Type()
var value float64
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value = float64(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value = float64(v.Uint())
case reflect.Float32, reflect.Float64:
value = v.Float()
case reflect.String:
var err error
value, err = strconv.ParseFloat(v.String(), 64)
if err != nil {
return 0, err
}
default:
return 0.0, fmt.Errorf("unsupported type %s", t.Kind())
}
if value < math.SmallestNonzeroFloat32 || value > math.MaxFloat32 {
return 0, fmt.Errorf("value out of range for Float32 type. Maybe use double(float64) instead")
}
return float32(value), nil
}
func boolOfValue(val any) (bool, error) {
if val == nil {
return false, fmt.Errorf("value is nil")
}
v := reflect.ValueOf(val)
t := v.Type()
switch t.Kind() {
case reflect.String:
return strconv.ParseBool(v.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.ParseBool(fmt.Sprint(v.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.ParseBool(fmt.Sprint(v.Uint()))
case reflect.Float32, reflect.Float64:
return strconv.ParseBool(fmt.Sprint(v.Float()))
case reflect.Bool:
return v.Bool(), nil
default:
return false, fmt.Errorf("unsupported type %s", t.Kind())
}
}
func stringOfValue(val interface{}) (string, error) {
if val == nil {
return "", fmt.Errorf("value is nil")
}
v := reflect.ValueOf(val)
t := v.Type()
k := t.Kind()
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf("%d", v.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return fmt.Sprintf("%d", v.Uint()), nil
case reflect.Float32, reflect.Float64:
return fmt.Sprintf("%f", v.Float()), nil
case reflect.String:
return v.String(), nil
case reflect.Bool:
return fmt.Sprintf("%t", v.Bool()), nil
case reflect.Struct:
if t.String() == "time.Time" {
return val.(time.Time).Format(time.RFC3339), nil
}
return "", fmt.Errorf("unsupported type %s (%s)", t.String(), t.Kind())
default:
return "", fmt.Errorf("unsupported type %s", t.Kind())
}
}
// Takes a URL and strips away everything up to the last / or #
func stripURL(url string) string {
if url == "" {
return ""
}
lastSlash := strings.LastIndex(url, "/")
lastHash := strings.LastIndex(url, "#")
if lastSlash > lastHash {
return url[lastSlash+1:]
} else if lastHash > lastSlash {
return url[lastHash+1:]
} else {
return url
}
}
func (mapper *Mapper) MapEntityToItem(entity *egdm.Entity, item Item) error {
// do map named as this is the more general case, then do the property mappings
if mapper.incomingMappingConfig.MapNamed {
for _, propertyName := range item.GetPropertyNames() {
entityPropertyName := mapper.incomingMappingConfig.BaseURI + propertyName
if propertyValue, ok := entity.Properties[entityPropertyName]; ok {
item.SetValue(propertyName, propertyValue)
}
}
}
for _, mapping := range mapper.incomingMappingConfig.PropertyMappings {
propertyName := mapping.Property
entityPropertyName := mapping.EntityProperty
if !strings.HasPrefix(entityPropertyName, "http") && entityPropertyName != "" {
if mapper.incomingMappingConfig.BaseURI == "" {
return fmt.Errorf("base uri is required for mapping and entity_property isnt full URI. mapping: %+v", mapping)
}
entityPropertyName = mapper.incomingMappingConfig.BaseURI + entityPropertyName
}
if mapping.IsIdentity {
if mapping.StripReferencePrefix {
item.SetValue(propertyName, stripURL(entity.ID))
} else {
item.SetValue(propertyName, entity.ID)
}
} else if mapping.IsReference {
// reference property
if referenceValue, ok := entity.References[entityPropertyName]; ok {
switch v := referenceValue.(type) {
case []string:
values := make([]string, len(v))
for i, val := range v {
if mapping.StripReferencePrefix {
values[i] = stripURL(val)
} else {
values[i] = val
}
}
item.SetValue(propertyName, values)
case string:
if mapping.StripReferencePrefix {
item.SetValue(propertyName, stripURL(v))
} else {
item.SetValue(propertyName, v)
}
default:
return fmt.Errorf("unsupported reference type %s, value %v, entityId: %s", reflect.TypeOf(referenceValue), referenceValue, entity.ID)
}
}
} else if mapping.IsDeleted {
item.SetValue(propertyName, entity.IsDeleted)
} else if mapping.IsRecorded {
item.SetValue(propertyName, entity.Recorded)
} else {
// regular property
propertyValue := entity.Properties[entityPropertyName]
item.SetValue(propertyName, propertyValue)
}
}
// apply custom transforms
if len(mapper.entityToItemCustomTransform) > 0 {
for _, transform := range mapper.entityToItemCustomTransform {
err := transform(entity, item)
if err != nil {
return fmt.Errorf("custom transform failed. mapper: %+v, entity: %+v, error: %w", mapper, entity, err)
}
}
}
return nil
}
type Item interface {
// GetValue returns the value of the property with the given name
GetValue(name string) any
// SetValue sets the value of the property with the given name
SetValue(name string, value any)
// NativeItem returns the underlying native item
NativeItem() any
// GetPropertyNames returns the names of all properties in the item
GetPropertyNames() []string
}