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

caddyfile: Expose Tokenize function for modules to use the lexer #3549

Merged
merged 1 commit into from
Jul 20, 2020

Conversation

francislavoie
Copy link
Member

As requested by @lucaslorentz in this comment https://github.com/lucaslorentz/caddy-docker-proxy/pull/160/files#r449789804

This exposes a Tokenize function which was only in the lexer test previously to do the work of lexing bytes as input to get the list of tokens.

One quirk that I'm not sure how best to handle is the filename param. In other languages I'd make that an optional param, but Go doesn't have that, so I'm just using "" as the empty value to keep File nil.

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thanks! Just a few suggestions before we merge...

caddyconfig/caddyfile/lexer.go Outdated Show resolved Hide resolved
caddyconfig/caddyfile/lexer.go Show resolved Hide resolved
// a list of tokens that can be parsed as a Caddyfile.
// Also takes a filename to fill the token's File as
// the source of the tokens, which is important to
// determine relative paths for `import` directives.
Copy link
Member

Choose a reason for hiding this comment

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

Add a scary warning that this function is experimental and is not guaranteed to be stable.

Copy link
Member Author

@francislavoie francislavoie Jul 6, 2020

Choose a reason for hiding this comment

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

I don't think it's experimental... It's the exact same code that was in parser.go and also in the tests (with I guess very minor differences due to types)

Copy link
Member

Choose a reason for hiding this comment

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

Whether it's experimental or not, we need to say that it's not stable ("experimental" is a good consistent term) because I wasn't ever expecting to export this API when I designed 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'm not sure I understand how it wouldn't be stable if it's actually used by the Caddyfile parser itself.

Copy link
Member

Choose a reason for hiding this comment

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

I just mean that right now we can change it whenever we want, because we know it's unexported and nobody else is using it. I just try to avoid exporting things.

Choose a reason for hiding this comment

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

As I mentioned in the other PR, this is not a must-have.
We can give it some time and see how caddy-docker-proxy code will evolve.
And also see if the lexer will change that often in caddy

Copy link
Member

Choose a reason for hiding this comment

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

@lucaslorentz Is Tokenize() the right function to export? Or do you need env var replacements, I can't remember. If you need env var replacements, maybe allTokens is better: https://github.com/caddyserver/caddy/pull/3549/files#diff-06ecc7b9618d6f991e3d8846d817e0dcR85

Copy link
Member Author

@francislavoie francislavoie Jul 17, 2020

Choose a reason for hiding this comment

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

I'm pretty certain we don't want env var replacement to happen because the step that caddy-docker-proxy takes is just rebuilding a valid Caddyfile from the labels the user inputs. The env vars can be replaced later when Caddy actually loads that rebuilt config. If env var replacement was done at that stage, then it would use the env of the controller (the part that is doing the reading of labels off the docker containers), which might not be the correct stuff.

Copy link

@lucaslorentz lucaslorentz Jul 17, 2020

Choose a reason for hiding this comment

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

I don't need want env var replacements.

I have my own in memory representation of caddyfile, that doesn't depend on plugins structs and is still easy to manipulate.
https://github.com/lucaslorentz/caddy-docker-proxy/blob/master/plugin/caddyfile/caddyfile.go

I also have a tiny parser, that parses all tokens into my in memory caddyfile struct:
https://github.com/lucaslorentz/caddy-docker-proxy/blob/master/plugin/caddyfile/marshal.go#L164

Currently, the tokenization is an overlap between caddy implementation and my own implementation. Thus, we're considering exposing that feature.

But, another option, is to expose in caddy a function similar to the existing caddyfile.Parse, but that keeps env variables and snippets untouched. Then I could change my parser to read ServerBlocks returned by parseCaddyfile and tokens from each ServerBlocks.

Copy link
Member

Choose a reason for hiding this comment

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

I see, okay. Thanks!

@mholt mholt merged commit fb9d874 into caddyserver:master Jul 20, 2020
@mholt mholt added this to the v2.2.0 milestone Jul 20, 2020
@francislavoie francislavoie deleted the refactor-lexer branch July 20, 2020 20:50
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.

3 participants