-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7009 from roc-lang/lower-module-params
Lower module params
- Loading branch information
Showing
33 changed files
with
1,547 additions
and
178 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module { appId, protocol } -> [ | ||
baseUrl, | ||
getUser, | ||
getPost, | ||
getPosts, | ||
getPostComments, | ||
getCompanies, | ||
baseUrlAliased, | ||
getPostAliased, | ||
getUserSafe, | ||
getPostComment, | ||
] | ||
|
||
## value def referencing params | ||
baseUrl : Str | ||
baseUrl = | ||
protocol "api.example.com/$(appId)" | ||
|
||
## function def referencing params | ||
getUser : U32 -> Str | ||
getUser = \userId -> | ||
# purposefully not using baseUrl to test top-level fn referencing param | ||
protocol "api.example.com/$(appId)/users/$(Num.toStr userId)" | ||
|
||
## function def referencing top-level value | ||
getPost : U32 -> Str | ||
getPost = \postId -> | ||
"$(baseUrl)/posts/$(Num.toStr postId)" | ||
|
||
## function def passing top-level function | ||
getPosts : List U32 -> List Str | ||
getPosts = \ids -> | ||
List.map ids getPost | ||
|
||
## function def calling top-level function | ||
getPostComments : U32 -> Str | ||
getPostComments = \postId -> | ||
"$(getPost postId)/comments" | ||
|
||
## function def passing nested function | ||
getCompanies : List U32 -> List Str | ||
getCompanies = \ids -> | ||
getCompany = \id -> | ||
protocol "api.example.com/$(appId)/companies/$(Num.toStr id)" | ||
|
||
List.map ids getCompany | ||
|
||
## aliasing top-level value | ||
baseUrlAliased : Str | ||
baseUrlAliased = | ||
baseUrl | ||
|
||
## aliasing top-level fn | ||
getPostAliased : U32 -> Str | ||
getPostAliased = | ||
getPost | ||
|
||
## top-level value returning functions | ||
getUserSafe : U32 -> Str | ||
getUserSafe = | ||
if Str.startsWith appId "prod_" then | ||
\id -> "$(getUser id)?safe=true" | ||
else | ||
getUser | ||
|
||
## two-argument function | ||
getPostComment : U32, U32 -> Str | ||
getPostComment = \postId, commentId -> | ||
"$(getPost postId)/comments/$(Num.toStr commentId)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module { appId } -> [fnAnnotatedAsValue, missingArg] | ||
|
||
fnAnnotatedAsValue : Str | ||
fnAnnotatedAsValue = \postId, commentId -> | ||
"/posts/$(postId)/comments/$(Num.toStr commentId)" | ||
|
||
missingArg : Str -> Str | ||
missingArg = \postId, _ -> | ||
"/posts/$(postId)/comments" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
app [main] { | ||
pf: platform "../fixtures/multi-dep-str/platform/main.roc", | ||
} | ||
|
||
import Api { appId: "one", protocol: https } as App1 | ||
import Api { appId: "two", protocol: http } as App2 | ||
import Api { appId: "prod_1", protocol: http } as Prod | ||
|
||
https = \url -> "https://$(url)" | ||
http = \url -> "http://$(url)" | ||
|
||
usersApp1 = | ||
# pass top-level fn in a module with params | ||
List.map [1, 2, 3] App1.getUser | ||
|
||
main = | ||
app3Id = "three" | ||
|
||
import Api { appId: app3Id, protocol: https } as App3 | ||
|
||
getUserApp3Nested = \userId -> | ||
# use captured params def | ||
App3.getUser userId | ||
|
||
usersApp3Passed = | ||
# pass top-level fn in a nested def | ||
List.map [1, 2, 3] App3.getUser | ||
|
||
""" | ||
App1.baseUrl: $(App1.baseUrl) | ||
App2.baseUrl: $(App2.baseUrl) | ||
App3.baseUrl: $(App3.baseUrl) | ||
App1.getUser 1: $(App1.getUser 1) | ||
App2.getUser 2: $(App2.getUser 2) | ||
App3.getUser 3: $(App3.getUser 3) | ||
App1.getPost 1: $(App1.getPost 1) | ||
App2.getPost 2: $(App2.getPost 2) | ||
App3.getPost 3: $(App3.getPost 3) | ||
App1.getPosts [1, 2]: $(Inspect.toStr (App1.getPosts [1, 2])) | ||
App2.getPosts [3, 4]: $(Inspect.toStr (App2.getPosts [3, 4])) | ||
App2.getPosts [5, 6]: $(Inspect.toStr (App2.getPosts [5, 6])) | ||
App1.getPostComments 1: $(App1.getPostComments 1) | ||
App2.getPostComments 2: $(App2.getPostComments 2) | ||
App2.getPostComments 3: $(App2.getPostComments 3) | ||
App1.getCompanies [1, 2]: $(Inspect.toStr (App1.getCompanies [1, 2])) | ||
App2.getCompanies [3, 4]: $(Inspect.toStr (App2.getCompanies [3, 4])) | ||
App2.getCompanies [5, 6]: $(Inspect.toStr (App2.getCompanies [5, 6])) | ||
App1.getPostAliased 1: $(App1.getPostAliased 1) | ||
App2.getPostAliased 2: $(App2.getPostAliased 2) | ||
App3.getPostAliased 3: $(App3.getPostAliased 3) | ||
App1.baseUrlAliased: $(App1.baseUrlAliased) | ||
App2.baseUrlAliased: $(App2.baseUrlAliased) | ||
App3.baseUrlAliased: $(App3.baseUrlAliased) | ||
App1.getUserSafe 1: $(App1.getUserSafe 1) | ||
Prod.getUserSafe 2: $(Prod.getUserSafe 2) | ||
usersApp1: $(Inspect.toStr usersApp1) | ||
getUserApp3Nested 3: $(getUserApp3Nested 3) | ||
usersApp3Passed: $(Inspect.toStr usersApp3Passed) | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
app [main] { | ||
pf: platform "../fixtures/multi-dep-str/platform/main.roc", | ||
} | ||
|
||
import Api { appId: "one", protocol: https } | ||
|
||
https = \url -> "https://$(url)" | ||
|
||
main = | ||
""" | ||
# too many args | ||
$(Api.getUser 1 2) | ||
$(Api.baseUrl 1) | ||
# too few args | ||
$(Api.getPostComment 1) | ||
""" |
Oops, something went wrong.