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

Source parser and stream utils #117

Merged
merged 12 commits into from
Apr 21, 2023
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ implementation("com.akuleshov7:ktoml-file:0.4.0")
## How to use
:heavy_exclamation_mark: as TOML is a foremost language for config files, we have also supported the deserialization from file.
However, we are using [okio](https://github.com/square/okio) to read the file, so it will be added as a dependency to your
project if you will import [ktoml-file](https://search.maven.org/artifact/com.akuleshov7/ktoml-file).
For basic scenarios of decoding strings you can simple use [ktoml-core](https://search.maven.org/artifact/com.akuleshov7/ktoml-core).
project if you will import [ktoml-file](https://search.maven.org/artifact/com.akuleshov7/ktoml-file).
Same about okio `Source` (for example if you need Streaming): [ktoml-source](https://search.maven.org/artifact/com.akuleshov7/ktoml-source).
For basic scenarios of decoding strings you can simply use [ktoml-core](https://search.maven.org/artifact/com.akuleshov7/ktoml-core).

:heavy_exclamation_mark: don't forget to add the serialization plugin `kotlin("plugin.serialization")` to your project.
Otherwise, `@Serialization` annotation won't work properly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun Project.configureDiktat() {
exclude("src/test/**/*.kt", "src/commonTest/**/*.kt") // path matching this pattern will not be checked by diktat
} else {
include("src/**/*.kt", "**/*.kts")
exclude("src/**test/**/*.kt", "src/commonTest/**/*.kt") // path matching this pattern will not be checked by diktat
exclude("src/**test/**/*.kt", "src/commonTest/**/*.kt", "src/jvmTest/**/*.kt") // path matching this pattern will not be checked by diktat
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ kotlin.code.style=official
kotlin.mpp.stability.nowarn=true
# gradle performance
org.gradle.parallel=true
org.gradle.vfs.watch=true
org.gradle.vfs.watch=true

org.gradle.jvmargs=-Xmx4096M
184 changes: 92 additions & 92 deletions gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
@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
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@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" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
@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
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@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" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
116 changes: 50 additions & 66 deletions ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/Toml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ public open class Toml(
public val tomlParser: TomlParser = TomlParser(inputConfig)
public val tomlWriter: TomlWriter = TomlWriter(outputConfig)

@Deprecated(
message = "config parameter split into inputConfig and outputConfig. Will be removed in next releases."
)
public constructor(
config: TomlConfig,
serializersModule: SerializersModule = EmptySerializersModule(),
) : this(
config.input,
config.output,
serializersModule
)

// ================== basic overrides ===============

/**
Expand All @@ -61,7 +49,6 @@ public open class Toml(

override fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String {
val toml = TomlMainEncoder.encode(serializer, value, outputConfig, serializersModule)

return tomlWriter.writeToString(file = toml)
}

Expand All @@ -78,24 +65,49 @@ public open class Toml(
public fun <T> decodeFromString(
deserializer: DeserializationStrategy<T>,
toml: List<String>,
config: TomlInputConfig
config: TomlInputConfig = this.inputConfig
): T = decodeFromString(deserializer, toml.asSequence(), config)

/**
* simple deserializer of a sequence of strings in a toml format
*
* @param toml sequence with strings in toml format
* @param deserializer deserialization strategy
* @param config
* @return deserialized object of type T
*/
public fun <T> decodeFromString(
deserializer: DeserializationStrategy<T>,
toml: Sequence<String>,
config: TomlInputConfig = this.inputConfig
): T {
val parsedToml = tomlParser.parseStringsToTomlTree(toml, config)
return TomlMainDecoder.decode(deserializer, parsedToml, config)
return TomlMainDecoder.decode(deserializer, parsedToml, this.inputConfig)
}

@Deprecated(
message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases.",
replaceWith = ReplaceWith(
"decodeFromString(deserializer, toml, config)",
"com.akuleshov7.ktoml.TomlInputConfig"
)
)
public fun <T> decodeFromString(
/**
* partial deserializer of a sequence of lines in a toml format.
* Will deserialize only the part presented under the tomlTableName table.
* If such table is missing in he input - will throw an exception
*
* (!) Useful when you would like to deserialize only ONE table
* and you do not want to reproduce whole object structure in the code
*
* @param deserializer deserialization strategy
* @param tomlLines sequence of TOML lines
* @param tomlTableName fully qualified name of the toml table (it should be the full name - a.b.c.d)
* @param config
* @return deserialized object of type T
*/
public fun <T> partiallyDecodeFromLines(
deserializer: DeserializationStrategy<T>,
toml: List<String>,
config: TomlConfig
): T = decodeFromString(deserializer, toml, config.input)
tomlLines: Sequence<String>,
tomlTableName: String,
config: TomlInputConfig = this.inputConfig
): T {
val fakeFileNode = generateFakeTomlStructureForPartialParsing(tomlLines, tomlTableName, config, TomlParser::parseLines)
return TomlMainDecoder.decode(deserializer, fakeFileNode, this.inputConfig)
}

/**
* partial deserializer of a string in a toml format (separated by newlines).
Expand All @@ -115,78 +127,50 @@ public open class Toml(
deserializer: DeserializationStrategy<T>,
toml: String,
tomlTableName: String,
config: TomlInputConfig = TomlInputConfig()
config: TomlInputConfig = this.inputConfig
): T {
val fakeFileNode = generateFakeTomlStructureForPartialParsing(toml, tomlTableName, config, TomlParser::parseString)
return TomlMainDecoder.decode(deserializer, fakeFileNode, config)
}

@Deprecated(
message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases.",
replaceWith = ReplaceWith(
"partiallyDecodeFromString(deserializer, toml, tomlTableName, config)",
"com.akuleshov7.ktoml.TomlInputConfig"
)
)
public fun <T> partiallyDecodeFromString(
deserializer: DeserializationStrategy<T>,
toml: String,
tomlTableName: String,
config: TomlConfig
): T = partiallyDecodeFromString(deserializer, toml, tomlTableName, config.input)

/**
* partial deserializer of a string in a toml format (separated by newlines).
* partial deserializer of a sequence of lines in a toml format.
* Will deserialize only the part presented under the tomlTableName table.
* If such table is missing in he input - will throw an exception
*
* (!) Useful when you would like to deserialize only ONE table
* and you do not want to reproduce whole object structure in the code
*
* @param deserializer deserialization strategy
* @param toml list of strings with toml input
* @param tomlLines sequence of strings with toml input
* @param tomlTableName fully qualified name of the toml table (it should be the full name - a.b.c.d)
* @param config
* @return deserialized object of type T
*/
public fun <T> partiallyDecodeFromString(
deserializer: DeserializationStrategy<T>,
toml: List<String>,
tomlLines: Sequence<String>,
tomlTableName: String,
config: TomlInputConfig = TomlInputConfig()
config: TomlInputConfig = this.inputConfig
): T {
val fakeFileNode = generateFakeTomlStructureForPartialParsing(
toml.joinToString("\n"),
tomlLines,
tomlTableName,
config,
TomlParser::parseString,
TomlParser::parseLines,
)
return TomlMainDecoder.decode(deserializer, fakeFileNode, config)
return TomlMainDecoder.decode(deserializer, fakeFileNode, this.inputConfig)
}

@Deprecated(
message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases.",
replaceWith = ReplaceWith(
"partiallyDecodeFromString(deserializer, toml, tomlTableName, config)",
"com.akuleshov7.ktoml.TomlInputConfig"
)
)
public fun <T> partiallyDecodeFromString(
deserializer: DeserializationStrategy<T>,
toml: List<String>,
tomlTableName: String,
config: TomlConfig = TomlConfig()
): T = partiallyDecodeFromString(deserializer, toml, tomlTableName, config.input)

// ================== other ===============
@Suppress("TYPE_ALIAS")
private fun generateFakeTomlStructureForPartialParsing(
toml: String,
private fun <I> generateFakeTomlStructureForPartialParsing(
tomlInput: I,
tomlTableName: String,
config: TomlInputConfig = TomlInputConfig(),
parsingFunction: (TomlParser, String) -> TomlFile
parsingFunction: (TomlParser, I) -> TomlFile
): TomlFile {
val tomlFile = parsingFunction(TomlParser(config), toml)
val tomlFile = parsingFunction(TomlParser(config), tomlInput)
val parsedToml = findPrimitiveTableInAstByName(listOf(tomlFile), tomlTableName)
?: throw MissingRequiredPropertyException(
"Cannot find table with name <$tomlTableName> in the toml input. " +
Expand Down
Loading