-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First draft of elm encoder backend #9
Changes from 3 commits
46c8fc5
fe75fc1
a134670
6f65be9
4f2de2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
port module Morphir.Elm.EncodersCLI exposing (..) | ||
|
||
import Dict as Dict exposing (..) | ||
import Elm.Syntax.Declaration as S exposing (..) | ||
import Elm.Syntax.Exposing exposing (..) | ||
import Elm.Syntax.File exposing (..) | ||
import Elm.Syntax.Module exposing (..) | ||
import Elm.Syntax.Node exposing (..) | ||
import Elm.Syntax.Range exposing (..) | ||
import Elm.Writer exposing (..) | ||
import Json.Decode as Decode exposing (..) | ||
import Morphir.Elm.Backend.Codec.Gen exposing (..) | ||
import Morphir.Elm.Frontend exposing (..) | ||
import Morphir.IR.AccessControlled exposing (..) | ||
import Morphir.IR.Advanced.Module as Advanced exposing (..) | ||
import Morphir.IR.Name exposing (..) | ||
import Set exposing (..) | ||
|
||
|
||
main = | ||
Platform.worker | ||
{ init = init, update = update, subscriptions = subscriptions } | ||
|
||
|
||
type Msg | ||
= PackageDefinitionFromSource ( Decode.Value, List SourceFile ) | ||
|
||
|
||
type alias Model = | ||
() | ||
|
||
|
||
init : () -> ( Model, Cmd Msg ) | ||
init _ = | ||
( (), Cmd.none ) | ||
|
||
|
||
update : Msg -> Model -> ( Model, Cmd Msg ) | ||
update (PackageDefinitionFromSource ( _, sourceFiles )) model = | ||
( model, genEncodersFile sourceFiles |> elmEncoderBackend ) | ||
|
||
|
||
subscriptions : Model -> Sub Msg | ||
subscriptions _ = | ||
elmFrontEnd PackageDefinitionFromSource | ||
|
||
|
||
port elmEncoderBackend : String -> Cmd msg | ||
|
||
|
||
port elmFrontEnd : (( Decode.Value, List SourceFile ) -> msg) -> Sub msg | ||
|
||
|
||
genEncodersFile : List SourceFile -> String | ||
genEncodersFile sources = | ||
let | ||
file = | ||
{ moduleDefinition = | ||
emptyRangeNode <| | ||
NormalModule | ||
{ moduleName = emptyRangeNode [ "AEncoders" ] | ||
, exposingList = emptyRangeNode (All emptyRange) | ||
} | ||
, imports = [] | ||
, declarations = | ||
case encoderDeclarations sources of | ||
Ok maybList -> | ||
case maybList of | ||
Just list -> | ||
list | ||
|
||
Nothing -> | ||
[] | ||
|
||
Err _ -> | ||
[] | ||
, comments = [] | ||
} | ||
in | ||
writeFile file |> write | ||
|
||
|
||
encoderDeclarations : List SourceFile -> Result Errors (Maybe (List (Node S.Declaration))) | ||
encoderDeclarations sourceFiles = | ||
packageDefinitionFromSource emptyPackageInfo sourceFiles | ||
|> Result.map .modules | ||
|> Result.map (Dict.get [ [ "a" ] ]) | ||
|> Result.map (Maybe.map getEncodersFromModuleDef) | ||
|
||
|
||
getEncodersFromModuleDef : AccessControlled (Advanced.Definition SourceLocation) -> List (Node S.Declaration) | ||
getEncodersFromModuleDef accessCtrlModuleDef = | ||
case accessCtrlModuleDef of | ||
Public { types, values } -> | ||
Dict.toList types | ||
|> List.map | ||
(\typeNameAndDef -> | ||
typeDefToEncoder emptySourceLocation | ||
(Tuple.first typeNameAndDef) | ||
(Tuple.second typeNameAndDef) | ||
) | ||
|> List.map (Node emptyRange) | ||
|
||
_ -> | ||
[] | ||
|
||
|
||
emptySourceLocation : SourceLocation | ||
emptySourceLocation = | ||
{ source = | ||
{ path = "" | ||
, content = "" | ||
} | ||
, range = | ||
{ start = | ||
{ row = 0 | ||
, column = 0 | ||
} | ||
, end = | ||
{ row = 0 | ||
, column = 0 | ||
} | ||
} | ||
} | ||
|
||
|
||
emptyPackageInfo : PackageInfo | ||
emptyPackageInfo = | ||
{ name = [] | ||
, exposedModules = Set.fromList [ [ fromString "a" ] ] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
"description": "Elm bindings for Morphir", | ||
"scripts": { | ||
"test": "elm-test", | ||
"make-cli": "cd cli && elm make src/Morphir/Elm/CLI.elm --output Morphir.Elm.CLI.js --optimize" | ||
"make-cli": "cd cli && elm make src/Morphir/Elm/CLI.elm --output Morphir.Elm.CLI.js --optimize && elm make src/Morphir/Elm/EncodersCLI.elm --output Morphir.Elm.EncodersCLI.js --optimize" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use a better build tool such as Gulp. Created issue for it: #10 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no experience with JS build tools. Would be fun to know what's out there since I have always been intrigued by the choices available in the JS ecosystem. |
||
}, | ||
"repository": { | ||
"type": "git", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Longer term I think we should just have one worker and use it as a facade to consolidate all the functionality but I'm guessing this was meant to be a temporary solution anyway so OK for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good suggestion. I did think of that but like you said, for the purposes of getting something out quicker I fell short of consolidating it.