-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathYamlDeprecated.kt
More file actions
184 lines (153 loc) · 5.78 KB
/
YamlDeprecated.kt
File metadata and controls
184 lines (153 loc) · 5.78 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
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
package ftl.args.yml
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.MissingNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.google.common.annotations.VisibleForTesting
import ftl.args.ArgsHelper.yamlMapper
import ftl.util.FlankFatalError
import ftl.util.copyAndClose
import java.io.Reader
import java.io.StringReader
import java.nio.file.Files
import java.nio.file.Path
object YamlDeprecated {
@Suppress("EnumEntryName", "EnumNaming")
enum class Parent { gcloud, flank }
enum class Level { Warning, Error }
private data class Key(
val parent: Parent,
val name: String
)
private data class ModifiedKey(
val old: Key,
val new: Key,
val level: Level
)
private val transforms = listOf(
ModifiedKey(
Key(Parent.gcloud, "flaky-test-attempts"),
Key(Parent.gcloud, "num-flaky-test-attempts"),
Level.Warning
),
// flank: testShards -> flank: maxTestShards
ModifiedKey(
Key(Parent.flank, "testShards"),
Key(Parent.flank, "max-test-shards"),
Level.Warning
),
ModifiedKey(
Key(Parent.flank, "shardTime"),
Key(Parent.flank, "shard-time"),
Level.Warning
),
ModifiedKey(
Key(Parent.flank, "repeatTests"),
Key(Parent.flank, "num-test-runs"),
Level.Warning
),
ModifiedKey(
Key(Parent.flank, "repeat-tests"),
Key(Parent.flank, "num-test-runs"),
Level.Warning
),
ModifiedKey(
Key(Parent.flank, "smartFlankGcsPath"),
Key(Parent.flank, "smart-flank-gcs-path"),
Level.Warning
),
ModifiedKey(
Key(Parent.flank, "disableSharding"),
Key(Parent.flank, "disable-sharding"),
Level.Warning
),
ModifiedKey(
Key(Parent.gcloud, "project"),
Key(Parent.flank, "project"),
Level.Warning
)
)
private data class Transform(
val keyValue: JsonNode,
val key: ModifiedKey
)
private fun JsonNode.remove(parent: Parent, child: String) {
(this[parent.toString()] as ObjectNode).remove(child)
}
private fun JsonNode.replace(parent: Parent, child: String, value: JsonNode) {
val parentKey = parent.toString()
(this[parentKey] as ObjectNode).replace(child, value)
}
private fun JsonNode.createParents() {
Parent.values().forEach { parent ->
val parentKey = parent.toString()
val parentValue = this[parentKey]
// if the parent node ('flank:') doesn't exist then add it ('flank: {}') to the YAML
val nullParent = parentValue == null || parentValue.toString() == "null"
if (nullParent) (this as ObjectNode).set(parentKey, JsonNodeFactory.instance.objectNode()) as JsonNode
}
}
private fun mutate(parsed: JsonNode, changes: List<Transform>) {
changes.forEach { transform ->
mutateNode(parsed, transform.keyValue, transform.key.old, transform.key.new)
}
}
private fun mutateNode(parsed: JsonNode, keyValue: JsonNode, old: Key, new: Key) {
parsed.remove(parent = old.parent, child = old.name)
parsed.replace(parent = new.parent, child = new.name, value = keyValue)
}
private fun validate(key: Key, keyValue: JsonNode): Transform? {
transforms.forEach {
if (it.old == key) {
println("${it.level}: `${it.old.parent}: ${it.old.name}:` renamed to `${it.new.parent}: ${it.new.name}:`")
return Transform(keyValue, it)
}
}
return null
}
private val yamlWriter by lazy { yamlMapper.writerWithDefaultPrettyPrinter() }
fun modify(yamlPath: Path): Boolean {
if (yamlPath.toFile().exists().not()) throw FlankFatalError("Flank yml doesn't exist at path $yamlPath")
val (errorDetected, string) = modify(Files.newBufferedReader(yamlPath))
copyAndClose(
from = StringReader(string),
to = Files.newBufferedWriter(yamlPath)
)
println("\nUpdated ${yamlPath.fileName} file")
return errorDetected
}
// Throw exception when Level.Error modified key is found.
fun modifyAndThrow(yamlReader: Reader, android: Boolean): String {
val (error, data) = modify(yamlReader)
if (error) {
val platform = if (android) "android" else "ios"
throw FlankFatalError("Invalid keys detected! Auto fix with: flank $platform doctor --fix")
}
return data
}
@VisibleForTesting
internal fun modify(yamlData: Reader): Pair<Boolean, String> {
val mappedYaml = yamlMapper.readTree(yamlData)
val parsed = if (mappedYaml == null || mappedYaml is MissingNode) {
JsonNodeFactory.instance.objectNode()
} else {
mappedYaml
}
parsed.createParents()
yamlMapper.writerWithDefaultPrettyPrinter()
var errorDetected = false
val changes = mutableListOf<Transform>()
listOf("gcloud", "flank").forEach { keyType ->
parsed[keyType]?.fields()?.forEach { (keyName, keyValue) ->
val type = Parent.valueOf(keyType)
val newChange = validate(Key(type, keyName), keyValue)
if (newChange != null) {
changes.add(newChange)
if (newChange.key.level == Level.Error) errorDetected = true
}
}
}
mutate(parsed, changes)
return errorDetected to yamlWriter.writeValueAsString(parsed)
}
}