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

Add spotless to gradle build for format linting and reformatting #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 31 additions & 2 deletions klaxon-root.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ buildscript {
}

plugins {
// https://github.com/diffplug/spotless/tree/master/plugin-gradle
id("com.diffplug.gradle.spotless") version "3.14.0"
id("com.jfrog.bintray") version "1.2"
`maven-publish`
}

val kotlinVersion = property("kotlin.version").toString()
val ktlintVersion = "0.28.0"

allprojects {
apply {
plugin("com.diffplug.gradle.spotless")
}

version = "3.0.8"
group = "com.beust"

Expand All @@ -30,6 +37,17 @@ allprojects {
setUrl("http://oss.sonatype.org/content/repositories/snapshots")
}
}

spotless {
/*
* We use spotless to lint the Gradle Kotlin DSL files that make up the build.
* These checks are dependencies of the `check` task.
*/
kotlinGradle {
ktlint(ktlintVersion)
trimTrailingWhitespace()
}
}
}

// Projects that will be published.
Expand Down Expand Up @@ -59,6 +77,15 @@ configure(sourceProjects) {
tasks.withType<Test>().configureEach {
useTestNG()
}

spotless {
kotlin {
ktlint(ktlintVersion)
trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()
}
}
}

configure(publishedProjects) {
Expand All @@ -70,6 +97,10 @@ configure(publishedProjects) {
classifier = "sources"
from(java.sourceSets["main"].allSource)
}

tasks.withType<Test>().configureEach {
useTestNG()
}
}

tasks.withType<Wrapper>().configureEach {
Expand All @@ -82,5 +113,3 @@ tasks.withType<Wrapper>().configureEach {
*/
val Project.java: org.gradle.api.plugins.JavaPluginConvention
get() = convention.getPluginByName("java")


2 changes: 1 addition & 1 deletion klaxon/src/main/java/com/beust/klaxon/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ annotation class Json(
val path: String = ""
)

fun Json.nameInitialized() = this.name != NAME_NOT_INITIALIZED
fun Json.nameInitialized() = this.name != NAME_NOT_INITIALIZED
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ class KlaxonJsonProvider: JsonProvider() {
}

}
*/
*/
2 changes: 1 addition & 1 deletion klaxon/src/main/java/com/beust/klaxon/PathMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface PathMatcher {
/**
* @return true if we want to observe this path.
*/
fun pathMatches(path: String) : Boolean
fun pathMatches(path: String): Boolean

/**
* Note that the value can only be a basic type (Int, String, ...) and never JsonObject or JsonArray.
Expand Down
7 changes: 2 additions & 5 deletions klaxon/src/main/kotlin/com/beust/klaxon/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class Annotations {
emptyList()
}

fun findNonIgnoredProperties(kc: KClass<*>?, strategies: List<PropertyStrategy>)
: List<KProperty1<out Any, Any?>> {
fun findNonIgnoredProperties(kc: KClass<*>?, strategies: List<PropertyStrategy>): List<KProperty1<out Any, Any?>> {
val result = findProperties(kc)
.filter {
// Visibility
Expand Down Expand Up @@ -102,6 +101,4 @@ class Annotations {

fun isList(kClass: KClass<*>) = kotlin.collections.List::class.java.isAssignableFrom(kClass.java)
}


}
}
4 changes: 2 additions & 2 deletions klaxon/src/main/kotlin/com/beust/klaxon/Converter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Converter {
/**
* @return true if this converter can convert this class.
*/
fun canConvert(cls: Class<*>) : Boolean
fun canConvert(cls: Class<*>): Boolean

/**
* @return the JSON representation of the given value.
Expand All @@ -17,5 +17,5 @@ interface Converter {
/**
* Convert the given Json value into an object.
*/
fun fromJson(jv: JsonValue) : Any
fun fromJson(jv: JsonValue): Any
}
2 changes: 1 addition & 1 deletion klaxon/src/main/kotlin/com/beust/klaxon/Debug.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ class Debug {
companion object {
val verbose = false
}
}
}
18 changes: 6 additions & 12 deletions klaxon/src/main/kotlin/com/beust/klaxon/DefaultConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.beust.klaxon
import com.beust.klaxon.Reflection.Companion.isAssignableFromAny
import java.lang.reflect.ParameterizedType
import java.math.BigDecimal
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmErasure

