diff --git a/src/Morphir/IR/SDK/LocalDate.elm b/src/Morphir/IR/SDK/LocalDate.elm index f5ae67b27..6b2b830ae 100644 --- a/src/Morphir/IR/SDK/LocalDate.elm +++ b/src/Morphir/IR/SDK/LocalDate.elm @@ -138,6 +138,37 @@ 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 + ) ] diff --git a/src/Morphir/Value/Native.elm b/src/Morphir/Value/Native.elm index 4a681f75f..2a992a067 100644 --- a/src/Morphir/Value/Native.elm +++ b/src/Morphir/Value/Native.elm @@ -462,18 +462,38 @@ 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.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 -> + Ok localDate + + 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" ] ) v) + ) {-| -} diff --git a/tests-integration/reference-model/morphir-tests.json b/tests-integration/reference-model/morphir-tests.json index 8a998ecfd..470f4c132 100644 --- a/tests-integration/reference-model/morphir-tests.json +++ b/tests-integration/reference-model/morphir-tests.json @@ -63,5 +63,528 @@ "description": "" } ] + ], + [ + [ + [ + [ + "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": "" + } + ] + ], + [ + [ + [ + [ + "morphir" + ], + [ + "reference" + ], + [ + "model" + ] + ], + [ + [ + "test", + "model" + ], + [ + "testing" + ] + ], + [ + "add", + "7" + ] + ], + [ + { + "inputs": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7 + ], + "expectedOutput": 28, + "description": "" + } + ] ] ] \ No newline at end of file diff --git a/tests/Morphir/SDK/LocalDateTests.elm b/tests/Morphir/SDK/LocalDateTests.elm index 579dd1d60..4b9025956 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 ]