Skip to content

Commit

Permalink
tidy up code to pass detekt v1.22.0 test
Browse files Browse the repository at this point in the history
  • Loading branch information
danurahadi committed Jan 10, 2023
1 parent 9ee6e08 commit 8703122
Show file tree
Hide file tree
Showing 25 changed files with 40 additions and 48 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ext {

coroutineVersion = "1.6.4"
arrowVersion = "0.7.2"
detektVersion = "1.22.0"

logbackVersion = '1.2.10'
logstashLogbackEncoderVersion = '7.0.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ dependencies {
testRuntimeOnly("org.jetbrains.spek:spek-junit-platform-engine:$spekVersion") {
exclude group: 'org.jetbrains.kotlin'
}

detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:$detektVersion"
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,3 @@ fun Logger.log(log: LogType, defaultParam: Map<String, Any> = emptyMap(), vararg
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ fun <T : Any> env(clazz: KClass<T>, key: String, defaultValue: T? = null): T {
clazz.isSubclassOf(Boolean::class) -> value.toBoolean() as T
else -> throw IllegalArgumentException("${clazz.qualifiedName} Not Supported")
}
} else defaultValue ?: throw IllegalArgumentException("Illegal: $key not found and default value is null!")
} else {
defaultValue ?: throw IllegalArgumentException("Illegal: $key not found and default value is null!")
}
}