/**
Expand All @@ -17,7 +16,7 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
val value = jv.inside
val propertyType = jv.propertyClass
val result =
when(value) {
when (value) {
is Boolean, is String, is Long -> value
is Int -> fromInt(value, propertyType)
is Double ->
Expand All @@ -34,12 +33,11 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
}
}
return result

}

override fun toJson(value: Any): String {
fun joinToString(list: Collection<*>, open: String, close: String)
= open + list.joinToString(", ") + close
fun joinToString(list: Collection<*>, open: String, close: String) =
open + list.joinToString(", ") + close

val result = when (value) {
is String, is Enum<*> -> "\"" + Render.escapeString(value.toString()) + "\""
Expand Down Expand Up @@ -79,7 +77,6 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
""""$value""""
}
}

}
return result
}
Expand Down Expand Up @@ -131,7 +128,6 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
throw KlaxonException("Don't know how to convert null value in array $jv")
}
}

}

val result =
Expand Down Expand Up @@ -175,8 +171,8 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
result
}
isCollection -> {
val type =jt.actualTypeArguments[0]
when(type) {
val type = jt.actualTypeArguments[0]
when (type) {
is Class<*> -> {
val cls = jt.actualTypeArguments[0] as Class<*>
klaxon.fromJsonObject(value, cls, cls.kotlin)
Expand All @@ -185,7 +181,6 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
val result2 = JsonObjectConverter(klaxon, HashMap<String, Any>()).fromJson(value,
jv.propertyKClass!!.jvmErasure)
result2

}
else -> {
throw IllegalArgumentException("Couldn't interpret type $type")
Expand All @@ -206,5 +201,4 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
}
return result
}

}
}
4 changes: 2 additions & 2 deletions klaxon/src/main/kotlin/com/beust/klaxon/EnumConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.beust.klaxon
/**
* Convert an enum to and from JSON.
*/
class EnumConverter: Converter {
class EnumConverter : Converter {
override fun toJson(value: Any): String {
return "\"" + value.toString() + "\""
}
Expand All @@ -14,7 +14,7 @@ class EnumConverter: Converter {

override fun fromJson(jv: JsonValue): Enum<*> {
val javaClass = jv.propertyClass
val result : Enum<*> =
val result: Enum<*> =
if (javaClass is Class<*> && javaClass.isEnum) {
val valueOf = javaClass.getMethod("valueOf", String::class.java)
valueOf.invoke(null, jv.inside) as Enum<*>
Expand Down
28 changes: 14 additions & 14 deletions klaxon/src/main/kotlin/com/beust/klaxon/JsonArray.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.beust.klaxon

import java.math.BigInteger
import java.util.*
import java.util.Arrays

// Needs to be a separate function since as a constructor, its signature conflicts with the List constructor
fun <T> JsonArray(list : List<T> = emptyList()) : JsonArray<T> =
fun <T> JsonArray(list: List<T> = emptyList()): JsonArray<T> =
JsonArray(list.toMutableList())

data class JsonArray<T>(val value : MutableList<T>) : JsonBase, MutableList<T> by value {
constructor(vararg items : T) : this(ArrayList(Arrays.asList(*items)))
data class JsonArray<T>(val value: MutableList<T>) : JsonBase, MutableList<T> by value {
constructor(vararg items: T) : this(ArrayList(Arrays.asList(*items)))

override fun appendJsonStringImpl(result: Appendable, prettyPrint: Boolean, canonical : Boolean, level: Int) {
override fun appendJsonStringImpl(result: Appendable, prettyPrint: Boolean, canonical: Boolean, level: Int) {
result.append("[")

var comma = false
Expand All @@ -29,26 +29,26 @@ data class JsonArray<T>(val value : MutableList<T>) : JsonBase, MutableList<T> b
result.append("]")
}

fun string(id: String) : JsonArray<String?> = mapChildren { it.string(id) }
fun obj(id: String) : JsonArray<JsonObject?> = mapChildren { it.obj(id) }
fun long(id: String) : JsonArray<Long?> = mapChildren { it.long(id) }
fun int(id: String) : JsonArray<Int?> = mapChildren { it.int(id) }
fun bigInt(id: String) : JsonArray<BigInteger?> = mapChildren { it.bigInt(id) }
fun double(id: String) : JsonArray<Double?> = mapChildren { it.double(id) }
fun string(id: String): JsonArray<String?> = mapChildren { it.string(id) }
fun obj(id: String): JsonArray<JsonObject?> = mapChildren { it.obj(id) }
fun long(id: String): JsonArray<Long?> = mapChildren { it.long(id) }
fun int(id: String): JsonArray<Int?> = mapChildren { it.int(id) }
fun bigInt(id: String): JsonArray<BigInteger?> = mapChildren { it.bigInt(id) }
fun double(id: String): JsonArray<Double?> = mapChildren { it.double(id) }

fun <T> mapChildrenObjectsOnly(block : (JsonObject) -> T) : JsonArray<T> =
fun <T> mapChildrenObjectsOnly(block: (JsonObject) -> T): JsonArray<T> =
JsonArray(flatMapTo(ArrayList<T>(size)) {
if (it is JsonObject) listOf(block(it))
else if (it is JsonArray<*>) it.mapChildrenObjectsOnly(block)
else listOf()
})

fun <T : Any> mapChildren(block : (JsonObject) -> T?) : JsonArray<T?> =
fun <T : Any> mapChildren(block: (JsonObject) -> T?): JsonArray<T?> =
JsonArray(flatMapTo(ArrayList<T?>(size)) {
if (it is JsonObject) listOf(block(it))
else if (it is JsonArray<*>) it.mapChildren(block)
else listOf(null)
})

operator fun get(key : String) : JsonArray<Any?> = mapChildren { it[key] }
operator fun get(key: String): JsonArray<Any?> = mapChildren { it[key] }
}
6 changes: 3 additions & 3 deletions klaxon/src/main/kotlin/com/beust/klaxon/JsonBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.beust.klaxon

interface JsonBase {
fun appendJsonStringImpl(result: Appendable, prettyPrint: Boolean, canonical: Boolean, level: Int)
fun appendJsonString(result : Appendable, prettyPrint: Boolean = false, canonical: Boolean = false) =
fun appendJsonString(result: Appendable, prettyPrint: Boolean = false, canonical: Boolean = false) =
appendJsonStringImpl(result, prettyPrint, canonical, 0)
fun toJsonString(prettyPrint: Boolean = false, canonical: Boolean = false) : String =
fun toJsonString(prettyPrint: Boolean = false, canonical: Boolean = false): String =
StringBuilder().apply { appendJsonString(this, prettyPrint, canonical) }.toString()
}
}
26 changes: 11 additions & 15 deletions klaxon/src/main/kotlin/com/beust/klaxon/JsonObject.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.beust.klaxon

import java.math.BigInteger
import java.util.*

fun JsonObject(map : Map<String, Any?> = emptyMap()) : JsonObject =
fun JsonObject(map: Map<String, Any?> = emptyMap()): JsonObject =
JsonObject(LinkedHashMap(map))

data class JsonObject(val map: MutableMap<String, Any?>) : JsonBase, MutableMap<String, Any?>
by map {
// constructor() : this(mutableMapOf<String, Any?>()) {}

override fun appendJsonStringImpl(result: Appendable, prettyPrint: Boolean, canonical: Boolean, level: Int) {
fun indent(a: Appendable, level: Int) {
Expand All @@ -19,7 +17,7 @@ by map {

result.append("{")
var comma = false
for ((k, v) in (if(canonical) map.toSortedMap() else map)) {
for ((k, v) in (if (canonical) map.toSortedMap() else map)) {
if (comma) {
result.append(",")
} else {
Expand Down Expand Up @@ -50,31 +48,29 @@ by map {
override fun toString() = keys.joinToString(",")

@Suppress("UNCHECKED_CAST")
fun <T> array(fieldName: String) : JsonArray<T>? = get(fieldName) as JsonArray<T>?
fun <T> array(fieldName: String): JsonArray<T>? = get(fieldName) as JsonArray<T>?

fun obj(fieldName: String) : JsonObject? = get(fieldName) as JsonObject?
fun obj(fieldName: String): JsonObject? = get(fieldName) as JsonObject?

fun int(fieldName: String) : Int? {
fun int(fieldName: String): Int? {
val value = get(fieldName)
return when (value) {
is Number -> value.toInt()
else -> value as Int?
}
}

fun long(fieldName: String) : Long? {
fun long(fieldName: String): Long? {
val value = get(fieldName)
return when (value) {
is Number -> value.toLong()
else -> value as Long?
}
}

fun bigInt(fieldName: String) : BigInteger? = get(fieldName) as BigInteger
fun string(fieldName: String) : String? = get(fieldName) as String?
fun double(fieldName: String) : Double? = get(fieldName) as Double?
fun float(fieldName: String) : Float? = (get(fieldName) as Double?)?.toFloat()
fun boolean(fieldName: String) : Boolean? = get(fieldName) as Boolean?


fun bigInt(fieldName: String): BigInteger? = get(fieldName) as BigInteger
fun string(fieldName: String): String? = get(fieldName) as String?
fun double(fieldName: String): Double? = get(fieldName) as Double?
fun float(fieldName: String): Float? = (get(fieldName) as Double?)?.toFloat()
fun boolean(fieldName: String): Boolean? = get(fieldName) as Boolean?
}
Loading