Skip to content

Commit

Permalink
Merge pull request #1096 from nwokafor-choongsaeng/irjvm
Browse files Browse the repository at this point in the history
Changes for better Scala code generation
  • Loading branch information
AttilaMihaly committed Jan 30, 2024
2 parents 39ca775 + f023918 commit 15c1241
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 66 deletions.
2 changes: 1 addition & 1 deletion cli/src/Morphir/Elm/CLI.elm
Expand Up @@ -29,8 +29,8 @@ import Morphir.Elm.Target exposing (BackendOptions, decodeOptions, mapDistributi
import Morphir.File.FileMap exposing (FileMap)
import Morphir.File.FileMap.Codec exposing (encodeFileMap)
import Morphir.IR.Distribution as Distribution exposing (Distribution(..))
import Morphir.IR.Distribution.Codec as DistributionCodec
import Morphir.IR.FQName as FQName
import Morphir.IR.FormatVersion.Codec as DistributionCodec
import Morphir.IR.Package as Package exposing (PackageName)
import Morphir.IR.SDK as SDK
import Morphir.IR.Type exposing (Type)
Expand Down
2 changes: 1 addition & 1 deletion cli/src/Morphir/Web/DevelopApp.elm
Expand Up @@ -58,8 +58,8 @@ import Morphir.Correctness.Test exposing (TestCase, TestSuite)
import Morphir.IR.Decoration exposing (AllDecorationConfigAndData, DecorationData, DecorationID)
import Morphir.IR.Decoration.Codec exposing (decodeAllDecorationConfigAndData, decodeDecorationData, encodeDecorationData)
import Morphir.IR.Distribution as Distribution exposing (Distribution(..))
import Morphir.IR.Distribution.Codec as DistributionCodec
import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.FormatVersion.Codec as DistributionCodec
import Morphir.IR.Module as Module exposing (ModuleName)
import Morphir.IR.Name as Name exposing (Name)
import Morphir.IR.NodeId exposing (NodeID(..))
Expand Down
2 changes: 1 addition & 1 deletion cli/src/Morphir/Web/Editor.elm
Expand Up @@ -6,8 +6,8 @@ import Html exposing (Html)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Morphir.IR.Distribution exposing (Distribution)
import Morphir.IR.Distribution.Codec exposing (decodeVersionedDistribution)
import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.FormatVersion.Codec exposing (decodeVersionedDistribution)
import Morphir.IR.Type as Type exposing (Type)
import Morphir.IR.Type.DataCodec as DataCodec exposing (decodeData)
import Morphir.IR.Value exposing (RawValue, Value(..))
Expand Down
2 changes: 1 addition & 1 deletion cli/src/Morphir/Web/Insight.elm
Expand Up @@ -7,8 +7,8 @@ import Element.Font as Font
import Html exposing (Html)
import Json.Decode as Decode exposing (Decoder, string)
import Morphir.IR.Distribution as Distribution exposing (Distribution(..))
import Morphir.IR.Distribution.Codec as DistributionCodec
import Morphir.IR.FQName exposing (FQName)
import Morphir.IR.FormatVersion.Codec as DistributionCodec
import Morphir.IR.Name exposing (Name)
import Morphir.IR.Package as Package exposing (PackageName)
import Morphir.IR.QName as QName exposing (QName(..))
Expand Down
2 changes: 1 addition & 1 deletion cli/src/Morphir/Web/InsightTestApp.elm
Expand Up @@ -12,8 +12,8 @@ import Http
import Morphir.Correctness.Codec exposing (decodeTestSuite)
import Morphir.Correctness.Test exposing (TestCases, TestSuite)
import Morphir.IR.Distribution as Distribution exposing (Distribution(..))
import Morphir.IR.Distribution.Codec as DistributionCodec
import Morphir.IR.FQName exposing (FQName)
import Morphir.IR.FormatVersion.Codec as DistributionCodec
import Morphir.IR.Name as Name exposing (Name)
import Morphir.IR.SDK as SDK
import Morphir.IR.Type exposing (Type)
Expand Down
2 changes: 1 addition & 1 deletion cli2/src/Morphir/Elm/CLI.elm
Expand Up @@ -31,7 +31,7 @@ import Morphir.File.FileMap.Codec exposing (encodeFileMap)
import Morphir.File.FileSnapshot as FileSnapshot exposing (FileSnapshot)
import Morphir.File.FileSnapshot.Codec as FileSnapshotCodec
import Morphir.IR.Distribution as Distribution exposing (Distribution(..), lookupPackageName, lookupPackageSpecification)
import Morphir.IR.Distribution.Codec as DistroCodec
import Morphir.IR.FormatVersion.Codec as DistroCodec
import Morphir.IR.Name as Name exposing (Name)
import Morphir.IR.Package exposing (PackageName, Specification)
import Morphir.IR.Path as Path exposing (Path)
Expand Down
7 changes: 3 additions & 4 deletions cli2/src/Morphir/Elm/Generator.elm
Expand Up @@ -5,10 +5,9 @@ import Json.Decode as Decode
import Json.Encode as Encode exposing (Value)
import Morphir.Elm.Generator.API as Generator
import Morphir.Elm.Generator.ValueGenerators as ValueGenerator
import Morphir.IR as IR
import Morphir.IR.Distribution exposing (Distribution(..))
import Morphir.IR.Distribution.Codec as DistroCodec
import Morphir.IR.Distribution as IR exposing (Distribution(..))
import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.FormatVersion.Codec as DistroCodec
import Morphir.IR.Type as Type exposing (Type)
import Morphir.IR.Type.DataCodec as DataCodec
import Morphir.IR.Value exposing (RawValue)
Expand Down Expand Up @@ -87,7 +86,7 @@ update msg () =
else
let
ir =
IR.fromDistribution distro
distro

fqnByTpeSpecsResult =
targets
Expand Down
2 changes: 2 additions & 0 deletions elm.json
Expand Up @@ -35,6 +35,8 @@
"Morphir.IR.Package.Codec",
"Morphir.IR.Distribution",
"Morphir.IR.Distribution.Codec",
"Morphir.IR.FormatVersion",
"Morphir.IR.FormatVersion.Codec",
"Morphir.Elm.Frontend",
"Morphir.Elm.Frontend.Codec",
"Morphir.Elm.IncrementalResolve",
Expand Down
3 changes: 2 additions & 1 deletion morphir.json
Expand Up @@ -11,6 +11,7 @@
"IR.Value",
"IR.Module",
"IR.Package",
"IR.Distribution"
"IR.Distribution",
"IR.FormatVersion"
]
}
2 changes: 1 addition & 1 deletion src/Morphir/IR/Decoration/Codec.elm
Expand Up @@ -14,8 +14,8 @@ import Json.Decode as Decode
import Json.Encode as Encode
import Morphir.IR.Decoration exposing (AllDecorationConfigAndData, DecorationConfigAndData, DecorationData)
import Morphir.IR.Distribution exposing (Distribution)
import Morphir.IR.Distribution.Codec exposing (decodeVersionedDistribution)
import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.FormatVersion.Codec exposing (decodeVersionedDistribution)
import Morphir.IR.NodeId exposing (NodeID(..), nodeIdFromString, nodeIdToString)
import Morphir.IR.Type as Type
import Morphir.IR.Type.DataCodec as DataCodec
Expand Down
50 changes: 2 additions & 48 deletions src/Morphir/IR/Distribution/Codec.elm
Expand Up @@ -15,14 +15,14 @@
-}


