Scoop API

scoop:scoop.http.server:0.1.0

src/query/record.scoop

Selected production source · src/query/record.scoop

1package scoop.http.server
3import scoop.collections.LinkedHashMap
4import scoop.collections.toList
6internal class QueryRecord(private val fields: LinkedHashMap<String, QueryField>,
7 private val options: QueryDecodeOptions) : KeyedDecodingContainer {
8 public override val keys: List<String> get() = fields.keys.toList()
10 public override fun optional(name: String): Decoder? {
11 val field = fields[name]
12 if (!present(field)) return None
13 field!!.accessed = true
14 return Some<Decoder>(QueryDecoder(QueryNode.Field(field!!), field!!.path))
15 }
17 public override fun required(name: String): Decoder = optional(name)
18 ?: throw DecodingException(queryPath(name), "missing required query field")
20 public override fun end() {
21 if (options.unknownFields == QueryUnknownFields.Ignore) return
22 for (entry in fields) if (!entry.value.accessed)
23 throw DecodingException(entry.value.path, "unknown query field")
24 }
26 fun location(path: String): String {
27 for (entry in fields) {
28 val field = entry.value
29 if (field.path == path) {
30 val result = StringBuilder().append("query items ")
31 for (index in 0L until field.items.size) {
32 if (index != 0L) result.append(", ")
33 result.append(field.items[index].position.toString())
34 }
35 return result.toString()
36 }
37 for (index in 0L until field.items.size) {
38 if (path == field.path + "/" + index.toString())
39 return "query item " + field.items[index].position.toString()
40 }
41 }
42 return ""
43 }
46internal class QuerySequence(private val field: QueryField) : UnkeyedDecodingContainer {
47 private var cursor: Long = 0L
48 public override val hasNext: Boolean get() = cursor < field.items.size
50 public override fun element(): Decoder {
51 val path = field.path + "/" + cursor.toString()
52 if (!hasNext) throw DecodingException(path, "missing query element")
53 val result = QueryDecoder(QueryNode.Item(field.items[cursor]), path)
54 cursor += 1L
55 return result
56 }
58 public override fun end() {
59 if (hasNext) throw DecodingException(field.path, "extra query elements")
60 }