Skip to content

Commit

Permalink
UUID fix (#1164)
Browse files Browse the repository at this point in the history
* Fix issues with UUID

* Fix issue in registering UUID in the SDK
  • Loading branch information
DamianReeves authored May 18, 2024
1 parent 4f31014 commit ae62019
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 47 deletions.
6 changes: 6 additions & 0 deletions cli/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"TSFoster/elm-uuid": "4.2.0",
"chain-partners/elm-bignum": "1.0.1",
"cuducos/elm-format-number": "9.0.1",
"dillonkearns/elm-markdown": "7.0.1",
Expand Down Expand Up @@ -37,10 +38,15 @@
"stil4m/elm-syntax": "7.2.9"
},
"indirect": {
"TSFoster/elm-bytes-extra": "1.3.0",
"TSFoster/elm-md5": "2.0.1",
"TSFoster/elm-sha1": "2.1.1",
"avh4/elm-color": "1.0.0",
"avh4/elm-fifo": "1.0.4",
"danfishgold/base64-bytes": "1.1.0",
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/random": "1.0.0",
"elm/virtual-dom": "1.0.3",
"elm-community/intdict": "3.0.0",
"miniBill/elm-unicode": "1.0.3",
Expand Down
7 changes: 6 additions & 1 deletion cli2/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"TSFoster/elm-uuid": "4.2.0",
"chain-partners/elm-bignum": "1.0.1",
"cuducos/elm-format-number": "8.1.4",
"dillonkearns/elm-markdown": "7.0.0",
Expand All @@ -20,8 +21,8 @@
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"elm/svg": "1.0.1",
"elm/url": "1.0.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm-community/array-extra": "2.3.0",
"elm-community/graph": "6.0.0",
"elm-community/list-extra": "8.2.3",
Expand All @@ -35,8 +36,12 @@
"stil4m/elm-syntax": "7.2.0"
},
"indirect": {
"TSFoster/elm-bytes-extra": "1.3.0",
"TSFoster/elm-md5": "2.0.1",
"TSFoster/elm-sha1": "2.1.1",
"avh4/elm-color": "1.0.0",
"avh4/elm-fifo": "1.0.4",
"danfishgold/base64-bytes": "1.1.0",
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/virtual-dom": "1.0.2",
Expand Down
5 changes: 3 additions & 2 deletions src/Morphir/IR/SDK.elm
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import Morphir.IR.SDK.Set as Set
import Morphir.IR.SDK.StatefulApp as StatefulApp
import Morphir.IR.SDK.String as String
import Morphir.IR.SDK.Tuple as Tuple
import Morphir.Value.Native as Native
import Morphir.IR.SDK.UUID as UUID
import Morphir.Value.Native as Native


packageName : PackageName
Expand Down Expand Up @@ -76,7 +76,7 @@ packageSpec =
, ( [ [ "number" ] ], Number.moduleSpec )
, ( [ [ "key" ] ], Key.moduleSpec )
, ( [ [ "aggregate" ] ], Aggregate.moduleSpec )
, ( [ [ "uuid" ] ], UUID.moduleSpec)
, ( [ [ "u", "u", "i", "d" ] ], UUID.moduleSpec )
]
}

