-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a5d91e
commit 40cc264
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
module Internal.Builtin.MiniBillCodec exposing (codeGen) | ||
|
||
import CodeGenerator exposing (CodeGenerator) | ||
import Elm.CodeGen as CG | ||
import ResolvedType | ||
import String.Extra | ||
import TypePattern exposing (TypePattern(..)) | ||
|
||
|
||
val = | ||
CG.fqVal [ "Codec" ] | ||
|
||
|
||
fn1 name arg = | ||
CG.apply [ CG.fqVal [ "Codec" ] name, arg ] | ||
|
||
|
||
codeGen : CodeGenerator | ||
codeGen = | ||
CodeGenerator.define | ||
{ id = "miniBill/elm-codec/Codec" | ||
, dependency = "miniBill/elm-codec" | ||
, typePattern = Typed [ "Codec" ] "Codec" [ GenericType "e", Target ] | ||
, makeName = \name -> String.Extra.decapitalize name ++ "Codec" | ||
} | ||
[ CodeGenerator.int (val "int") | ||
, CodeGenerator.float (val "float") | ||
, CodeGenerator.string (val "string") | ||
, CodeGenerator.list (fn1 "list") | ||
, CodeGenerator.maybe (fn1 "maybe") | ||
, CodeGenerator.dict (\key value -> CG.apply [ val "dict", key, value ]) | ||
, CodeGenerator.unit (val "unit") | ||
, CodeGenerator.tuple (\arg1 arg2 -> CG.apply [ val "tuple", arg1, arg2 ]) | ||
, CodeGenerator.triple (\arg1 arg2 arg3 -> CG.apply [ val "tuple", arg1, arg2, arg3 ]) | ||
, CodeGenerator.customType | ||
(\ctors exprs -> | ||
CG.pipe | ||
(CG.apply | ||
[ val "custom" | ||
, CG.lambda (List.map (\( ctorRef, _ ) -> CG.varPattern (String.Extra.decapitalize ctorRef.name ++ "Encoder")) ctors ++ [ CG.varPattern "value" ]) | ||
(ctors | ||
|> List.map | ||
(\( ctorRef, arguments ) -> | ||
( CG.fqNamedPattern ctorRef.modulePath ctorRef.name (thingsToPatterns arguments) | ||
, CG.apply (CG.val (String.Extra.decapitalize ctorRef.name ++ "Encoder") :: thingsToValues arguments) | ||
) | ||
) | ||
|> CG.caseExpr (CG.val "value") | ||
) | ||
] | ||
) | ||
(List.map Tuple.second exprs ++ [ val "buildCustom" ]) | ||
) | ||
, CodeGenerator.combiner | ||
(\t fn exprs -> | ||
case t of | ||
ResolvedType.Opaque _ args -> | ||
Just <| CG.apply ([ val ("variant" ++ String.fromInt (List.length args)), fn ] ++ exprs) | ||
|
||
ResolvedType.AnonymousRecord _ fields -> | ||
CG.pipe | ||
(CG.apply [ val "object", fn ]) | ||
(List.map2 | ||
(\( field, _ ) expr -> | ||
CG.apply | ||
[ val "field" | ||
, CG.string field | ||
, CG.accessFun ("." ++ field) | ||
, expr | ||
] | ||
) | ||
fields | ||
exprs | ||
++ [ val "buildObject" ] | ||
) | ||
|> Just | ||
|
||
ResolvedType.TypeAlias _ _ (ResolvedType.AnonymousRecord _ fields) -> | ||
CG.pipe (CG.apply [ val "object", fn ]) | ||
(List.map2 | ||
(\( field, _ ) expr -> | ||
CG.apply | ||
[ val "field" | ||
, CG.string field | ||
, CG.accessFun ("." ++ field) | ||
, expr | ||
] | ||
) | ||
fields | ||
exprs | ||
++ [ val "buildObject" ] | ||
) | ||
|> Just | ||
|
||
_ -> | ||
Nothing | ||
) | ||
, CodeGenerator.lambdaBreaker | ||
(\expr -> | ||
fn1 "lazy" (CG.lambda [ CG.unitPattern ] expr) | ||
) | ||
] | ||
|
||
|
||
thingsToPatterns : List a -> List CG.Pattern | ||
thingsToPatterns = | ||
List.indexedMap (\i _ -> CG.varPattern ("arg" ++ String.fromInt i)) | ||
|
||
|
||
thingsToValues : List a -> List CG.Expression | ||
thingsToValues = | ||
List.indexedMap (\i _ -> CG.val ("arg" ++ String.fromInt i)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters