-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update config format and add an example
- Loading branch information
Showing
4 changed files
with
150 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 45 additions & 9 deletions
54
src/main/kotlin/br/com/felipezorzo/zpa/cli/config/ConfigFile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,54 @@ | ||
package br.com.felipezorzo.zpa.cli.config | ||
|
||
import org.sonar.plsqlopen.rules.ActiveRuleConfiguration | ||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.* | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
|
||
class ConfigFile { | ||
val rules: List<RuleConfiguration> = listOf() | ||
} | ||
data class ConfigFile( | ||
val rules: Map<String, RuleConfiguration> | ||
) | ||
|
||
@JsonDeserialize(using = RuleCategoryDeserializer::class) | ||
@JsonSerialize(using = RuleCategorySerializer::class) | ||
class RuleConfiguration { | ||
var repositoryKey: String = "zpa" | ||
var key: String = "" | ||
var severity: String? = null | ||
var options: RuleOptions = RuleOptions() | ||
} | ||
|
||
class RuleOptions { | ||
var level: String? = null | ||
set(value) { | ||
field = value?.lowercase() | ||
} | ||
var parameters: Map<String, String> = emptyMap() | ||
} | ||
|
||
class RuleCategoryDeserializer : JsonDeserializer<RuleConfiguration>() { | ||
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): RuleConfiguration { | ||
val node: JsonNode = p.codec.readTree(p) | ||
val ruleConfiguration = RuleConfiguration() | ||
val mapper = jacksonObjectMapper() | ||
|
||
if (node.isTextual) { | ||
ruleConfiguration.options.level = node.asText().uppercase() | ||
} else if (node.isObject) { | ||
ruleConfiguration.options = mapper.treeToValue(node, RuleOptions::class.java) | ||
} | ||
|
||
return ruleConfiguration | ||
} | ||
} | ||
|
||
class RuleCategorySerializer : JsonSerializer<RuleConfiguration>() { | ||
override fun serialize(value: RuleConfiguration, gen: JsonGenerator, serializers: SerializerProvider) { | ||
val mapper = jacksonObjectMapper() | ||
|
||
fun toActiveRuleConfiguration(): ActiveRuleConfiguration { | ||
return ActiveRuleConfiguration(repositoryKey, key, severity, parameters) | ||
if (value.options.parameters.isEmpty()) { | ||
gen.writeString(value.options.level.toString()) | ||
} else { | ||
gen.writeTree(mapper.valueToTree(value.options)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{ | ||
"rules": { | ||
"EmptyBlock": "minor", | ||
"ParsingError": "info", | ||
"CollapsibleIfStatements": "major", | ||
"InequalityUsage": "major", | ||
"ComparisonWithNull": "blocker", | ||
"TooManyRowsHandler": "critical", | ||
"InsertWithoutColumns": "critical", | ||
"DeclareSectionWithoutDeclarations": "info", | ||
"NvlWithNullParameter": "blocker", | ||
"ComparisonWithBoolean": "minor", | ||
"CharacterDatatypeUsage": "minor", | ||
"SelectAllColumns": "major", | ||
"ColumnsShouldHaveTableName": "major", | ||
"SelectWithRownumAndOrderBy": "blocker", | ||
"ToDateWithoutFormat": "major", | ||
"ExplicitInParameter": "minor", | ||
"VariableInitializationWithNull": "minor", | ||
"UselessParenthesis": "minor", | ||
"IdenticalExpression": "blocker", | ||
"EmptyStringAssignment": "minor", | ||
"DuplicatedValueInIn": "blocker", | ||
"VariableInitializationWithFunctionCall": "major", | ||
"IfWithExit": "minor", | ||
"FunctionWithOutParameter": "major", | ||
"SameCondition": "blocker", | ||
"AddParenthesesInNestedExpression": "major", | ||
"RaiseStandardException": "major", | ||
"NotFound": "minor", | ||
"QueryWithoutExceptionHandling": { | ||
"level": "critical", | ||
"parameters": { | ||
"strict": "true" | ||
} | ||
}, | ||
"UnusedVariable": "major", | ||
"VariableHiding": "major", | ||
"DbmsOutputPut": "minor", | ||
"ReturnOfBooleanExpression": "minor", | ||
"UnnecessaryElse": "minor", | ||
"DeadCode": "major", | ||
"ConcatenationWithNull": "minor", | ||
"SameBranch": "major", | ||
"UnusedParameter": { | ||
"level": "major", | ||
"parameters": { | ||
"ignoreMethods": "" | ||
} | ||
}, | ||
"CommitRollback": "major", | ||
"UnnecessaryNullStatement": "minor", | ||
"DuplicateConditionIfElsif": "blocker", | ||
"UnnecessaryAliasInQuery": { | ||
"level": "minor", | ||
"parameters": { | ||
"acceptedLength": "3" | ||
} | ||
}, | ||
"VariableInCount": "blocker", | ||
"UnhandledUserDefinedException": "critical", | ||
"UnusedCursor": "major", | ||
"NotASelectedExpression": "critical", | ||
"InvalidReferenceToObject": "major", | ||
"CursorBodyInPackageSpec": "major", | ||
"XPath": { | ||
"level": "major", | ||
"parameters": { | ||
"xpathQuery": "", | ||
"message": "The XPath expression matches this piece of code" | ||
} | ||
}, | ||
"VariableName": { | ||
"level": "minor", | ||
"parameters": { | ||
"regexp": "[a-zA-Z]([a-zA-Z0-9_]*[a-zA-Z0-9])?" | ||
} | ||
}, | ||
"ToCharInOrderBy": "major", | ||
"DisabledTest": "major", | ||
"RedundantExpectation": "major", | ||
"UnnecessaryLike": "minor" | ||
} | ||
} |