Skip to content

Commit 3b4dbb8

Browse files
committed
updating task to account for default values and multi-line config definitions
1 parent f1397f6 commit 3b4dbb8

File tree

1 file changed

+59
-16
lines changed

1 file changed

+59
-16
lines changed

buildSrc/src/main/kotlin/datadog/gradle/plugin/config/ConfigInversionLinter.kt

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ private fun registerCheckEnvironmentVariablesUsage(project: Project) {
126126
}
127127
}
128128

129-
/** Registers `checkConfigStrings` to validate config strings against documented supported configurations. */
129+
/** Registers `checkConfigStrings` to validate config definitions against documented supported configurations. */
130130
private fun registerCheckConfigStringsTask(project: Project, extension: SupportedTracerConfigurations) {
131131
val ownerPath = extension.configOwnerPath
132132
val generatedFile = extension.className
133133

134134
project.tasks.register("checkConfigStrings") {
135135
group = "verification"
136-
description = "Validates that all config definitions in dd-trace-api/src/main/java/datadog/trace/api/config exist in metadata/supported-configurations.json"
136+
description = "Validates that all config definitions in `dd-trace-api/src/main/java/datadog/trace/api/config` exist in `metadata/supported-configurations.json`"
137137

138138
val mainSourceSetOutput = ownerPath.map {
139139
project.project(it)
@@ -161,38 +161,81 @@ private fun registerCheckConfigStringsTask(project: Project, extension: Supporte
161161
Pair(supportedSet, aliasMappingMap)
162162
}
163163

164-
val stringFieldRegex = Regex("""public\s+static\s+final\s+String\s+\w+\s*=\s*"([^"]+)"\s*;""")
164+
// Single-line: `public static final String FIELD_NAME = "value";`
165+
val singleLineRegex = Regex("""public static final String (\w+) = "([^"]+)";""")
166+
// Multi-line start: `public static final String FIELD_NAME =`
167+
val multiLineStartRegex = Regex("""public static final String (\w+) =$""")
168+
// Multi-line value: `"value";`
169+
val valueLineRegex = Regex(""""([^"]+)";""")
165170

166171
val violations = buildList {
167-
configDir.listFiles()?.filter { it.extension == "java" }?.forEach { file ->
172+
configDir.listFiles()?.forEach { file ->
168173
var inBlockComment = false
169-
file.readLines().forEachIndexed { idx, line ->
174+
val lines = file.readLines()
175+
var i = 0
176+
while (i < lines.size) {
177+
val line = lines[i]
170178
val trimmed = line.trim()
171179

172-
if (trimmed.startsWith("//")) return@forEachIndexed
180+
if (trimmed.startsWith("//")) {
181+
i++
182+
continue
183+
}
173184
if (!inBlockComment && trimmed.contains("/*")) inBlockComment = true
174185
if (inBlockComment) {
175186
if (trimmed.contains("*/")) inBlockComment = false
176-
return@forEachIndexed
187+
i++
188+
continue
177189
}
178190

179-
stringFieldRegex.findAll(line).forEach { match ->
180-
val configValue = match.groupValues[1]
191+
// Try single-line pattern first
192+
val singleLineMatch = singleLineRegex.find(line)
193+
if (singleLineMatch != null) {
194+
val fieldName = singleLineMatch.groupValues[1]
195+
val configValue = singleLineMatch.groupValues[2]
181196

182-
val normalized = "DD_" + configValue.uppercase()
183-
.replace("-", "_")
184-
.replace(".", "_")
185-
186-
if (normalized !in supported && normalized !in aliasMapping) {
187-
add("${file.name}:${idx + 1} -> Config '$configValue' normalizes to '$normalized' which is not in supported-configurations.json")
197+
// Skip fields that end with _DEFAULT (default values defined in ProfilingConfig.java only)
198+
if (!fieldName.endsWith("_DEFAULT")) {
199+
val normalized = "DD_" + configValue.uppercase()
200+
.replace("-", "_")
201+
.replace(".", "_")
202+
203+
if (normalized !in supported && normalized !in aliasMapping) {
204+
add("${file.name}:${i + 1} -> Config '$configValue' normalizes to '$normalized' which is not in supported-configurations.json")
205+
}
206+
}
207+
} else {
208+
val multiLineMatch = multiLineStartRegex.find(line)
209+
if (multiLineMatch != null) {
210+
val fieldName = multiLineMatch.groupValues[1]
211+
if (!fieldName.endsWith("_DEFAULT")) {
212+
var j = i + 1
213+
while (j < lines.size) {
214+
val nextLine = lines[j].trim()
215+
val valueMatch = valueLineRegex.find(nextLine)
216+
if (valueMatch != null) {
217+
val configValue = valueMatch.groupValues[1]
218+
val normalized = "DD_" + configValue.uppercase()
219+
.replace("-", "_")
220+
.replace(".", "_")
221+
if (normalized !in supported && normalized !in aliasMapping) {
222+
add("${file.name}:${i + 1} -> Config '$configValue' normalizes to '$normalized' " +
223+
"which is not in supported-configurations.json")
224+
}
225+
break
226+
}
227+
j++
228+
}
229+
}
188230
}
189231
}
232+
i++
190233
}
191234
}
192235
}
193236

194237
if (violations.isNotEmpty()) {
195-
project.logger.lifecycle("\nFound config strings not in supported-configurations.json:")
238+
project.logger.lifecycle("\nFound config definitions not in supported-configurations.json:")
196239
violations.forEach { project.logger.lifecycle(it) }
197240
throw GradleException("Config strings validation failed. See errors above.")
198241
} else {

0 commit comments

Comments
 (0)