-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
156 lines (122 loc) · 4.51 KB
/
doc.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
/*
Package maptrans provides a generic map to manipulate dictionaries.
There are many cases where we have some JSON object and we want to convert it to
another JSON object. Since Go is strongly typed, a useful approach is to first
translate JSON object into a map from string to an interface. Such map is called
Maptrans. We can then define a translation of one Maptrans into another Maptrans.
Translations are defined by the specially constructed Maptrans.
Translation Types
Translating field to another field with a different name
The simplest case is when we take a field from one object and present it in the
result under a different name. In this case we just write the source and
destination strings.
Example of name conversions
var someMap map[string]interface{} = map[string]interface{}{
"uuid": "UUID",
"name": "Name",
"age": "Age",
}
Translating field to another field and changing value.
We can provide a function which will translate field value to another value.
The function can also perform some verification of the input. For this we need
to describe convewrsion using MapElement object which is defined as
type MapElement struct {
TargetName string // Name of destination field
MapFunc MapFunc // Function that value to new value
ModFunc ModFunc // Function for object modification
Type TranslationType // Type of translation
Mandatory bool // The field must be present if true
SubTranslation ObjectMapDescription // Subtranslation map for children
}
There are several predefined MapFunc translators:
- IDMap translates any value to itself. It can be used to map common objects
to themselves.
- StringMap translates string to a string, trimming leading and trailing spaces.
- StringToLowerMap translates a string to lower-case string (and trims spaces).
- IdentifierMap does a string translation but rejects invalid identifiers.
An identifier should start with a letter or underscore and have only letters,
digits and underscores in it.
- IPAddrMap does a string translation of IP addresses which should be valid.
- CIDRMap does a string translation of IP addresses in a slash notation, e.g
. 1.2.3.4/24
- BoolMap converts boolean or string to a boolean.
- UUIDMap converts string to a string verifying that the source string is a
valid UUID
- StringArrayMap converts array of strings into another array of strings.
When Mandatory field is specified, the field must be present in the source
object.
Translating maps to maps
To translate one map into asnother, the Type should be specified as
ObjectTranslationn. The SubTranslation is the translation specification for
the internal object.
Translating array of objects.
To translate an arary of objects into another array of objects, the Type
should be specified as ObjectArrayTranslation. The SubTranslation defines
translation for each element of an array.
Using values to modify the original objects.
Example JSON object
{
"name": "myname"
"value": {
"fruit": "apple"
}
}
If we want to present this as a "flat" object
{
"name": "myname"
"fruit": "apple"
}
we need a ObjectArrayTranslation method.
Example
var translationDescr = map[string]interface{}{
"name": "Name"
"uuid": maptrans.MapElement{
TargetName: "UUID",
Mandatory: true,
MapFunc: maptrans.UUIDMap,
},
"alias": maptrans.MapElement{
TargetName: "Alias",
MapFunc: maptrans.IdentifierMap,
},
"force": maptrans.MapElement{
TargetName: "Force",
MapFunc: maptrans.BoolMap,
},
"info": maptrans.MapElement{
TargetName: "Info",
Mandatory: true,
Type: maptrans.ObjectTranslation,
SubTranslation: map[string]interface{}{
"Port": maptrans.MapElement{
Name: "port",
Mandatory: false,
MapFunc: maptrans.IntegerMap,
},
"IPAddress": maptrans.MapElement{
TargetName: "address",
Mandatory: true,
MapFunc: maptrans.CIDRMap,
},
"Route": maptrans.MapElement{
TargetName: "route",
Type: maptrans.ObjectArrayTranslation,
Mandatory: false,
SubTranslation: map[string]interface{}{
"Destination": maptrans.MapElement{
TargetName: "destination",
Mandatory: true,
MapFunc: maptrans.CIDRMap,
},
"Gateway": maptrans.MapElement{
TargetName: "gateway",
Mandatory: true,
MapFunc: maptrans.IPAddrMap,
},
},
},
},
},
}
*/
package maptrans