Skip to content

Commit

Permalink
Merge pull request #1149 from AttilaMihaly/feature/sdk-functions-loca…
Browse files Browse the repository at this point in the history
…ldate

Fixing PR #1062
  • Loading branch information
AttilaMihaly authored Mar 12, 2024
2 parents fb659e7 + 8681ddd commit 0e5041e
Show file tree
Hide file tree
Showing 4 changed files with 621 additions and 12 deletions.
31 changes: 31 additions & 0 deletions src/Morphir/IR/SDK/LocalDate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
]


Expand Down
44 changes: 32 additions & 12 deletions src/Morphir/Value/Native.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)


{-| -}
Expand Down
Loading

0 comments on commit 0e5041e

Please sign in to comment.