Skip to content

Commit

Permalink
Merge pull request #9 from reesh-a/master
Browse files Browse the repository at this point in the history
First draft of elm encoder backend
  • Loading branch information
reesh-a authored Mar 11, 2020
2 parents dbcc283 + 4f2de2b commit 77bd8cc
Show file tree
Hide file tree
Showing 10 changed files with 846 additions and 13 deletions.
25 changes: 21 additions & 4 deletions cli/morphir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ const util = require('util')
const fs = require('fs')
const readdir = util.promisify(fs.readdir)
const readFile = util.promisify(fs.readFile)
const worker = require('./Morphir.Elm.CLI').Elm.Morphir.Elm.CLI.init()
const packageDefWorker = require('./Morphir.Elm.CLI').Elm.Morphir.Elm.CLI.init()
const elmEncoderWorker = require('./Morphir.Elm.EncodersCLI').Elm.Morphir.Elm.EncodersCLI.init()


worker.ports.decodeError.subscribe(res => {
packageDefWorker.ports.decodeError.subscribe(res => {
console.error(res)
})

worker.ports.packageDefinitionFromSourceResult.subscribe(res => {
packageDefWorker.ports.packageDefinitionFromSourceResult.subscribe(res => {
console.log(JSON.stringify(res))
})

elmEncoderWorker.ports.elmEncoderBackend.subscribe(res => {
console.log(res)
})


const packageInfo = {
name: "morphir",
Expand All @@ -23,10 +28,11 @@ const packageInfo = {


const sourceDir = "../src"
const testDir = "../tests/Morphir/Codec/Tests"

readElmSources(sourceDir)
.then((sourceFiles) => {
worker.ports.packageDefinitionFromSource.send([packageInfo, sourceFiles])
packageDefWorker.ports.packageDefinitionFromSource.send([packageInfo, sourceFiles])
sourceFiles.forEach(element => {
console.log(element.path)
});
Expand All @@ -35,6 +41,17 @@ readElmSources(sourceDir)
console.error(err)
})

readElmSources(testDir)
.then((sourceFiles) => {
console.log ("Generating elm encoders for following:")
sourceFiles.forEach(element => {
console.log(element.path)
});
console.log ("")
elmEncoderWorker.ports.elmFrontEnd.send([packageInfo, sourceFiles])
console.log ("")
})



async function readElmSources(dir) {
Expand Down
131 changes: 131 additions & 0 deletions cli/src/Morphir/Elm/EncodersCLI.elm
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" ] ]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 77bd8cc

Please sign in to comment.