Skip to content

Commit

Permalink
Merge pull request #36 from uchuhimo/modularize
Browse files Browse the repository at this point in the history
Modularize
  • Loading branch information
uchuhimo authored Aug 19, 2019
2 parents 29e844b + f87f1fe commit cf1a2bf
Show file tree
Hide file tree
Showing 181 changed files with 2,600 additions and 935 deletions.
1 change: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.8
548 changes: 267 additions & 281 deletions build.gradle.kts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object Versions {
// wait for 0.9.20 if using JDK11 (issue: https://github.com/Kotlin/dokka/issues/428)
val dokka = "0.9.17"
val apiguardian = "1.1.0"
val reflections = "0.9.11"
val hocon = "1.3.4"
val yaml = "1.24"
val toml4j = "0.7.2"
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
18 changes: 17 additions & 1 deletion gradlew
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##############################################################################
##
## Gradle start up script for UN*X
Expand Down Expand Up @@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
18 changes: 17 additions & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
Expand All @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
34 changes: 34 additions & 0 deletions konf-all/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
sourceSets {
register("snippet")
}

val implementation by configurations
val snippetImplementation by configurations
snippetImplementation.extendsFrom(implementation)

dependencyManagement {
dependencies {
dependency("com.typesafe:config:${Versions.hocon}")
dependency("org.yaml:snakeyaml:${Versions.yaml}")
dependency("com.moandjiezana.toml:toml4j:${Versions.toml4j}")
dependency("org.dom4j:dom4j:${Versions.dom4j}")
dependency("org.eclipse.jgit:org.eclipse.jgit:${Versions.jgit}")
}
}

dependencies {
for (name in listOf(
":konf-core",
":konf-git",
":konf-hocon",
":konf-toml",
":konf-xml",
":konf-yaml"
)) {
implementation(project(name))
testImplementation(project(name).dependencyProject.sourceSets["test"].output)
}

val main by sourceSets
snippetImplementation(main.output)
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.uchuhimo.konf.snippet

import com.uchuhimo.konf.Config
import com.uchuhimo.konf.ConfigSpec
import com.uchuhimo.konf.source.yaml

object ServerSpec : ConfigSpec() {
val host by optional("0.0.0.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.uchuhimo.konf.source

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import com.uchuhimo.konf.Config
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on

object MultipleDefaultLoadersSpec : Spek({
on("load from multiple sources") {
val config = Config {
addSpec(DefaultLoadersConfig)
}
val item = DefaultLoadersConfig.type
val afterLoadEnv = config.from.env()
System.setProperty(config.nameOf(DefaultLoadersConfig.type), "system")
val afterLoadSystemProperties = afterLoadEnv.from.systemProperties()
val afterLoadHocon = afterLoadSystemProperties.from.hocon.string(hoconContent)
val afterLoadJson = afterLoadHocon.from.json.string(jsonContent)
val afterLoadProperties = afterLoadJson.from.properties.string(propertiesContent)
val afterLoadToml = afterLoadProperties.from.toml.string(tomlContent)
val afterLoadXml = afterLoadToml.from.xml.string(xmlContent)
val afterLoadYaml = afterLoadXml.from.yaml.string(yamlContent)
val afterLoadFlat = afterLoadYaml.from.map.flat(mapOf("source.test.type" to "flat"))
val afterLoadKv = afterLoadFlat.from.map.kv(mapOf("source.test.type" to "kv"))
val afterLoadHierarchical = afterLoadKv.from.map.hierarchical(
mapOf("source" to
mapOf("test" to
mapOf("type" to "hierarchical"))))
it("should load the corresponding value in each layer") {
assertThat(afterLoadEnv[item], equalTo("env"))
assertThat(afterLoadSystemProperties[item], equalTo("system"))
assertThat(afterLoadHocon[item], equalTo("conf"))
assertThat(afterLoadJson[item], equalTo("json"))
assertThat(afterLoadProperties[item], equalTo("properties"))
assertThat(afterLoadToml[item], equalTo("toml"))
assertThat(afterLoadXml[item], equalTo("xml"))
assertThat(afterLoadYaml[item], equalTo("yaml"))
assertThat(afterLoadFlat[item], equalTo("flat"))
assertThat(afterLoadKv[item], equalTo("kv"))
assertThat(afterLoadHierarchical[item], equalTo("hierarchical"))
}
}
})

const val jsonContent = """
{
"source": {
"test": {
"type": "json"
}
}
}
"""
3 changes: 3 additions & 0 deletions konf-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
jmhImplementation(kotlin("stdlib"))
}
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.uchuhimo.konf

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
import com.typesafe.config.impl.ConfigImplUtil
import com.uchuhimo.konf.source.ParseException
import java.io.Serializable
import java.math.BigDecimal
Expand Down Expand Up @@ -49,10 +48,10 @@ data class SizeInBytes(
@JsonCreator
@JvmStatic
fun parse(input: String): SizeInBytes {
val s = ConfigImplUtil.unicodeTrim(input)
val s = input.trim()
val unitString = getUnits(s)
val numberString = ConfigImplUtil.unicodeTrim(s.substring(0,
s.length - unitString.length))
val numberString = s.substring(0,
s.length - unitString.length).trim()

// this would be caught later anyway, but the error message
// is more helpful if we check it here.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ import com.uchuhimo.konf.source.base.FlatSource
import com.uchuhimo.konf.source.base.KVSource
import com.uchuhimo.konf.source.base.MapSource
import com.uchuhimo.konf.source.env.EnvProvider
import com.uchuhimo.konf.source.hocon.HoconProvider
import com.uchuhimo.konf.source.json.JsonProvider
import com.uchuhimo.konf.source.properties.PropertiesProvider
import com.uchuhimo.konf.source.toml.TomlProvider
import com.uchuhimo.konf.source.xml.XmlProvider
import com.uchuhimo.konf.source.yaml.YamlProvider
import kotlinx.coroutines.Dispatchers
import org.eclipse.jgit.api.TransportCommand
import org.eclipse.jgit.lib.Constants
import java.io.File
import java.net.URL
import java.util.concurrent.TimeUnit
Expand All @@ -54,11 +48,11 @@ class DefaultLoaders(
*/
private val transform: ((Source) -> Source)? = null
) {
private val optional = config.isEnabled(Feature.OPTIONAL_SOURCE_BY_DEFAULT)
private fun Provider.orMapped(): Provider =
val optional = config.isEnabled(Feature.OPTIONAL_SOURCE_BY_DEFAULT)
fun Provider.orMapped(): Provider =
if (transform != null) this.map(transform) else this

private fun Source.orMapped(): Source = transform?.invoke(this) ?: this
fun Source.orMapped(): Source = transform?.invoke(this) ?: this

/**
* Returns default loaders applied the given [transform] function.
Expand Down Expand Up @@ -90,12 +84,6 @@ class DefaultLoaders(

fun disabled(feature: Feature): DefaultLoaders = mapped { it.disabled(feature) }

/**
* Loader for HOCON source.
*/
@JvmField
val hocon = Loader(config, HoconProvider.orMapped())

/**
* Loader for JSON source.
*/
Expand All @@ -109,28 +97,18 @@ class DefaultLoaders(
val properties = Loader(config, PropertiesProvider.orMapped())

/**
* Loader for TOML source.
*/
@JvmField
val toml = Loader(config, TomlProvider.orMapped())

/**
* Loader for XML source.
*/
@JvmField
val xml = Loader(config, XmlProvider.orMapped())

/**
* Loader for YAML source.
* Loader for map source.
*/
@JvmField
val yaml = Loader(config, YamlProvider.orMapped())
val map = MapLoader(config, transform)

/**
* Loader for map source.
* Loader for a source from the specified provider.
*
* @param provider the specified provider
* @return a loader for a source from the specified provider
*/
@JvmField
val map = MapLoader(config, transform)
fun source(provider: Provider) = Loader(config, provider.orMapped())

/**
* Returns a child config containing values from system environment.
Expand Down Expand Up @@ -366,79 +344,6 @@ class DefaultLoaders(
context: CoroutineContext = Dispatchers.Default,
optional: Boolean = this.optional
): Config = watchUrl(URL(url), delayTime, unit, context, optional)

/**
* Returns a child config containing values from a specified git repository.
*
* Format of the url is auto-detected from the url extension.
* Supported url formats and the corresponding extensions:
* - HOCON: conf
* - JSON: json
* - Properties: properties
* - TOML: toml
* - XML: xml
* - YAML: yml, yaml
*
* Throws [UnsupportedExtensionException] if the url extension is unsupported.
*
* @param repo git repository
* @param file file in the git repository
* @param dir local directory of the git repository
* @param branch the initial branch
* @param optional whether the source is optional
* @param action additional action when cloning/pulling
* @return a child config containing values from a specified git repository
* @throws UnsupportedExtensionException
*/
fun git(
repo: String,
file: String,
dir: String? = null,
branch: String = Constants.HEAD,
optional: Boolean = this.optional,
action: TransportCommand<*, *>.() -> Unit = {}
): Config = dispatchExtension(File(file).extension, "{repo: $repo, file: $file}")
.git(repo, file, dir, branch, optional, action)

/**
* Returns a child config containing values from a specified git repository,
* and reloads values periodically.
*
* Format of the url is auto-detected from the url extension.
* Supported url formats and the corresponding extensions:
* - HOCON: conf
* - JSON: json
* - Properties: properties
* - TOML: toml
* - XML: xml
* - YAML: yml, yaml
*
* Throws [UnsupportedExtensionException] if the url extension is unsupported.
*
* @param repo git repository
* @param file file in the git repository
* @param dir local directory of the git repository
* @param branch the initial branch
* @param period reload period. The default value is 1.
* @param unit time unit of reload period. The default value is [TimeUnit.MINUTES].
* @param context context of the coroutine. The default value is [Dispatchers.Default].
* @param optional whether the source is optional
* @param action additional action when cloning/pulling
* @return a child config containing values from a specified git repository
* @throws UnsupportedExtensionException
*/
fun watchGit(
repo: String,
file: String,
dir: String? = null,
branch: String = Constants.HEAD,
period: Long = 1,
unit: TimeUnit = TimeUnit.MINUTES,
context: CoroutineContext = Dispatchers.Default,
optional: Boolean = this.optional,
action: TransportCommand<*, *>.() -> Unit = {}
): Config = dispatchExtension(File(file).extension, "{repo: $repo, file: $file}")
.watchGit(repo, file, dir, branch, period, unit, context, optional, action)
}

/**
Expand Down
Loading

0 comments on commit cf1a2bf

Please sign in to comment.