Skip to content

Commit

Permalink
Apply the elm-formatter in the Snowpark files (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-aramirezfuentes authored and sfc-gh-lfallasavendano committed Jan 8, 2024
1 parent e9bb030 commit 054fd06
Show file tree
Hide file tree
Showing 32 changed files with 6,612 additions and 4,489 deletions.
205 changes: 123 additions & 82 deletions src/Morphir/Snowpark/AccessElementMapping.elm
Original file line number Diff line number Diff line change
@@ -1,130 +1,171 @@
module Morphir.Snowpark.AccessElementMapping exposing (
mapConstructorAccess
module Morphir.Snowpark.AccessElementMapping exposing
( mapConstructorAccess
, mapFieldAccess
, mapReferenceAccess
, mapVariableAccess )
, mapVariableAccess
)

{-| This module contains functions to generate code like `a.b` or `a`.
|-}
|
-}

import Dict
import Morphir.IR.FQName as FQName
import Morphir.IR.Literal exposing (Literal(..))
import Morphir.IR.Name as Name
import Morphir.IR.Type as IrType
import Morphir.IR.Value as Value exposing (TypedValue, Value(..))
import Morphir.IR.FQName as FQName
import Morphir.IR.Literal exposing (Literal(..))
import Morphir.Scala.AST as Scala
import Morphir.Snowpark.MappingContext exposing (isUnionTypeWithoutParams
, getReplacementForIdentifier
, ValueMappingContext)
import Morphir.IR.FQName as FQName
import Morphir.Snowpark.ReferenceUtils exposing (isValueReferenceToSimpleTypesRecord
, scalaReferenceToUnionTypeCase
, scalaPathToModule)
import Morphir.Snowpark.Constants exposing (applySnowparkFunc, ValueGenerationResult, MapValueType)
import Morphir.Snowpark.MappingContext as MappingContext exposing (isUnionTypeWithParams, isAnonymousRecordWithSimpleTypes)
import Morphir.Snowpark.ReferenceUtils exposing (errorValueAndIssue)
import Morphir.Snowpark.Constants exposing (MapValueType, ValueGenerationResult, applySnowparkFunc)
import Morphir.Snowpark.MappingContext
exposing
( ValueMappingContext
, getReplacementForIdentifier
, isAnonymousRecordWithSimpleTypes
, isDataFrameFriendlyType
, isListOfDataFrameFriendlyType
, isLocalFunctionName
, isUnionTypeWithParams
, isUnionTypeWithoutParams
)
import Morphir.Snowpark.ReferenceUtils
exposing
( errorValueAndIssue
, isValueReferenceToSimpleTypesRecord
, scalaPathToModule
, scalaReferenceToUnionTypeCase
)


checkForDataFrameVariableReference : Value ta (IrType.Type ()) -> ValueMappingContext -> Maybe String
checkForDataFrameVariableReference value ctx =
case Value.valueAttribute value of
IrType.Reference _ typeName _ ->
IrType.Reference _ typeName _ ->
Dict.get typeName ctx.dataFrameColumnsObjects
_ ->

_ ->
Nothing


mapFieldAccess : va -> TypedValue -> Name.Name -> ValueMappingContext -> MapValueType -> ValueGenerationResult
mapFieldAccess _ value name ctx mapValue =
let
simpleFieldName = name |> Name.toCamelCase
valueIsFunctionParameter =
let
simpleFieldName =
name |> Name.toCamelCase

valueIsFunctionParameter =
case value of
Value.Variable _ varName ->
Value.Variable _ varName ->
if List.member varName ctx.parameters then
Just <| Name.toCamelCase varName

else
Nothing
_ ->

_ ->
Nothing
valueIsDataFrameColumnAccess =
case (value, checkForDataFrameVariableReference value ctx) of
(Value.Variable _ _, Just replacement) ->

valueIsDataFrameColumnAccess =
case ( value, checkForDataFrameVariableReference value ctx ) of
( Value.Variable _ _, Just replacement ) ->
Just replacement
_ ->

_ ->
Nothing
in
case (isValueReferenceToSimpleTypesRecord value ctx.typesContextInfo, valueIsFunctionParameter, valueIsDataFrameColumnAccess) of
(_,Just replacement, _ ) ->
(Scala.Ref [replacement] simpleFieldName, [])
(_,_, Just replacement) ->
(Scala.Ref [replacement] simpleFieldName, [])
(Just (path, refererName), Nothing, Nothing) ->
(Scala.Ref (path ++ [refererName |> Name.toTitleCase]) simpleFieldName, [])
_ ->
(if isAnonymousRecordWithSimpleTypes (value |> Value.valueAttribute) ctx.typesContextInfo then
(applySnowparkFunc "col" [Scala.Literal (Scala.StringLit (Name.toCamelCase name)) ], [])
else
in
case ( isValueReferenceToSimpleTypesRecord value ctx.typesContextInfo, valueIsFunctionParameter, valueIsDataFrameColumnAccess ) of
( _, Just replacement, _ ) ->
( Scala.Ref [ replacement ] simpleFieldName, [] )

( _, _, Just replacement ) ->
( Scala.Ref [ replacement ] simpleFieldName, [] )

( Just ( path, refererName ), Nothing, Nothing ) ->
( Scala.Ref (path ++ [ refererName |> Name.toTitleCase ]) simpleFieldName, [] )

_ ->
if isAnonymousRecordWithSimpleTypes (value |> Value.valueAttribute) ctx.typesContextInfo then
( applySnowparkFunc "col" [ Scala.Literal (Scala.StringLit (Name.toCamelCase name)) ], [] )

else
let
(mappedValue, issues) =
( mappedValue, issues ) =
mapValue value ctx
in
(Scala.Select mappedValue (Name.toCamelCase name), issues))
( Scala.Select mappedValue (Name.toCamelCase name), issues )


