Uncomment code

This commit is contained in:
LilyRose2798 2024-04-03 10:22:37 +11:00
parent 3f8af51754
commit 9a3bceff68
2 changed files with 224 additions and 236 deletions

View File

@ -348,21 +348,6 @@ fn json_parser(options: JsonParseOptions) -> JsonParser(JsonValue) {
|> between(allow_comments(options, ws(options)), end()) |> between(allow_comments(options, ws(options)), end())
} }
import gleam/io
pub fn main() {
// io.debug(parse_jsonc(
// " /* asdf */ [1 , // asdf \n 2/* asdf */, { \"k\"/* asdf */ :// asdf\n 3}]",
// ))
// io.debug(parse_jsonc(
// "{
// \"foo\":/* * / */ \"121234\",
// \"foo\":/* *//* /*** */ \"121234\"
// } // asdf",
// ))
io.debug(parse_json5("{bar: -0x14,\"foo\": \"a\\x23\\\r\nsd\\Af\",}"))
}
pub type JsonParseOptions { pub type JsonParseOptions {
JsonParseOptions( JsonParseOptions(
comments: Bool, comments: Bool,
@ -546,188 +531,189 @@ pub fn stringify_jsonl(values: List(JsonValue)) -> String {
|> list.map(stringify_json) |> list.map(stringify_json)
|> string.join("\n") |> string.join("\n")
} }
// pub type JsonQuery {
// Root
// Key(query: JsonQuery, key: String)
// KeyOr(query: JsonQuery, key: String, or: JsonValue)
// Index(query: JsonQuery, index: Int)
// IndexOr(query: JsonQuery, index: Int, or: JsonValue)
// Filter(query: JsonQuery, predicate: fn(JsonValue) -> Bool)
// Map(query: JsonQuery, mapping: fn(JsonValue) -> JsonValue)
// MapKeys(query: JsonQuery, mapping: fn(String) -> String)
// MapValues(query: JsonQuery, mapping: fn(String, JsonValue) -> JsonValue)
// FilterMap(query: JsonQuery, mapping: fn(JsonValue) -> Result(JsonValue, Nil))
// ForEach(query: JsonQuery)
// ForEachOk(query: JsonQuery)
// }
// type InvJsonQuery { pub type JsonQuery {
// InvEnd Root
// InvKey(key: String, query: InvJsonQuery) Key(query: JsonQuery, key: String)
// InvKeyOr(key: String, or: JsonValue, query: InvJsonQuery) KeyOr(query: JsonQuery, key: String, or: JsonValue)
// InvIndex(index: Int, query: InvJsonQuery) Index(query: JsonQuery, index: Int)
// InvIndexOr(index: Int, or: JsonValue, query: InvJsonQuery) IndexOr(query: JsonQuery, index: Int, or: JsonValue)
// InvFilter(predicate: fn(JsonValue) -> Bool, query: InvJsonQuery) Filter(query: JsonQuery, predicate: fn(JsonValue) -> Bool)
// InvMap(mapping: fn(JsonValue) -> JsonValue, query: InvJsonQuery) Map(query: JsonQuery, mapping: fn(JsonValue) -> JsonValue)
// InvMapKeys(mapping: fn(String) -> String, query: InvJsonQuery) MapKeys(query: JsonQuery, mapping: fn(String) -> String)
// InvMapValues(mapping: fn(String, JsonValue) -> JsonValue, query: InvJsonQuery) MapValues(query: JsonQuery, mapping: fn(String, JsonValue) -> JsonValue)
// InvFilterMap( FilterMap(query: JsonQuery, mapping: fn(JsonValue) -> Result(JsonValue, Nil))
// mapping: fn(JsonValue) -> Result(JsonValue, Nil), ForEach(query: JsonQuery)
// query: InvJsonQuery, ForEachOk(query: JsonQuery)
// ) }
// InvForEach(query: InvJsonQuery)
// InvForEachOk(query: InvJsonQuery)
// }
// fn invert_query_rec(query: JsonQuery, state: InvJsonQuery) -> InvJsonQuery { type InvJsonQuery {
// case query { InvEnd
// Root -> state InvKey(key: String, query: InvJsonQuery)
// Key(query, key) -> invert_query_rec(query, InvKey(key, state)) InvKeyOr(key: String, or: JsonValue, query: InvJsonQuery)
// KeyOr(query, key, o) -> invert_query_rec(query, InvKeyOr(key, o, state)) InvIndex(index: Int, query: InvJsonQuery)
// Index(query, index) -> invert_query_rec(query, InvIndex(index, state)) InvIndexOr(index: Int, or: JsonValue, query: InvJsonQuery)
// IndexOr(query, index, or) -> InvFilter(predicate: fn(JsonValue) -> Bool, query: InvJsonQuery)
// invert_query_rec(query, InvIndexOr(index, or, state)) InvMap(mapping: fn(JsonValue) -> JsonValue, query: InvJsonQuery)
// Filter(query, predicate) -> InvMapKeys(mapping: fn(String) -> String, query: InvJsonQuery)
// invert_query_rec(query, InvFilter(predicate, state)) InvMapValues(mapping: fn(String, JsonValue) -> JsonValue, query: InvJsonQuery)
// Map(query, mapping) -> invert_query_rec(query, InvMap(mapping, state)) InvFilterMap(
// MapKeys(query, mapping) -> mapping: fn(JsonValue) -> Result(JsonValue, Nil),
// invert_query_rec(query, InvMapKeys(mapping, state)) query: InvJsonQuery,
// MapValues(query, mapping) -> )
// invert_query_rec(query, InvMapValues(mapping, state)) InvForEach(query: InvJsonQuery)
// FilterMap(query, mapping) -> InvForEachOk(query: InvJsonQuery)
// invert_query_rec(query, InvFilterMap(mapping, state)) }
// ForEach(query) -> invert_query_rec(query, InvForEach(state))
// ForEachOk(query) -> invert_query_rec(query, InvForEachOk(state))
// }
// }
// fn invert_query(query: JsonQuery) -> InvJsonQuery { fn invert_query_rec(query: JsonQuery, state: InvJsonQuery) -> InvJsonQuery {
// invert_query_rec(query, InvEnd) case query {
// } Root -> state
Key(query, key) -> invert_query_rec(query, InvKey(key, state))
KeyOr(query, key, o) -> invert_query_rec(query, InvKeyOr(key, o, state))
Index(query, index) -> invert_query_rec(query, InvIndex(index, state))
IndexOr(query, index, or) ->
invert_query_rec(query, InvIndexOr(index, or, state))
Filter(query, predicate) ->
invert_query_rec(query, InvFilter(predicate, state))
Map(query, mapping) -> invert_query_rec(query, InvMap(mapping, state))
MapKeys(query, mapping) ->
invert_query_rec(query, InvMapKeys(mapping, state))
MapValues(query, mapping) ->
invert_query_rec(query, InvMapValues(mapping, state))
FilterMap(query, mapping) ->
invert_query_rec(query, InvFilterMap(mapping, state))
ForEach(query) -> invert_query_rec(query, InvForEach(state))
ForEachOk(query) -> invert_query_rec(query, InvForEachOk(state))
}
}
// pub type JsonQueryError { fn invert_query(query: JsonQuery) -> InvJsonQuery {
// UnexpectedType(JsonValue) invert_query_rec(query, InvEnd)
// MissingObjectKey(JsonValue, key: String) }
// IndexOutOfBounds(JsonValue, index: Int)
// }
// fn query_json_rec( pub type JsonQueryError {
// json: JsonValue, UnexpectedType(JsonValue)
// query: InvJsonQuery, MissingObjectKey(JsonValue, key: String)
// ) -> Result(JsonValue, JsonQueryError) { IndexOutOfBounds(JsonValue, index: Int)
// case query { }
// InvEnd -> Ok(json)
// InvKey(key, q) ->
// case json {
// Object(obj) as j ->
// obj
// |> dict.get(key)
// |> result.replace_error(MissingObjectKey(j, key))
// j -> Error(UnexpectedType(j))
// }
// |> result.map(query_json_rec(_, q))
// |> result.flatten
// InvKeyOr(key, or, q) ->
// case json {
// Object(obj) ->
// obj
// |> dict.get(key)
// |> result.unwrap(or)
// |> Ok
// j -> Error(UnexpectedType(j))
// }
// |> result.map(query_json_rec(_, q))
// |> result.flatten
// InvIndex(index, q) ->
// case json {
// Array(arr) as j ->
// arr
// |> list.at(index)
// |> result.replace_error(IndexOutOfBounds(j, index))
// j -> Error(UnexpectedType(j))
// }
// |> result.map(query_json_rec(_, q))
// |> result.flatten
// InvIndexOr(index, or, q) ->
// case json {
// Array(arr) ->
// arr
// |> list.at(index)
// |> result.unwrap(or)
// |> Ok
// j -> Error(UnexpectedType(j))
// }
// |> result.map(query_json_rec(_, q))
// |> result.flatten
// InvFilter(predicate, q) ->
// case json {
// Array(arr) ->
// arr
// |> list.filter(predicate)
// |> Array
// |> query_json_rec(q)
// j -> Error(UnexpectedType(j))
// }
// InvMap(mapping, q) ->
// case json {
// Array(arr) ->
// arr
// |> list.map(mapping)
// |> Array
// |> query_json_rec(q)
// j -> Error(UnexpectedType(j))
// }
// InvMapKeys(mapping, q) ->
// case json {
// Object(obj) ->
// obj
// |> dict.to_list
// |> list.map(fn(kv) { #(mapping(kv.0), kv.1) })
// |> dict.from_list
// |> Object
// |> query_json_rec(q)
// j -> Error(UnexpectedType(j))
// }
// InvMapValues(mapping, q) ->
// case json {
// Object(obj) ->
// obj
// |> dict.map_values(mapping)
// |> Object
// |> query_json_rec(q)
// j -> Error(UnexpectedType(j))
// }
// InvFilterMap(mapping, q) ->
// case json {
// Array(arr) ->
// arr
// |> list.filter_map(mapping)
// |> Array
// |> query_json_rec(q)
// j -> Error(UnexpectedType(j))
// }
// InvForEach(q) ->
// case json {
// Array(arr) ->
// arr
// |> list.map(query_json_rec(_, q))
// |> result.all
// |> result.map(Array)
// j -> Error(UnexpectedType(j))
// }
// InvForEachOk(q) ->
// case json {
// Array(arr) ->
// arr
// |> list.map(query_json_rec(_, q))
// |> result.values
// |> Array
// |> Ok
// j -> Error(UnexpectedType(j))
// }
// }
// }
// pub fn query_json(json: JsonValue, query: JsonQuery) { fn query_json_rec(
// query_json_rec(json, invert_query(query)) json: JsonValue,
// } query: InvJsonQuery,
) -> Result(JsonValue, JsonQueryError) {
case query {
InvEnd -> Ok(json)
InvKey(key, q) ->
case json {
Object(obj) as j ->
obj
|> dict.get(key)
|> result.replace_error(MissingObjectKey(j, key))
j -> Error(UnexpectedType(j))
}
|> result.map(query_json_rec(_, q))
|> result.flatten
InvKeyOr(key, or, q) ->
case json {
Object(obj) ->
obj
|> dict.get(key)
|> result.unwrap(or)
|> Ok
j -> Error(UnexpectedType(j))
}
|> result.map(query_json_rec(_, q))
|> result.flatten
InvIndex(index, q) ->
case json {
Array(arr) as j ->
arr
|> list.at(index)
|> result.replace_error(IndexOutOfBounds(j, index))
j -> Error(UnexpectedType(j))
}
|> result.map(query_json_rec(_, q))
|> result.flatten
InvIndexOr(index, or, q) ->
case json {
Array(arr) ->
arr
|> list.at(index)
|> result.unwrap(or)
|> Ok
j -> Error(UnexpectedType(j))
}
|> result.map(query_json_rec(_, q))
|> result.flatten
InvFilter(predicate, q) ->
case json {
Array(arr) ->
arr
|> list.filter(predicate)
|> Array
|> query_json_rec(q)
j -> Error(UnexpectedType(j))
}
InvMap(mapping, q) ->
case json {
Array(arr) ->
arr
|> list.map(mapping)
|> Array
|> query_json_rec(q)
j -> Error(UnexpectedType(j))
}
InvMapKeys(mapping, q) ->
case json {
Object(obj) ->
obj
|> dict.to_list
|> list.map(fn(kv) { #(mapping(kv.0), kv.1) })
|> dict.from_list
|> Object
|> query_json_rec(q)
j -> Error(UnexpectedType(j))
}
InvMapValues(mapping, q) ->
case json {
Object(obj) ->
obj
|> dict.map_values(mapping)
|> Object
|> query_json_rec(q)
j -> Error(UnexpectedType(j))
}
InvFilterMap(mapping, q) ->
case json {
Array(arr) ->
arr
|> list.filter_map(mapping)
|> Array
|> query_json_rec(q)
j -> Error(UnexpectedType(j))
}
InvForEach(q) ->
case json {
Array(arr) ->
arr
|> list.map(query_json_rec(_, q))
|> result.all
|> result.map(Array)
j -> Error(UnexpectedType(j))
}
InvForEachOk(q) ->
case json {
Array(arr) ->
arr
|> list.map(query_json_rec(_, q))
|> result.values
|> Array
|> Ok
j -> Error(UnexpectedType(j))
}
}
}
pub fn query_json(json: JsonValue, query: JsonQuery) {
query_json_rec(json, invert_query(query))
}

View File

@ -2,7 +2,8 @@ import gleeunit
import gleeunit/should import gleeunit/should
import gleam/dict import gleam/dict
import jasper.{ import jasper.{
type JsonValue, Array, Boolean, Null, Number, Object, String, parse_json, type JsonValue, Array, Boolean, Index, IndexOutOfBounds, Key, MissingObjectKey,
Null, Number, Object, Root, String, UnexpectedType, parse_json, query_json,
} }
pub fn main() { pub fn main() {
@ -76,44 +77,45 @@ pub fn parse_objects_test() {
), ),
) )
} }
// pub fn query_test() {
// query_json(String("foo"), Root) pub fn query_test() {
// |> should.equal(Ok(String("foo"))) query_json(String("foo"), Root)
// query_json( |> should.equal(Ok(String("foo")))
// String("foo"), query_json(
// Root String("foo"),
// |> Key("foo"), Root
// ) |> Key("foo"),
// |> should.equal(Error(UnexpectedType(String("foo")))) )
// query_json( |> should.equal(Error(UnexpectedType(String("foo"))))
// String("foo"), query_json(
// Root String("foo"),
// |> Index(2), Root
// ) |> Index(2),
// |> should.equal(Error(UnexpectedType(String("foo")))) )
// query_json( |> should.equal(Error(UnexpectedType(String("foo"))))
// Array([String("foo")]), query_json(
// Root Array([String("foo")]),
// |> Index(2), Root
// ) |> Index(2),
// |> should.equal(Error(IndexOutOfBounds(Array([String("foo")]), 2))) )
// query_json( |> should.equal(Error(IndexOutOfBounds(Array([String("foo")]), 2)))
// Object(dict.from_list([#("bar", Array([String("foo")]))])), query_json(
// Root Object(dict.from_list([#("bar", Array([String("foo")]))])),
// |> Key("bar") Root
// |> Index(2), |> Key("bar")
// ) |> Index(2),
// |> should.equal(Error(IndexOutOfBounds(Array([String("foo")]), 2))) )
// query_json( |> should.equal(Error(IndexOutOfBounds(Array([String("foo")]), 2)))
// Object(dict.from_list([#("bar", Array([String("foo")]))])), query_json(
// Root Object(dict.from_list([#("bar", Array([String("foo")]))])),
// |> Key("foo") Root
// |> Index(2), |> Key("foo")
// ) |> Index(2),
// |> should.equal( )
// Error(MissingObjectKey( |> should.equal(
// Object(dict.from_list([#("bar", Array([String("foo")]))])), Error(MissingObjectKey(
// "foo", Object(dict.from_list([#("bar", Array([String("foo")]))])),
// )), "foo",
// ) )),
// } )
}