Data stored in parsing get lost in provision

Code here: floaty/floaty.go at 0e650860d41d8a4ff9170588667223331373854e · ltgcgo/floaty · GitHub

During Caddyfile parsing, the plugin will attempt to store data in the new module instance. Those data however gets lost when provision began, which shouldn’t happen at all.

Most evident in FloatyModule.i, which gets set to 2 in line 35, but got reverted to the unintialized 0 (line 128) when provision begins.

I’m not sure why this happens at all, the most I can muster is some sort of bug I’m not aware of. The code was inspired by lolPants/caddy-requestid, and apart from the order which stuff gets declared in code, the rewritten version of Floaty is similar to the plugin it originated from.

[Floaty] Parsing Caddyfile... 1711398781224
[Floaty] Map not yet provisioned. Creating the map.
[Floaty] Root parameters parsed.
2
[Floaty] No errors are present in the Caddyfile.
[Floaty] Map not yet parsed. Creating the map.
0
[Floaty] Now provisioned!
1 Like

The job of the Caddyfile adapter is to produce a JSON config that Caddy will actually run with. You’re not meant to do any other work in the UnmarshalCaddyfile.

Anything in your module’s struct that doesn’t have a json tag won’t get serialized to JSON, so it will get lost after adapting. Anything that needs to be determined/modified at runtime should be initialized in Provision, and not be configuration.

You should test your Caddyfile adapter with caddy adapt --pretty --config <your-Caddyfile> to see what the JSON output looks like.

Your code looks messy and non-idiomatic. I suggest you use VSCode with the Go extension so it cleans up after you on save. You have ; everywhere, which is not a useful syntactical element in Go. You also use parentheses in your ifs which is not required. Make sure to use gofmt to clean up.

2 Likes

The job of the Caddyfile adapter is to produce a JSON config that Caddy will actually run with. You’re not meant to do any other work in the UnmarshalCaddyfile.

Oh… That’s why it didn’t store anything. Gonna try to produce a pure JSON config.

Your code looks messy and non-idiomatic.

Well I’m writing Go as if writing other C-like languages, so I’d like to keep the semicolons and parentheses for the familiarity. And no VSCode, but maybe just go fmt.

Found the real reason JSON data didn’t get stored: they require SnakeCase, not camelCase.

That’s not snake_case, that’s PascalCase.

In Go, fields with an uppercase first letter are exported (public) whereas fields with a lowercase first letter are unexported (private).

I really, strongly recommend you use VSCode, or at least a plugin/extension for whatever editor you’re using. It makes a HUGE difference. Cleans up code automatically for you on save, warnings about problems before you try to compile, etc.

Running gofmt will strip away the semicolons and parentheses because they’re non-idiomatic.

You should try to write proper Go code. The more you fight the language, the more the language will fight you back.

2 Likes