Skip to content

Commit

Permalink
Support the literals into Snowpark (#39)
Browse files Browse the repository at this point in the history
Add support to the literals using the snowpark function lit for boolean, string, char, float and Int.
Add test for lit literals.
  • Loading branch information
sfc-gh-aramirezfuentes authored and sfc-gh-lfallasavendano committed Jan 8, 2024
1 parent 6a7243b commit 55598ea
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 15 deletions.
26 changes: 24 additions & 2 deletions src/Morphir/Snowpark/Backend.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import Morphir.Scala.Common exposing (mapValueName)
import Morphir.IR.Value as Value exposing (Pattern(..), Value(..))
import Morphir.Snowpark.MappingContext as MappingContext
import Morphir.IR.FQName as FQName
import Morphir.IR.Literal exposing (Literal(..))
import Morphir.Snowpark.Constants as Constants

type alias Options =
{}
Expand Down Expand Up @@ -126,5 +128,25 @@ mapFunctionBody value =
Maybe.Just (mapValue value.body)

mapValue : Value ta (Type ()) -> Scala.Value
mapValue _ =
Scala.Literal (Scala.StringLit "To Do")
mapValue value =
case value of
Literal tpe literal ->
mapLiteral tpe literal
_ ->
Scala.Literal (Scala.StringLit "To Do")

mapLiteral : ta -> Literal -> Scala.Value
mapLiteral tpe literal =
case literal of
CharLiteral val ->
Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.CharacterLit val))]
StringLiteral val ->
Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.StringLit val))]
BoolLiteral val ->
Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.BooleanLit val))]
WholeNumberLiteral val ->
Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.IntegerLit val))]
FloatLiteral val ->
Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.FloatLit val))]
_ ->
Debug.todo "The type '_' is not implemented"
11 changes: 11 additions & 0 deletions src/Morphir/Snowpark/Constants.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Morphir.Snowpark.Constants exposing (..)
import Morphir.Scala.AST as Scala

functionsNamespace : List String
functionsNamespace = ["com", "snowpark", "functions"]

applySnowparkFunc : String -> List Scala.Value -> Scala.Value
applySnowparkFunc name args =
Scala.Apply
(Scala.Ref functionsNamespace name)
(args |> List.map (\v -> Scala.ArgValue Nothing v))
85 changes: 85 additions & 0 deletions tests/Morphir/Snowpark/MapValueLiteralTests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module Morphir.Snowpark.MapValueLiteralTests exposing (mapValueLiteralTests)
import Expect
import Test exposing (Test, describe, test)
import Morphir.IR.Literal as Literal
import Morphir.Snowpark.Backend exposing (mapValue)
import Morphir.Scala.AST as Scala
import Morphir.IR.Value as Value
import Morphir.IR.Type as Type

functionNamespace : List String
functionNamespace = ["com", "snowpark", "functions"]

booleanReference : Type.Type ()
booleanReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "boolean" ] ) []
booleanTest : Scala.Value
booleanTest = Scala.Apply
(Scala.Ref functionNamespace "lit")
([Scala.ArgValue
Nothing (Scala.Literal (Scala.BooleanLit True))])

stringReference : Type.Type ()
stringReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "boolean" ] ) []
stringTest : Scala.Value
stringTest = Scala.Apply
(Scala.Ref functionNamespace "lit")
([Scala.ArgValue
Nothing (Scala.Literal (Scala.StringLit "Hello world"))])

characterReference : Type.Type ()
characterReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "character" ] ) []
characterTest : Scala.Value
characterTest = Scala.Apply
(Scala.Ref functionNamespace "lit")
([Scala.ArgValue
Nothing (Scala.Literal (Scala.CharacterLit 'C'))])

floatReference : Type.Type ()
floatReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "float" ] ) []
floatTest : Scala.Value
floatTest = Scala.Apply
(Scala.Ref functionNamespace "lit")
([Scala.ArgValue
Nothing (Scala.Literal (Scala.FloatLit 3.24))])


integerReference : Type.Type ()
integerReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "integer" ] ) []
integerTest : Scala.Value
integerTest = Scala.Apply
(Scala.Ref functionNamespace "lit")
([Scala.ArgValue
Nothing (Scala.Literal (Scala.IntegerLit 5))])

mapValueLiteralTests: Test
mapValueLiteralTests =
let
assertBooleanLiteral =
test ("Convert boolean") <|
\_ ->
Expect.equal booleanTest (mapValue (Value.Literal booleanReference (Literal.BoolLiteral True)))
assertStringLiteral =
test ("Convert string") <|
\_ ->
Expect.equal stringTest (mapValue (Value.Literal stringReference (Literal.StringLiteral "Hello world")))
assertCharacterLiteral =
test ("Convert character") <|
\_ ->
Expect.equal characterTest (mapValue (Value.Literal characterReference (Literal.CharLiteral 'C')))
assertFloatLiteral =
test ("Convert float") <|
\_ ->
Expect.equal floatTest (mapValue (Value.Literal floatReference (Literal.FloatLiteral 3.24)))
assertIntegerLiteral =
test ("Convert integer") <|
\_ ->
Expect.equal integerTest (mapValue (Value.Literal integerReference (Literal.WholeNumberLiteral 5)))
in
describe "literalMapTransform"
[
assertBooleanLiteral,
assertStringLiteral,
assertCharacterLiteral,
assertFloatLiteral,
assertIntegerLiteral
]
15 changes: 2 additions & 13 deletions tests/Morphir/Snowpark/MappingContextTests.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module Morphir.Snowpark.MappingContextTests exposing (typeClassificationTests)

import Dict exposing (Dict)
import Set exposing (Set)
import Dict
import Test exposing (Test, describe, test)
import Expect
import Morphir.IR.Distribution exposing (Distribution)
import Morphir.IR.Distribution as Distribution
import Morphir.IR.Path as Path
import Morphir.IR.Module exposing (emptyDefinition)
import Morphir.IR.AccessControlled exposing (public)
Expand Down Expand Up @@ -64,15 +61,7 @@ testDistributionPackage =
{ name = Name.fromString "head", tpe = (Reference () (FQName.fromString "UTest:MyMod:Emp1" ":") []) }
]) })
] } )
]})

testDistribution : Distribution
testDistribution =
Distribution.Library
testDistributionName
Dict.empty
testDistributionPackage

]})

typeClassificationTests : Test
typeClassificationTests =
Expand Down

0 comments on commit 55598ea

Please sign in to comment.