Skip to content
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

Pass globals as command-line options #78

Merged
merged 6 commits into from Aug 4, 2022
Merged

Pass globals as command-line options #78

merged 6 commits into from Aug 4, 2022

Conversation

robrix
Copy link
Member

@robrix robrix commented Jul 28, 2022

@robrix robrix changed the title Globular Pass globals as command-line options Jul 28, 2022
@robrix
Copy link
Member Author

robrix commented Jul 28, 2022

cc @BekaValentine — this might be convenient for you.

@robrix
Copy link
Member Author

robrix commented Aug 2, 2022

I'm ok with leaving both the "how do we handle non-String?" and "how do we supply empty strings?" questions as future work. This PR is already useful as-is and we can extend later. Furthermore, I think the current behaviour doesn't prevent us from resolving either of those questions. If folks are ok with this, let's see about getting it merged in.

@robrix robrix marked this pull request as ready for review August 2, 2022 18:27
Copy link
Member Author

@robrix robrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for review; see above for comment about non-string-valued globals & empty strings. If we're ok with leaving them as future work, I'll file issues when merging this PR.

version = "2.32"
version = "3.2"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to bump this to be able to supply the args the way I wanted.

Comment on lines -39 to +42
.short("q")
.short('q')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consequence of bumping clap.

Comment on lines +79 to +89
let globals = matches.get_many::<String>("global").unwrap_or_default();
let mut globals_ = Variables::new();
for kv in globals {
let kv_ = kv
.split_once('=')
.with_context(|| format!("Expected key-value pair separated by '=', got {}.", kv))?;
globals_.add(
Identifier::from(kv_.0),
graph::Value::String(kv_.1.to_string()),
)?;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have preferred to do this with a single map but I couldn't figure out how to express the two nested levels of failure (failure in parsing global values, and failure in the surrounding context). If anyone has suggestions, I'm all ears; I think we should be able to avoid both the second variable and the mutability, and (ideally) end up with something more readable as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like how you've done it here! As you point out, it becomes messy with the two levels of error handling, and I think this rendering makes the control flow clearer.

(It's doable, and probably involves the collect method, which takes a vec of results and turns it into a result of a vec. But again, I'd rather we keep it as you've written it here.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also, if you want to bound the region where globals_ is mutable, you can do something like this:

let globals_ = globals_;

after you're done mutating it. That makes it immutable for the remainder of the scope.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like how you've done it here! As you point out, it becomes messy with the two levels of error handling, and I think this rendering makes the control flow clearer.

(It's doable, and probably involves the collect method, which takes a vec of results and turns it into a result of a vec. But again, I'd rather we keep it as you've written it here.)

Cool, if this is idiomatic then I'll be happy to :shipit:

(Also, if you want to bound the region where globals_ is mutable, you can do something like this:

let globals_ = globals_;

after you're done mutating it. That makes it immutable for the remainder of the scope.)

Oh, that's a lovely use of shadowing! Thank you.

Comment on lines -120 to +139
let mut config = ExecutionConfig::new(&mut functions, &globals).lazy(lazy);
let mut config = ExecutionConfig::new(&mut functions, &globals_).lazy(lazy);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key to how this all works: instead of passing in an empty map of Variables, we pass in the one we built above instead. Really quite tidy, that.

@robrix robrix requested a review from a team August 2, 2022 18:33
Copy link
Contributor

@dcreager dcreager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me!

Comment on lines +79 to +89
let globals = matches.get_many::<String>("global").unwrap_or_default();
let mut globals_ = Variables::new();
for kv in globals {
let kv_ = kv
.split_once('=')
.with_context(|| format!("Expected key-value pair separated by '=', got {}.", kv))?;
globals_.add(
Identifier::from(kv_.0),
graph::Value::String(kv_.1.to_string()),
)?;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like how you've done it here! As you point out, it becomes messy with the two levels of error handling, and I think this rendering makes the control flow clearer.

(It's doable, and probably involves the collect method, which takes a vec of results and turns it into a result of a vec. But again, I'd rather we keep it as you've written it here.)

@robrix
Copy link
Member Author

robrix commented Aug 4, 2022

Filed #86 & #87.

@robrix robrix merged commit d6b90aa into main Aug 4, 2022
@robrix robrix deleted the globular branch August 4, 2022 13:52
@hendrikvanantwerpen
Copy link
Collaborator

Nice improvement @robrix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Supply globals from the CLI
3 participants