Skip to content

Commit

Permalink
Add miniBill codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSStewart committed May 8, 2024
1 parent 5a5d91e commit 40cc264
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/Internal/Builtin/MiniBillCodec.elm
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))
2 changes: 2 additions & 0 deletions src/NoDebug/TodoItForMe.elm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Internal.Builtin.Fuzzer
import Internal.Builtin.JsonDecoder
import Internal.Builtin.JsonEncoder
import Internal.Builtin.ListAllVariants
import Internal.Builtin.MiniBillCodec
import Internal.Builtin.Random
import Internal.Builtin.ToString
import Internal.CodeGenTodo exposing (CodeGenTodo)
Expand Down Expand Up @@ -80,6 +81,7 @@ rule generators =
, Internal.Builtin.JsonEncoder.codeGen
, Internal.Builtin.JsonDecoder.codeGen
, Internal.Builtin.Codec.codeGen
, Internal.Builtin.MiniBillCodec.codeGen
, Internal.Builtin.Fuzzer.codeGen
, Internal.Builtin.ListAllVariants.codeGen
, Internal.Builtin.ToString.codeGen
Expand Down

0 comments on commit 40cc264

Please sign in to comment.