module Morphir.IR.Distribution.Codec exposing (encodeVersionedDistribution, decodeVersionedDistribution, encodeDistribution, decodeDistribution)
module Morphir.IR.Distribution.Codec exposing (encodeDistribution, decodeDistribution)

{-| Codecs for types in the `Morphir.IR.Distribution` module.
# Distribution
@docs encodeVersionedDistribution, decodeVersionedDistribution, encodeDistribution, decodeDistribution
@docs encodeDistribution, decodeDistribution
-}

Expand All @@ -37,52 +37,6 @@ import Morphir.IR.Path.Codec exposing (decodePath, encodePath)
import Morphir.IR.Type.Codec exposing (decodeType, encodeType)


{-| This is a manually managed version number to be able to handle breaking changes in the IR format more explicitly.
-}
currentFormatVersion : Int
currentFormatVersion =
3


{-| Encode distribution including a version number.
-}
encodeVersionedDistribution : Distribution -> Encode.Value
encodeVersionedDistribution distro =
Encode.object
[ ( "formatVersion", Encode.int currentFormatVersion )
, ( "distribution", encodeDistribution distro )
]


{-| Decode distribution including a version number.
-}
decodeVersionedDistribution : Decode.Decoder Distribution
decodeVersionedDistribution =
Decode.oneOf
[ Decode.field "formatVersion" Decode.int
|> Decode.andThen
(\formatVersion ->
if formatVersion == currentFormatVersion then
Decode.field "distribution" decodeDistribution

else if formatVersion == 1 then
Decode.field "distribution" CodecV1.decodeDistribution

else
Decode.fail
(String.concat
[ "The IR is using format version "
, String.fromInt formatVersion
, " but the latest format version is "
, String.fromInt currentFormatVersion
, ". Please regenerate it!"
]
)
)
, Decode.fail "The IR is in an old format that doesn't have a format version on it. Please regenerate it!"
]


