Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use kotlinx.serialization instead of jackson #393

Merged
merged 18 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,4 @@ style:
WildcardImport:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**']
excludeImports: ['org.cqfn.diktat.ruleset.utils.*', 'java.util.*', 'kotlinx.android.synthetic.*']
excludeImports: ['org.cqfn.diktat.ruleset.utils.*', 'java.util.*', 'kotlinx.android.synthetic.*', 'kotlinx.serialization']
1 change: 0 additions & 1 deletion diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@
- name: STRING_TEMPLATE_CURLY_BRACES
enabled: true
configuration: {}
typeReferenceLength: '25' # max length of type reference
# Variables with `val` modifier - are immutable (read-only). Usage of such variables instead of `var` variables increases
# robustness and readability of code, because `var` variables can be reassigned several times in the business logic.
# This rule prohibits usage of `var`s as local variables - the only exception is accumulators and counters
Expand Down
34 changes: 18 additions & 16 deletions diktat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,14 @@
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-json-jvm</artifactId>
<version>${kotlinx.serialization.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<groupId>com.charleskorn.kaml</groupId>
<artifactId>kaml</artifactId>
<version>0.24.0</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand Down Expand Up @@ -88,6 +78,18 @@
</configuration>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>kotlinx-serialization</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-serialization</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package org.cqfn.diktat.common.cli

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import org.apache.commons.cli.Option
import kotlinx.serialization.*

/**
* This class is used to serialize/deserialize json representation
* that is used to store command line arguments
*/
@JsonIgnoreProperties(ignoreUnknown = true)
class CliArgument @JsonCreator internal constructor(
@Serializable
data class CliArgument (
// short argument representation like -h
@param:JsonProperty("shortName") private val shortName: String,
@param:JsonProperty("helpDescr") private val helpDescr: String,
private val shortName: String,
private val helpDescr: String,
// long argument representation like --help
@param:JsonProperty("longName") private val longName: String,
private val longName: String,
// indicates if option should have explicit argument
@param:JsonProperty("hasArgs") private val hasArgs: Boolean,
@param:JsonProperty("isRequired") private val isRequired: Boolean) {
override fun toString(): String = "(shortName: " + shortName + ", helpDescr: " + helpDescr + ", longName: " +
longName + ", hasArgs: " + hasArgs + ", isRequired: " + isRequired + ")"

private val hasArgs: Boolean,
private val isRequired: Boolean) {
/**
* Converts parameters received from json to [Option]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

package org.cqfn.diktat.common.config.rules

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.readValue
import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import java.io.BufferedReader
import java.io.File
import org.cqfn.diktat.common.config.reader.JsonResourceConfigReader
Expand All @@ -30,7 +29,7 @@ interface Rule {
/**
* Configuration of individual [Rule]
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class RulesConfig(
val name: String,
val enabled: Boolean = true,
Expand All @@ -47,17 +46,17 @@ object EmptyConfiguration : RuleConfiguration(mapOf())
* class returns the list of configurations that we have read from a yml: diktat-analysis.yml
*/
open class RulesConfigReader(override val classLoader: ClassLoader) : JsonResourceConfigReader<List<RulesConfig>>() {
private val yamlSerializer by lazy { Yaml(configuration = YamlConfiguration(strictMode = true)) }

/**
* Parse resource file into list of [RulesConfig]
*
* @param fileStream a [BufferedReader] representing loaded rules config file
* @return list of [RulesConfig]
*/
override fun parseResource(fileStream: BufferedReader): List<RulesConfig> {
val mapper = ObjectMapper(YAMLFactory())
mapper.registerModule(KotlinModule())
return fileStream.use { stream ->
mapper.readValue(stream)
yamlSerializer.decodeFromString(stream.readLines().joinToString(separator = "\n"))
}
}

Expand Down Expand Up @@ -87,7 +86,9 @@ open class RulesConfigReader(override val classLoader: ClassLoader) : JsonResour
/**
* @return common configuration from list of all rules configuration
*/
fun List<RulesConfig>.getCommonConfiguration() = lazy { CommonConfiguration(getCommonConfig()?.configuration) }
fun List<RulesConfig>.getCommonConfiguration() = lazy {
CommonConfiguration(getCommonConfig()?.configuration)
}

/**
* class returns the list of common configurations that we have read from a configuration map
Expand Down
25 changes: 0 additions & 25 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,6 @@
<mainClass>org.cqfn.diktat.ruleset.generation.GenerationKt</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<testExcludes>
<testExclude>test/**</testExclude>
</testExcludes>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cqfn.diktat.ruleset.utils

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.charleskorn.kaml.Yaml
import kotlinx.serialization.encodeToString
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader
import org.cqfn.diktat.common.config.rules.getRuleConfig
Expand Down Expand Up @@ -35,15 +36,14 @@ class RulesConfigYamlTest {
allRulesFromCode.forEach { rule ->
val foundRule = allRulesFromConfig.getRuleConfig(rule)
val ymlCodeSnippet = RulesConfig(rule.ruleName(), true, mapOf())
val jacksonMapper = jacksonObjectMapper()

val ruleYaml = jacksonMapper.writeValueAsString(ymlCodeSnippet)
val ruleYaml = Yaml.default.encodeToString(ymlCodeSnippet)
Assertions.assertTrue(foundRule != null) {
"""
Cannot find warning ${rule.ruleName()} in $filePath.
You can fix it by adding the following code below to $filePath:
$ruleYaml
"""
""".trimIndent()
}
}

Expand Down
26 changes: 10 additions & 16 deletions diktat-test-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
Expand Down Expand Up @@ -88,6 +72,9 @@
<source>src/main/kotlin</source>
<source>src/main/resources</source>
</sourceDirs>
<compilerPlugins>
<plugin>kotlinx-serialization</plugin>
</compilerPlugins>
</configuration>
</execution>
<execution>
Expand All @@ -104,6 +91,13 @@
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-serialization</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.cqfn.diktat.test.framework.config

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import java.io.BufferedReader
import java.io.IOException
import java.util.stream.Collectors
Expand Down Expand Up @@ -90,7 +90,7 @@ class TestArgumentsReader(
@Throws(IOException::class)
override fun parseResource(fileStream: BufferedReader): List<CliArgument> {
val jsonValue = fileStream.lines().collect(Collectors.joining())
return jacksonObjectMapper().readValue(jsonValue)
return Json.decodeFromString<List<CliArgument>>(jsonValue)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package org.cqfn.diktat.test.framework.config

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* This class is used to serialize/deserialize json representation
* that is used to store command line arguments
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
@Suppress("ForbiddenComment")
class TestConfig @JsonCreator internal constructor(
class TestConfig internal constructor(
// command line execution command, use shell like "cmd", "bash" or other
@param:JsonProperty("executionCommand") val executionCommand: String,
val executionCommand: String,
// expected result file can be a full path or a relative path to a resource
@param:JsonProperty("expectedResultFile") val expectedResultFile: String,
val expectedResultFile: String,
// testFile can be a full path or a relative path to a resource
@param:JsonProperty("testFile") val testFile: String,
val testFile: String,
// executionType that controls processing of the test (like COMPARE, MIXED, CHECK_WARN, e.t.c)
@param:JsonProperty("executionType") val executionType: ExecutionType,
@param:JsonProperty("profile") val testProfile: TestProfile,
val executionType: ExecutionType,
@SerialName("profile") val testProfile: TestProfile,
// option that controls if changes and automatic fix should be done directly in file
@param:JsonProperty("inPlace", defaultValue = "false") val inPlace: Boolean
val inPlace: Boolean = false
) {
/**
* test name - it is not included in config content, but is injected on runtime by setter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cqfn.diktat.test.framework.config

import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import java.io.BufferedReader
import java.io.IOException
import java.util.stream.Collectors
Expand All @@ -11,7 +12,7 @@ class TestConfigReader(configFilePath: String, override val classLoader: ClassLo

@Throws(IOException::class)
override fun parseResource(fileStream: BufferedReader): TestConfig {
val jsonValue = fileStream.lines().collect(Collectors.joining())
return ObjectMapper().readValue(jsonValue, TestConfig::class.java)
val jsonValue: String = fileStream.lines().collect(Collectors.joining())
return Json.decodeFromString<TestConfig>(jsonValue)
}
}
29 changes: 2 additions & 27 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.4.10</kotlin.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<kotlinx.serialization.version>1.0.0</kotlinx.serialization.version>
<ktlint.version>0.39.0</ktlint.version>
<junit.version>5.7.0</junit.version>
<guava.version>29.0-jre</guava.version>
<jackson.version>2.11.3</jackson.version>
<slf4j.version>1.7.30</slf4j.version>
<commons-cli.version>1.4</commons-cli.version>
<diktat-check.version>0.1.2</diktat-check.version>
Expand Down Expand Up @@ -85,7 +85,7 @@
<version>${kotlin.version}</version>
</dependency>
<dependency>
<!-- temporary override for jackson-kotlin and kotlinpoet -->
<!-- temporary override for and kotlinpoet -->
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
Expand All @@ -105,31 +105,6 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
Expand Down