diff options
author | Eduardo Julian | 2021-07-27 03:51:10 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-07-27 03:51:10 -0400 |
commit | 061fd8a209bbcaffc2bfb850ac6046752a567d50 (patch) | |
tree | 8cd83ad7d0bc06ded7976eb5420467e485733ae8 /stdlib/source/poly/lux/data | |
parent | e64b6d0114c26a455e19a416b5f02a4d19dd711f (diff) |
Re-named wrap => in && unwrap => out.
Diffstat (limited to 'stdlib/source/poly/lux/data')
-rw-r--r-- | stdlib/source/poly/lux/data/format/json.lux | 185 |
1 files changed, 93 insertions, 92 deletions
diff --git a/stdlib/source/poly/lux/data/format/json.lux b/stdlib/source/poly/lux/data/format/json.lux index 1aa793323..9ce3b6a6e 100644 --- a/stdlib/source/poly/lux/data/format/json.lux +++ b/stdlib/source/poly/lux/data/format/json.lux @@ -1,4 +1,5 @@ -(.module: {#.doc "Codecs for values in the JSON format."} +(.module: + {#.doc "Codecs for values in the JSON format."} [library [lux #* ["." debug] @@ -70,8 +71,8 @@ (do <>.monad [high </>.number low </>.number] - (wrap (n.+ (|> high frac.int .nat (i64.left_shift 32)) - (|> low frac.int .nat)))))))) + (in (n.+ (|> high frac.int .nat (i64.left_shift 32)) + (|> low frac.int .nat)))))))) (implementation: int_codec (codec.Codec JSON Int) @@ -105,8 +106,8 @@ [(do ! [#let [g!_ (code.local_identifier "_______")] _ <matcher>] - (wrap (` (: (~ (@JSON\encode inputT)) - <encoder>))))] + (in (` (: (~ (@JSON\encode inputT)) + <encoder>))))] [(<type>.exactly Any) (function ((~ g!_) (~ (code.identifier ["" "0"]))) #/.Null)] [(<type>.sub Bit) (|>> #/.Boolean)] @@ -117,8 +118,8 @@ <time> (template [<type> <codec>] [(do ! [_ (<type>.exactly <type>)] - (wrap (` (: (~ (@JSON\encode inputT)) - (|>> (\ (~! <codec>) (~' encode)) #/.String)))))] + (in (` (: (~ (@JSON\encode inputT)) + (|>> (\ (~! <codec>) (~' encode)) #/.String)))))] ## [duration.Duration duration.codec] ## [instant.Instant instant.codec] @@ -137,8 +138,8 @@ (do ! [unitT (<type>.applied (<>.after (<type>.exactly unit.Qty) <type>.any))] - (wrap (` (: (~ (@JSON\encode inputT)) - (\ (~! qty_codec) (~' encode)))))) + (in (` (: (~ (@JSON\encode inputT)) + (\ (~! qty_codec) (~' encode)))))) (do ! [#let [g!_ (code.local_identifier "_______") g!key (code.local_identifier "_______key") @@ -147,76 +148,76 @@ (<type>.exactly d.Dictionary) (<type>.exactly .Text) encode))] - (wrap (` (: (~ (@JSON\encode inputT)) - (|>> ((~! d.entries)) - ((~! list\map) (function ((~ g!_) [(~ g!key) (~ g!val)]) - [(~ g!key) ((~ =val=) (~ g!val))])) - ((~! d.of_list) (~! text.hash)) - #/.Object))))) + (in (` (: (~ (@JSON\encode inputT)) + (|>> ((~! d.entries)) + ((~! list\map) (function ((~ g!_) [(~ g!key) (~ g!val)]) + [(~ g!key) ((~ =val=) (~ g!val))])) + ((~! d.of_list) (~! text.hash)) + #/.Object))))) (do ! [[_ =sub=] (<type>.applied ($_ <>.and (<type>.exactly .Maybe) encode))] - (wrap (` (: (~ (@JSON\encode inputT)) - ((~! ..nullable) (~ =sub=)))))) + (in (` (: (~ (@JSON\encode inputT)) + ((~! ..nullable) (~ =sub=)))))) (do ! [[_ =sub=] (<type>.applied ($_ <>.and (<type>.exactly .List) encode))] - (wrap (` (: (~ (@JSON\encode inputT)) - (|>> ((~! list\map) (~ =sub=)) ((~! row.of_list)) #/.Array))))) + (in (` (: (~ (@JSON\encode inputT)) + (|>> ((~! list\map) (~ =sub=)) ((~! row.of_list)) #/.Array))))) (do ! [#let [g!_ (code.local_identifier "_______") g!input (code.local_identifier "_______input")] members (<type>.variant (<>.many encode)) #let [last (dec (list.size members))]] - (wrap (` (: (~ (@JSON\encode inputT)) - (function ((~ g!_) (~ g!input)) - (case (~ g!input) - (~+ (list\join (list\map (function (_ [tag g!encode]) - (if (n.= last tag) - (list (` ((~ (code.nat (dec tag))) #1 (~ g!input))) - (` ((~! /.json) [(~ (code.frac (..tag (dec tag)))) - #1 - ((~ g!encode) (~ g!input))]))) - (list (` ((~ (code.nat tag)) #0 (~ g!input))) - (` ((~! /.json) [(~ (code.frac (..tag tag))) - #0 - ((~ g!encode) (~ g!input))]))))) - (list.enumeration members)))))))))) + (in (` (: (~ (@JSON\encode inputT)) + (function ((~ g!_) (~ g!input)) + (case (~ g!input) + (~+ (list\join (list\map (function (_ [tag g!encode]) + (if (n.= last tag) + (list (` ((~ (code.nat (dec tag))) #1 (~ g!input))) + (` ((~! /.json) [(~ (code.frac (..tag (dec tag)))) + #1 + ((~ g!encode) (~ g!input))]))) + (list (` ((~ (code.nat tag)) #0 (~ g!input))) + (` ((~! /.json) [(~ (code.frac (..tag tag))) + #0 + ((~ g!encode) (~ g!input))]))))) + (list.enumeration members)))))))))) (do ! [g!encoders (<type>.tuple (<>.many encode)) #let [g!_ (code.local_identifier "_______") g!members (|> (list.size g!encoders) list.indices (list\map (|>> n\encode code.local_identifier)))]] - (wrap (` (: (~ (@JSON\encode inputT)) - (function ((~ g!_) [(~+ g!members)]) - ((~! /.json) [(~+ (list\map (function (_ [g!member g!encode]) - (` ((~ g!encode) (~ g!member)))) - (list.zipped/2 g!members g!encoders)))])))))) + (in (` (: (~ (@JSON\encode inputT)) + (function ((~ g!_) [(~+ g!members)]) + ((~! /.json) [(~+ (list\map (function (_ [g!member g!encode]) + (` ((~ g!encode) (~ g!member)))) + (list.zipped/2 g!members g!encoders)))])))))) ## Type recursion (do ! [[selfC non_recC] (<type>.recursive encode) #let [g! (code.local_identifier "____________")]] - (wrap (` (: (~ (@JSON\encode inputT)) - ((~! ..rec_encode) (.function ((~ g!) (~ selfC)) - (~ non_recC))))))) + (in (` (: (~ (@JSON\encode inputT)) + ((~! ..rec_encode) (.function ((~ g!) (~ selfC)) + (~ non_recC))))))) <type>.recursive_self ## Type applications (do ! [partsC (<type>.applied (<>.many encode))] - (wrap (` ((~+ partsC))))) + (in (` ((~+ partsC))))) ## Polymorphism (do ! [[funcC varsC bodyC] (<type>.polymorphic encode)] - (wrap (` (: (All [(~+ varsC)] - (-> (~+ (list\map (function (_ varC) (` (-> (~ varC) /.JSON))) - varsC)) - (-> ((~ (poly.to_code *env* inputT)) (~+ varsC)) - /.JSON))) - (function ((~ funcC) (~+ varsC)) - (~ bodyC)))))) + (in (` (: (All [(~+ varsC)] + (-> (~+ (list\map (function (_ varC) (` (-> (~ varC) /.JSON))) + varsC)) + (-> ((~ (poly.to_code *env* inputT)) (~+ varsC)) + /.JSON))) + (function ((~ funcC) (~+ varsC)) + (~ bodyC)))))) <type>.parameter <type>.recursive_call ## If all else fails... @@ -228,8 +229,8 @@ [<basic> (template [<matcher> <decoder>] [(do ! [_ <matcher>] - (wrap (` (: (~ (@JSON\decode inputT)) - (~! <decoder>)))))] + (in (` (: (~ (@JSON\decode inputT)) + (~! <decoder>)))))] [(<type>.exactly Any) </>.null] [(<type>.sub Bit) </>.boolean] @@ -240,8 +241,8 @@ <time> (template [<type> <codec>] [(do ! [_ (<type>.exactly <type>)] - (wrap (` (: (~ (@JSON\decode inputT)) - ((~! <>.codec) (~! <codec>) (~! </>.string))))))] + (in (` (: (~ (@JSON\decode inputT)) + ((~! <>.codec) (~! <codec>) (~! </>.string))))))] ## [duration.Duration duration.codec] ## [instant.Instant instant.codec] @@ -260,65 +261,65 @@ (do ! [unitT (<type>.applied (<>.after (<type>.exactly unit.Qty) <type>.any))] - (wrap (` (: (~ (@JSON\decode inputT)) - ((~! <>.codec) (~! qty_codec) (~! </>.any)))))) + (in (` (: (~ (@JSON\decode inputT)) + ((~! <>.codec) (~! qty_codec) (~! </>.any)))))) (do ! [[_ _ valC] (<type>.applied ($_ <>.and (<type>.exactly d.Dictionary) (<type>.exactly .Text) decode))] - (wrap (` (: (~ (@JSON\decode inputT)) - ((~! </>.dictionary) (~ valC)))))) + (in (` (: (~ (@JSON\decode inputT)) + ((~! </>.dictionary) (~ valC)))))) (do ! [[_ subC] (<type>.applied (<>.and (<type>.exactly .Maybe) decode))] - (wrap (` (: (~ (@JSON\decode inputT)) - ((~! </>.nullable) (~ subC)))))) + (in (` (: (~ (@JSON\decode inputT)) + ((~! </>.nullable) (~ subC)))))) (do ! [[_ subC] (<type>.applied (<>.and (<type>.exactly .List) decode))] - (wrap (` (: (~ (@JSON\decode inputT)) - ((~! </>.array) ((~! <>.some) (~ subC))))))) + (in (` (: (~ (@JSON\decode inputT)) + ((~! </>.array) ((~! <>.some) (~ subC))))))) (do ! [members (<type>.variant (<>.many decode)) #let [last (dec (list.size members))]] - (wrap (` (: (~ (@JSON\decode inputT)) - ($_ ((~! <>.or)) - (~+ (list\map (function (_ [tag memberC]) - (if (n.= last tag) - (` (|> (~ memberC) - ((~! <>.after) ((~! </>.boolean!) (~ (code.bit #1)))) - ((~! <>.after) ((~! </>.number!) (~ (code.frac (..tag (dec tag)))))) - ((~! </>.array)))) - (` (|> (~ memberC) - ((~! <>.after) ((~! </>.boolean!) (~ (code.bit #0)))) - ((~! <>.after) ((~! </>.number!) (~ (code.frac (..tag tag))))) - ((~! </>.array)))))) - (list.enumeration members)))))))) + (in (` (: (~ (@JSON\decode inputT)) + ($_ ((~! <>.or)) + (~+ (list\map (function (_ [tag memberC]) + (if (n.= last tag) + (` (|> (~ memberC) + ((~! <>.after) ((~! </>.boolean!) (~ (code.bit #1)))) + ((~! <>.after) ((~! </>.number!) (~ (code.frac (..tag (dec tag)))))) + ((~! </>.array)))) + (` (|> (~ memberC) + ((~! <>.after) ((~! </>.boolean!) (~ (code.bit #0)))) + ((~! <>.after) ((~! </>.number!) (~ (code.frac (..tag tag))))) + ((~! </>.array)))))) + (list.enumeration members)))))))) (do ! [g!decoders (<type>.tuple (<>.many decode))] - (wrap (` (: (~ (@JSON\decode inputT)) - ((~! </>.array) ($_ ((~! <>.and)) (~+ g!decoders))))))) + (in (` (: (~ (@JSON\decode inputT)) + ((~! </>.array) ($_ ((~! <>.and)) (~+ g!decoders))))))) ## Type recursion (do ! [[selfC bodyC] (<type>.recursive decode) #let [g! (code.local_identifier "____________")]] - (wrap (` (: (~ (@JSON\decode inputT)) - ((~! <>.rec) (.function ((~ g!) (~ selfC)) - (~ bodyC))))))) + (in (` (: (~ (@JSON\decode inputT)) + ((~! <>.rec) (.function ((~ g!) (~ selfC)) + (~ bodyC))))))) <type>.recursive_self ## Type applications (do ! [[funcC argsC] (<type>.applied (<>.and decode (<>.many decode)))] - (wrap (` ((~ funcC) (~+ argsC))))) + (in (` ((~ funcC) (~+ argsC))))) ## Polymorphism (do ! [[funcC varsC bodyC] (<type>.polymorphic decode)] - (wrap (` (: (All [(~+ varsC)] - (-> (~+ (list\map (|>> (~) </>.Parser (`)) varsC)) - (</>.Parser ((~ (poly.to_code *env* inputT)) (~+ varsC))))) - (function ((~ funcC) (~+ varsC)) - (~ bodyC)))))) + (in (` (: (All [(~+ varsC)] + (-> (~+ (list\map (|>> (~) </>.Parser (`)) varsC)) + (</>.Parser ((~ (poly.to_code *env* inputT)) (~+ varsC))))) + (function ((~ funcC) (~+ varsC)) + (~ bodyC)))))) <type>.parameter <type>.recursive_call ## If all else fails... @@ -343,10 +344,10 @@ #dictionary (Dictionary Text Frac)}) (derived: (..codec Record)))} - (wrap (list (` (: (codec.Codec /.JSON (~ inputT)) - (implementation - (def: (~' encode) - ((~! ..encode) (~ inputT))) - (def: (~' decode) - ((~! </>.run) ((~! ..decode) (~ inputT)))) - )))))) + (in (list (` (: (codec.Codec /.JSON (~ inputT)) + (implementation + (def: (~' encode) + ((~! ..encode) (~ inputT))) + (def: (~' decode) + ((~! </>.run) ((~! ..decode) (~ inputT)))) + )))))) |