-
Notifications
You must be signed in to change notification settings - Fork 489
river/token/builder: add builder package to build files #1910
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
Conversation
The river/token/builder package exposes an API to construct a river file, inspired by github.com/hashicorp/v2/hclwrite. It builds a list of tokens and then formats them back to the user. It can also encode Go values directly into tokens.
func = function | ||
capsule = capsule("chan int") |
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 is kind of a weird way to represent function and capsule values, and needs some thought in the future for how we should display them to users.
Note that however they get displayed must be valid River syntax, since we need to be able to parse the tokens to format them.
@@ -0,0 +1,186 @@ | |||
// Package builder exposes an API to create a River configuration file by | |||
// constructing a set of tokens. | |||
package builder |
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'm not sure about this as a package name, but I don't want it to be too long either.
This avoids a situation where the map order is randomized. We only need to make sure one key works anyway.
pkg/river/token/builder/builder.go
Outdated
} | ||
} | ||
|
||
toks = append(toks, Token{Type: token.ILLEGAL, Lit: " "}) |
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.
Same here and below, not sure I get the ILLEGAL part. Is it because it's just for presentation so it doesn't matter?
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.
See the Token docstring:
// A Token is a wrapper around token.Token which contains the token type
// alongside its literal. Use token.ILLEGAL to write literal characters such as
// whitespace.
We just unfortunately don't have a good way to say "this should be written as-is" other than re-using token.ILLEGAL.
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.
LGTM 🎉
pkg/river/token/builder/token.go
Outdated
// river/token doesn't have the capability of representing literal strings, so | ||
// LiteralTok uses token.ILLEGAL as a way to uniquely identify when literal | ||
// strings should be injected. | ||
const LiteralTok = token.ILLEGAL |
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 feels slightly weird. It represents "whitespace" commands correct? It works, would adding a whitespace token work?
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.
It could be any literal text. We could add token.LITERAL, but we'd have to make it clear that the scanner never emits it and it's just for the token builder package.
WDYT?
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.
That feels better to me than the verbiage of illegal, but if it makes more sense to you to use illegal then keep it.
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 think it's probably fine to add a token.LITERAL since there's multiple consumers of the token package anyway
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.
LGTM , question on literaltoken but thats it
PR Description
The river/token/builder package exposes an API to construct a River file, inspired by github.com/hashicorp/v2/hclwrite. It builds a list of tokens and then formats them back to the user.
It can also encode Go values directly into tokens.
Which issue(s) this PR fixes
Related to #1839.
Notes to the Reviewer
This also fixes some new small formatting bugs found when printing out lists that contain objects.
PR Checklist