aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/polytypic/lux/data/format/json.lux
diff options
context:
space:
mode:
authorEduardo Julian2022-06-14 10:17:32 -0400
committerEduardo Julian2022-06-14 10:17:32 -0400
commit9a835bde8908e4ebd1c8972211acadc5895d720a (patch)
treec4bd81cfff7357a3895389a1544eaa66230203ec /stdlib/source/polytypic/lux/data/format/json.lux
parentc4d938ebb2f5245b4c3faa22c4f217e7e818589f (diff)
De-sigil-ification: suffix : [Part 8]
Diffstat (limited to '')
-rw-r--r--stdlib/source/polytypic/lux/data/format/json.lux446
1 files changed, 224 insertions, 222 deletions
diff --git a/stdlib/source/polytypic/lux/data/format/json.lux b/stdlib/source/polytypic/lux/data/format/json.lux
index 801a16e19..b86f256a2 100644
--- a/stdlib/source/polytypic/lux/data/format/json.lux
+++ b/stdlib/source/polytypic/lux/data/format/json.lux
@@ -35,7 +35,7 @@
["[0]" month]]
["[0]" type (.only)
["[0]" unit]
- ["[0]" poly (.only poly:)]]]]
+ ["[0]" poly (.only polytypic)]]]]
[\\library
["[0]" / (.only JSON)]])
@@ -96,233 +96,235 @@
(|>> (at ..int_codec decoded)
(at try.functor each (debug.private unit.in'))))))
-(poly: encoded
- (with_expansions
- [<basic> (with_template [<matcher> <encoder>]
- [(do !
- [.let [g!_ (code.local "_______")]
- _ <matcher>]
- (in (` (is (~ (@JSON#encoded inputT))
- <encoder>))))]
+(def: encoded
+ (polytypic encoded
+ (with_expansions
+ [<basic> (with_template [<matcher> <encoder>]
+ [(do !
+ [.let [g!_ (code.local "_______")]
+ _ <matcher>]
+ (in (` (is (~ (@JSON#encoded inputT))
+ <encoder>))))]
- [(<type>.exactly Any) (function ((~ g!_) (~ (code.symbol ["" "0"]))) {/.#Null})]
- [(<type>.sub Bit) (|>> {/.#Boolean})]
- [(<type>.sub Nat) (at (~! ..nat_codec) (~' encoded))]
- [(<type>.sub Int) (at (~! ..int_codec) (~' encoded))]
- [(<type>.sub Frac) (|>> {/.#Number})]
- [(<type>.sub Text) (|>> {/.#String})])
- <time> (with_template [<type> <codec>]
- [(do !
- [_ (<type>.exactly <type>)]
- (in (` (is (~ (@JSON#encoded inputT))
- (|>> (at (~! <codec>) (~' encoded)) {/.#String})))))]
+ [(<type>.exactly Any) (function ((~ g!_) (~ (code.symbol ["" "0"]))) {/.#Null})]
+ [(<type>.sub Bit) (|>> {/.#Boolean})]
+ [(<type>.sub Nat) (at (~! ..nat_codec) (~' encoded))]
+ [(<type>.sub Int) (at (~! ..int_codec) (~' encoded))]
+ [(<type>.sub Frac) (|>> {/.#Number})]
+ [(<type>.sub Text) (|>> {/.#String})])
+ <time> (with_template [<type> <codec>]
+ [(do !
+ [_ (<type>.exactly <type>)]
+ (in (` (is (~ (@JSON#encoded inputT))
+ (|>> (at (~! <codec>) (~' encoded)) {/.#String})))))]
- ... [duration.Duration duration.codec]
- ... [instant.Instant instant.codec]
- [date.Date date.codec]
- [day.Day day.codec]
- [month.Month month.codec])]
- (do [! <>.monad]
- [*env* <type>.env
- .let [g!_ (code.local "_______")
- @JSON#encoded (is (-> Type Code)
- (function (_ type)
- (` (-> (~ (poly.code *env* type)) /.JSON))))]
- inputT <type>.next]
- (all <>.either
- <basic>
- <time>
- (do !
- [unitT (<type>.applied (<>.after (<type>.exactly unit.Qty)
- <type>.any))]
- (in (` (is (~ (@JSON#encoded inputT))
- (at (~! qty_codec) (~' encoded))))))
- (do !
- [.let [g!_ (code.local "_______")
- g!key (code.local "_______key")
- g!val (code.local "_______val")]
- [_ _ =val=] (<type>.applied (all <>.and
- (<type>.exactly dictionary.Dictionary)
- (<type>.exactly .Text)
+ ... [duration.Duration duration.codec]
+ ... [instant.Instant instant.codec]
+ [date.Date date.codec]
+ [day.Day day.codec]
+ [month.Month month.codec])]
+ (do [! <>.monad]
+ [*env* <type>.env
+ .let [g!_ (code.local "_______")
+ @JSON#encoded (is (-> Type Code)
+ (function (_ type)
+ (` (-> (~ (poly.code *env* type)) /.JSON))))]
+ inputT <type>.next]
+ (all <>.either
+ <basic>
+ <time>
+ (do !
+ [unitT (<type>.applied (<>.after (<type>.exactly unit.Qty)
+ <type>.any))]
+ (in (` (is (~ (@JSON#encoded inputT))
+ (at (~! qty_codec) (~' encoded))))))
+ (do !
+ [.let [g!_ (code.local "_______")
+ g!key (code.local "_______key")
+ g!val (code.local "_______val")]
+ [_ _ =val=] (<type>.applied (all <>.and
+ (<type>.exactly dictionary.Dictionary)
+ (<type>.exactly .Text)
+ encoded))]
+ (in (` (is (~ (@JSON#encoded inputT))
+ (|>> ((~! dictionary.entries))
+ ((~! list#each) (function ((~ g!_) [(~ g!key) (~ g!val)])
+ [(~ g!key) ((~ =val=) (~ g!val))]))
+ ((~! dictionary.of_list) (~! text.hash))
+ {/.#Object})))))
+ (do !
+ [[_ =sub=] (<type>.applied (all <>.and
+ (<type>.exactly .Maybe)
encoded))]
- (in (` (is (~ (@JSON#encoded inputT))
- (|>> ((~! dictionary.entries))
- ((~! list#each) (function ((~ g!_) [(~ g!key) (~ g!val)])
- [(~ g!key) ((~ =val=) (~ g!val))]))
- ((~! dictionary.of_list) (~! text.hash))
- {/.#Object})))))
- (do !
- [[_ =sub=] (<type>.applied (all <>.and
- (<type>.exactly .Maybe)
- encoded))]
- (in (` (is (~ (@JSON#encoded inputT))
- ((~! ..nullable) (~ =sub=))))))
- (do !
- [[_ =sub=] (<type>.applied (all <>.and
- (<type>.exactly .List)
- encoded))]
- (in (` (is (~ (@JSON#encoded inputT))
- (|>> ((~! list#each) (~ =sub=)) ((~! sequence.of_list)) {/.#Array})))))
- (do !
- [.let [g!_ (code.local "_______")
- g!input (code.local "_______input")]
- members (<type>.variant (<>.many encoded))
- .let [last (-- (list.size members))]]
- (in (` (is (~ (@JSON#encoded inputT))
- (function ((~ g!_) (~ g!input))
- (case (~ g!input)
- (~+ (list#conjoint (list#each (function (_ [tag g!encoded])
- (if (n.= last tag)
- (.list (` {(~ (code.nat (-- tag))) #1 (~ g!input)})
- (` ((~! /.json) [(~ (code.frac (..tag (-- tag))))
- #1
- ((~ g!encoded) (~ g!input))])))
- (.list (` {(~ (code.nat tag)) #0 (~ g!input)})
- (` ((~! /.json) [(~ (code.frac (..tag tag)))
- #0
- ((~ g!encoded) (~ g!input))])))))
- (list.enumeration members))))))))))
- (do !
- [g!encoders (<type>.tuple (<>.many encoded))
- .let [g!_ (code.local "_______")
- g!members (|> (list.size g!encoders)
- list.indices
- (list#each (|>> n#encoded code.local)))]]
- (in (` (is (~ (@JSON#encoded inputT))
- (function ((~ g!_) [(~+ g!members)])
- ((~! /.json) [(~+ (list#each (function (_ [g!member g!encoded])
- (` ((~ g!encoded) (~ g!member))))
- (list.zipped_2 g!members g!encoders)))]))))))
- ... Type recursion
- (do !
- [[selfC non_recC] (<type>.recursive encoded)
- .let [g! (code.local "____________")]]
- (in (` (is (~ (@JSON#encoded inputT))
- ((~! ..rec_encoded) (.function ((~ g!) (~ selfC))
- (~ non_recC)))))))
- <type>.recursive_self
- ... Type applications
- (do !
- [partsC (<type>.applied (<>.many encoded))]
- (in (` ((~+ partsC)))))
- ... Polymorphism
- (do !
- [[funcC varsC bodyC] (<type>.polymorphic encoded)]
- (in (` (is (All ((~ g!_) (~+ varsC))
- (-> (~+ (list#each (function (_ varC) (` (-> (~ varC) /.JSON)))
- varsC))
- (-> ((~ (poly.code *env* inputT)) (~+ varsC))
- /.JSON)))
- (function ((~ funcC) (~+ varsC))
- (~ bodyC))))))
- <type>.parameter
- <type>.recursive_call
- ... If all else fails...
- (<>.failure (format "Cannot create JSON encoder for: " (type.format inputT)))
- ))))
+ (in (` (is (~ (@JSON#encoded inputT))
+ ((~! ..nullable) (~ =sub=))))))
+ (do !
+ [[_ =sub=] (<type>.applied (all <>.and
+ (<type>.exactly .List)
+ encoded))]
+ (in (` (is (~ (@JSON#encoded inputT))
+ (|>> ((~! list#each) (~ =sub=)) ((~! sequence.of_list)) {/.#Array})))))
+ (do !
+ [.let [g!_ (code.local "_______")
+ g!input (code.local "_______input")]
+ members (<type>.variant (<>.many encoded))
+ .let [last (-- (list.size members))]]
+ (in (` (is (~ (@JSON#encoded inputT))
+ (function ((~ g!_) (~ g!input))
+ (case (~ g!input)
+ (~+ (list#conjoint (list#each (function (_ [tag g!encoded])
+ (if (n.= last tag)
+ (.list (` {(~ (code.nat (-- tag))) #1 (~ g!input)})
+ (` ((~! /.json) [(~ (code.frac (..tag (-- tag))))
+ #1
+ ((~ g!encoded) (~ g!input))])))
+ (.list (` {(~ (code.nat tag)) #0 (~ g!input)})
+ (` ((~! /.json) [(~ (code.frac (..tag tag)))
+ #0
+ ((~ g!encoded) (~ g!input))])))))
+ (list.enumeration members))))))))))
+ (do !
+ [g!encoders (<type>.tuple (<>.many encoded))
+ .let [g!_ (code.local "_______")
+ g!members (|> (list.size g!encoders)
+ list.indices
+ (list#each (|>> n#encoded code.local)))]]
+ (in (` (is (~ (@JSON#encoded inputT))
+ (function ((~ g!_) [(~+ g!members)])
+ ((~! /.json) [(~+ (list#each (function (_ [g!member g!encoded])
+ (` ((~ g!encoded) (~ g!member))))
+ (list.zipped_2 g!members g!encoders)))]))))))
+ ... Type recursion
+ (do !
+ [[selfC non_recC] (<type>.recursive encoded)
+ .let [g! (code.local "____________")]]
+ (in (` (is (~ (@JSON#encoded inputT))
+ ((~! ..rec_encoded) (.function ((~ g!) (~ selfC))
+ (~ non_recC)))))))
+ <type>.recursive_self
+ ... Type applications
+ (do !
+ [partsC (<type>.applied (<>.many encoded))]
+ (in (` ((~+ partsC)))))
+ ... Polymorphism
+ (do !
+ [[funcC varsC bodyC] (<type>.polymorphic encoded)]
+ (in (` (is (All ((~ g!_) (~+ varsC))
+ (-> (~+ (list#each (function (_ varC) (` (-> (~ varC) /.JSON)))
+ varsC))
+ (-> ((~ (poly.code *env* inputT)) (~+ varsC))
+ /.JSON)))
+ (function ((~ funcC) (~+ varsC))
+ (~ bodyC))))))
+ <type>.parameter
+ <type>.recursive_call
+ ... If all else fails...
+ (<>.failure (format "Cannot create JSON encoder for: " (type.format inputT)))
+ )))))
-(poly: decoded
- (with_expansions
- [<basic> (with_template [<matcher> <decoder>]
- [(do !
- [_ <matcher>]
- (in (` (is (~ (@JSON#decoded inputT))
- (~! <decoder>)))))]
+(def: decoded
+ (polytypic decoded
+ (with_expansions
+ [<basic> (with_template [<matcher> <decoder>]
+ [(do !
+ [_ <matcher>]
+ (in (` (is (~ (@JSON#decoded inputT))
+ (~! <decoder>)))))]
- [(<type>.exactly Any) </>.null]
- [(<type>.sub Bit) </>.boolean]
- [(<type>.sub Nat) (<>.codec ..nat_codec </>.any)]
- [(<type>.sub Int) (<>.codec ..int_codec </>.any)]
- [(<type>.sub Frac) </>.number]
- [(<type>.sub Text) </>.string])
- <time> (with_template [<type> <codec>]
- [(do !
- [_ (<type>.exactly <type>)]
- (in (` (is (~ (@JSON#decoded inputT))
- ((~! <>.codec) (~! <codec>) (~! </>.string))))))]
+ [(<type>.exactly Any) </>.null]
+ [(<type>.sub Bit) </>.boolean]
+ [(<type>.sub Nat) (<>.codec ..nat_codec </>.any)]
+ [(<type>.sub Int) (<>.codec ..int_codec </>.any)]
+ [(<type>.sub Frac) </>.number]
+ [(<type>.sub Text) </>.string])
+ <time> (with_template [<type> <codec>]
+ [(do !
+ [_ (<type>.exactly <type>)]
+ (in (` (is (~ (@JSON#decoded inputT))
+ ((~! <>.codec) (~! <codec>) (~! </>.string))))))]
- ... [duration.Duration duration.codec]
- ... [instant.Instant instant.codec]
- [date.Date date.codec]
- [day.Day day.codec]
- [month.Month month.codec])]
- (do [! <>.monad]
- [*env* <type>.env
- .let [g!_ (code.local "_______")
- @JSON#decoded (is (-> Type Code)
- (function (_ type)
- (` (</>.Parser (~ (poly.code *env* type))))))]
- inputT <type>.next]
- (all <>.either
- <basic>
- <time>
- (do !
- [unitT (<type>.applied (<>.after (<type>.exactly unit.Qty)
- <type>.any))]
- (in (` (is (~ (@JSON#decoded inputT))
- ((~! <>.codec) (~! qty_codec) (~! </>.any))))))
- (do !
- [[_ _ valC] (<type>.applied (all <>.and
- (<type>.exactly dictionary.Dictionary)
- (<type>.exactly .Text)
- decoded))]
- (in (` (is (~ (@JSON#decoded inputT))
- ((~! </>.dictionary) (~ valC))))))
- (do !
- [[_ subC] (<type>.applied (<>.and (<type>.exactly .Maybe)
- decoded))]
- (in (` (is (~ (@JSON#decoded inputT))
- ((~! </>.nullable) (~ subC))))))
- (do !
- [[_ subC] (<type>.applied (<>.and (<type>.exactly .List)
- decoded))]
- (in (` (is (~ (@JSON#decoded inputT))
- ((~! </>.array) ((~! <>.some) (~ subC)))))))
- (do !
- [members (<type>.variant (<>.many decoded))
- .let [last (-- (list.size members))]]
- (in (` (is (~ (@JSON#decoded inputT))
- (all ((~! <>.or))
- (~+ (list#each (function (_ [tag memberC])
- (if (n.= last tag)
- (` (|> (~ memberC)
- ((~! <>.after) ((~! </>.this_boolean) (~ (code.bit #1))))
- ((~! <>.after) ((~! </>.this_number) (~ (code.frac (..tag (-- tag))))))
- ((~! </>.array))))
- (` (|> (~ memberC)
- ((~! <>.after) ((~! </>.this_boolean) (~ (code.bit #0))))
- ((~! <>.after) ((~! </>.this_number) (~ (code.frac (..tag tag)))))
- ((~! </>.array))))))
- (list.enumeration members))))))))
- (do !
- [g!decoders (<type>.tuple (<>.many decoded))]
- (in (` (is (~ (@JSON#decoded inputT))
- ((~! </>.array) (all ((~! <>.and)) (~+ g!decoders)))))))
- ... Type recursion
- (do !
- [[selfC bodyC] (<type>.recursive decoded)
- .let [g! (code.local "____________")]]
- (in (` (is (~ (@JSON#decoded inputT))
- ((~! <>.rec) (.function ((~ g!) (~ selfC))
- (~ bodyC)))))))
- <type>.recursive_self
- ... Type applications
- (do !
- [[funcC argsC] (<type>.applied (<>.and decoded (<>.many decoded)))]
- (in (` ((~ funcC) (~+ argsC)))))
- ... Polymorphism
- (do !
- [[funcC varsC bodyC] (<type>.polymorphic decoded)]
- (in (` (is (All ((~ g!_) (~+ varsC))
- (-> (~+ (list#each (|>> (~) </>.Parser (`)) varsC))
- (</>.Parser ((~ (poly.code *env* inputT)) (~+ varsC)))))
- (function ((~ funcC) (~+ varsC))
- (~ bodyC))))))
- <type>.parameter
- <type>.recursive_call
- ... If all else fails...
- (<>.failure (format "Cannot create JSON decoder for: " (type.format inputT)))
- ))))
+ ... [duration.Duration duration.codec]
+ ... [instant.Instant instant.codec]
+ [date.Date date.codec]
+ [day.Day day.codec]
+ [month.Month month.codec])]
+ (do [! <>.monad]
+ [*env* <type>.env
+ .let [g!_ (code.local "_______")
+ @JSON#decoded (is (-> Type Code)
+ (function (_ type)
+ (` (</>.Parser (~ (poly.code *env* type))))))]
+ inputT <type>.next]
+ (all <>.either
+ <basic>
+ <time>
+ (do !
+ [unitT (<type>.applied (<>.after (<type>.exactly unit.Qty)
+ <type>.any))]
+ (in (` (is (~ (@JSON#decoded inputT))
+ ((~! <>.codec) (~! qty_codec) (~! </>.any))))))
+ (do !
+ [[_ _ valC] (<type>.applied (all <>.and
+ (<type>.exactly dictionary.Dictionary)
+ (<type>.exactly .Text)
+ decoded))]
+ (in (` (is (~ (@JSON#decoded inputT))
+ ((~! </>.dictionary) (~ valC))))))
+ (do !
+ [[_ subC] (<type>.applied (<>.and (<type>.exactly .Maybe)
+ decoded))]
+ (in (` (is (~ (@JSON#decoded inputT))
+ ((~! </>.nullable) (~ subC))))))
+ (do !
+ [[_ subC] (<type>.applied (<>.and (<type>.exactly .List)
+ decoded))]
+ (in (` (is (~ (@JSON#decoded inputT))
+ ((~! </>.array) ((~! <>.some) (~ subC)))))))
+ (do !
+ [members (<type>.variant (<>.many decoded))
+ .let [last (-- (list.size members))]]
+ (in (` (is (~ (@JSON#decoded inputT))
+ (all ((~! <>.or))
+ (~+ (list#each (function (_ [tag memberC])
+ (if (n.= last tag)
+ (` (|> (~ memberC)
+ ((~! <>.after) ((~! </>.this_boolean) (~ (code.bit #1))))
+ ((~! <>.after) ((~! </>.this_number) (~ (code.frac (..tag (-- tag))))))
+ ((~! </>.array))))
+ (` (|> (~ memberC)
+ ((~! <>.after) ((~! </>.this_boolean) (~ (code.bit #0))))
+ ((~! <>.after) ((~! </>.this_number) (~ (code.frac (..tag tag)))))
+ ((~! </>.array))))))
+ (list.enumeration members))))))))
+ (do !
+ [g!decoders (<type>.tuple (<>.many decoded))]
+ (in (` (is (~ (@JSON#decoded inputT))
+ ((~! </>.array) (all ((~! <>.and)) (~+ g!decoders)))))))
+ ... Type recursion
+ (do !
+ [[selfC bodyC] (<type>.recursive decoded)
+ .let [g! (code.local "____________")]]
+ (in (` (is (~ (@JSON#decoded inputT))
+ ((~! <>.rec) (.function ((~ g!) (~ selfC))
+ (~ bodyC)))))))
+ <type>.recursive_self
+ ... Type applications
+ (do !
+ [[funcC argsC] (<type>.applied (<>.and decoded (<>.many decoded)))]
+ (in (` ((~ funcC) (~+ argsC)))))
+ ... Polymorphism
+ (do !
+ [[funcC varsC bodyC] (<type>.polymorphic decoded)]
+ (in (` (is (All ((~ g!_) (~+ varsC))
+ (-> (~+ (list#each (|>> (~) </>.Parser (`)) varsC))
+ (</>.Parser ((~ (poly.code *env* inputT)) (~+ varsC)))))
+ (function ((~ funcC) (~+ varsC))
+ (~ bodyC))))))
+ <type>.parameter
+ <type>.recursive_call
+ ... If all else fails...
+ (<>.failure (format "Cannot create JSON decoder for: " (type.format inputT)))
+ )))))
(def: .public codec
(syntax (_ [inputT <code>.any])