Scoop API

scoop:scoop.http.server:0.1.0

src/query/decoder.scoop

Selected production source · src/query/decoder.scoop

1package scoop.http.server
3import scoop.text.*
5internal enum QueryNode { Record(QueryRecord), Field(QueryField), Item(QueryItem) }
7internal class QueryDecoder(private val node: QueryNode, public override val path: String)
8 : Decoder, SingleValueDecodingContainer {
9 public override fun keyed(): KeyedDecodingContainer = when (node) {
10 case QueryNode.Record(val record) -> record
11 else -> throw QueryCodecException(path, "nested query records are not supported")
12 }
14 public override fun unkeyed(): UnkeyedDecodingContainer = when (node) {
15 case QueryNode.Field(val field) -> QuerySequence(field)
16 else -> throw QueryCodecException(path, "only a query field can be a sequence")
17 }
19 public override fun singleValue(): SingleValueDecodingContainer = this
20 public override fun isNull(): Boolean = false
21 public override fun readNull() { throw QueryCodecException(path, "query has no null representation") }
23 public override fun readString(): String = when (node) {
24 case QueryNode.Field(val field) -> {
25 if (field.items.size != 1L) throw DecodingException(path, "duplicate scalar query field")
26 field.items[0L].text(path)
27 }
28 case QueryNode.Item(val item) -> item.text(path)
29 case QueryNode.Record(_) -> throw QueryCodecException(path, "query root must be a named record")
30 }
32 public override fun readBoolean(): Boolean = when (readString()) {
33 case "true" -> true
34 case "false" -> false
35 else -> throw DecodingException(path, "expected true or false")
36 }
38 public override fun readLong(): Long = readString().toLongOrNone()
39 ?: throw DecodingException(path, "expected an in-range signed decimal integer")
41 public override fun readULong(): ULong = readString().toULongOrNone()
42 ?: throw DecodingException(path, "expected an in-range unsigned decimal integer")
44 public override fun readFloat(): Float {
45 val value = readString().toFloatOrNone()
46 ?: throw DecodingException(path, "expected an in-range decimal Float")
47 if (!value.isFinite()) throw DecodingException(path, "expected a finite Float")
48 return value
49 }
51 public override fun readDouble(): Double {
52 val value = readString().toDoubleOrNone()
53 ?: throw DecodingException(path, "expected an in-range decimal Double")
54 if (!value.isFinite()) throw DecodingException(path, "expected a finite Double")
55 return value
56 }