Expand Down Expand Up @@ -111,4 +111,5 @@ nativeFunctions =
, moduleFunctions "Decimal" Decimal.nativeFunctions
, moduleFunctions "Aggregate" Aggregate.nativeFunctions
, moduleFunctions "LocalDate" LocalDate.nativeFunctions
, moduleFunctions "UUID" UUID.nativeFunctions
]
57 changes: 54 additions & 3 deletions src/Morphir/IR/SDK/UUID.elm
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import Morphir.IR.SDK.Basics exposing (intType, orderType, boolType)
import Morphir.Value.Native as Native
import Morphir.Value.Native exposing (eval1, eval2)
import Morphir.SDK.UUID as UUID
import Morphir.Value.Error as ValueError
import Morphir.Value.Native exposing (decodeLiteral, encodeLiteral, stringLiteral, intLiteral, encodeMaybe)
import Morphir.Value.Native exposing (eval0)
import Morphir.Value.Native exposing (encodeUUID, encodeUUID2)
import Morphir.Value.Native exposing (encodeUUID)
import Morphir.IR.SDK.Maybe exposing (maybeType)
import Morphir.Value.Native exposing (decodeUUID)
import Morphir.Value.Native.Comparable exposing (compareValue)
Expand All @@ -33,6 +34,17 @@ moduleSpec =
{ types =
Dict.fromList
[ (Name.fromString "UUID", OpaqueTypeSpecification [] |> Documented "Type that represents a UUID v5")
, (Name.fromString "Error"
, CustomTypeSpecification []
(Dict.fromList
[ (Name.fromString "WrongFormat", [])
, (Name.fromString "WrongLength", [])
, (Name.fromString "UnsupportedVariant", [])
, (Name.fromString "IsNil", [])
, (Name.fromString "NoVersion", [])
]
)
|> Documented "Type that represents an UUID parsing error")
]
, values =
Dict.fromList
Expand Down Expand Up @@ -60,12 +72,33 @@ errorType: a -> Type a
errorType attributes =
Reference attributes (toFQName moduleName "Error") []

wrongFormat: va -> Value ta va
wrongFormat attributes =
Value.Constructor attributes (toFQName moduleName "WrongFormat")

wrongLength: va -> Value ta va
wrongLength attributes =
Value.Constructor attributes (toFQName moduleName "WrongLength")

unsupportedVariant: va -> Value ta va
unsupportedVariant attributes =
Value.Constructor attributes (toFQName moduleName "UnsupportedVariant")

isNil: va -> Value ta va
isNil attributes =
Value.Constructor attributes (toFQName moduleName "IsNil")

noVersion: va -> Value ta va
noVersion attributes =
Value.Constructor attributes (toFQName moduleName "NoVersion")


nativeFunctions : List ( String, Native.Function )
nativeFunctions =
[ ( "forName"
, eval2 UUID.forName (decodeLiteral stringLiteral) decodeUUID (encodeUUID))
, ( "parse"
, eval1 UUID.parse (decodeLiteral stringLiteral) (encodeUUID2))
, eval1 UUID.parse (decodeLiteral stringLiteral) (encodeUUIDParseResult))
, ( "fromString"
, eval1 UUID.fromString (decodeLiteral stringLiteral) (encodeMaybe encodeUUID))
, ( "toString"
Expand Down Expand Up @@ -108,4 +141,22 @@ encodeOrder =
EQ ->
"EQ"
in
Value.Constructor () (toFQName moduleName val)
Value.Constructor () (toFQName moduleName val)

{-| -}
encodeUUIDParseResult : (Result UUID.Error UUID.UUID) -> Result ValueError.Error Value.RawValue
encodeUUIDParseResult result =
case result of
Ok uuid ->
UUID.toString uuid
|> StringLiteral
|> Value.Literal ()
|> Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "uuid" ] ], [ "from", "string" ] ))
|> Ok
Err UUID.WrongFormat -> wrongFormat () |> Ok
Err UUID.WrongLength -> wrongLength () |> Ok
Err UUID.UnsupportedVariant -> unsupportedVariant () |> Ok
Err UUID.IsNil -> isNil () |> Ok
Err UUID.NoVersion -> noVersion () |> Ok