{-| Encode Distribution.
-}
encodeDistribution : Distribution -> Encode.Value
Expand Down
52 changes: 52 additions & 0 deletions src/Morphir/IR/FormatVersion.elm
@@ -0,0 +1,52 @@
{-
Copyright 2020 Morgan Stanley
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}


module Morphir.IR.FormatVersion exposing
( VersionedDistribution
, currentFormatVersion
)

{-| This module provides a wrapper for specifying a version of a Distribution
# Type Expression
@docs VersionedDistribution
# VersionedDistribution
@docs currentFormatVersion
-}

import Morphir.IR.Distribution exposing (Distribution)


{-| A wrapper associates a distribution with a specific version.
-}
type alias VersionedDistribution =
{ formatVersion : Int
, distribution : Distribution
}


{-| This is a manually managed version number to be able to handle breaking changes in the IR format more explicitly.
-}
currentFormatVersion : Int
currentFormatVersion =
3
73 changes: 73 additions & 0 deletions src/Morphir/IR/FormatVersion/Codec.elm
@@ -0,0 +1,73 @@
{-
Copyright 2020 Morgan Stanley
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}


module Morphir.IR.FormatVersion.Codec exposing (encodeVersionedDistribution, decodeVersionedDistribution)

{-| Codecs provided for encoding and decoding a versioned distribution.
# VersionedDistribution
@docs encodeVersionedDistribution, decodeVersionedDistribution
-}

import Json.Decode as Decode
import Json.Encode as Encode
import Morphir.IR.Distribution exposing (Distribution)
import Morphir.IR.Distribution.Codec exposing (decodeDistribution, encodeDistribution)
import Morphir.IR.Distribution.CodecV1 as CodecV1
import Morphir.IR.FormatVersion exposing (currentFormatVersion)


{-| Encode distribution including a version number.
-}
encodeVersionedDistribution : Distribution -> Encode.Value
encodeVersionedDistribution distro =
Encode.object
[ ( "formatVersion", Encode.int currentFormatVersion )
, ( "distribution", encodeDistribution distro )
]


