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
Add support for template arguments #179
Conversation
@prashantv, thanks for your PR! By analyzing the history of the files in this pull request, we identified @kriskowal, @blampe and @eklitzke to be potential reviewers. |
templateargs/argparser/arg.rl
Outdated
Value string | ||
} | ||
|
||
// Parse parses strings in the format, ${NAME} and ${NAME: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.
Add a Lit struct and return Term ( Lit | Var). Lit should produce a string with \$ => $
and \\ => \
.
templateargs/process.go
Outdated
return processMap(req, argsUnmarshalled) | ||
} | ||
|
||
func processString(v string, args map[string]interface{}) (interface{}, error) { |
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 function should unconditionally parse the string in its DSL, including escaping \$
and \\
. The grammar should return a simple AST with either a Lit or a Var term, and this function should just switch on that.
This leaves open the possibility of extending the grammar to support string interpolation like ${host}:${port}
. We can forbid this for now.
@@ -0,0 +1,3 @@ | |||
service: foo | |||
request: | |||
name: ${user:foo |
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.
Needs test for \${user}
. Would be good to test \\
, \$
.
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.
Might be good to test requests with embedded non-string arguments, e.g., as in yab -A peer-list:$(cat peers.yaml)
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.
We cover a lot of the edge cases such as \\
and \$
in TestProcessString
and TestProcessMap
already. but added a couple more to args.yaml
-- it has a fallback which is not a string, a list replacement, and testing for escapes.
8d62082
to
ce37c72
Compare
templateargs/process.go
Outdated
} | ||
|
||
// Anything that starts with "${ " should be processed as a template arg. | ||
if !strings.HasPrefix(v, "${") { |
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.
and ends with }
?
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.
Updated to use the interpolate package, which addresses most of the feedback (it parses everything, including simple literals, and allows for concatenations like ${host}:${port}
@@ -0,0 +1,3 @@ | |||
service: foo | |||
request: | |||
name: ${user:foo |
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.
We cover a lot of the edge cases such as \\
and \$
in TestProcessString
and TestProcessMap
already. but added a couple more to args.yaml
-- it has a fallback which is not a string, a list replacement, and testing for escapes.
@@ -0,0 +1,8 @@ | |||
service: foo | |||
headers: | |||
header1: ${user:foo} |
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.
Note: this is not replaced at the moment, template arguments are used for request bodies.
Before 1.0, we may want to think about processing everything in the YAML file? We would probably decode the template to a map
, use templateargs.ProcessMap
, then pass it through mapdecode
. Thoughts @kriskowal @abhinav
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.
Sounds reasonable. Although mapdecode
expects to decode into a known struct shape, not dynamic Thrift shapes.
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.
Yep, we want to use it to load into the Template
struct (known Go shape). Won't affect the body which is still a map.
Template arguments are of the format ${argname:fallback}. Arguments can be specified on the commandline using `--arg` or `-A`. Arguments are parsed as YAML values, and allow more complex types (e.g., lists) to be specified as arguments. We only use template arguments for request bodies currently. Argument parsing is based on @abhinav's work for YARPC configuration.
options.go
Outdated
@@ -52,6 +52,7 @@ type RequestOptions struct { | |||
Health bool `long:"health" description:"Hit the health endpoint, Meta::health"` | |||
Timeout timeMillisFlag `long:"timeout" default:"1s" description:"The timeout for each request. E.g., 100ms, 0.5s, 1s. If no unit is specified, milliseconds are assumed."` | |||
YamlTemplate string `short:"y" long:"yaml-template" description:"Send a tchannel request specified by a yaml template"` | |||
TemplateArgs map[string]string `short:"A" long:"arg" description:"A list of key-value template arguments"` |
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.
add example of key-value argument format to the help. Is it -A foo=bar
or -A foo:bar
?
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.
👍
Template arguments are of the format ${argname:fallback}. Arguments can
be specified on the commandline using
--arg
or-A
. Arguments areparsed as YAML values, and allow more complex types (e.g., lists) to be
specified as arguments.
We only use template arguments for request bodies currently.
Argument parsing is based on @abhinav's work for YARPC configuration.