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

Separate parsing from writing in node constructors #165

Merged
merged 7 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/Toml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public open class Toml(
}

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

return tomlWriter.writeToString(file = toml)
}
Expand Down Expand Up @@ -195,7 +195,7 @@ public open class Toml(
)

// adding a fake file node to restore the structure and parse only the part of te toml
val fakeFileNode = TomlFile(config)
val fakeFileNode = TomlFile()
parsedToml.children.forEach {
fakeFileNode.appendChild(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public abstract class TomlAbstractDecoder : AbstractDecoder() {
}
} catch (e: ClassCastException) {
throw IllegalTypeException(
"Cannot decode the key [${keyValue.key.content}] with the value [${keyValue.value.content}]" +
"Cannot decode the key [${keyValue.key.last()}] with the value [${keyValue.value.content}]" +
" with the provided type [${T::class}]. Please check the type in your Serializable class or it's nullability",
keyValue.lineNo
)
Expand Down Expand Up @@ -112,7 +112,7 @@ public abstract class TomlAbstractDecoder : AbstractDecoder() {
throw IllegalTypeException(
"<$typeName> type is not allowed by toml specification," +
" use <$requiredType> instead" +
" (key = ${keyValue.key.content}; value = ${keyValue.value.content})", keyValue.lineNo
" (key = ${keyValue.key.last()}; value = ${keyValue.value.content})", keyValue.lineNo
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public class TomlArrayDecoder(
rootNode.lineNo,
comments = emptyList(),
inlineComment = "",
rootNode.key.content,
config
)
)
return nextElementIndex++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,14 @@ public class TomlMainDecoder(
is TomlTablePrimitive -> {
val firstTableChild = nextProcessingNode.getFirstChild() ?: throw InternalDecodingException(
"Decoding process failed due to invalid structure of parsed AST tree: missing children" +
" in a table <${nextProcessingNode.fullTableName}>"
" in a table <${nextProcessingNode.fullTableKey}>"
)
checkMissingRequiredProperties(firstTableChild.getNeighbourNodes(), descriptor)
TomlMainDecoder(firstTableChild, config)
}
else -> throw InternalDecodingException(
"Incorrect decoding state in the beginStructure()" +
" with $nextProcessingNode (${nextProcessingNode.content})[${nextProcessingNode.name}]"
" with $nextProcessingNode ($nextProcessingNode)[${nextProcessingNode.name}]"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.akuleshov7.ktoml.encoders

import com.akuleshov7.ktoml.TomlInputConfig
import com.akuleshov7.ktoml.TomlOutputConfig
import com.akuleshov7.ktoml.exceptions.InternalEncodingException
import com.akuleshov7.ktoml.exceptions.UnsupportedEncodingFeatureException
Expand All @@ -23,15 +22,13 @@ import kotlinx.serialization.modules.SerializersModule
*
* @property elementIndex The current element index.
* @property attributes The current attributes.
* @property inputConfig The input config, used for constructing nodes.
* @property outputConfig The output config.
* @property serializersModule
*/
@OptIn(ExperimentalSerializationApi::class)
public abstract class TomlAbstractEncoder protected constructor(
protected var elementIndex: Int,
protected val attributes: TomlEncoderAttributes,
protected val inputConfig: TomlInputConfig,
protected val outputConfig: TomlOutputConfig,
override val serializersModule: SerializersModule,
) : AbstractEncoder() {
Expand Down Expand Up @@ -82,13 +79,13 @@ public abstract class TomlAbstractEncoder protected constructor(

override fun encodeBoolean(value: Boolean) {
if (!encodeAsKey(value, "Boolean")) {
appendValue(TomlBoolean(value, elementIndex))
appendValue(TomlBoolean(value))
}
}

override fun encodeDouble(value: Double) {
if (!encodeAsKey(value, "Double")) {
appendValue(TomlDouble(value, elementIndex))
appendValue(TomlDouble(value))
}
}

Expand All @@ -104,12 +101,12 @@ public abstract class TomlAbstractEncoder protected constructor(
}

if (!encodeAsKey(value, "Long")) {
appendValue(TomlLong(value, elementIndex))
appendValue(TomlLong(value))
}
}

override fun encodeNull() {
appendValue(TomlNull(elementIndex))
appendValue(TomlNull())
}

override fun encodeString(value: String) {
Expand All @@ -122,9 +119,9 @@ public abstract class TomlAbstractEncoder protected constructor(
if (!encodeAsKey(value)) {
appendValue(
if (attributes.isLiteral) {
TomlLiteralString(value as Any, elementIndex)
TomlLiteralString(value)
} else {
TomlBasicString(value as Any, elementIndex)
TomlBasicString(value)
}
)
}
Expand All @@ -135,7 +132,7 @@ public abstract class TomlAbstractEncoder protected constructor(
instantDescriptor,
localDateTimeDescriptor,
localDateDescriptor -> if (!encodeAsKey(value as Any, desc.serialName)) {
appendValue(TomlDateTime(value as Any, elementIndex))
appendValue(TomlDateTime(value))
}
else -> when (val kind = desc.kind) {
is StructureKind,
Expand Down Expand Up @@ -262,7 +259,6 @@ public abstract class TomlAbstractEncoder protected constructor(
parent = this,
elementIndex,
attributes,
inputConfig,
outputConfig,
serializersModule
)
Expand All @@ -279,7 +275,6 @@ public abstract class TomlAbstractEncoder protected constructor(
parent = this,
elementIndex,
attributes.child(),
inputConfig,
outputConfig,
serializersModule
)
Expand All @@ -295,7 +290,6 @@ public abstract class TomlAbstractEncoder protected constructor(
rootNode,
elementIndex,
attributes.child(),
inputConfig,
outputConfig,
serializersModule
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.akuleshov7.ktoml.encoders

import com.akuleshov7.ktoml.TomlInputConfig
import com.akuleshov7.ktoml.TomlOutputConfig
import com.akuleshov7.ktoml.exceptions.InternalEncodingException
import com.akuleshov7.ktoml.exceptions.UnsupportedEncodingFeatureException
Expand All @@ -11,7 +10,6 @@ import com.akuleshov7.ktoml.tree.nodes.TomlNode
import com.akuleshov7.ktoml.tree.nodes.pairs.keys.TomlKey
import com.akuleshov7.ktoml.tree.nodes.pairs.values.TomlArray
import com.akuleshov7.ktoml.tree.nodes.pairs.values.TomlValue

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.SerialDescriptor
Expand All @@ -28,13 +26,11 @@ public class TomlArrayEncoder internal constructor(
private val parent: TomlAbstractEncoder?,
elementIndex: Int,
attributes: TomlEncoderAttributes,
inputConfig: TomlInputConfig,
outputConfig: TomlOutputConfig,
serializersModule: SerializersModule
) : TomlAbstractEncoder(
elementIndex,
attributes,
inputConfig,
outputConfig,
serializersModule
) {
Expand All @@ -52,15 +48,13 @@ public class TomlArrayEncoder internal constructor(
rootNode: TomlNode,
elementIndex: Int,
attributes: TomlEncoderAttributes,
inputConfig: TomlInputConfig,
outputConfig: TomlOutputConfig,
serializersModule: SerializersModule
) : this(
rootNode,
parent = null,
elementIndex,
attributes,
inputConfig,
outputConfig,
serializersModule
)
Expand Down Expand Up @@ -107,8 +101,7 @@ public class TomlArrayEncoder internal constructor(
val element = TomlArrayOfTablesElement(
elementIndex,
attributes.comments,
attributes.inlineComment,
inputConfig
attributes.inlineComment
)

tables += element
Expand All @@ -117,15 +110,14 @@ public class TomlArrayEncoder internal constructor(
element,
nextElementIndex(),
attributes,
inputConfig,
outputConfig,
serializersModule
)
}

override fun endStructure(descriptor: SerialDescriptor) {
if (attributes.isInline) {
val array = TomlArray(values, "", elementIndex)
val array = TomlArray(values)

parent?.let {
appendValueTo(array, parent)
Expand All @@ -139,9 +131,7 @@ public class TomlArrayEncoder internal constructor(
array,
elementIndex,
attributes.comments,
attributes.inlineComment,
key,
inputConfig
attributes.inlineComment
)
)
}
Expand Down Expand Up @@ -171,9 +161,8 @@ public class TomlArrayEncoder internal constructor(
}

val tableArray = TomlArrayOfTables(
"[[${attributes.parent!!.getFullKey()}]]",
TomlKey(attributes.parent!!.getFullKey(), elementIndex),
elementIndex,
inputConfig,
isSynthetic
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.akuleshov7.ktoml.encoders

import com.akuleshov7.ktoml.TomlInputConfig
import com.akuleshov7.ktoml.TomlOutputConfig
import com.akuleshov7.ktoml.exceptions.InternalEncodingException
import com.akuleshov7.ktoml.tree.nodes.TomlInlineTable
Expand All @@ -10,7 +9,6 @@ import com.akuleshov7.ktoml.tree.nodes.TomlNode
import com.akuleshov7.ktoml.tree.nodes.pairs.keys.TomlKey
import com.akuleshov7.ktoml.tree.nodes.pairs.values.TomlArray
import com.akuleshov7.ktoml.tree.nodes.pairs.values.TomlValue

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.SerialKind
Expand All @@ -29,13 +27,11 @@ public class TomlInlineTableEncoder internal constructor(
private val parent: TomlAbstractEncoder?,
elementIndex: Int,
attributes: TomlEncoderAttributes,
inputConfig: TomlInputConfig,
outputConfig: TomlOutputConfig,
serializersModule: SerializersModule
) : TomlAbstractEncoder(
elementIndex,
attributes,
inputConfig,
outputConfig,
serializersModule
) {
Expand All @@ -52,15 +48,13 @@ public class TomlInlineTableEncoder internal constructor(
rootNode: TomlNode,
elementIndex: Int,
attributes: TomlEncoderAttributes,
inputConfig: TomlInputConfig,
outputConfig: TomlOutputConfig,
serializersModule: SerializersModule
) : this(
rootNode,
parent = null,
elementIndex,
attributes,
inputConfig,
outputConfig,
serializersModule
)
Expand All @@ -79,18 +73,14 @@ public class TomlInlineTableEncoder internal constructor(
elementIndex,
comments = emptyList(),
inlineComment = "",
name,
inputConfig
)
} else {
TomlKeyValuePrimitive(
key,
value,
elementIndex,
comments = emptyList(),
inlineComment = "",
name,
inputConfig
inlineComment = ""
)
}

Expand All @@ -108,13 +98,11 @@ public class TomlInlineTableEncoder internal constructor(
val name = attributes.keyOrThrow()

val inlineTable = TomlInlineTable(
"",
elementIndex,
name,
TomlKey(name, elementIndex),
pairs,
elementIndex,
comments,
inlineComment,
inputConfig
inlineComment
)

when (parent) {
Expand Down Expand Up @@ -148,27 +136,21 @@ public class TomlInlineTableEncoder internal constructor(
child.value,
child.lineNo,
child.comments,
child.inlineComment,
name,
inputConfig
child.inlineComment
)
is TomlKeyValueArray -> TomlKeyValueArray(
TomlKey(name, child.lineNo),
child.value,
child.lineNo,
child.comments,
child.inlineComment,
name,
inputConfig
child.inlineComment
)
is TomlInlineTable -> TomlInlineTable(
"",
child.lineNo,
name,
TomlKey(name, elementIndex),
child.tomlKeyValues,
child.lineNo,
child.comments,
child.inlineComment,
inputConfig
child.inlineComment
)
else -> throw InternalEncodingException("Not a pair")
}
Expand Down
Loading