-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement standard library glob function #511
Implement standard library glob function #511
Conversation
src/modules/condition/failed.rs
Outdated
@@ -80,23 +80,27 @@ impl TranslateModule for Failed { | |||
if self.is_question_mark { | |||
// if the failed expression is in the main block we need to clear the return value | |||
let clear_return = if !self.is_main { | |||
let (name, id, variant) = meta.fun_name.clone().expect("Function name not set"); | |||
format!("__AF_{name}{id}_v{variant}=''") | |||
let fun_name = meta.fun_name.as_ref().expect("Function name not set"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bundled (name, id, variant)
into a new struct FunctionName
, because we were duplicating mangled name generation in three places.
src/modules/condition/failed.rs
Outdated
let (name, id, variant) = meta.fun_name.clone().expect("Function name not set"); | ||
format!("__AF_{name}{id}_v{variant}=''") | ||
let fun_name = meta.fun_name.as_ref().expect("Function name not set"); | ||
format!("{}={}", fun_name.mangled_name(), fun_name.default_value()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes a bug where failing functions would return the previous array value with the first item blanked out, because the default return value was Bash string ''
not Bash array ()
. Try running the following Amber script with commits up to "5212ef5 Add glob functions to standard library":
hwalters@Ghostwheel ~/git/amber (HEAD)
$ cat glob.ab
#!/usr/bin/env amber
import * from "std/fs"
echo "Calling glob(\"*.md\")"
loop file in trust glob("*.md") {
echo "Found \"{file}\""
}
echo "Calling glob(\"*.xyz\")"
loop file in trust glob("*.xyz") {
echo "Found \"{file}\""
}
hwalters@Ghostwheel ~/git/amber (HEAD)
$ ./glob.ab
Calling glob("*.md")
Found "CONTRIBUTING.md"
Found "LICENSE.md"
Found "NIX.md"
Found "README.md"
Calling glob("*.xyz")
Found ""
Found "LICENSE.md"
Found "NIX.md"
Found "README.md"
This is what it does with the latest commit:
hwalters@Ghostwheel ~/git/amber (implement-stdlib-glob-function)
$ ./glob.ab
Calling glob("*.md")
Found "CONTRIBUTING.md"
Found "LICENSE.md"
Found "NIX.md"
Found "README.md"
Calling glob("*.xyz")
src/modules/types.rs
Outdated
@@ -30,6 +30,14 @@ impl Type { | |||
pub fn is_allowed_in(&self, other: &Type) -> bool { | |||
self == other || self.is_subset_of(other) | |||
} | |||
|
|||
pub fn is_array(&self) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may cause problems if the union types change supports union types in return values. If a function could return Text | [Text]
(which seems like something we would want to disallow, tbh) what would the default return type be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the default can be the first type of the union, Text
in your case. No need to disallow this, it's one of union's use cases.
src/std/fs.ab
Outdated
/// } | ||
/// } | ||
/// ``` | ||
pub fun glob_multiple(path: [Text]): [Text]? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a good idea to eventually support either glob("*.txt")
or glob(["*.txt", "*.xyz"])
. This function is here as a placeholder for that functionality, so we can write unit tests. To be merged into the main function when the union types PR is merged.
@@ -7,5 +7,5 @@ main { | |||
} else { | |||
echo "failed" | |||
} | |||
trust $rm {tmpdir}$ | |||
trust $rm -fr {tmpdir}$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is also part of a different pending PR. Shouldn't cause merge conflicts, but this is implemented in a commit of its own, so I can drop it and rebase if it causes problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally speaking this is a very good looking PR. Just a few nitpicks, and it would be good to safeguard the escaping logic with some tests.
src/modules/types.rs
Outdated
@@ -30,6 +30,14 @@ impl Type { | |||
pub fn is_allowed_in(&self, other: &Type) -> bool { | |||
self == other || self.is_subset_of(other) | |||
} | |||
|
|||
pub fn is_array(&self) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the default can be the first type of the union, Text
in your case. No need to disallow this, it's one of union's use cases.
this is very confusing to review. are you fixing an underlying issue right away which blocks you from implementing the function in stdlib? then it should be in two different PR's, with this one depending on the one that resolves the issue in the compiler also the default return thing is a new language feature, it should be a feature request issue first, and then a PR |
To summarise our private discussion, I'll create a new issue and PR, to fix the "default return" problem, then rebase this PR once that's been merged. But in return, you have to review both of them! (grin) |
Created issue #515. |
5dc0e08
to
d9569af
Compare
Fixes #463, by implementing
glob
as a standard library function. I also fixed a bug where failing functions would return the previous array value with the first item blanked out, because the default return value was Bash string''
not Bash array()
.