/
MapNeat.kt
180 lines (149 loc) · 5.43 KB
/
MapNeat.kt
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
package net.andreinc.mapneat.dsl
import net.andreinc.mapneat.model.MapNeatObjectMap
import net.andreinc.mapneat.model.MapNeatSource
import net.andreinc.mapneat.operation.*
import org.apache.logging.log4j.kotlin.Logging
import java.util.*
/**
* This is the starting point of the DSL.
*
* The MapNeat class extends MapNeatObjectMap which holds internal representation of a JSON as a MutableMap<String, Any>
*/
class MapNeat(val inputJson: String, val parentObject: MapNeat? = null, val transformationId : String = UUID.randomUUID().toString()) : MapNeatObjectMap(inputJson), Logging {
fun hasParent() : Boolean {
return parentObject != null
}
fun parent() : MapNeat? {
return this.parentObject
}
init {
// All the operations associated with a transformation will be logged using the same ID
// for an easier tracking inside the logs
val printInput = if (inputJson == parent()?.inputJson) "INHERITED" else inputJson
logger.info { "Transformation(id=$transformationId, parentId=${parentObject?.transformationId}) INPUT = ${printInput}"}
}
private constructor(source: MapNeatSource) : this(source.getStringContent())
infix operator fun String.divAssign(constantValue: Any) {
Assign(sourceCtx, targetMap, transformationId).apply {
fullFieldPath = this@divAssign
value = constantValue
}.doOperation()
}
infix operator fun String.divAssign(acc: AssignOperationMethod) {
Assign(sourceCtx, targetMap, transformationId).apply {
fullFieldPath = this@divAssign
method = acc
}.doOperation()
}
infix fun String.assign(constantValue: Any) {
Assign(sourceCtx, targetMap, transformationId).apply {
fullFieldPath = this@assign
value = constantValue
}.doOperation()
}
infix fun String.assign(acc: AssignOperationMethod) {
Assign(sourceCtx, targetMap, transformationId).apply {
fullFieldPath = this@assign
method = acc
}.doOperation()
}
// Shift transformation DSL methods
infix operator fun String.timesAssign(value: String) {
Shift(sourceCtx, targetMap, transformationId).apply {
jsonPath = JsonPathQuery().apply {
expression = value
}
fullFieldPath = this@timesAssign
}.doOperation()
}
infix operator fun String.timesAssign(value: JsonPathQuery.() -> Unit) {
Shift(sourceCtx, targetMap, transformationId).apply {
jsonPath = JsonPathQuery().apply(value)
fullFieldPath = this@timesAssign
}.doOperation()
}
infix fun String.shift(value: String) {
Shift(sourceCtx, targetMap, transformationId).apply {
jsonPath = JsonPathQuery().apply {
expression = value
}
fullFieldPath = this@shift
}.doOperation()
}
infix fun String.shift(value: JsonPathQuery.() -> Unit) {
Shift(sourceCtx, targetMap, transformationId).apply {
jsonPath = JsonPathQuery().apply(value)
fullFieldPath = this@shift
}.doOperation()
}
// Delete transformation DSL methods
fun delete(value: String) {
Delete(sourceCtx, targetMap, transformationId)
.apply {
fullFieldPath = value
}.doOperation()
}
operator fun String.unaryMinus() {
Delete(sourceCtx, targetMap, transformationId)
.apply {
fullFieldPath = this@unaryMinus
}
.doOperation()
}
// Move transformation DSL methods
infix operator fun String.remAssign(value : String) {
Move(sourceCtx, targetMap, transformationId)
.apply {
this.fullFieldPath = this@remAssign
this.newField = value
}
.doOperation()
}
infix fun String.move(value: String) {
Move(sourceCtx, targetMap, transformationId)
.apply {
this.fullFieldPath = this@move
this.newField = value
}
.doOperation()
}
// Copy transformation DSL methods
infix fun String.copy(value: String) {
Copy(sourceCtx, targetMap, transformationId).apply{
fullFieldPath = this@copy
destination = value
}.doOperation()
}
infix operator fun String.rem(value: String) {
Copy(sourceCtx, targetMap, transformationId).apply {
fullFieldPath = this@rem
destination = value
}.doOperation()
}
override fun toString(): String {
return getPrettyString()
}
fun json(init: MapNeat.() -> Unit) : Map<String, Any> {
return MapNeat(super.source, this, transformationId)
.apply(init)
.getObjectMap()
}
fun json(json: String, init: MapNeat.() -> Unit) : Map<String, Any> {
return MapNeat(json, this, transformationId)
.apply(init)
.getObjectMap()
}
fun json(source: MapNeatSource, init: MapNeat.() -> Unit): Map<String, Any> {
return MapNeat(source.content, this, transformationId)
.apply(init)
.getObjectMap()
}
}
fun json(json: String, init: MapNeat.() -> Unit) : MapNeat {
return MapNeat(json)
.apply(init)
}
fun json(source: MapNeatSource, init: MapNeat.() -> Unit) : MapNeat {
return MapNeat(source.content)
.apply(init)
}