22 changes: 18 additions & 4 deletions src/Morphir/SDK/UUID.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Morphir.SDK.UUID exposing (
UUID
, Error
, Error(..)
, parse
, fromString
, forName
Expand All @@ -18,6 +18,7 @@ module Morphir.SDK.UUID exposing (
# The datatype
@docs UUID
@docs Error
# Convert from a known UUID String
Expand Down Expand Up @@ -71,16 +72,20 @@ import UUID as U
type alias UUID =
U.UUID

-- Make this a type with one option that is UUID, we can hold off on this for now
{-| The error type for UUIDs -}
type alias Error =
U.Error
type Error
= WrongFormat
| WrongLength
| UnsupportedVariant
| IsNil
| NoVersion


{-| You can attempt to create a UUID from a string. This function can interpret a fairly broad range of formatted (and mis-formatted) UUIDs, including ones with too much whitespace, too many (or not enough) hyphens, or uppercase characters.-}
parse : String -> Result Error UUID
parse s =
U.fromString s
|> Result.mapError fromUUIDError

{-| Includes all the functionality as `parse`, however only returns a `Maybe` on failures instead of an `Error`.-}
fromString : String -> Maybe UUID
Expand Down Expand Up @@ -141,3 +146,12 @@ oidNamespace =
x500Namespace : UUID
x500Namespace =
U.x500Namespace

fromUUIDError : U.Error -> Error
fromUUIDError e =
case e of
U.WrongFormat -> WrongFormat
U.WrongLength -> WrongLength
U.UnsupportedVariant -> UnsupportedVariant
U.IsNil -> IsNil
U.NoVersion -> NoVersion
3 changes: 0 additions & 3 deletions src/Morphir/Scala/AST.elm
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ generator uses. It's a relatively large portion of the language but it's not aim
-}

import Decimal exposing (Decimal)
import UUID exposing (UUID)


{-| -}
type alias Name =
Expand Down Expand Up @@ -289,7 +287,6 @@ type Lit
| IntegerLit Int
| FloatLit Float
| DecimalLit Decimal
| UUIDLit UUID
| NullLit


Expand Down
4 changes: 0 additions & 4 deletions src/Morphir/Scala/PrettyPrinter.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module Morphir.Scala.PrettyPrinter exposing (Options, mapCompilationUnit, mapMem
-}

import Decimal
import UUID
import Morphir.File.SourceCode exposing (Doc, concat, dot, dotSep, empty, indent, indentLines, newLine, parens, space)
import Morphir.IR.Name as Name
import Morphir.Scala.AST exposing (..)
Expand Down Expand Up @@ -678,9 +677,6 @@ mapLit lit =
DecimalLit decimal ->
Decimal.toString decimal

UUIDLit uuid ->
UUID.toString uuid

NullLit ->
"null"

Expand Down
4 changes: 4 additions & 0 deletions src/Morphir/Value/Error.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Error
| ExpectedMaybe RawValue
| ExpectedResult RawValue
| ExpectedDerivedType FQName RawValue
| ExpectedUUID RawValue
| IfThenElseConditionShouldEvaluateToBool RawValue RawValue
| FieldNotFound RawValue Name
| RecordExpected RawValue RawValue
Expand Down Expand Up @@ -103,6 +104,9 @@ toString error =
ExpectedDerivedType _ val ->
differentValueExpected "Derived Type" val

ExpectedUUID val ->
differentValueExpected "UUID" val

IfThenElseConditionShouldEvaluateToBool a b ->
"The if-then-else condition " ++ Value.toString a ++ " should evaluate to True/False instead of " ++ Value.toString b

Expand Down
27 changes: 5 additions & 22 deletions src/Morphir/Value/Native.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Morphir.Value.Native exposing
, Eval
, unaryLazy, unaryStrict, binaryLazy, binaryStrict, boolLiteral, charLiteral, eval0, eval1, eval2, eval3
, floatLiteral, intLiteral, oneOf, stringLiteral, decimalLiteral
, decodeFun1, decodeList, decodeLiteral, decodeMaybe, decodeLocalDate, decodeRaw, decodeTuple2, decodeUUID, encodeList, encodeLiteral, encodeMaybe, encodeLocalDate, encodeMaybeResult, encodeRaw, encodeResultList, encodeTuple2, decodeDict, decodeFun2, encodeDict, encodeUUID, encodeUUID2
, decodeFun1, decodeList, decodeLiteral, decodeMaybe, decodeLocalDate, decodeRaw, decodeTuple2, decodeUUID, encodeList, encodeLiteral, encodeMaybe, encodeLocalDate, encodeMaybeResult, encodeRaw, encodeResultList, encodeTuple2, decodeDict, decodeFun2, encodeDict, encodeUUID
, trinaryLazy, trinaryStrict
)

