2 | import Derive.Prelude
4 | %language ElabReflection
8 | data SumEncoding : Type where
23 | UntaggedValue : SumEncoding
28 | ObjectWithSingleField : SumEncoding
33 | TwoElemArray : SumEncoding
44 | TaggedObject : (tagFieldName : String)
45 | -> (contentsFieldName : String)
48 | %runElab derive "SumEncoding" [Show,Eq]
52 | defaultTaggedObject : SumEncoding
53 | defaultTaggedObject = TaggedObject "tag" "contents"
56 | record Options where
57 | constructor MkOptions
68 | replaceMissingKeysWithNull : String -> Bool
72 | unwrapRecords : Bool
76 | constructorTagModifier : String -> String
80 | fieldNameModifier : String -> String
83 | defaultOptions : Options
84 | defaultOptions = MkOptions defaultTaggedObject True (const False) True id id
87 | fieldName : Named a => Options -> a -> String
88 | fieldName o v = o.fieldNameModifier v.nameStr
91 | fieldNamePrim : Named a => Options -> a -> TTImp
92 | fieldNamePrim o v = primVal (Str $
fieldName o v)
95 | constructorTag : Named a => Options -> a -> String
96 | constructorTag o v = o.constructorTagModifier v.nameStr
99 | constructorTagPrim : Named a => Options -> a -> TTImp
100 | constructorTagPrim o v = primVal (Str $
constructorTag o v)
107 | data ArgInfo : Type where
109 | Fields : SnocList (BoundArg 2 RegularNamed) -> ArgInfo
110 | Values : SnocList (BoundArg 2 Regular) -> ArgInfo
121 | argInfo : SnocList (BoundArg 2 Regular) -> ArgInfo
122 | argInfo [<] = Const
123 | argInfo sx = maybe (Values sx) Fields $
traverse toNamed sx
126 | isConst : DCon -> Bool
127 | isConst (DC _ _ _ _ Const) = True
131 | toRegular : BoundArg n RegularNamed -> BoundArg n Regular
132 | toRegular (BA arg vars prf) = BA arg vars %search
135 | dcon : Options -> Con n vs -> DCon
137 | let xs := freshNames "x" c.arty
138 | ys := freshNames "y" c.arty
140 | ac := `(Right
~(applyCon c xs))
141 | sx := boundArgs regular c.args [xs,ys]
142 | tag := constructorTagPrim o c
143 | in DC c.name bc ac tag $
argInfo sx