From c0315174d8272e4b93099fe24a8ffa25527295d9 Mon Sep 17 00:00:00 2001 From: Osborne Yaw Sapaty Date: Thu, 8 Jun 2023 08:38:36 +0000 Subject: [PATCH 1/4] implementation of missing SDK functions for LocalDate --- src/Morphir/IR/SDK/LocalDate.elm | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Morphir/IR/SDK/LocalDate.elm b/src/Morphir/IR/SDK/LocalDate.elm index d3a335105..3401817bf 100644 --- a/src/Morphir/IR/SDK/LocalDate.elm +++ b/src/Morphir/IR/SDK/LocalDate.elm @@ -81,4 +81,35 @@ nativeFunctions = , ( "toISOString" , Native.eval1 LocalDate.toISOString Native.decodeLocalDate (Native.encodeLiteral Literal.StringLiteral) ) + , ( "fromParts" + , Native.eval3 LocalDate.fromParts + (Native.decodeLiteral Native.intLiteral) + (Native.decodeLiteral Native.intLiteral) + (Native.decodeLiteral Native.intLiteral) + (Native.encodeMaybe Native.encodeLocalDate) + ) + , ( "diffInDays" + , Native.eval2 LocalDate.diffInDays Native.decodeLocalDate Native.decodeLocalDate (Native.encodeLiteral Literal.intLiteral) + ) + , ( "diffInWeeks" + , Native.eval2 LocalDate.diffInWeeks Native.decodeLocalDate Native.decodeLocalDate (Native.encodeLiteral Literal.intLiteral) + ) + , ( "diffInMonths" + , Native.eval2 LocalDate.diffInMonths Native.decodeLocalDate Native.decodeLocalDate (Native.encodeLiteral Literal.intLiteral) + ) + , ( "diffInYears" + , Native.eval2 LocalDate.diffInYears Native.decodeLocalDate Native.decodeLocalDate (Native.encodeLiteral Literal.intLiteral) + ) + , ( "addDays" + , Native.eval2 LocalDate.addDays (Native.decodeLiteral Native.intLiteral) Native.decodeLocalDate Native.encodeLocalDate + ) + , ( "addWeeks" + , Native.eval2 LocalDate.addWeeks (Native.decodeLiteral Native.intLiteral) Native.decodeLocalDate Native.encodeLocalDate + ) + , ( "addMonths" + , Native.eval2 LocalDate.addMonths (Native.decodeLiteral Native.intLiteral) Native.decodeLocalDate Native.encodeLocalDate + ) + , ( "addYears" + , Native.eval2 LocalDate.addYears (Native.decodeLiteral Native.intLiteral) Native.decodeLocalDate Native.encodeLocalDate + ) ] From 86bc0bd10ce9eeecbed51f8892eff68cb5d083d5 Mon Sep 17 00:00:00 2001 From: Osborne Yaw Sapaty Date: Thu, 15 Jun 2023 09:53:02 +0000 Subject: [PATCH 2/4] added missing tests for localdate and code fix for decodeLocalDate --- src/Morphir/Value/Native.elm | 28 ++++++++++++---------- tests/Morphir/SDK/LocalDateTests.elm | 35 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Morphir/Value/Native.elm b/src/Morphir/Value/Native.elm index d5767e58d..2bee3bf3b 100644 --- a/src/Morphir/Value/Native.elm +++ b/src/Morphir/Value/Native.elm @@ -420,18 +420,22 @@ encodeLocalDate localDate = {-| -} decodeLocalDate : Decoder LocalDate -decodeLocalDate _ value = - case value of - Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "from", "i", "s", "o" ] )) (Value.Literal () (StringLiteral str)) -> - case LocalDate.fromISO str of - Just localDate -> - Ok localDate - - Nothing -> - Err <| ErrorWhileEvaluatingDerivedType ("Invalid ISO format: " ++ str) - - _ -> - Err (ExpectedDerivedType ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "local", "date" ] ) value) +decodeLocalDate e value = + e value + |> Result.andThen + (\v -> + case v of + Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "from", "i", "s", "o" ] )) (Value.Literal () (StringLiteral str)) -> + case LocalDate.fromISO str of + Just localDate -> + Ok localDate + + Nothing -> + Err <| ErrorWhileEvaluatingDerivedType ("Invalid ISO format: " ++ str) + + _ -> + Err (ExpectedDerivedType ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "local", "date" ] ) value) + ) {-| -} diff --git a/tests/Morphir/SDK/LocalDateTests.elm b/tests/Morphir/SDK/LocalDateTests.elm index 220c37fd0..7d5e7c65b 100644 --- a/tests/Morphir/SDK/LocalDateTests.elm +++ b/tests/Morphir/SDK/LocalDateTests.elm @@ -66,6 +66,41 @@ mathTests = Date.fromCalendarDate 2020 Feb 1 |> LocalDate.addYears -1 |> Expect.equal (Date.fromCalendarDate 2019 Feb 1) + , test "fromISO string to localDate" <| + \_ -> + LocalDate.fromISO "2023-06-13" + |> Maybe.withDefault (Date.fromCalendarDate 2019 Feb 1) + |> Expect.equal (Date.fromCalendarDate 2023 Jun 13) + , test "toISOString" <| + \_ -> + Date.fromCalendarDate 2023 Jun 13 + |> LocalDate.toISOString + |> Expect.equal "2023-06-13" + , test "fromParts" <| + \_ -> + LocalDate.fromParts 2023 6 9 + |> Maybe.withDefault (Date.fromCalendarDate 2019 Feb 1) + |> Expect.equal (Date.fromCalendarDate 2023 Jun 9) + , test "diffInDays" <| + \_ -> + Date.fromCalendarDate 2023 Jun 19 + |> LocalDate.diffInDays (Date.fromCalendarDate 2023 Jun 9) + |> Expect.equal 10 + , test "diffInWeeks" <| + \_ -> + Date.fromCalendarDate 2023 Jun 19 + |> LocalDate.diffInWeeks (Date.fromCalendarDate 2023 Jun 9) + |> Expect.equal 1 + , test "diffInMonths" <| + \_ -> + Date.fromCalendarDate 2023 Jun 19 + |> LocalDate.diffInMonths (Date.fromCalendarDate 2023 Jun 9) + |> Expect.equal 0 + , test "diffInYears" <| + \_ -> + Date.fromCalendarDate 2024 Jun 19 + |> LocalDate.diffInYears (Date.fromCalendarDate 2023 Jun 9) + |> Expect.equal 1 ] From e25fb21cde9beaa74c11f65d0272a0258e95f766 Mon Sep 17 00:00:00 2001 From: Osborne Yaw Sapaty Date: Tue, 20 Jun 2023 12:56:20 +0000 Subject: [PATCH 3/4] morphir testing added for localDate --- cli/src/Morphir/Elm/CLI.elm | 10 +- src/Morphir/Value/Native.elm | 18 +- .../reference-model/morphir-tests.json | 480 ++++++++++++++++++ 3 files changed, 505 insertions(+), 3 deletions(-) diff --git a/cli/src/Morphir/Elm/CLI.elm b/cli/src/Morphir/Elm/CLI.elm index ac9545083..3bfd6f1f7 100644 --- a/cli/src/Morphir/Elm/CLI.elm +++ b/cli/src/Morphir/Elm/CLI.elm @@ -189,8 +189,14 @@ update msg model = let resultIR : Result Decode.Error Distribution resultIR = - distributionJson - |> Decode.decodeValue DistributionCodec.decodeVersionedDistribution + case distributionJson |> Decode.decodeValue DistributionCodec.decodeVersionedDistribution of + Ok packageDist -> + case packageDist of + Library packageName dependencies packageDef -> + Ok (Library packageName (Dict.union Frontend.defaultDependencies dependencies) packageDef) + + Err err -> + Err err in case resultIR of Ok ir -> diff --git a/src/Morphir/Value/Native.elm b/src/Morphir/Value/Native.elm index 2bee3bf3b..d0d1deddd 100644 --- a/src/Morphir/Value/Native.elm +++ b/src/Morphir/Value/Native.elm @@ -425,6 +425,14 @@ decodeLocalDate e value = |> Result.andThen (\v -> case v of + Value.Apply () (Value.Constructor _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "maybe" ] ], [ "just" ] )) (Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "from", "i", "s", "o" ] )) (Value.Literal () (StringLiteral str))) -> + case LocalDate.fromISO str of + Just localDate -> + Ok localDate + + Nothing -> + Err <| ErrorWhileEvaluatingDerivedType ("Invalid ISO format: " ++ str) + Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "from", "i", "s", "o" ] )) (Value.Literal () (StringLiteral str)) -> case LocalDate.fromISO str of Just localDate -> @@ -433,8 +441,16 @@ decodeLocalDate e value = Nothing -> Err <| ErrorWhileEvaluatingDerivedType ("Invalid ISO format: " ++ str) + Value.Literal () (StringLiteral str) -> + case LocalDate.fromISO str of + Just localDate -> + Ok localDate + + Nothing -> + Err <| ErrorWhileEvaluatingDerivedType ("Invalid ISO format: " ++ str) + _ -> - Err (ExpectedDerivedType ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "local", "date" ] ) value) + Err (ExpectedDerivedType ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "local", "date" ] ], [ "local", "date" ] ) v) ) diff --git a/tests-integration/reference-model/morphir-tests.json b/tests-integration/reference-model/morphir-tests.json index 27cc404ec..23ea05f45 100644 --- a/tests-integration/reference-model/morphir-tests.json +++ b/tests-integration/reference-model/morphir-tests.json @@ -214,6 +214,486 @@ } ] ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "add", + "days" + ] + ], + [ + { + "inputs": [ + 1, + "2023-06-19" + ], + "expectedOutput": "2023-06-20", + "description": "" + }, + { + "inputs": [ + -1, + "2023-06-20" + ], + "expectedOutput": "2023-06-19", + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "add", + "months" + ] + ], + [ + { + "inputs": [ + 1, + "2023-06-20" + ], + "expectedOutput": "2023-07-20", + "description": "" + }, + { + "inputs": [ + -1, + "2023-06-20" + ], + "expectedOutput": "2023-05-20", + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "add", + "weeks" + ] + ], + [ + { + "inputs": [ + 2, + "2023-06-20" + ], + "expectedOutput": "2023-07-04", + "description": "" + }, + { + "inputs": [ + -2, + "2023-06-20" + ], + "expectedOutput": "2023-06-06", + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "add", + "years" + ] + ], + [ + { + "inputs": [ + 2, + "2023-06-20" + ], + "expectedOutput": "2025-06-20", + "description": "" + }, + { + "inputs": [ + -2, + "2023-06-20" + ], + "expectedOutput": "2021-06-20", + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "diff", + "in", + "days" + ] + ], + [ + { + "inputs": [ + "2023-06-20", + "2023-06-30" + ], + "expectedOutput": 10, + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "diff", + "in", + "months" + ] + ], + [ + { + "inputs": [ + "2023-06-20", + "2023-11-20" + ], + "expectedOutput": 5, + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "diff", + "in", + "weeks" + ] + ], + [ + { + "inputs": [ + "2023-06-20", + "2023-07-04" + ], + "expectedOutput": 2, + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "diff", + "in", + "years" + ] + ], + [ + { + "inputs": [ + "2023-06-20", + "2024-06-20" + ], + "expectedOutput": 1, + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "from", + "i", + "s", + "o" + ] + ], + [ + { + "inputs": [ + "2023-06-20" + ], + "expectedOutput": "2023-06-20", + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "from", + "parts" + ] + ], + [ + { + "inputs": [ + 2023, + 6, + 20 + ], + "expectedOutput": "2023-06-20", + "description": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "s", + "d", + "k" + ], + [ + "local", + "date" + ] + ], + [ + "to", + "i", + "s", + "o", + "string" + ] + ], + [ + { + "inputs": [ + "2023-06-20" + ], + "expectedOutput": "2023-06-20", + "description": "" + } + ] + ], [ [ [ From 12b8b8dda1e92d279f6b397cfa720a23f071a57b Mon Sep 17 00:00:00 2001 From: Osborne Yaw Sapaty Date: Tue, 20 Jun 2023 13:16:05 +0000 Subject: [PATCH 4/4] trigger github action