Expand Down Expand Up @@ -39,7 +39,7 @@ Various utilities to help with implementing native functions.
@docs unaryLazy, unaryStrict, binaryLazy, binaryStrict, boolLiteral, charLiteral, eval0, eval1, eval2, eval3
@docs floatLiteral, intLiteral, oneOf, stringLiteral, decimalLiteral
@docs decodeFun1, decodeList, decodeLiteral, decodeMaybe, decodeLocalDate, decodeRaw, decodeTuple2, encodeList, encodeLiteral, encodeMaybe, encodeLocalDate, encodeMaybeResult, encodeRaw, encodeResultList, encodeTuple2, decodeDict, decodeFun2, encodeDict
@docs decodeFun1, decodeList, decodeLiteral, decodeMaybe, decodeLocalDate, decodeRaw, decodeTuple2, decodeUUID, encodeList, encodeLiteral, encodeMaybe, encodeLocalDate, encodeMaybeResult, encodeRaw, encodeResultList, encodeTuple2, decodeDict, decodeFun2, encodeDict, encodeUUID
@docs trinaryLazy, trinaryStrict
-}
Expand All @@ -49,13 +49,9 @@ import Morphir.IR.Value as Value exposing (RawValue, Value)
import Morphir.SDK.Decimal exposing (Decimal)
import Morphir.SDK.Dict as Dict exposing (Dict)
import Morphir.SDK.LocalDate as LocalDate exposing (LocalDate)
import Morphir.SDK.UUID exposing (UUID)
import Morphir.SDK.UUID as UUID
import Morphir.SDK.ResultList as ListOfResults
import Morphir.Value.Error exposing (Error(..))
import Morphir.SDK.UUID as UUID
import UUID as U
import Json.Encode exposing (encode)


{-| Type that represents a native function. It's a function that takes two arguments:
Expand Down Expand Up @@ -455,30 +451,17 @@ decodeMaybe decodeItem eval value =


{-| -}
encodeUUID : UUID -> Result Error RawValue
encodeUUID : UUID.UUID -> Result Error RawValue
encodeUUID uuid =
UUID.toString uuid
|> StringLiteral
|> Value.Literal ()
|> Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "uuid" ] ], [ "from", "string" ] ))
|> Ok


{-| -}
encodeUUID2 : (Result Error UUID) -> Result Error RawValue
encodeUUID2 result =
case result of
Ok uuid ->
UUID.toString uuid
|> StringLiteral
|> Value.Literal ()
|> Value.Apply () (Value.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "uuid" ] ], [ "from", "string" ] ))
|> Ok
Err error ->
Err error

{-| -}
decodeUUID : Decoder UUID
decodeUUID : Decoder UUID.UUID
decodeUUID e value =
e value
|> Result.andThen
Expand Down
15 changes: 9 additions & 6 deletions tests-integration/reference-model/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@
"../../src",
"src"
],
"exposed-modules": [
"Morphir.Reference.Model.Types",
"Morphir.Reference.Model.Formulas"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"TSFoster/elm-uuid": "4.2.0",
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm-explorations/test": "1.2.2"
},
"indirect": {
"TSFoster/elm-bytes-extra": "1.3.0",
"TSFoster/elm-md5": "2.0.1",
"TSFoster/elm-sha1": "2.1.1",
"danfishgold/base64-bytes": "1.1.0",
"elm/bytes": "1.0.8",
"elm/json": "1.1.3",
"elm/random": "1.0.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2"
"elm/virtual-dom": "1.0.2",
"rtfeldman/elm-hex": "1.0.0"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Morphir.Reference.Model.SDK.UUID exposing (..)

import Morphir.SDK.UUID as UUID exposing (Error, UUID)


parseUUID : String -> Result Error UUID
parseUUID input =
UUID.parse input


fromString : String -> Maybe UUID
fromString input =
UUID.fromString input
Loading

0 comments on commit ae62019

Please sign in to comment.