inline fun <reified T : Any> env(key: String, defaultValue: T? = null): T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ private const val DEFAULT_WIDTH: Int = 42
fun String.abbreviate(width: Int = DEFAULT_WIDTH): String {
return StringUtils.abbreviate(this, width)
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException


fun Int.toYear(): Year {
return Year.of(this)
}
Expand Down Expand Up @@ -66,4 +65,3 @@ fun String.fromUnixTimestamp(): LocalDateTime {
fun LocalDate?.toFormat(pattern: String): String? {
return this?.format(DateTimeFormatter.ofPattern(pattern))
}

1 change: 1 addition & 0 deletions core/src/main/kotlin/id/yoframework/core/json/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf

private val log = logger("Json Extension")

/**
* Convert [JsonObject] to [T] object, and throws [IllegalArgumentException] if failed.
* Require [com.fasterxml.jackson.module.kotlin.KotlinModule] installed on Json.mapper.
Expand Down
19 changes: 14 additions & 5 deletions core/src/main/kotlin/id/yoframework/core/json/validator/Number.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ inline fun <reified T : Number> JsonObject.equalTo(key: String, target: T): Vali
}
}

inline fun <reified T : Number> JsonObject.greaterThan(key: String, target: T, equals: Boolean = false)
: Validated<ValidationError, T> {
inline fun <reified T : Number> JsonObject.greaterThan(
key: String,
target: T,
equals: Boolean = false
): Validated<ValidationError, T> {
val value = this.saveGet(T::class, key) ?: return NumericError<T>(
"$key is not exists ",
Numeric.GreaterThan(target, equals)
Expand All @@ -109,8 +112,11 @@ inline fun <reified T : Number> JsonObject.greaterThan(key: String, target: T, e
}
}

inline fun <reified T : Number> JsonObject.lessThan(key: String, target: T, equals: Boolean = false)
: Validated<ValidationError, T> {
inline fun <reified T : Number> JsonObject.lessThan(
key: String,
target: T,
equals: Boolean = false
): Validated<ValidationError, T> {
val value = this.saveGet(T::class, key) ?: return NumericError<T>(
"$key is not exists ",
Numeric.LessThan(target, equals)
Expand Down Expand Up @@ -143,7 +149,10 @@ inline fun <reified T : Number> JsonObject.numeric(
}
}
.filter { it.isInvalid }
.map { @Suppress("UNCHECKED_CAST") (it as Invalid<NumericError<T>>).e }
.map {
@Suppress("UNCHECKED_CAST")
(it as Invalid<NumericError<T>>).e
}

return if (errors.isNotEmpty()) {
NumericErrors("$key is invalid", errors).invalid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,4 @@ class CoreModule(
val validatorFactory = Validation.buildDefaultValidatorFactory()
return validatorFactory.validator
}

}
1 change: 0 additions & 1 deletion db/src/main/kotlin/id/yoframework/db/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ fun createHikariPoolDataSource(
driver: String,
config: HikariConfig.() -> Unit = {}
): HikariDataSource {

val hikariConfig = HikariConfig()

hikariConfig.poolName = name
Expand Down
7 changes: 2 additions & 5 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ complexity:
threshold: 10
includeStaticDeclarations: false
includePrivateDeclarations: false
ComplexMethod:
CyclomaticComplexMethod:
active: true
ignoreSingleWhenExpression: true
LargeClass:
Expand Down Expand Up @@ -93,10 +93,6 @@ style:
active: true
values: ['TODO:', 'FIXME:', 'STOPSHIP:', '@author']
excludes: ['**/detekt-rules-style/**/ForbiddenComment.kt']
LibraryCodeMustSpecifyReturnType:
active: true
excludes: ['**/*.kt']
includes: ['**/detekt-api/src/main/**/api/*.kt']
MaxLineLength:
active: true
excludes: ['**/test/**', '**/*.Test.kt', '**/*.Spec.kt']
Expand Down Expand Up @@ -126,3 +122,4 @@ style:
performance:
SpreadOperator:
active: false

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package id.yoframework.extra.extension.codec
package id.yoframework.extra.extension.codec

import id.yoframework.core.extension.string.abbreviate
import org.apache.commons.codec.binary.Base32
Expand All @@ -33,7 +33,6 @@ fun String.base32EncodeToByteArray(): ByteArray {
return base32Codec.encode(this.toByteArray())
}


fun <T : Serializable> T.base32Encode(): String {
return base32Codec.encodeToString(this.serializeToByteArray())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package id.yoframework.extra.extension.codec
package id.yoframework.extra.extension.codec

import id.yoframework.core.extension.string.abbreviate
import org.apache.commons.codec.binary.Base64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ object Pebble {
engine.value()
}
}

}

fun Pebble.compileStringTemplate(templateString: String, strictMode: Boolean = false): PebbleTemplate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ fun String.matchWith(pattern: String): Boolean {
* @return Matcher
*/
fun String.matchTo(pattern: String): Matcher {
if (pattern.isEmpty()) {
throw IllegalArgumentException("Regex pattern is empty")
}
require(pattern.isEmpty()) { "Regex pattern is empty" }
val p: Pattern = Pattern.compile(pattern)

return p.matcher(this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import java.util.concurrent.atomic.AtomicInteger

private class SnowFlake(private val machineId: Int) {
init {
if (machineId >= MAX_MACHINE_ID || machineId < 0) {
throw IllegalArgumentException("Machine Number must between 0 - ${MAX_MACHINE_ID - 1}")
require(machineId >= MAX_MACHINE_ID || machineId < 0) {
"Machine Number must between 0 - ${MAX_MACHINE_ID - 1}"
}
}

Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Set the project version
version=0.4.8
version=0.4.9

# Set the JVM heap size
org.gradle.jvmargs=-Xms1g -Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options=-Xmx1G
Expand All @@ -8,10 +8,10 @@ org.gradle.jvmargs=-Xms1g -Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfM
org.gradle.daemon=false

# Enable the build cache
org.gradle.caching=false
org.gradle.caching=true

# Enable parallelize build
org.gradle.parallel=false
org.gradle.parallel=true

# Enable configure on demand
org.gradle.configureondemand=false
org.gradle.configureondemand=true
2 changes: 0 additions & 2 deletions grpc/src/main/kotlin/id/yoframework/grpc/Grpc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fun Vertx.buildGrpcServer(
vararg services: BindableService,
configuration: (VertxServerBuilder) -> VertxServerBuilder = { it }
): VertxServer {

return VertxServerBuilder
.forAddress(this, host, port)
.let {
Expand Down Expand Up @@ -61,4 +60,3 @@ fun Vertx.buildGrpcChannel(
return configuration(VertxChannelBuilder.forAddress(this, host, port))
.build()
}

3 changes: 0 additions & 3 deletions hystrix/src/main/kotlin/id/yoframework/hystrix/Hystrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ fun buildObservableCommandConfig(
.applyIf(commandPropertiesDefault) {
andCommandPropertiesDefaults(it)
}

}

fun buildCommandConfig(
Expand All @@ -61,7 +60,6 @@ fun buildCommandConfig(
commandProperties: CommandPropertiesSetter? = null,
threadPoolProperties: ThreadPoolPropertiesSetter? = null
): HystrixCommand.Setter {

return HystrixCommand.Setter
.withGroupKey(groupKey)
.applyIf(commandKey) {
Expand All @@ -76,7 +74,6 @@ fun buildCommandConfig(
.applyIf(threadPoolProperties) {
andThreadPoolPropertiesDefaults(it)
}

}

fun buildCommandProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,3 @@ open class HystrixCommand<T : Any>(
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class MorphiaModule {
MongoClient(server)
} else {
val credentials = MongoCredential.createScramSha1Credential(
mongoUsername, databaseName, mongoPassword.toCharArray()
mongoUsername,
databaseName,
mongoPassword.toCharArray()
)
val credentialsList = listOf(credentials)
log.info("Initialize MongoClient with $host:$port")
Expand Down
4 changes: 2 additions & 2 deletions web/src/main/kotlin/id/yoframework/web/exception/Exception.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ data class RequestException(
}

infix fun <T> T?.orNotFound(message: String): T {
return this ?: throw NullObjectException(message)
return this ?: throw NullObjectException(message)
}

infix fun <T> T?.orBadRequest(message: String): T {
return this ?: throw BadRequestException(message)
return this ?: throw BadRequestException(message)
}

infix fun <T> T?.orUnauthorized(message: String): T {
Expand Down
6 changes: 3 additions & 3 deletions web/src/main/kotlin/id/yoframework/web/extension/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ fun RoutingContext.principal(): JsonObject? {
return this.user()?.principal()
}

//@Suppress("deprecation")
//fun RoutingContext.securityUser(): SecurityUser? {
// @Suppress("deprecation")
// fun RoutingContext.securityUser(): SecurityUser? {
// return this.user() as? SecurityUser
//}
// }

fun RoutingContext.getRemoteIpAddress(): String {
return this.request()?.remoteAddress()?.host() ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ suspend fun WebClient.postForm(
val payload = formData.toList().fold(MultiMap.caseInsensitiveMultiMap()) { form, (key, value) ->
form.set(key, value)
}

return awaitResult {
this.postAbs(absoluteURI).apply { headers().addAll(header) }.sendForm(payload, it)
}
Expand Down Expand Up @@ -86,4 +85,3 @@ fun HttpResponse<Buffer>.jsonBody(): JsonObject? {
fun HttpResponse<Buffer>.jsonArrayBody(): JsonArray? {
return this.bodyAsJsonArray()
}

1 change: 0 additions & 1 deletion web/src/main/kotlin/id/yoframework/web/module/WebModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ class WebModule {
fun provideWebClient(vertx: Vertx): WebClient {
return WebClient.create(vertx)
}

}

0 comments on commit 8703122

Please sign in to comment.