Skip to content

Commit

Permalink
refactored usages of Morphir.IR.Distribution.Codec to use Morphir.IR.…
Browse files Browse the repository at this point in the history
…FormatVersion.Codec
  • Loading branch information
nwokafor-choongsaeng committed Aug 30, 2023
1 parent 37f151e commit 955f84d
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cli/src/Morphir/Elm/CLI.elm
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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(..))
Expand Down
2 changes: 1 addition & 1 deletion cli/src/Morphir/Web/Editor.elm
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Morphir/IR/Decoration/Codec.elm
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
45 changes: 44 additions & 1 deletion src/Morphir/IR/FormatVersion.elm
Original file line number Diff line number Diff line change
@@ -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
73 changes: 73 additions & 0 deletions src/Morphir/IR/FormatVersion/Codec.elm
Original file line number Diff line number Diff line change
@@ -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 tests/Morphir/IR/Distribution/CodecTests.elm
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 955f84d

Please sign in to comment.