-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to load json schema documents which are implemented in yaml.
## Change details * add snakeyaml depencency * add `parseStringIntoRawSchema(string): IJsonValue` function, which first tries to parse its input as json, then as yaml * reworked `SchemaLoader(string)` constructor and `SchemaClient#getParsed()` to use `parseStringIntoRawSchema()` * added internal `loadFromYaml(node: org.yaml.snakeyaml.nodes.Node)` for mapping from snakeyaml `Node` to `IJsonValue`
- Loading branch information
Showing
10 changed files
with
234 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/github/erosb/jsonsKema/YamlJsonObject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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 | ||
|
||
internal fun loadFromYaml(node: Node, ptr: JsonPointer = JsonPointer()): JsonValue { | ||
val location = SourceLocation( | ||
node.startMark.line + 1, | ||
node.startMark.column + 1, | ||
ptr, | ||
null, | ||
) | ||
when (node) { | ||
is ScalarNode -> { | ||
if (node.tag == Tag.NULL) { | ||
return JsonNull(location) | ||
} else if (node.tag == Tag.STR) { | ||
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) | ||
} else if (node.tag == Tag.INT) { | ||
return JsonNumber(node.value.toInt(), location) | ||
} else if (node.tag == Tag.FLOAT) { | ||
return JsonNumber(node.value.toDouble(), location) | ||
} | ||
} | ||
is MappingNode -> { | ||
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) | ||
} | ||
} | ||
TODO("unhandled type ${node.javaClass} / ${node.tag}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/test/kotlin/com/github/erosb/jsonsKema/SnakeYamlTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.github.erosb.jsonsKema | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.jupiter.api.Test | ||
import org.yaml.snakeyaml.Yaml | ||
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.parser.ParserException | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
import java.io.StringReader | ||
|
||
|
||
class SnakeYamlTest { | ||
|
||
@Test | ||
fun readNull() { | ||
val yaml = Yaml().compose(StringReader("null")) | ||
val actual = loadFromYaml(yaml) | ||
assertThat(actual).isEqualTo(JsonNull( | ||
SourceLocation(1, 1, JsonPointer()) | ||
)) | ||
} | ||
|
||
@Test | ||
fun readString() { | ||
val yaml = Yaml().compose(StringReader(""" | ||
"null" | ||
""")) | ||
val actual = loadFromYaml(yaml) | ||
assertThat(actual).isEqualTo(JsonString("null", | ||
SourceLocation(1, 1, JsonPointer()) | ||
)) | ||
} | ||
|
||
@Test | ||
fun readObject() { | ||
val yaml = Yaml().compose(StringReader(""" | ||
propA: val-a | ||
propB: null | ||
""".trimIndent())) | ||
|
||
val actual = loadFromYaml(yaml) | ||
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) | ||
))) | ||
} | ||
|
||
@Test | ||
fun loadSchemaFromYaml() { | ||
val schema = SchemaLoader.forURL("classpath://yaml/schema.yml") | ||
} | ||
|
||
@Test | ||
fun loadMalformedYamlSchema() { | ||
assertThatThrownBy { SchemaLoader.forURL("classpath://yaml/malformed.yml") } | ||
.isInstanceOf(YamlDocumentLoadingException::class.java) | ||
} | ||
|
||
@Test | ||
fun loadSchemaFromYamlString() { | ||
val schema = SchemaLoader(""" | ||
$schema: https://json-schema.org/draft/2020-12/schema | ||
type: object | ||
additionalProperties: false | ||
properties: | ||
str: | ||
type: string | ||
num: | ||
type: number | ||
minimum: 0.5 | ||
int: | ||
type: integer | ||
maximum: 1 | ||
bool: | ||
type: boolean | ||
nullish: | ||
type: "null" | ||
""".trimIndent())() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
myObj: | ||
prop: value | ||
arr: | ||
- 1 | ||
- 3 | ||
- null | ||
nullProp: | ||
objProp: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
x: | ||
- [[[[ | ||
[[[[]y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
$schema: https://json-schema.org/draft/2020-12/schema | ||
type: object | ||
additionalProperties: false | ||
properties: | ||
str: | ||
type: string | ||
num: | ||
type: number | ||
minimum: 0 | ||
int: | ||
type: integer | ||
maximum: 1 | ||
bool: | ||
type: boolean | ||
nullish: | ||
type: null | ||
|