mapVariableAccess : Name.Name -> TypedValue -> ValueMappingContext -> ValueGenerationResult
mapVariableAccess name nameAccess ctx =
case (getReplacementForIdentifier name ctx, checkForDataFrameVariableReference nameAccess ctx) of
(Just replacement, _) ->
(replacement, [])
(_, Just replacementStr) ->
(Scala.Variable replacementStr, [])
case ( getReplacementForIdentifier name ctx, checkForDataFrameVariableReference nameAccess ctx ) of
( Just replacement, _ ) ->
( replacement, [] )

( _, Just replacementStr ) ->
( Scala.Variable replacementStr, [] )

_ ->
(Scala.Variable (name |> Name.toCamelCase), [])
( Scala.Variable (name |> Name.toCamelCase), [] )


mapConstructorAccess : (IrType.Type a) -> FQName.FQName -> ValueMappingContext -> ValueGenerationResult
mapConstructorAccess : IrType.Type a -> FQName.FQName -> ValueMappingContext -> ValueGenerationResult
mapConstructorAccess tpe name ctx =
case (tpe, name) of
((IrType.Reference _ _ _), ([ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "maybe" ] ], [ "nothing" ])) ->
(applySnowparkFunc "lit" [Scala.Literal Scala.NullLit], [])
(IrType.Reference _ typeName _, _) ->
case ( tpe, name ) of
( IrType.Reference _ _ _, ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "maybe" ] ], [ "nothing" ] ) ) ->
( applySnowparkFunc "lit" [ Scala.Literal Scala.NullLit ], [] )

( IrType.Reference _ typeName _, _ ) ->
if isUnionTypeWithoutParams typeName ctx.typesContextInfo then
(scalaReferenceToUnionTypeCase typeName name, [])
( scalaReferenceToUnionTypeCase typeName name, [] )

else if isUnionTypeWithParams typeName ctx.typesContextInfo then
((applySnowparkFunc "object_construct" [
applySnowparkFunc "lit" [Scala.Literal <| Scala.StringLit "__tag"],
applySnowparkFunc "lit" [Scala.Literal <| Scala.StringLit (name |> FQName.getLocalName |> Name.toTitleCase)] ]),
[])
( applySnowparkFunc "object_construct"
[ applySnowparkFunc "lit" [ Scala.Literal <| Scala.StringLit "__tag" ]
, applySnowparkFunc "lit" [ Scala.Literal <| Scala.StringLit (name |> FQName.getLocalName |> Name.toTitleCase) ]
]
, []
)

else
errorValueAndIssue <| "Constructor access not converted" ++ (FQName.toString name)
_ ->
errorValueAndIssue <| "Constructor access not converted" ++ (FQName.toString name)
errorValueAndIssue <| "Constructor access not converted" ++ FQName.toString name

_ ->
errorValueAndIssue <| "Constructor access not converted" ++ FQName.toString name


mapReferenceAccess : (IrType.Type ()) -> FQName.FQName -> MapValueType -> ValueMappingContext -> ValueGenerationResult
mapReferenceAccess : IrType.Type () -> FQName.FQName -> MapValueType -> ValueMappingContext -> ValueGenerationResult
mapReferenceAccess tpe name mapValue ctx =
if Dict.member name ctx.globalValuesToInline then
let
inlinedResult =
ctx.globalValuesToInline
|> Dict.get name
|> Maybe.map (\definition -> mapValue definition.body ctx)
|> Maybe.withDefault (Scala.Wildcard, [])
if Dict.member name ctx.globalValuesToInline then
let
inlinedResult =
ctx.globalValuesToInline
|> Dict.get name
|> Maybe.map (\definition -> mapValue definition.body ctx)
|> Maybe.withDefault ( Scala.Wildcard, [] )
in
inlinedResult
else if MappingContext.isDataFrameFriendlyType tpe ctx.typesContextInfo ||
MappingContext.isListOfDataFrameFriendlyType tpe ctx.typesContextInfo then

else if
isDataFrameFriendlyType tpe ctx.typesContextInfo
|| isListOfDataFrameFriendlyType tpe ctx.typesContextInfo
then
let
nsName = scalaPathToModule name
containerObjectFieldName = FQName.getLocalName name |> Name.toCamelCase
in
(Scala.Ref (nsName ) containerObjectFieldName, [])
else
nsName =
scalaPathToModule name

containerObjectFieldName =
FQName.getLocalName name |> Name.toCamelCase
in
( Scala.Ref nsName containerObjectFieldName, [] )

else
case tpe of
IrType.Function _ _ _ ->
if MappingContext.isLocalFunctionName name ctx then
(Scala.Ref (scalaPathToModule name) (name |> FQName.getLocalName |> Name.toCamelCase), [])
else
errorValueAndIssue ("Reference access to function not converted" ++ (FQName.toString name))
_ ->
errorValueAndIssue ("Reference access not converted: " ++ (FQName.toString name))
IrType.Function _ _ _ ->
if isLocalFunctionName name ctx then
( Scala.Ref (scalaPathToModule name) (name |> FQName.getLocalName |> Name.toCamelCase), [] )

else
errorValueAndIssue ("Reference access to function not converted" ++ FQName.toString name)

_ ->
errorValueAndIssue ("Reference access not converted: " ++ FQName.toString name)
Loading

0 comments on commit 054fd06

Please sign in to comment.