diff --git a/README.md b/README.md index 0f4c134b..fd58d631 100644 --- a/README.md +++ b/README.md @@ -183,8 +183,8 @@ val resultFromList = TomlFileReader.partiallyDecodeFromFile(serializer( import com.akuleshov7.ktoml.parsers.TomlParser import com.akuleshov7.ktoml.TomlConfig /* ========= */ -var tomlAST = TomlParser(TomlConfig()).parseStringsToTomlTree(/* list with toml strings */) -tomlAST = TomlParser(TomlConfig()).parseString(/* the string that you want to parse */) +var tomlAST = TomlParser(TomlInputConfig()).parseStringsToTomlTree(/* list with toml strings */) +tomlAST = TomlParser(TomlInputConfig()).parseString(/* the string that you want to parse */) tomlAST.prettyPrint() ``` @@ -195,7 +195,7 @@ special configuration class that can be passed to the decoder method: ```kotlin Toml( - config = TomlConfig( + inputConfig = TomlInputConfig( // allow/prohibit unknown names during the deserialization, default false ignoreUnknownNames = false, // allow/prohibit empty values like "a = # comment", default true @@ -206,6 +206,8 @@ Toml( allowEscapedQuotesInLiteralStrings = true, // allow/prohibit processing of empty toml, if false - throws an InternalDecodingException exception, default is true allowEmptyToml = true, + ), + outputConfig = TomlOutputConfig( // indentation symbols for serialization, default 4 spaces indentation = Indentation.FOUR_SPACES, ) diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/Toml.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/Toml.kt index 69178761..51e05f53 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/Toml.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/Toml.kt @@ -8,12 +8,10 @@ import com.akuleshov7.ktoml.utils.findPrimitiveTableInAstByName import com.akuleshov7.ktoml.writers.TomlWriter import kotlin.native.concurrent.ThreadLocal - import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationStrategy import kotlinx.serialization.StringFormat - import kotlinx.serialization.modules.EmptySerializersModule import kotlinx.serialization.modules.SerializersModule @@ -21,18 +19,32 @@ import kotlinx.serialization.modules.SerializersModule * Toml class - is a general entry point in the core, * that is used to serialize/deserialize TOML file or string * - * @property config - configuration for the serialization + * @property inputConfig - configuration for deserialization + * @property outputConfig - configuration for serialization * @property serializersModule - default overridden */ @OptIn(ExperimentalSerializationApi::class) public open class Toml( - private val config: TomlConfig = TomlConfig(), + protected val inputConfig: TomlInputConfig = TomlInputConfig(), + protected val outputConfig: TomlOutputConfig = TomlOutputConfig(), override val serializersModule: SerializersModule = EmptySerializersModule ) : StringFormat { // parser and writer are created once after the creation of the class, to reduce // the number of created parsers and writers for each toml - public val tomlParser: TomlParser = TomlParser(config) - public val tomlWriter: TomlWriter = TomlWriter(config) + 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 =============== @@ -44,7 +56,7 @@ public open class Toml( */ override fun decodeFromString(deserializer: DeserializationStrategy, string: String): T { val parsedToml = tomlParser.parseString(string) - return TomlMainDecoder.decode(deserializer, parsedToml, config) + return TomlMainDecoder.decode(deserializer, parsedToml, inputConfig) } override fun encodeToString(serializer: SerializationStrategy, value: T): String { @@ -64,12 +76,25 @@ public open class Toml( public fun decodeFromString( deserializer: DeserializationStrategy, toml: List, - config: TomlConfig + config: TomlInputConfig ): T { val parsedToml = tomlParser.parseStringsToTomlTree(toml, config) - return TomlMainDecoder.decode(deserializer, parsedToml, this.config) + return TomlMainDecoder.decode(deserializer, parsedToml, config) } + @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 decodeFromString( + deserializer: DeserializationStrategy, + toml: List, + config: TomlConfig + ): T = decodeFromString(deserializer, toml, config.input) + /** * partial deserializer of a string in a toml format (separated by newlines). * Will deserialize only the part presented under the tomlTableName table. @@ -88,12 +113,26 @@ public open class Toml( deserializer: DeserializationStrategy, toml: String, tomlTableName: String, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ): T { val fakeFileNode = generateFakeTomlStructureForPartialParsing(toml, tomlTableName, config, TomlParser::parseString) - return TomlMainDecoder.decode(deserializer, fakeFileNode, this.config) + 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 partiallyDecodeFromString( + deserializer: DeserializationStrategy, + 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). * Will deserialize only the part presented under the tomlTableName table. @@ -112,7 +151,7 @@ public open class Toml( deserializer: DeserializationStrategy, toml: List, tomlTableName: String, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ): T { val fakeFileNode = generateFakeTomlStructureForPartialParsing( toml.joinToString("\n"), @@ -120,18 +159,32 @@ public open class Toml( config, TomlParser::parseString, ) - return TomlMainDecoder.decode(deserializer, fakeFileNode, this.config) + 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 partiallyDecodeFromString( + deserializer: DeserializationStrategy, + toml: List, + tomlTableName: String, + config: TomlConfig = TomlConfig() + ): T = partiallyDecodeFromString(deserializer, toml, tomlTableName, config.input) + // ================== other =============== @Suppress("TYPE_ALIAS") private fun generateFakeTomlStructureForPartialParsing( toml: String, tomlTableName: String, - config: TomlConfig = TomlConfig(), + config: TomlInputConfig = TomlInputConfig(), parsingFunction: (TomlParser, String) -> TomlFile ): TomlFile { - val tomlFile = parsingFunction(TomlParser(this.config), toml) + val tomlFile = parsingFunction(TomlParser(config), toml) val parsedToml = findPrimitiveTableInAstByName(listOf(tomlFile), tomlTableName) ?: throw MissingRequiredPropertyException( "Cannot find table with name <$tomlTableName> in the toml input. " + @@ -154,5 +207,8 @@ public open class Toml( * ThreadLocal annotation is used here for caching. */ @ThreadLocal - public companion object Default : Toml(TomlConfig()) + public companion object Default : Toml( + inputConfig = TomlInputConfig(), + outputConfig = TomlOutputConfig() + ) } diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/TomlConfig.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/TomlConfig.kt index e425c8b6..ad6dee69 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/TomlConfig.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/TomlConfig.kt @@ -24,6 +24,9 @@ public class KtomlConf( * @property indentation - the number of spaces in the indents for the serialization * @property allowEmptyToml - controls if empty toml can be processed, if false - will throw an exception */ +@Deprecated( + message = "Class split into TomlInputConfig and TomlOutputConfig. Will be removed in next releases." +) public open class TomlConfig( public val ignoreUnknownNames: Boolean = false, public val allowEmptyValues: Boolean = true, @@ -32,14 +35,110 @@ public open class TomlConfig( public val indentation: Indentation = Indentation.FOUR_SPACES, public val allowEmptyToml: Boolean = true, ) { + internal val input = TomlInputConfig( + ignoreUnknownNames, + allowEmptyValues, + allowNullValues, + allowEmptyToml, + allowEscapedQuotesInLiteralStrings + ) + internal val output = TomlOutputConfig( + indentation.toTomlIndentation(), + allowEscapedQuotesInLiteralStrings + ) + /** * @property value - string with indents, used for the formatting of serialization */ + @Deprecated( + message = "Enum moved to top-level.", + replaceWith = ReplaceWith( + "TomlIndentation", + "com.akuleshov7.ktoml.TomlIndentation" + ) + ) public enum class Indentation(public val value: String) { FOUR_SPACES(" "), NONE(""), TAB("\t"), TWO_SPACES(" "), ; + + internal fun toTomlIndentation() = TomlIndentation.valueOf(name) } } + +/** + * A config to change parsing behavior. + * @property ignoreUnknownNames Whether to allow/prohibit unknown names during the deserialization + * @property allowEmptyValues Whether to allow/prohibit empty values: a = # comment + * @property allowNullValues Whether to allow/prohibit null values: a = null + * @property allowEmptyToml Whether empty toml can be processed, if false - will throw an exception + * @property allowEscapedQuotesInLiteralStrings Whether to allow/prohibit escaping of single quotes in literal strings + */ +public data class TomlInputConfig( + public val ignoreUnknownNames: Boolean = false, + public val allowEmptyValues: Boolean = true, + public val allowNullValues: Boolean = true, + public val allowEmptyToml: Boolean = true, + public val allowEscapedQuotesInLiteralStrings: Boolean = true +) { + public companion object { + /** + * Creates a config populated with values compliant with the TOML spec. + * + * @param ignoreUnknownNames Whether to allow/prohibit unknown names during the deserialization + * @param allowEmptyToml Whether empty toml can be processed, if false - will throw an exception + * @return A TOML spec-compliant input config + */ + public fun compliant( + ignoreUnknownNames: Boolean = false, + allowEmptyToml: Boolean = true + ): TomlInputConfig = + TomlInputConfig( + ignoreUnknownNames, + allowEmptyValues = false, + allowNullValues = false, + allowEmptyToml, + allowEscapedQuotesInLiteralStrings = false + ) + } +} + +/** + * A config to change writing behavior. + * + * @property indentation The number of spaces in the indents for the serialization + * @property allowEscapedQuotesInLiteralStrings Whether to allow/prohibit escaping of single quotes in literal strings + */ +public data class TomlOutputConfig( + public val indentation: TomlIndentation = TomlIndentation.FOUR_SPACES, + public val allowEscapedQuotesInLiteralStrings: Boolean = true, +) { + public companion object { + /** + * Creates a config populated with values compliant with the TOML spec. + * + * @param indentation The number of spaces in the indents for the serialization + * @return A TOML spec-compliant output config + */ + public fun compliant( + indentation: TomlIndentation = TomlIndentation.FOUR_SPACES + ): TomlOutputConfig = + TomlOutputConfig( + indentation, + allowEscapedQuotesInLiteralStrings = false + ) + } +} + +/** + * @property value The indent string, used for the formatting during serialization + */ +public enum class TomlIndentation(public val value: String) { + FOUR_SPACES(" "), + NONE(""), + TAB("\t"), + TWO_SPACES(" "), + ; +} diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlArrayDecoder.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlArrayDecoder.kt index 24a254a8..98fcb4a3 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlArrayDecoder.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlArrayDecoder.kt @@ -1,11 +1,8 @@ package com.akuleshov7.ktoml.decoders import com.akuleshov7.ktoml.TomlConfig -import com.akuleshov7.ktoml.tree.TomlKeyValue -import com.akuleshov7.ktoml.tree.TomlKeyValueArray -import com.akuleshov7.ktoml.tree.TomlKeyValuePrimitive -import com.akuleshov7.ktoml.tree.TomlNull -import com.akuleshov7.ktoml.tree.TomlValue +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.tree.* import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.descriptors.SerialDescriptor @@ -21,7 +18,7 @@ import kotlinx.serialization.modules.SerializersModule @Suppress("UNCHECKED_CAST") public class TomlArrayDecoder( private val rootNode: TomlKeyValueArray, - private val config: TomlConfig, + private val config: TomlInputConfig, ) : TomlAbstractDecoder() { private var nextElementIndex = 0 private val list = rootNode.value.content as List @@ -29,6 +26,14 @@ public class TomlArrayDecoder( private lateinit var currentElementDecoder: TomlPrimitiveDecoder private lateinit var currentPrimitiveElementOfArray: TomlValue + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + rootNode: TomlKeyValueArray, + config: TomlConfig + ) : this(rootNode, config.input) + private fun haveStartedReadingElements() = nextElementIndex > 0 override fun decodeCollectionSize(descriptor: SerialDescriptor): Int = list.size diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlMainDecoder.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlMainDecoder.kt index 13d96b5e..6b78560e 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlMainDecoder.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlMainDecoder.kt @@ -3,10 +3,9 @@ package com.akuleshov7.ktoml.decoders import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig import com.akuleshov7.ktoml.exceptions.* import com.akuleshov7.ktoml.tree.* -import com.akuleshov7.ktoml.tree.TomlNull -import com.akuleshov7.ktoml.tree.TomlTablePrimitive import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.descriptors.SerialDescriptor @@ -26,11 +25,24 @@ import kotlinx.serialization.modules.SerializersModule @ExperimentalSerializationApi public class TomlMainDecoder( private var rootNode: TomlNode, - private val config: TomlConfig, + private val config: TomlInputConfig, private var elementIndex: Int = 0 ) : TomlAbstractDecoder() { override val serializersModule: SerializersModule = EmptySerializersModule + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + rootNode: TomlNode, + config: TomlConfig, + elementIndex: Int = 0 + ) : this( + rootNode, + config.input, + elementIndex + ) + override fun decodeValue(): Any = decodeKeyValue().value.content override fun decodeNotNullMark(): Boolean { @@ -254,7 +266,7 @@ public class TomlMainDecoder( public fun decode( deserializer: DeserializationStrategy, rootNode: TomlNode, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ): T { val decoder = TomlMainDecoder(rootNode, config) return decoder.decodeSerializableValue(deserializer) diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/parsers/TomlParser.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/parsers/TomlParser.kt index 86d12251..35015a0a 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/parsers/TomlParser.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/parsers/TomlParser.kt @@ -1,6 +1,7 @@ package com.akuleshov7.ktoml.parsers import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig import com.akuleshov7.ktoml.tree.* import kotlin.jvm.JvmInline @@ -9,7 +10,12 @@ import kotlin.jvm.JvmInline */ @JvmInline @Suppress("WRONG_MULTIPLE_MODIFIERS_ORDER") -public value class TomlParser(private val config: TomlConfig) { +public value class TomlParser(private val config: TomlInputConfig) { + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor(config: TomlConfig) : this(config.input) + /** * Method for parsing of TOML string (this string should be split with newlines \n or \r\n) * @@ -31,7 +37,7 @@ public value class TomlParser(private val config: TomlConfig) { * @throws InternalAstException - if toml node does not inherit TomlNode class */ @Suppress("TOO_LONG_FUNCTION") - public fun parseStringsToTomlTree(tomlLines: List, config: TomlConfig): TomlFile { + public fun parseStringsToTomlTree(tomlLines: List, config: TomlInputConfig): TomlFile { var currentParentalNode: TomlNode = TomlFile(config) // link to the head of the tree val tomlFileHead = currentParentalNode as TomlFile @@ -152,7 +158,7 @@ public fun String.parseTomlKeyValue( lineNo: Int, comments: List, inlineComment: String, - config: TomlConfig + config: TomlInputConfig ): TomlNode { val keyValuePair = this.splitKeyValue(lineNo, config) return when { diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlArrayOfTables.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlArrayOfTables.kt index f455ce85..8e11d228 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlArrayOfTables.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlArrayOfTables.kt @@ -5,6 +5,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.exceptions.ParseException import com.akuleshov7.ktoml.parsers.splitKeyToTokens import com.akuleshov7.ktoml.parsers.takeBeforeComment @@ -21,7 +23,7 @@ import com.akuleshov7.ktoml.writers.TomlEmitter public class TomlArrayOfTables( content: String, lineNo: Int, - config: TomlConfig = TomlConfig(), + config: TomlInputConfig = TomlInputConfig(), isSynthetic: Boolean = false ) : TomlTable( content, @@ -66,10 +68,25 @@ public class TomlArrayOfTables( } } - override fun TomlEmitter.writeHeader(headerKey: TomlKey, config: TomlConfig) { + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + content: String, + lineNo: Int, + config: TomlConfig, + isSynthetic: Boolean = false + ) : this( + content, + lineNo, + config.input, + isSynthetic + ) + + override fun TomlEmitter.writeHeader(headerKey: TomlKey, config: TomlOutputConfig) { startTableArrayHeader() - headerKey.write(emitter = this, config) + headerKey.write(emitter = this) endTableArrayHeader() } @@ -77,7 +94,7 @@ public class TomlArrayOfTables( override fun TomlEmitter.writeChildren( headerKey: TomlKey, children: List, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { val last = children.lastIndex @@ -125,7 +142,7 @@ public class TomlArrayOfTablesElement( lineNo: Int, comments: List, inlineComment: String, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) : TomlNode( EMPTY_TECHNICAL_NODE, lineNo, @@ -137,7 +154,7 @@ public class TomlArrayOfTablesElement( override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ): Unit = emitter.writeChildren(children, config, multiline) } diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlFile.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlFile.kt index 9a9615e9..1d7fe906 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlFile.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlFile.kt @@ -1,13 +1,15 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.exceptions.InternalAstException import com.akuleshov7.ktoml.writers.TomlEmitter /** * A root node for TOML Abstract Syntax Tree */ -public class TomlFile(config: TomlConfig = TomlConfig()) : TomlNode( +public class TomlFile(config: TomlInputConfig = TomlInputConfig()) : TomlNode( "rootNode", 0, comments = emptyList(), @@ -16,12 +18,17 @@ public class TomlFile(config: TomlConfig = TomlConfig()) : TomlNode( ) { override val name: String = "rootNode" + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor(config: TomlConfig) : this(config.input) + override fun getNeighbourNodes(): MutableList = throw InternalAstException("Invalid call to getNeighbourNodes() for TomlFile node") override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ): Unit = emitter.writeChildren( diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlInlineTable.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlInlineTable.kt index 5a01cbac..cb9c77f3 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlInlineTable.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlInlineTable.kt @@ -1,6 +1,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.exceptions.ParseException import com.akuleshov7.ktoml.parsers.parseTomlKeyValue import com.akuleshov7.ktoml.parsers.trimCurlyBraces @@ -17,7 +19,7 @@ public class TomlInlineTable( lineNo: Int, comments: List = emptyList(), inlineComment: String = "", - config: TomlConfig = TomlConfig(), + config: TomlInputConfig = TomlInputConfig(), ) : TomlNode( "${keyValuePair.first} = ${keyValuePair.second}", lineNo, @@ -32,6 +34,23 @@ public class TomlInlineTable( tomlKeyValues = keyValuePair.second.parseInlineTableValue() } + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + keyValuePair: Pair, + lineNo: Int, + comments: List = emptyList(), + inlineComment: String = "", + config: TomlConfig + ) : this( + keyValuePair, + lineNo, + comments, + inlineComment, + config.input + ) + private fun String.parseInlineTableValue(): List { val parsedList = this .trimCurlyBraces() @@ -85,12 +104,12 @@ public class TomlInlineTable( public override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { val key = TomlKey(name, 0) - key.write(emitter, config) + key.write(emitter) emitter.emitPairDelimiter() .startInlineTable() diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKey.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKey.kt index a486014c..85bff5cc 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKey.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKey.kt @@ -42,7 +42,13 @@ public class TomlKey(public val rawContent: String, public val lineNo: Int) { return false } - public fun write(emitter: TomlEmitter, config: TomlConfig) { + @Deprecated( + message = "TomlConfig is deprecated. Will be removed in next releases.", + replaceWith = ReplaceWith("write(emitter)") + ) + public fun write(emitter: TomlEmitter, config: TomlConfig): Unit = write(emitter) + + public fun write(emitter: TomlEmitter) { val keys = keyParts if (keys.isEmpty() || keys.any(String::isEmpty)) { diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValue.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValue.kt index 2f29fb7d..4e1dca10 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValue.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValue.kt @@ -1,6 +1,7 @@ package com.akuleshov7.ktoml.tree -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.exceptions.ParseException import com.akuleshov7.ktoml.parsers.takeBeforeComment import com.akuleshov7.ktoml.writers.TomlEmitter @@ -28,7 +29,7 @@ internal interface TomlKeyValue { * @param config * @return the table that is parsed from a dotted key */ - fun createTomlTableFromDottedKey(parentNode: TomlNode, config: TomlConfig = TomlConfig()): TomlTablePrimitive { + fun createTomlTableFromDottedKey(parentNode: TomlNode, config: TomlInputConfig = TomlInputConfig()): TomlTablePrimitive { // for a key: a.b.c it will be [a, b] val syntheticTablePrefix = this.key.keyParts.dropLast(1) // creating new key with the last dot-separated fragment @@ -50,10 +51,10 @@ internal interface TomlKeyValue { fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { - key.write(emitter, config) + key.write(emitter) emitter.emitPairDelimiter() @@ -69,7 +70,7 @@ internal interface TomlKeyValue { * @param config * @return an object of type Array that was parsed from string */ -public fun String.parseList(lineNo: Int, config: TomlConfig): TomlArray = TomlArray(this, lineNo, config) +public fun String.parseList(lineNo: Int, config: TomlInputConfig): TomlArray = TomlArray(this, lineNo, config) /** * parse and split a string in a key-value format @@ -79,7 +80,7 @@ public fun String.parseList(lineNo: Int, config: TomlConfig): TomlArray = TomlAr * @return a resulted key-value pair * @throws ParseException */ -public fun String.splitKeyValue(lineNo: Int, config: TomlConfig = TomlConfig()): Pair { +public fun String.splitKeyValue(lineNo: Int, config: TomlInputConfig = TomlInputConfig()): Pair { // finding the index of the last quote, if no quotes are found, then use the length of the string val closingQuoteIndex = listOf( this.lastIndexOf("\""), @@ -122,7 +123,7 @@ public fun String.splitKeyValue(lineNo: Int, config: TomlConfig = TomlConfig()): * @param config * @return parsed TomlNode value */ -public fun String.parseValue(lineNo: Int, config: TomlConfig): TomlValue = when (this) { +public fun String.parseValue(lineNo: Int, config: TomlInputConfig): TomlValue = when (this) { // ===== null values "null", "nil", "NULL", "NIL", "" -> if (config.allowNullValues) { TomlNull(lineNo) @@ -162,7 +163,7 @@ private inline fun String.tryParseValue( private fun String.checkNotEmpty( log: String, content: String, - config: TomlConfig = TomlConfig(), + config: TomlInputConfig = TomlInputConfig(), lineNo: Int ): String = this.also { diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValueArray.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValueArray.kt index 1123755d..674241af 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValueArray.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValueArray.kt @@ -1,6 +1,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.writers.TomlEmitter /** @@ -17,7 +19,7 @@ public class TomlKeyValueArray( comments: List, inlineComment: String, override val name: String, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) : TomlNode( key, value, @@ -32,7 +34,7 @@ public class TomlKeyValueArray( lineNo: Int, comments: List = emptyList(), inlineComment: String = "", - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) : this( TomlKey(keyValuePair.first, lineNo), keyValuePair.second.parseList(lineNo, config), @@ -42,9 +44,47 @@ public class TomlKeyValueArray( TomlKey(keyValuePair.first, lineNo).content ) + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + key: TomlKey, + value: TomlValue, + lineNo: Int, + comments: List = emptyList(), + inlineComment: String = "", + name: String, + config: TomlConfig + ) : this( + key, + value, + lineNo, + comments, + inlineComment, + name, + config.input + ) + + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + keyValuePair: Pair, + lineNo: Int, + comments: List = emptyList(), + inlineComment: String = "", + config: TomlConfig + ) : this( + keyValuePair, + lineNo, + comments, + inlineComment, + config.input + ) + public override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean - ): Unit = super.write(emitter, config, multiline) + ): Unit = super.write(emitter, config, multiline) } diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValuePrimitive.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValuePrimitive.kt index ea660dc1..8616b865 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValuePrimitive.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlKeyValuePrimitive.kt @@ -1,6 +1,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.writers.TomlEmitter /** @@ -17,7 +19,7 @@ public class TomlKeyValuePrimitive( comments: List, inlineComment: String, override val name: String, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) : TomlNode( key, value, @@ -32,7 +34,7 @@ public class TomlKeyValuePrimitive( lineNo: Int, comments: List = emptyList(), inlineComment: String = "", - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) : this( TomlKey(keyValuePair.first, lineNo), keyValuePair.second.parseValue(lineNo, config), @@ -42,9 +44,47 @@ public class TomlKeyValuePrimitive( TomlKey(keyValuePair.first, lineNo).content ) + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + key: TomlKey, + value: TomlValue, + lineNo: Int, + comments: List = emptyList(), + inlineComment: String = "", + name: String, + config: TomlConfig + ) : this( + key, + value, + lineNo, + comments, + inlineComment, + name, + config.input + ) + + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + keyValuePair: Pair, + lineNo: Int, + comments: List = emptyList(), + inlineComment: String = "", + config: TomlConfig + ) : this( + keyValuePair, + lineNo, + comments, + inlineComment, + config.input + ) + public override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean - ): Unit = super.write(emitter, config, multiline) + ): Unit = super.write(emitter, config, multiline) } diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlNode.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlNode.kt index d6277e62..4934f487 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlNode.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlNode.kt @@ -5,6 +5,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.exceptions.InternalAstException import com.akuleshov7.ktoml.writers.TomlEmitter @@ -27,7 +29,7 @@ public sealed class TomlNode( public open val lineNo: Int, comments: List, public val inlineComment: String, - public open val config: TomlConfig = TomlConfig() + public open val config: TomlInputConfig = TomlInputConfig() ) { /** * A list of comments prepended to the node. @@ -49,7 +51,7 @@ public sealed class TomlNode( lineNo: Int, comments: List, inlineComment: String, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) : this( "${key.content}=${value.content}", lineNo, @@ -225,6 +227,19 @@ public sealed class TomlNode( } } + @Deprecated( + message = "TomlConfig is deprecated; use TomlOutputConfig instead. Will be removed in next releases.", + replaceWith = ReplaceWith( + "write(emitter, config, multiline)", + "com.akuleshov7.ktoml.TomlOutputConfig" + ) + ) + public fun write( + emitter: TomlEmitter, + config: TomlConfig, + multiline: Boolean = false + ): Unit = write(emitter, config.output, multiline) + /** * Writes this node as text to [emitter]. * @@ -234,13 +249,13 @@ public sealed class TomlNode( */ public abstract fun write( emitter: TomlEmitter, - config: TomlConfig = this.config, + config: TomlOutputConfig = TomlOutputConfig(), multiline: Boolean = false ) protected open fun TomlEmitter.writeChildren( children: List, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { val last = children.lastIndex diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlStubEmptyNode.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlStubEmptyNode.kt index 1f4186c1..d6105e8b 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlStubEmptyNode.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlStubEmptyNode.kt @@ -1,6 +1,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.writers.TomlEmitter /** @@ -9,7 +11,7 @@ import com.akuleshov7.ktoml.writers.TomlEmitter * * Instances of this stub will be added as children to such parsed tables */ -public class TomlStubEmptyNode(lineNo: Int, config: TomlConfig = TomlConfig()) : TomlNode( +public class TomlStubEmptyNode(lineNo: Int, config: TomlInputConfig = TomlInputConfig()) : TomlNode( EMPTY_TECHNICAL_NODE, lineNo, comments = emptyList(), @@ -18,9 +20,14 @@ public class TomlStubEmptyNode(lineNo: Int, config: TomlConfig = TomlConfig()) : ) { override val name: String = EMPTY_TECHNICAL_NODE + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor(lineNo: Int, config: TomlConfig) : this(lineNo, config.input) + override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { // Nothing to write in stub nodes. diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlTable.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlTable.kt index 9d7c64ee..b9ad480c 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlTable.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlTable.kt @@ -5,6 +5,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.writers.TomlEmitter /** @@ -20,7 +22,7 @@ public abstract class TomlTable( override val lineNo: Int, comments: List, inlineComment: String, - override val config: TomlConfig = TomlConfig(), + override val config: TomlInputConfig = TomlInputConfig(), public val isSynthetic: Boolean = false ) : TomlNode( content, @@ -33,9 +35,28 @@ public abstract class TomlTable( public abstract var tablesList: List public abstract val type: TableType + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + content: String, + lineNo: Int, + comments: List = emptyList(), + inlineComment: String = "", + config: TomlConfig, + isSynthetic: Boolean = false + ) : this( + content, + lineNo, + comments, + inlineComment, + config.input, + isSynthetic + ) + override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { // Todo: Option to explicitly define super tables? @@ -69,13 +90,13 @@ public abstract class TomlTable( protected abstract fun TomlEmitter.writeChildren( headerKey: TomlKey, children: List, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) protected abstract fun TomlEmitter.writeHeader( headerKey: TomlKey, - config: TomlConfig + config: TomlOutputConfig ) /** diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlTablePrimitive.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlTablePrimitive.kt index 3966b0be..2725cffc 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlTablePrimitive.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlTablePrimitive.kt @@ -5,6 +5,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.exceptions.ParseException import com.akuleshov7.ktoml.parsers.* import com.akuleshov7.ktoml.writers.TomlEmitter @@ -20,7 +22,7 @@ public class TomlTablePrimitive( lineNo: Int, comments: List = emptyList(), inlineComment: String = "", - config: TomlConfig = TomlConfig(), + config: TomlInputConfig = TomlInputConfig(), isSynthetic: Boolean = false ) : TomlTable( content, @@ -65,13 +67,32 @@ public class TomlTablePrimitive( } } + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + content: String, + lineNo: Int, + comments: List = emptyList(), + inlineComment: String = "", + config: TomlConfig, + isSynthetic: Boolean = false + ) : this( + content, + lineNo, + comments, + inlineComment, + config.input, + isSynthetic + ) + override fun TomlEmitter.writeHeader( headerKey: TomlKey, - config: TomlConfig + config: TomlOutputConfig ) { startTableHeader() - headerKey.write(emitter = this, config) + headerKey.write(emitter = this) endTableHeader() } @@ -79,7 +100,7 @@ public class TomlTablePrimitive( override fun TomlEmitter.writeChildren( headerKey: TomlKey, children: List, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { if (children.first() is TomlStubEmptyNode) { diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlValue.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlValue.kt index 942a9b04..4720a345 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlValue.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/tree/TomlValue.kt @@ -5,6 +5,8 @@ package com.akuleshov7.ktoml.tree import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.exceptions.ParseException import com.akuleshov7.ktoml.exceptions.TomlWritingException import com.akuleshov7.ktoml.parsers.trimBrackets @@ -23,6 +25,19 @@ import kotlinx.datetime.* public sealed class TomlValue(public val lineNo: Int) { public abstract var content: Any + @Deprecated( + message = "TomlConfig is deprecated; use TomlOutputConfig instead. Will be removed in next releases.", + replaceWith = ReplaceWith( + "write(emitter, config, multiline)", + "com.akuleshov7.ktoml.TomlOutputConfig" + ) + ) + public fun write( + emitter: TomlEmitter, + config: TomlConfig, + multiline: Boolean = false + ): Unit = write(emitter, config.output, multiline) + /** * Writes this value to the specified [emitter], optionally writing the value * [multiline] (if supported by the value type). @@ -33,7 +48,7 @@ public sealed class TomlValue(public val lineNo: Int) { */ public abstract fun write( emitter: TomlEmitter, - config: TomlConfig = TomlConfig(), + config: TomlOutputConfig = TomlOutputConfig(), multiline: Boolean = false ) } @@ -52,12 +67,25 @@ internal constructor( public constructor( content: String, lineNo: Int, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) : this(content.verifyAndTrimQuotes(lineNo, config), lineNo) + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + content: String, + lineNo: Int, + config: TomlConfig + ) : this( + content, + lineNo, + config.input + ) + override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { if (multiline) { @@ -76,7 +104,7 @@ internal constructor( } public companion object { - private fun String.verifyAndTrimQuotes(lineNo: Int, config: TomlConfig): Any = + private fun String.verifyAndTrimQuotes(lineNo: Int, config: TomlInputConfig): Any = if (startsWith("'") && endsWith("'")) { val contentString = trimSingleQuotes() if (config.allowEscapedQuotesInLiteralStrings) contentString.convertSingleQuotes() else contentString @@ -97,7 +125,7 @@ internal constructor( */ private fun String.convertSingleQuotes(): String = this.replace("\\'", "'") - private fun String.escapeQuotesAndVerify(config: TomlConfig) = + private fun String.escapeQuotesAndVerify(config: TomlOutputConfig) = when { controlCharacterRegex in this -> throw TomlWritingException( @@ -139,7 +167,7 @@ internal constructor( override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { if (multiline) { @@ -298,7 +326,7 @@ internal constructor( override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { emitter.emitValue(content as Long) @@ -320,7 +348,7 @@ internal constructor( override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { emitter.emitValue(content as Double) @@ -340,7 +368,7 @@ internal constructor( override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { emitter.emitValue(content as Boolean) @@ -360,7 +388,7 @@ internal constructor( override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { when (val content = content) { @@ -404,7 +432,7 @@ public class TomlNull(lineNo: Int) : TomlValue(lineNo) { override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { emitter.emitNullValue() @@ -424,7 +452,7 @@ internal constructor( public constructor( rawContent: String, lineNo: Int, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) : this( rawContent.parse(lineNo, config), rawContent, @@ -433,13 +461,35 @@ internal constructor( validateQuotes() } + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases." + ) + public constructor( + rawContent: String, + lineNo: Int, + config: TomlConfig + ) : this( + rawContent, + lineNo, + config.input + ) + + @Deprecated( + message = "TomlConfig is deprecated; use TomlInputConfig instead. Will be removed in next releases.", + replaceWith = ReplaceWith( + "parse(config)", + "com.akuleshov7.ktoml.TomlInputConfig" + ) + ) + public fun parse(config: TomlConfig): List = parse(config.input) + /** * small adaptor to make proper testing of parsing * * @param config * @return converted array to a list */ - public fun parse(config: TomlConfig = TomlConfig()): List = rawContent.parse(lineNo, config) + public fun parse(config: TomlInputConfig = TomlInputConfig()): List = rawContent.parse(lineNo, config) /** * small validation for quotes: each quote should be closed in a key @@ -456,7 +506,7 @@ internal constructor( @Suppress("UNCHECKED_CAST") public override fun write( emitter: TomlEmitter, - config: TomlConfig, + config: TomlOutputConfig, multiline: Boolean ) { emitter.startArray() @@ -509,7 +559,7 @@ internal constructor( /** * recursively parse TOML array from the string: [ParsingArray -> Trimming values -> Parsing Nested Arrays] */ - private fun String.parse(lineNo: Int, config: TomlConfig = TomlConfig()): List = + private fun String.parse(lineNo: Int, config: TomlInputConfig = TomlInputConfig()): List = this.parseArray() .map { it.trim() } .map { if (it.startsWith("[")) it.parse(lineNo, config) else it.parseValue(lineNo, config) } diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlEmitter.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlEmitter.kt index 263904fb..b64d7f9d 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlEmitter.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlEmitter.kt @@ -1,6 +1,6 @@ package com.akuleshov7.ktoml.writers -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.writers.IntegerRepresentation.* import kotlin.jvm.JvmStatic @@ -11,7 +11,7 @@ import kotlinx.datetime.LocalDateTime /** * Abstracts the specifics of writing TOML files into "emit" operations. */ -public abstract class TomlEmitter(config: TomlConfig) { +public abstract class TomlEmitter(config: TomlOutputConfig) { private val indentation = config.indentation.value /** diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlStringEmitter.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlStringEmitter.kt index d766ae98..5bd96068 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlStringEmitter.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlStringEmitter.kt @@ -1,13 +1,13 @@ package com.akuleshov7.ktoml.writers -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlOutputConfig /** * A [TomlEmitter] implementation that writes to a [StringBuilder]. */ internal class TomlStringEmitter( private val stringBuilder: StringBuilder, - config: TomlConfig + config: TomlOutputConfig ) : TomlEmitter(config) { override fun emit(fragment: String): TomlEmitter { stringBuilder.append(fragment) diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlWriter.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlWriter.kt index 1be38170..4edbfb5c 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlWriter.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlWriter.kt @@ -1,6 +1,7 @@ package com.akuleshov7.ktoml.writers import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.tree.TomlFile import kotlin.jvm.JvmInline @@ -8,7 +9,13 @@ import kotlin.jvm.JvmInline * @property config - object that stores configuration options for a writer */ @JvmInline -public value class TomlWriter(private val config: TomlConfig) { +public value class TomlWriter(private val config: TomlOutputConfig) { + @Deprecated( + message = "TomlConfig is deprecated; use TomlOutputConfig instead. Will be removed in next releases." + ) + @Suppress("DEPRECATION") + public constructor(config: TomlConfig) : this(config.output) + public fun writeToString( file: TomlFile, stringBuilder: StringBuilder = StringBuilder() diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/DottedKeysDecoderTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/DottedKeysDecoderTest.kt index e8dba493..63b7fa6c 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/DottedKeysDecoderTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/DottedKeysDecoderTest.kt @@ -1,7 +1,7 @@ package com.akuleshov7.ktoml.decoders import com.akuleshov7.ktoml.Toml -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -128,7 +128,7 @@ class DottedKeysDecoderTest { fun tableTest() { assertEquals( SimpleNestedExample(table2 = Table4(b = B(f = 2, d = 2), e = 5)), - Toml(TomlConfig(true)).decodeFromString( + Toml(TomlInputConfig(true)).decodeFromString( """ |table2.b.d = 2 |[table2] @@ -143,7 +143,7 @@ class DottedKeysDecoderTest { fun tableAndDottedKeys() { assertEquals( SimpleNestedExample(table2 = Table4(b = B(f = 7, d = 2), e = 6)), - Toml(TomlConfig(true)).decodeFromString( + Toml(TomlInputConfig(true)).decodeFromString( """ |[table2] |table2."foo bar".d = 2 diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/GeneralDecoderTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/GeneralDecoderTest.kt index fbff18f5..92b0c992 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/GeneralDecoderTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/GeneralDecoderTest.kt @@ -1,7 +1,7 @@ package com.akuleshov7.ktoml.decoders import com.akuleshov7.ktoml.Toml -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig import com.akuleshov7.ktoml.exceptions.InvalidEnumValueException import com.akuleshov7.ktoml.exceptions.MissingRequiredPropertyException import com.akuleshov7.ktoml.exceptions.ParseException @@ -178,7 +178,7 @@ class GeneralDecoderTest { ) { // 'err' key is unknown, but this should not trigger an error becuase of ignoreUnknown // 'b' key is not provided and should trigger an error - Toml(TomlConfig(true)).decodeFromString( + Toml(TomlInputConfig(true)).decodeFromString( " a = true \n" + " d = 5 \n" + " e = \"my test\"\n" + @@ -193,7 +193,7 @@ class GeneralDecoderTest { "Invalid number of arguments provided for deserialization." + " Missing required field in the input" ) { - Toml(TomlConfig(true)).decodeFromString( + Toml(TomlInputConfig(true)).decodeFromString( "[tableUNKNOWN] \n" + " a = true \n" + " d = 5 \n" + @@ -209,7 +209,7 @@ class GeneralDecoderTest { " b = \"A\" \n" + " d = 55 \n") - assertEquals(Table3(true, b = TestEnum.A, d = 55), Toml(TomlConfig(true)).decodeFromString(test)) + assertEquals(Table3(true, b = TestEnum.A, d = 55), Toml(TomlInputConfig(true)).decodeFromString(test)) // e is missing, because it has a default value @@ -222,7 +222,7 @@ class GeneralDecoderTest { "a = true \n" + " b = \"A\" \n" - Toml(TomlConfig(true)).decodeFromString(failMissing) + Toml(TomlInputConfig(true)).decodeFromString(failMissing) } } @@ -236,7 +236,7 @@ class GeneralDecoderTest { "f = NIL\n") assertEquals(NullableValues(null, null, null, null, null, null), Toml.decodeFromString(test)) assertFailsWith { - Toml(TomlConfig(allowNullValues = false)).decodeFromString(test) + Toml(TomlInputConfig(allowNullValues = false)).decodeFromString(test) } } @@ -246,7 +246,7 @@ class GeneralDecoderTest { "Invalid number of arguments provided for deserialization." + " Missing required field in the input" ) { - Toml(TomlConfig(true)).decodeFromString("[table3] \n a = true") + Toml(TomlInputConfig(true)).decodeFromString("[table3] \n a = true") } } @@ -256,7 +256,7 @@ class GeneralDecoderTest { "Invalid number of arguments provided for deserialization." + " Missing required field in the input" ) { - Toml(TomlConfig(true)).decodeFromString( + Toml(TomlInputConfig(true)).decodeFromString( "[table1] \n a = 5 \n b = 6" ) } @@ -269,7 +269,7 @@ class GeneralDecoderTest { assertEquals( ComplexPlainTomlCase(Table3(a = true, d = 5, b = TestEnum.B)), Toml( - TomlConfig(true) + TomlInputConfig(true) ).decodeFromString(test) ) } @@ -282,7 +282,7 @@ class GeneralDecoderTest { | [a] | a = true """.trimMargin() - TomlConfig(true) + TomlInputConfig(true) assertEquals(ChildTableBeforeParent(A(B(5), true)), Toml.decodeFromString(test)) } @@ -290,7 +290,7 @@ class GeneralDecoderTest { @Test fun testIncorrectEnumValue() { assertFailsWith { - Toml(TomlConfig(true)).decodeFromString( + Toml(TomlInputConfig(true)).decodeFromString( ("a = true \n" + " b = \"F\"\n" + " e = \"my string\"\n" + diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/parsers/ValueParserTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/parsers/ValueParserTest.kt index 734cdd0d..26004495 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/parsers/ValueParserTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/parsers/ValueParserTest.kt @@ -1,6 +1,6 @@ package com.akuleshov7.ktoml.parsers -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig import com.akuleshov7.ktoml.exceptions.ParseException import com.akuleshov7.ktoml.tree.* import kotlin.test.Test @@ -33,7 +33,7 @@ class ValueParserTest { fun nullParsingTest() { testTomlValue("a" to "null", NodeType.NULL) assertFailsWith { - testTomlValue("a" to "null", NodeType.NULL, TomlConfig(allowNullValues = false)) + testTomlValue("a" to "null", NodeType.NULL, TomlInputConfig(allowNullValues = false)) } } @@ -154,7 +154,7 @@ fun getNodeType(v: TomlValue): NodeType = when (v) { fun testTomlValue( keyValuePair: Pair, expectedType: NodeType, - config: TomlConfig = TomlConfig() + config: TomlInputConfig = TomlInputConfig() ) { assertEquals(expectedType, getNodeType(TomlKeyValuePrimitive(keyValuePair, 0, config = config).value)) } diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/ArrayOfTablesWriteTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/ArrayOfTablesWriteTest.kt index 1be25575..b66e96a3 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/ArrayOfTablesWriteTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/ArrayOfTablesWriteTest.kt @@ -1,7 +1,8 @@ package com.akuleshov7.ktoml.writers import com.akuleshov7.ktoml.Toml -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import kotlin.test.Test import kotlin.test.assertEquals @@ -143,18 +144,19 @@ class ArrayOfTablesWriteTest { fun testTableArray( expected: String, - config: TomlConfig = TomlConfig() + inputConfig: TomlInputConfig = TomlInputConfig(), + outputConfig: TomlOutputConfig = TomlOutputConfig() ) { - val file = Toml(config).tomlParser.parseString(expected) + val file = Toml(inputConfig).tomlParser.parseString(expected) file.prettyPrint() assertEquals( expected, buildString(expected.length) { - val emitter = TomlStringEmitter(this, config) + val emitter = TomlStringEmitter(this, outputConfig) - file.write(emitter, config) + file.write(emitter, outputConfig) } ) } \ No newline at end of file diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/KeyValueWriteTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/KeyValueWriteTest.kt index 4b625f72..96dee10e 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/KeyValueWriteTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/KeyValueWriteTest.kt @@ -1,6 +1,7 @@ package com.akuleshov7.ktoml.writers -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.tree.TomlInlineTable import com.akuleshov7.ktoml.tree.TomlKeyValueArray import com.akuleshov7.ktoml.tree.TomlKeyValuePrimitive @@ -50,8 +51,8 @@ class KeyValueWriteTest { testTomlPrimitivePair(dottedKey to "1979-05-27") // Null - testTomlPrimitivePair(bareKey to "null", TomlConfig(allowNullValues = true)) - testTomlPrimitivePair(dottedKey to "null", TomlConfig(allowNullValues = true)) + testTomlPrimitivePair(bareKey to "null", TomlInputConfig(allowNullValues = true)) + testTomlPrimitivePair(dottedKey to "null", TomlInputConfig(allowNullValues = true)) } @Test @@ -93,39 +94,42 @@ class KeyValueWriteTest { fun testTomlPrimitivePair( pair: Pair, - config: TomlConfig = TomlConfig() + inputConfig: TomlInputConfig = TomlInputConfig(), + outputConfig: TomlOutputConfig = TomlOutputConfig() ) = testTomlPair( - TomlKeyValuePrimitive(pair, 0, config = config), + TomlKeyValuePrimitive(pair, 0, config = inputConfig), expectedString = "${pair.first} = ${pair.second}", - config, + outputConfig, multiline = false ) fun testTomlArrayPair( pair: Pair, multiline: Boolean, - config: TomlConfig = TomlConfig(), + inputConfig: TomlInputConfig = TomlInputConfig(), + outputConfig: TomlOutputConfig = TomlOutputConfig() ) = testTomlPair( - TomlKeyValueArray(pair, 0, config = config), + TomlKeyValueArray(pair, 0, config = inputConfig), expectedString = "${pair.first} = ${pair.second}", - config, + outputConfig, multiline ) fun testTomlInlineTablePair( pair: Pair, - config: TomlConfig = TomlConfig(), + inputConfig: TomlInputConfig = TomlInputConfig(), + outputConfig: TomlOutputConfig = TomlOutputConfig() ) = testTomlPair( - TomlInlineTable(pair, 0, config = config), + TomlInlineTable(pair, 0, config = inputConfig), expectedString = "${pair.first} = ${pair.second}", - config, + outputConfig, multiline = false ) fun testTomlPair( pair: TomlNode, expectedString: String, - config: TomlConfig, + config: TomlOutputConfig = TomlOutputConfig(), multiline: Boolean ) { assertEquals( diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/TableWriteTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/TableWriteTest.kt index 394306cf..dcf0cfc6 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/TableWriteTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/TableWriteTest.kt @@ -1,7 +1,8 @@ package com.akuleshov7.ktoml.writers import com.akuleshov7.ktoml.Toml -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import kotlin.test.Test import kotlin.test.assertEquals @@ -80,18 +81,19 @@ class TableWriteTest { fun testTable( expected: String, - config: TomlConfig = TomlConfig() + inputConfig: TomlInputConfig = TomlInputConfig(), + outputConfig: TomlOutputConfig = TomlOutputConfig() ) { - val file = Toml(config).tomlParser.parseString(expected) + val file = Toml(inputConfig, outputConfig).tomlParser.parseString(expected) file.prettyPrint() assertEquals( expected, buildString(expected.length) { - val emitter = TomlStringEmitter(this, config) + val emitter = TomlStringEmitter(this, outputConfig) - file.write(emitter, config) + file.write(emitter, outputConfig) } ) } \ No newline at end of file diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/ValueWriteTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/ValueWriteTest.kt index ccf1949d..ae5362b3 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/ValueWriteTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/writers/ValueWriteTest.kt @@ -1,6 +1,6 @@ package com.akuleshov7.ktoml.writers -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.exceptions.TomlWritingException import com.akuleshov7.ktoml.tree.* import kotlinx.datetime.Instant @@ -28,7 +28,7 @@ class PrimitiveValueWriteTest { // Escaped single quotes - val disallowQuotes = TomlConfig(allowEscapedQuotesInLiteralStrings = false) + val disallowQuotes = TomlOutputConfig(allowEscapedQuotesInLiteralStrings = false) val escapedSingleQuotes = TomlLiteralString("'escaped quotes'" as Any, 0) @@ -143,7 +143,7 @@ class PrimitiveValueWriteTest { fun testTomlValue( value: TomlValue, expectedString: String, - config: TomlConfig = TomlConfig(), + config: TomlOutputConfig = TomlOutputConfig(), multiline: Boolean = false ) { assertEquals( @@ -158,7 +158,7 @@ fun testTomlValue( fun testTomlValueFailure( value: TomlValue, - config: TomlConfig = TomlConfig() + config: TomlOutputConfig = TomlOutputConfig() ) { assertFailsWith { val emitter = TomlStringEmitter(StringBuilder(), config) diff --git a/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlFileReader.kt b/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlFileReader.kt index 3443580d..95b75170 100644 --- a/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlFileReader.kt +++ b/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlFileReader.kt @@ -3,6 +3,8 @@ package com.akuleshov7.ktoml.file import com.akuleshov7.ktoml.Toml import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import kotlin.native.concurrent.ThreadLocal import kotlinx.serialization.DeserializationStrategy @@ -16,10 +18,28 @@ import kotlinx.serialization.modules.SerializersModule * @property serializersModule */ @OptIn(ExperimentalSerializationApi::class) -public open class TomlFileReader( - private val config: TomlConfig = TomlConfig(), - override val serializersModule: SerializersModule = EmptySerializersModule -) : Toml(config, serializersModule) { +public open class TomlFileReader : Toml { + public constructor( + inputConfig: TomlInputConfig = TomlInputConfig(), + outputConfig: TomlOutputConfig = TomlOutputConfig(), + serializersModule: SerializersModule = EmptySerializersModule + ) : super( + inputConfig, + outputConfig, + serializersModule + ) + + @Deprecated( + message = "config parameter split into inputConfig and outputConfig. Will be removed in next releases." + ) + public constructor( + config: TomlConfig, + serializersModule: SerializersModule = EmptySerializersModule + ) : super( + config, + serializersModule + ) + /** * Simple deserializer of a file that contains toml. Reading file with okio native library * @@ -32,7 +52,7 @@ public open class TomlFileReader( tomlFilePath: String, ): T { val parsedToml = readAndParseFile(tomlFilePath) - return decodeFromString(deserializer, parsedToml, config) + return decodeFromString(deserializer, parsedToml, inputConfig) } /** @@ -54,7 +74,7 @@ public open class TomlFileReader( tomlTableName: String, ): T { val parsedToml = readAndParseFile(tomlFilePath) - return partiallyDecodeFromString(deserializer, parsedToml, tomlTableName, config) + return partiallyDecodeFromString(deserializer, parsedToml, tomlTableName, inputConfig) } /** @@ -63,5 +83,8 @@ public open class TomlFileReader( * ThreadLocal annotation is used here for caching. */ @ThreadLocal - public companion object Default : TomlFileReader(TomlConfig()) + public companion object Default : TomlFileReader( + inputConfig = TomlInputConfig(), + outputConfig = TomlOutputConfig() + ) } diff --git a/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlFileWriter.kt b/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlFileWriter.kt index ff63014a..cb424acc 100644 --- a/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlFileWriter.kt +++ b/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlFileWriter.kt @@ -2,6 +2,8 @@ package com.akuleshov7.ktoml.file import com.akuleshov7.ktoml.Toml import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlInputConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.tree.TomlFile import okio.use @@ -16,22 +18,40 @@ import kotlinx.serialization.modules.SerializersModule * @property serializersModule */ @OptIn(ExperimentalSerializationApi::class) -public open class TomlFileWriter( - private val config: TomlConfig = TomlConfig(), - override val serializersModule: SerializersModule = EmptySerializersModule -) : Toml(config, serializersModule) { +public open class TomlFileWriter : Toml { + public constructor( + inputConfig: TomlInputConfig = TomlInputConfig(), + outputConfig: TomlOutputConfig = TomlOutputConfig(), + serializersModule: SerializersModule = EmptySerializersModule + ) : super( + inputConfig, + outputConfig, + serializersModule + ) + + @Deprecated( + message = "TomlConfig is deprecated; use TomlOutputConfig instead. Will be removed in next releases." + ) + public constructor( + config: TomlConfig = TomlConfig(), + serializersModule: SerializersModule = EmptySerializersModule + ) : super( + config, + serializersModule + ) + public fun encodeToFile( serializer: SerializationStrategy, value: T, tomlFilePath: String ) { - val fileTree = TomlFile(config) + val fileTree = TomlFile(inputConfig) // Todo: Write an encoder implementation. TomlSinkEmitter( openFileForWrite(tomlFilePath), - config + outputConfig ).use { tomlWriter.write(fileTree, it) } diff --git a/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlSinkEmitter.kt b/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlSinkEmitter.kt index a97da8a0..3775966a 100644 --- a/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlSinkEmitter.kt +++ b/ktoml-file/src/commonMain/kotlin/com/akuleshov7/ktoml/file/TomlSinkEmitter.kt @@ -2,7 +2,7 @@ package com.akuleshov7.ktoml.file -import com.akuleshov7.ktoml.TomlConfig +import com.akuleshov7.ktoml.TomlOutputConfig import com.akuleshov7.ktoml.writers.TomlEmitter import okio.BufferedSink import okio.Closeable @@ -12,7 +12,7 @@ import okio.Closeable */ internal class TomlSinkEmitter( private val sink: BufferedSink, - config: TomlConfig + config: TomlOutputConfig ) : TomlEmitter(config), Closeable { override fun emit(fragment: String): TomlEmitter { sink.writeUtf8(fragment) diff --git a/ktoml-file/src/commonTest/kotlin/com/akuleshov7/ktoml/file/TomlFileParserTest.kt b/ktoml-file/src/commonTest/kotlin/com/akuleshov7/ktoml/file/TomlFileParserTest.kt index 3d53ec52..a9297e18 100644 --- a/ktoml-file/src/commonTest/kotlin/com/akuleshov7/ktoml/file/TomlFileParserTest.kt +++ b/ktoml-file/src/commonTest/kotlin/com/akuleshov7/ktoml/file/TomlFileParserTest.kt @@ -1,6 +1,6 @@ package com.akuleshov7.ktoml.file -import com.akuleshov7.ktoml.* +import com.akuleshov7.ktoml.TomlInputConfig import com.akuleshov7.ktoml.parsers.TomlParser import com.akuleshov7.ktoml.tree.TomlTablePrimitive import kotlinx.serialization.ExperimentalSerializationApi @@ -83,7 +83,7 @@ class TomlFileParserTest { assertEquals(test, TomlFileReader.decodeFromFile(serializer(), file)) // ==== checking how table discovery works val lines = readAndParseFile(file) - val parsedResult = TomlParser(TomlConfig()).parseStringsToTomlTree(lines, TomlConfig()) + val parsedResult = TomlParser(TomlInputConfig()).parseStringsToTomlTree(lines, TomlInputConfig()) assertEquals(listOf("a", "a.b.c", "a.d", "d", "d.a"), parsedResult.getRealTomlTables().map { it.fullTableName }) } @@ -195,8 +195,8 @@ class TomlFileParserTest { val lines = readAndParseFile(file) assertEquals( listOf("owner", "database"), - TomlParser(TomlConfig()) - .parseStringsToTomlTree(lines, TomlConfig()) + TomlParser(TomlInputConfig()) + .parseStringsToTomlTree(lines, TomlInputConfig()) .children .filterIsInstance() .filter { !it.isSynthetic }