From dd505e9d5c528388e80ca5a2cd3d08c8001ed634 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 5 Sep 2017 18:38:26 -0400 Subject: - Added polytypic JSON codec support for unit-types. --- stdlib/source/lux/macro/poly/json.lux | 22 ++++++- stdlib/test/test/lux/data/format/json.lux | 95 +++++++++++++++++++------------ stdlib/test/test/lux/time/date.lux | 2 +- stdlib/test/test/lux/time/duration.lux | 3 +- stdlib/test/test/lux/time/instant.lux | 9 +-- 5 files changed, 86 insertions(+), 45 deletions(-) diff --git a/stdlib/source/lux/macro/poly/json.lux b/stdlib/source/lux/macro/poly/json.lux index 2c87603d3..379be9f49 100644 --- a/stdlib/source/lux/macro/poly/json.lux +++ b/stdlib/source/lux/macro/poly/json.lux @@ -27,6 +27,7 @@ [code] [poly #+ poly:]) [type] + (type [unit]) )) (def: #hidden _map_ @@ -49,7 +50,7 @@ (struct: #hidden _ (Codec JSON Nat) (def: (encode input) - (let [high (|> input (bit;and high-mask) (bit;unsigned-shift-right +32)) + (let [high (|> input (bit;and high-mask) (bit;shift-right +32)) low (bit;and low-mask input)] (#..;Array (vector (|> high nat-to-int int-to-frac #..;Number) (|> low nat-to-int int-to-frac #..;Number))))) @@ -74,6 +75,13 @@ #;None #..;Null (#;Some value) (writer value)))) +(struct: #hidden (Codec carrier) + (All [unit] (-> unit (Codec JSON (unit;Qty unit)))) + (def: encode + (|>. unit;out (:: Codec encode))) + (def: decode + (|>. (:: Codec decode) (:: R;Functor map (unit;in carrier))))) + (poly: #hidden Codec//encode (with-expansions [ (do-template [ ] @@ -92,7 +100,7 @@ [(do @ [_ (poly;this )] (wrap (` (: (~ (@JSON//encode inputT)) - (|>. (:: (~' encode)) ..;string)))))] + (|>. (:: (~' encode)) #..;String)))))] [du;Duration du;Codec] [i;Instant i;Codec] @@ -108,6 +116,11 @@ ($_ p;either