This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_to_mapping.go
More file actions
64 lines (52 loc) · 1.47 KB
/
schema_to_mapping.go
File metadata and controls
64 lines (52 loc) · 1.47 KB
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
package dataloading
import (
"strings"
"regexp"
)
var nonFriendlyCharacters = regexp.MustCompile(`[^a-zA-Z0-9_]+`)
var tooManyUnderscores = regexp.MustCompile(`_+`)
func friendlyName(name string) string {
friendly := strings.ToLower(name)
friendly = nonFriendlyCharacters.ReplaceAllString(friendly, "_")
friendly = tooManyUnderscores.ReplaceAllString(friendly, "_")
friendly = strings.Trim(friendly, "_")
return friendly
}
func SchemaToMapping(schemas []SourceSchema) (*SourceMapping) {
mappings := SourceMapping{ make([]Mapping, len(schemas)) }
for schemaIndex, schema := range schemas {
destFields := make([]MappingField, len(schema.Fields) + 2)
mappings.Sources[schemaIndex] = Mapping{
Name: schema.SourceName,
Destinations: []Destination{
Destination{
Name: friendlyName(schema.SourceName),
Fields: destFields,
},
},
}
keySource := make([]string, len(schema.Fields))
for fieldIndex, field := range schema.Fields {
keySource[fieldIndex] = field.FieldName
}
destFields[0] = MappingField{
Source: keySource,
Dest: "id",
}
destFields[1] = MappingField{
Source: keySource,
Dest: "revision",
}
for fieldIndex, field := range schema.Fields {
destFields[fieldIndex + 2] = MappingField{
Source: field.FieldName,
Dest: friendlyName(field.FieldName),
Type: field.FieldType,
}
if field.FieldType == "string" {
destFields[fieldIndex + 2].MaxLength = field.MaxLength * 2
}
}
}
return &mappings
}