From b5594f07e327d892c56f6215aa9288281fe53bb4 Mon Sep 17 00:00:00 2001 From: Nwokafor Choongsaeng Date: Wed, 30 Aug 2023 10:48:00 +0000 Subject: [PATCH 1/3] code changes for better scala code generation. --- src/Morphir/IR/FormatVersion.elm | 9 +++++++++ src/Morphir/IR/Value.elm | 2 +- src/Morphir/Scala/PrettyPrinter.elm | 6 +++--- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 src/Morphir/IR/FormatVersion.elm diff --git a/src/Morphir/IR/FormatVersion.elm b/src/Morphir/IR/FormatVersion.elm new file mode 100644 index 000000000..a5c252bd9 --- /dev/null +++ b/src/Morphir/IR/FormatVersion.elm @@ -0,0 +1,9 @@ +module Morphir.IR.FormatVersion exposing (..) + +import Morphir.IR.Distribution exposing (Distribution) + + +type alias VersionedDistribution = + { formatVersion : Int + , distribution : Distribution + } diff --git a/src/Morphir/IR/Value.elm b/src/Morphir/IR/Value.elm index 9cdcd5857..c457fbfb6 100644 --- a/src/Morphir/IR/Value.elm +++ b/src/Morphir/IR/Value.elm @@ -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 diff --git a/src/Morphir/Scala/PrettyPrinter.elm b/src/Morphir/Scala/PrettyPrinter.elm index b0412987b..d9ae32496 100644 --- a/src/Morphir/Scala/PrettyPrinter.elm +++ b/src/Morphir/Scala/PrettyPrinter.elm @@ -339,7 +339,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 @@ -623,7 +623,7 @@ mapPattern pattern = "Nil" HeadTailMatch headPattern tailPattern -> - mapPattern headPattern ++ " :: " ++ mapPattern tailPattern + parens (mapPattern headPattern ++ " :: " ++ mapPattern tailPattern) CommentedPattern childPattern message -> mapPattern childPattern ++ " /* " ++ message ++ " */ " @@ -643,7 +643,7 @@ mapLit lit = "'" ++ String.fromChar char ++ "'" StringLit string -> - "\"" ++ string ++ "\"" + "\"\"\"" ++ string ++ "\"\"\"" IntegerLit int -> String.fromInt int From 37f151e47b0c001a5e0797000e803016b034ad6b Mon Sep 17 00:00:00 2001 From: Nwokafor Choongsaeng Date: Wed, 30 Aug 2023 10:48:58 +0000 Subject: [PATCH 2/3] expose IR.FormatVersion --- morphir.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/morphir.json b/morphir.json index 9d37d2444..9857a7027 100644 --- a/morphir.json +++ b/morphir.json @@ -11,6 +11,7 @@ "IR.Value", "IR.Module", "IR.Package", - "IR.Distribution" + "IR.Distribution", + "IR.FormatVersion" ] } From 955f84d856f9f6cdd86fb7bba5611456a7ca88e8 Mon Sep 17 00:00:00 2001 From: Nwokafor Choongsaeng Date: Wed, 30 Aug 2023 16:51:44 +0000 Subject: [PATCH 3/3] refactored usages of Morphir.IR.Distribution.Codec to use Morphir.IR.FormatVersion.Codec --- cli/src/Morphir/Elm/CLI.elm | 2 +- cli/src/Morphir/Web/DevelopApp.elm | 2 +- cli/src/Morphir/Web/Editor.elm | 2 +- cli/src/Morphir/Web/Insight.elm | 2 +- cli/src/Morphir/Web/InsightTestApp.elm | 2 +- cli2/src/Morphir/Elm/CLI.elm | 2 +- cli2/src/Morphir/Elm/Generator.elm | 7 +- elm.json | 2 + src/Morphir/IR/Decoration/Codec.elm | 2 +- src/Morphir/IR/Distribution/Codec.elm | 50 +------------- src/Morphir/IR/FormatVersion.elm | 45 +++++++++++- src/Morphir/IR/FormatVersion/Codec.elm | 73 ++++++++++++++++++++ tests/Morphir/IR/Distribution/CodecTests.elm | 2 +- tests/Morphir/TestCasesJson.elm | 2 +- 14 files changed, 133 insertions(+), 62 deletions(-) create mode 100644 src/Morphir/IR/FormatVersion/Codec.elm diff --git a/cli/src/Morphir/Elm/CLI.elm b/cli/src/Morphir/Elm/CLI.elm index ac9545083..b30f4055b 100644 --- a/cli/src/Morphir/Elm/CLI.elm +++ b/cli/src/Morphir/Elm/CLI.elm @@ -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) diff --git a/cli/src/Morphir/Web/DevelopApp.elm b/cli/src/Morphir/Web/DevelopApp.elm index 80521be54..b5858e149 100644 --- a/cli/src/Morphir/Web/DevelopApp.elm +++ b/cli/src/Morphir/Web/DevelopApp.elm @@ -56,8 +56,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 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.Module as Module exposing (ModuleName) import Morphir.IR.Name as Name exposing (Name) import Morphir.IR.NodeId exposing (NodeID(..)) diff --git a/cli/src/Morphir/Web/Editor.elm b/cli/src/Morphir/Web/Editor.elm index 8ecd098af..53e92f61b 100644 --- a/cli/src/Morphir/Web/Editor.elm +++ b/cli/src/Morphir/Web/Editor.elm @@ -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(..)) diff --git a/cli/src/Morphir/Web/Insight.elm b/cli/src/Morphir/Web/Insight.elm index 4484b1353..38333a5f2 100644 --- a/cli/src/Morphir/Web/Insight.elm +++ b/cli/src/Morphir/Web/Insight.elm @@ -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(..)) diff --git a/cli/src/Morphir/Web/InsightTestApp.elm b/cli/src/Morphir/Web/InsightTestApp.elm index 20cb29be0..5af2c7997 100644 --- a/cli/src/Morphir/Web/InsightTestApp.elm +++ b/cli/src/Morphir/Web/InsightTestApp.elm @@ -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) diff --git a/cli2/src/Morphir/Elm/CLI.elm b/cli2/src/Morphir/Elm/CLI.elm index 3532cf91d..de9f68ff8 100644 --- a/cli2/src/Morphir/Elm/CLI.elm +++ b/cli2/src/Morphir/Elm/CLI.elm @@ -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) diff --git a/cli2/src/Morphir/Elm/Generator.elm b/cli2/src/Morphir/Elm/Generator.elm index 2083d6a46..00a52c99c 100644 --- a/cli2/src/Morphir/Elm/Generator.elm +++ b/cli2/src/Morphir/Elm/Generator.elm @@ -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) @@ -87,7 +86,7 @@ update msg () = else let ir = - IR.fromDistribution distro + distro fqnByTpeSpecsResult = targets diff --git a/elm.json b/elm.json index 536d21fd5..40bb905a9 100644 --- a/elm.json +++ b/elm.json @@ -36,6 +36,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", diff --git a/src/Morphir/IR/Decoration/Codec.elm b/src/Morphir/IR/Decoration/Codec.elm index c8372816d..faa6a3af8 100644 --- a/src/Morphir/IR/Decoration/Codec.elm +++ b/src/Morphir/IR/Decoration/Codec.elm @@ -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 diff --git a/src/Morphir/IR/Distribution/Codec.elm b/src/Morphir/IR/Distribution/Codec.elm index 84bd068e0..410670c50 100644 --- a/src/Morphir/IR/Distribution/Codec.elm +++ b/src/Morphir/IR/Distribution/Codec.elm @@ -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 -} @@ -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 diff --git a/src/Morphir/IR/FormatVersion.elm b/src/Morphir/IR/FormatVersion.elm index a5c252bd9..4f4465f26 100644 --- a/src/Morphir/IR/FormatVersion.elm +++ b/src/Morphir/IR/FormatVersion.elm @@ -1,9 +1,52 @@ -module Morphir.IR.FormatVersion exposing (..) +{- + 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 diff --git a/src/Morphir/IR/FormatVersion/Codec.elm b/src/Morphir/IR/FormatVersion/Codec.elm new file mode 100644 index 000000000..6f690f2b9 --- /dev/null +++ b/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!" + ] diff --git a/tests/Morphir/IR/Distribution/CodecTests.elm b/tests/Morphir/IR/Distribution/CodecTests.elm index 7c347a391..6dec6568c 100644 --- a/tests/Morphir/IR/Distribution/CodecTests.elm +++ b/tests/Morphir/IR/Distribution/CodecTests.elm @@ -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 diff --git a/tests/Morphir/TestCasesJson.elm b/tests/Morphir/TestCasesJson.elm index a612cc545..f55929d37 100644 --- a/tests/Morphir/TestCasesJson.elm +++ b/tests/Morphir/TestCasesJson.elm @@ -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(..))