Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Prelude.JSON.omitNullFields #784

Merged
merged 5 commits into from Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
154 changes: 154 additions & 0 deletions Prelude/JSON/filterNullFields
@@ -0,0 +1,154 @@
{-
This utility filters out all `null` record fields, which is often the idiomatic
way for a configuration to encode absent fields
-}
let JSON/Type =
./Type sha256:5adb234f5868a5b0eddeb034d690aaba8cb94ea20d0d557003e90334fff6be3e
? ./Type

let string =
./string sha256:7a8ac435d30a96092d72889f3d48eabf7cba47ecf553fd6bc07a79fdf473e8d2
? ./string

let number =
./number sha256:534745568065ae19d2b0fe1d09eeb071e9717d0f392187eb0bc95f386b018bec
? ./number

let object =
./object sha256:a4e047cf157c3971b026b3942a87d474c85950d9b9654f8ebc8631740abf75a9
? ./object

let array =
./array sha256:3a4c06cf135f4c80619e48c0808f6600d19782705bc59ee7c27cfc2e0f097eb7
? ./array

let bool =
./bool sha256:018d29f030b45d642aba6bb81bf2c19a7bf183684612ce7a2c8afd2099783c48
? ./bool

let null =
./null sha256:52c1d45ab2ca54875b444bfb1afdea497c8c9b0652e5044fafd8b16d97f4b78d
? ./null

let List/concatMap =
../List/concatMap sha256:3b2167061d11fda1e4f6de0522cbe83e0d5ac4ef5ddf6bb0b2064470c5d3fb64
? ../List/concatMap

let List/map =
../List/map sha256:dd845ffb4568d40327f2a817eb42d1c6138b929ca758d50bc33112ef3c885680
? ../List/map

let filterNullFields
: JSON/Type → JSON/Type
= λ(old : JSON/Type)
→ λ(JSON : Type)
→ λ ( json
: { string : Text → JSON
, number : Double → JSON
, object : List { mapKey : Text, mapValue : JSON } → JSON
, array : List JSON → JSON
, bool : Bool → JSON
, null : JSON
}
)
→ let result =
old
{ value : JSON, isNull : Bool }
{ string =
λ(x : Text) → { value = json.string x, isNull = False }
, number =
λ(x : Double) → { value = json.number x, isNull = False }
, object =
λ ( keyValues
: List
{ mapKey : Text
, mapValue : { value : JSON, isNull : Bool }
}
)
→ let value =
json.object
( List/concatMap
{ mapKey : Text
, mapValue : { value : JSON, isNull : Bool }
}
{ mapKey : Text, mapValue : JSON }
( λ ( keyValue
: { mapKey : Text
, mapValue :
{ value : JSON, isNull : Bool }
}
)
→ if keyValue.mapValue.isNull

then [] : List
{ mapKey : Text
, mapValue : JSON
}

else [ keyValue.{ mapKey }
∧ { mapValue =
keyValue.mapValue.value
}
]
)
keyValues
)

in { value = value, isNull = False }
, array =
λ(xs : List { value : JSON, isNull : Bool })
→ let value =
json.array
( List/map
{ value : JSON, isNull : Bool }
JSON
( λ(x : { value : JSON, isNull : Bool })
→ x.value
)
xs
)

in { value = value, isNull = False }
, bool = λ(x : Bool) → { value = json.bool x, isNull = False }
, null = { value = json.null, isNull = True }
}

in result.value

let property =
λ(a : Text)
→ λ(b : Double)
→ λ(c : Bool)
→ assert
: filterNullFields
( object
( toMap
{ string = string a
, number = number b
, bool = bool c
, null = null
}
)
)
≡ object
(toMap { string = string a, number = number b, bool = bool c })

let example =
assert
: filterNullFields
( object
(toMap { array = array [ object (toMap { null = null }) ] })
)
≡ object
( toMap
{ array =
array
[ object
([] : List { mapKey : Text, mapValue : JSON/Type })
]
}
)

let example = assert : filterNullFields (array [ null ]) ≡ array [ null ]

in filterNullFields
3 changes: 3 additions & 0 deletions Prelude/JSON/package.dhall
Expand Up @@ -34,4 +34,7 @@
, render =
./render sha256:81f5a84efbb35211b1556838e86d17ed497912cf765bfa4ab76708b21e5371f1
? ./render
, filterNullFields =
./filterNullFields sha256:c28270c553f48c406bd161c61776963315e278af5dae9331c4a320c3f4ecb4ec
? ./filterNullFields
}
2 changes: 1 addition & 1 deletion Prelude/package.dhall
Expand Up @@ -29,7 +29,7 @@
./Optional/package.dhall sha256:7608f2d38dabee8bfe6865b4adc11289059984220f422d2b023b15b3908f7a4c
? ./Optional/package.dhall
, JSON =
./JSON/package.dhall sha256:0c3c40a63108f2e6ad59f23b789c18eb484d0e9aebc9416c5a4f338c6753084b
./JSON/package.dhall sha256:96c47b48ae8c361a1b6d52b63a1929fc383cf5e316f01df9111234306db34e6f
? ./JSON/package.dhall
, Text =
./Text/package.dhall sha256:0a0ad9f649aed94c2680491efb384925b5b2bb5b353f1b8a7eb134955c1ffe45
Expand Down