Skip to content

Commit

Permalink
finish mapping from yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
erosb committed Sep 29, 2024
1 parent 89ed310 commit 77ec0f4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
43 changes: 23 additions & 20 deletions src/main/kotlin/com/github/erosb/jsonsKema/YamlJsonObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.erosb.jsonsKema
import org.yaml.snakeyaml.nodes.MappingNode
import org.yaml.snakeyaml.nodes.Node
import org.yaml.snakeyaml.nodes.ScalarNode
import org.yaml.snakeyaml.nodes.SequenceNode
import org.yaml.snakeyaml.nodes.Tag

class YamlJsonObject(
Expand All @@ -17,38 +18,40 @@ class YamlJsonObject(
}
}

internal fun loadFromYaml(node: Node): JsonValue {


internal fun loadFromYaml(node: Node, ptr: JsonPointer = JsonPointer()): JsonValue {
val location = SourceLocation(
node.startMark.line + 1,
node.startMark.column + 1,
JsonPointer(),
ptr,
null,
)

when (node) {
is ScalarNode -> {
if (node.tag == Tag.NULL) {
return JsonNull(
location,
)
return JsonNull(location)
} else if (node.tag == Tag.STR) {
return JsonString(
node.value,
SourceLocation(
node.startMark.line + 1,
node.startMark.column + 1,
JsonPointer(),
null,
),
)
return JsonString(node.value, location)
} else if (node.tag == Tag.BOOL) {
val value = node.value.lowercase() in listOf("yes", "y", "on", "true")
return JsonBoolean(value, location)
}
}
is MappingNode -> {
val x = node.value.map { loadFromYaml(it.keyNode).requireString() as JsonString to loadFromYaml(it.valueNode) }.toMap()
return JsonObject(x)
val props = node.value.map {
val nextPtr = ptr + (it.keyNode as ScalarNode).value
loadFromYaml(it.keyNode).requireString() as JsonString to loadFromYaml(it.valueNode, nextPtr)
}.toMap()
return JsonObject(props, location)
}
is SequenceNode -> {
val items = node.value.mapIndexed { index, childNode ->
val childPtr = ptr + index.toString()
loadFromYaml(childNode, childPtr)
}
return JsonArray(items, location)
}

else -> TODO()
}
TODO()
TODO("unhandled type ${node.javaClass} / ${node.tag}")
}
34 changes: 30 additions & 4 deletions src/test/kotlin/com/github/erosb/jsonsKema/SnakeYamlTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,39 @@ class SnakeYamlTest {
fun readObject() {
val yaml = Yaml().compose(StringReader("""
propA: val-a
propB:
propB: null
""".trimIndent()))

val actual = loadFromYaml(yaml)
assertThat(actual).isEqualTo(JsonObject(mapOf(
JsonString("propA", SourceLocation(1, 1, JsonPointer())) to JsonString("val-a", SourceLocation(1, 8, JsonPointer())),
JsonString("propB", SourceLocation(2, 1, JsonPointer())) to JsonNull(SourceLocation(2, 8, JsonPointer()))
assertThat(actual).usingRecursiveComparison().isEqualTo(JsonObject(mapOf(
JsonString("propA", SourceLocation(1, 1, JsonPointer())) to JsonString("val-a", SourceLocation(1, 8, JsonPointer("propA"))),
JsonString("propB", SourceLocation(2, 1, JsonPointer())) to JsonNull(SourceLocation(2, 9, JsonPointer("propB")))
), SourceLocation(1, 1, JsonPointer())))
}

@Test
fun readSequence() {
val yaml = Yaml().compose(StringReader("""
- null
- "asd"
- true
""".trimIndent()))

val actual = loadFromYaml(yaml)
assertThat(actual).isEqualTo(JsonArray(listOf(
JsonNull(),
JsonString("asd"),
JsonBoolean(true)
)))
}

@Test
fun readBooleans() {
val yaml = Yaml().compose(StringReader("[yes, true, ON, No, false, off]"))
val actual = loadFromYaml(yaml)
assertThat(actual).isEqualTo(JsonArray(listOf(
JsonBoolean(true), JsonBoolean(true), JsonBoolean(true),
JsonBoolean(false), JsonBoolean(false), JsonBoolean(false)
)))
}

Expand Down

0 comments on commit 77ec0f4

Please sign in to comment.