{-| Decode distribution including a version number.
-}
decodeVersionedDistribution : Decode.Decoder Distribution
decodeVersionedDistribution =
Decode.oneOf
[ Decode.field "formatVersion" Decode.int
|> Decode.andThen
(\formatVersion ->
if formatVersion == currentFormatVersion then
Decode.field "distribution" decodeDistribution

else if formatVersion == 1 then
Decode.field "distribution" CodecV1.decodeDistribution

else
Decode.fail
(String.concat
[ "The IR is using format version "
, String.fromInt formatVersion
, " but the latest format version is "
, String.fromInt currentFormatVersion
, ". Please regenerate it!"
]
)
)
, Decode.fail "The IR is in an old format that doesn't have a format version on it. Please regenerate it!"
]
2 changes: 1 addition & 1 deletion src/Morphir/IR/Value.elm
Expand Up @@ -374,7 +374,7 @@ typeAndValueToDefinition valueType value =
case ( body, bodyType ) of
( Lambda va (AsPattern _ (WildcardPattern _) argName) lambdaBody, Type.Function _ argType returnType ) ->
liftLambdaArguments
(args ++ [ ( argName, va, argType ) ])
(List.append args [ ( argName, va, argType ) ])
returnType
lambdaBody

Expand Down
6 changes: 3 additions & 3 deletions src/Morphir/Scala/PrettyPrinter.elm
Expand Up @@ -357,7 +357,7 @@ mapArgDecl opt argDecl =
Nothing ->
empty
in
mapModifiers argDecl.modifiers ++ argDecl.name ++ ": " ++ mapType opt argDecl.tpe ++ defaultValueDoc
mapModifiers argDecl.modifiers ++ prefixKeyword argDecl.name ++ ": " ++ mapType opt argDecl.tpe ++ defaultValueDoc


mapType : Options -> Type -> Doc
Expand Down Expand Up @@ -646,7 +646,7 @@ mapPattern pattern =
"Nil"

HeadTailMatch headPattern tailPattern ->
mapPattern headPattern ++ " :: " ++ mapPattern tailPattern
parens (mapPattern headPattern ++ " :: " ++ mapPattern tailPattern)

CommentedPattern childPattern message ->
mapPattern childPattern ++ " /* " ++ message ++ " */ "
Expand All @@ -666,7 +666,7 @@ mapLit lit =
"'" ++ String.fromChar char ++ "'"

StringLit string ->
"\"" ++ string ++ "\""
"\"\"\"" ++ string ++ "\"\"\""

IntegerLit int ->
String.fromInt int
Expand Down
2 changes: 1 addition & 1 deletion tests/Morphir/IR/Distribution/CodecTests.elm
Expand Up @@ -7,7 +7,7 @@ import Json.Encode as Encode
import Morphir.Compiler.Codec as CompilerCodec
import Morphir.IR.AccessControlled exposing (private, public)
import Morphir.IR.Distribution as Distribution exposing (Distribution)
import Morphir.IR.Distribution.Codec as DistributionCodec
import Morphir.IR.FormatVersion.Codec as DistributionCodec
import Morphir.IR.Documented exposing (Documented)
import Morphir.IR.FQName exposing (fQName)
import Morphir.IR.Package as Package
Expand Down
2 changes: 1 addition & 1 deletion tests/Morphir/TestCasesJson.elm
Expand Up @@ -6,7 +6,7 @@ import Json.Decode as Decode
import Morphir.Correctness.Codec exposing (decodeTestSuite, encodeTestSuite)
import Morphir.Correctness.Test exposing (TestCase, TestSuite)
import Morphir.IR.Distribution exposing (Distribution(..))
import Morphir.IR.Distribution.Codec as DistributionCodec
import Morphir.IR.FormatVersion.Codec as DistributionCodec
import Morphir.IR.Literal exposing (Literal(..))
import Morphir.IR.Package as Package
import Morphir.IR.Value exposing (RawValue, Value(..))
Expand Down

0 comments on commit 15c1241

Please sign in to comment.