Skip to content

Commit

Permalink
#333 - Fixed an issue with generic functions (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianReeves authored Feb 13, 2021
1 parent eeecef7 commit b707e44
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dist
.idea
.metals/
morphir-ir.json
generated/
out/
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = "2.7.4"
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"version": "2.16.3",
"description": "Elm bindings for Morphir",
"scripts": {
"clean": "del-cli dist",
"clean": "del-cli dist tests-integration/generated/",
"test:integration":"npm run test:integration:clean && npm run test:integration:make && npm run test:integration:gen && npm run test:integration:build",
"test:integration:build": "cd tests-integration && mill __.compile",
"test:integration:clean": "del-cli tests-integration/generated/",
"test:integration:gen": "node dist/morphir elm gen -i ./tests-integration/generated/morphir-ir.json -o ./tests-integration/generated/refModel/src/scala/ -t Scala ",
"test:integration:make": "node dist/morphir elm make -p ./tests-integration/reference-model -o ./tests-integration/generated/morphir-ir.json",

"test": "elm-test",
"make-cli": "cd cli && elm make src/Morphir/Elm/CLI.elm --output Morphir.Elm.CLI.js --optimize && elm make src/Morphir/Elm/DaprCLI.elm --output Morphir.Elm.DaprCLI.js --optimize",
"make-cli-dev": "cd cli && elm make src/Morphir/Elm/CLI.elm --output Morphir.Elm.CLI.js && elm make src/Morphir/Elm/DaprCLI.elm --output Morphir.Elm.DaprCLI.js",
Expand Down
15 changes: 14 additions & 1 deletion src/Morphir/Scala/Backend.elm
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ mapModuleDefinition opt distribution currentPackagePath currentModulePath access

functionMembers : List (Scala.Annotated Scala.MemberDecl)
functionMembers =
let
gatherTypeNames tpe acc =
Type.collectVariables tpe |> Set.map Name.toTitleCase |> Set.union acc

gatherAllTypeNames inputTypes =
inputTypes
|> List.foldl gatherTypeNames Set.empty
|> Set.toList
|> List.map Scala.TypeVar
in
accessControlledModuleDef.value.values
|> Dict.toList
|> List.concatMap
Expand All @@ -214,7 +224,10 @@ mapModuleDefinition opt distribution currentPackagePath currentModulePath access
, name =
mapValueName valueName
, typeArgs =
[]
accessControlledValueDef.value.inputTypes
|> List.map (\( _, _, tpe ) -> tpe)
|> (::) accessControlledValueDef.value.outputType
|> gatherAllTypeNames
, args =
if List.isEmpty accessControlledValueDef.value.inputTypes then
[]
Expand Down
1 change: 1 addition & 0 deletions tests-integration/.mill-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.9.5
1 change: 1 addition & 0 deletions tests-integration/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = "2.7.4"
9 changes: 9 additions & 0 deletions tests-integration/build.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// build.sc
import mill._, scalalib._

object generated extends Module {
object refModel extends ScalaModule {
def scalaVersion = "2.11.12"
def ivyDeps = Agg(ivy"org.morphir::morphir-sdk-core:0.6.1")
}
}
48 changes: 48 additions & 0 deletions tests-integration/mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env sh

# This is a wrapper script, that automatically download mill from GitHub release pages
# You can give the required mill version with MILL_VERSION env variable
# If no version is given, it falls back to the value of DEFAULT_MILL_VERSION
DEFAULT_MILL_VERSION=0.7.0

set -e

if [ -z "$MILL_VERSION" ] ; then
if [ -f ".mill-version" ] ; then
MILL_VERSION="$(head -n 1 .mill-version 2> /dev/null)"
elif [ -f "mill" ] && [ "$BASH_SOURCE" != "mill" ] ; then
MILL_VERSION=$(grep -F "DEFAULT_MILL_VERSION=" "mill" | head -n 1 | cut -d= -f2)
else
MILL_VERSION=$DEFAULT_MILL_VERSION
fi
fi

if [ "x${XDG_CACHE_HOME}" != "x" ] ; then
MILL_DOWNLOAD_PATH="${XDG_CACHE_HOME}/mill/download"
else
MILL_DOWNLOAD_PATH="${HOME}/.cache/mill/download"
fi
MILL_EXEC_PATH="${MILL_DOWNLOAD_PATH}/${MILL_VERSION}"

version_remainder="$MILL_VERSION"
MILL_MAJOR_VERSION="${version_remainder%%.*}"; version_remainder="${version_remainder#*.}"
MILL_MINOR_VERSION="${version_remainder%%.*}"; version_remainder="${version_remainder#*.}"

if [ ! -x "$MILL_EXEC_PATH" ] ; then
mkdir -p $MILL_DOWNLOAD_PATH
if [ "$MILL_MAJOR_VERSION" -gt 0 ] || [ "$MILL_MINOR_VERSION" -ge 5 ] ; then
ASSEMBLY="-assembly"
fi
DOWNLOAD_FILE=$MILL_EXEC_PATH-tmp-download
MILL_DOWNLOAD_URL="https://github.com/lihaoyi/mill/releases/download/${MILL_VERSION%%-*}/$MILL_VERSION${ASSEMBLY}"
curl --fail -L -o "$DOWNLOAD_FILE" "$MILL_DOWNLOAD_URL"
chmod +x "$DOWNLOAD_FILE"
mv "$DOWNLOAD_FILE" "$MILL_EXEC_PATH"
unset DOWNLOAD_FILE
unset MILL_DOWNLOAD_URL
fi

unset MILL_DOWNLOAD_PATH
unset MILL_VERSION

exec $MILL_EXEC_PATH "$@"
95 changes: 95 additions & 0 deletions tests-integration/mill.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
@echo off

rem This is a wrapper script, that automatically download mill from GitHub release pages
rem You can give the required mill version with --mill-version parameter
rem If no version is given, it falls back to the value of DEFAULT_MILL_VERSION
rem
rem Project page: https://github.com/lefou/millw
rem
rem If you want to improve this script, please also contribute your changes back!
rem
rem Licensed under the Apache License, Version 2.0

rem setlocal seems to be unavailable on Windows 95/98/ME
rem but I don't think we need to support them in 2019
setlocal enabledelayedexpansion

set "DEFAULT_MILL_VERSION=0.7.3"

rem %~1% removes surrounding quotes
if [%~1%]==[--mill-version] (
rem shift command doesn't work within parentheses
if not [%~2%]==[] (
set MILL_VERSION=%~2%
set "STRIP_VERSION_PARAMS=true"
) else (
echo You specified --mill-version without a version.
echo Please provide a version that matches one provided on
echo https://github.com/lihaoyi/mill/releases
exit /b 1
)
)

if [!MILL_VERSION!]==[] (
if exist .mill-version (
set /p MILL_VERSION=<.mill-version
)
)

if [!MILL_VERSION!]==[] (
set MILL_VERSION=%DEFAULT_MILL_VERSION%
)

set MILL_DOWNLOAD_PATH=%USERPROFILE%\.mill\download

rem without bat file extension, cmd doesn't seem to be able to run it
set MILL=%MILL_DOWNLOAD_PATH%\!MILL_VERSION!.bat

if not exist "%MILL%" (
set VERSION_PREFIX=%MILL_VERSION:~0,4%
set DOWNLOAD_SUFFIX=-assembly
if [!VERSION_PREFIX!]==[0.0.] set DOWNLOAD_SUFFIX=
if [!VERSION_PREFIX!]==[0.1.] set DOWNLOAD_SUFFIX=
if [!VERSION_PREFIX!]==[0.2.] set DOWNLOAD_SUFFIX=
if [!VERSION_PREFIX!]==[0.3.] set DOWNLOAD_SUFFIX=
if [!VERSION_PREFIX!]==[0.4.] set DOWNLOAD_SUFFIX=
set VERSION_PREFIX=

rem there seems to be no way to generate a unique temporary file path (on native Windows)
set DOWNLOAD_FILE=%MILL%.tmp

echo Downloading mill %MILL_VERSION% from https://github.com/lihaoyi/mill/releases ...

rem curl is bundled with recent Windows 10
rem but I don't think we can expect all the users to have it in 2019
rem bitadmin seems to be available on Windows 7
rem without /dynamic, github returns 403
rem bitadmin is sometimes needlessly slow but it looks better with /priority foreground
if not exist "%MILL_DOWNLOAD_PATH%" mkdir "%MILL_DOWNLOAD_PATH%"
bitsadmin /transfer millDownloadJob /dynamic /priority foreground "https://github.com/lihaoyi/mill/releases/download/%MILL_VERSION%/%MILL_VERSION%!DOWNLOAD_SUFFIX!" "!DOWNLOAD_FILE!"
if not exist "!DOWNLOAD_FILE!" (
echo Could not download mill %MILL_VERSION%
exit 1
)

move /y "!DOWNLOAD_FILE!" "%MILL%"

set DOWNLOAD_FILE=
set DOWNLOAD_SUFFIX=
)

set MILL_DOWNLOAD_PATH=
set MILL_VERSION=

set MILL_PARAMS=%*

if defined STRIP_VERSION_PARAMS (
for /f "tokens=1-2*" %%a in ("%*") do (
rem strip %%a - It's the "--mill-version" option.
rem strip %%b - it's the version number that comes after the option.
rem keep %%c - It's the remaining options.
set MILL_PARAMS=%%c
)
)

"%MILL%" -i %MILL_PARAMS%
3 changes: 2 additions & 1 deletion tests-integration/reference-model/morphir.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"Issues.Issue227",
"Issues.Issue273",
"Issues.Issue330",
"Issues.Issue331"
"Issues.Issue331",
"Issues.Issue333"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
module Morphir.Reference.Model.Issues.Issue333 exposing (..)


type Month
= January
| February
| March
| April
| May
| June
| July
| August
| September
| October
| November
| December


type MonthParseError
= InvalidMonth Int


type alias RawLocalDate =
{ year : Int
, month : Int
, day : Int
}


type alias ValidLocalDate =
{ year : Int
, month : Month
, day : Int
}


type LocalDate
= Invalid RawLocalDate
| Valid ValidLocalDate


monthNumber : Month -> Int
monthNumber month =
case month of
January ->
1

February ->
2

March ->
3

April ->
4

May ->
5

June ->
6

July ->
7

August ->
8

September ->
9

October ->
10

November ->
11

December ->
12


fromInt : Int -> Maybe Month
fromInt month =
case month of
1 ->
Just January

2 ->
Just February

3 ->
Just March

4 ->
Just April

5 ->
Just May

6 ->
Just June

7 ->
Just July

8 ->
Just August

9 ->
Just September

10 ->
Just October

11 ->
Just November

12 ->
Just December

_ ->
Nothing


convertFromInt : Int -> Result MonthParseError Month
convertFromInt month =
case month of
1 ->
Ok January

2 ->
Ok February

3 ->
Ok March

4 ->
Ok April

5 ->
Ok May

6 ->
Ok June

7 ->
Ok July

8 ->
Ok August

9 ->
Ok September

10 ->
Ok October

11 ->
Ok November

12 ->
Ok December

m ->
InvalidMonth m |> Err


fold : (RawLocalDate -> out) -> (ValidLocalDate -> out) -> LocalDate -> out
fold whenInvalid whenValid date =
case date of
Valid validDate ->
whenValid validDate

Invalid rawDate ->
whenInvalid rawDate

0 comments on commit b707e44

Please sign in to comment.