Skip to content

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

Merged
merged 4 commits into from
Jul 18, 2022

Conversation

rfratto
Copy link
Member

@rfratto rfratto commented Jul 15, 2022

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

  • CHANGELOG updated (N/A)
  • Documentation added (N/A)
  • Tests updated

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.
@rfratto rfratto requested review from tpaschalis and mattdurham July 15, 2022 18:21
Comment on lines +80 to +81
func = function
capsule = capsule("chan int")
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 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
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'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.
}
}

toks = append(toks, Token{Type: token.ILLEGAL, Lit: " "})
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@tpaschalis tpaschalis left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

// 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
Copy link
Collaborator

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?

Copy link
Member Author

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?

Copy link
Collaborator

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.

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 think it's probably fine to add a token.LITERAL since there's multiple consumers of the token package anyway

Copy link
Collaborator

@mattdurham mattdurham left a 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

@rfratto rfratto enabled auto-merge (squash) July 18, 2022 21:01
@rfratto rfratto merged commit 1c6fab4 into grafana:main Jul 18, 2022
@rfratto rfratto deleted the river-build-token branch July 18, 2022 21:35
@github-actions github-actions bot added the frozen-due-to-age Locked due to a period of inactivity. Please open new issues or PRs if more discussion is needed. label Mar 21, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
frozen-due-to-age Locked due to a period of inactivity. Please open new issues or PRs if more discussion is needed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants