-
Notifications
You must be signed in to change notification settings - Fork 19
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
Empty map deserialized as string #14
Comments
Hello, I confirm the bug, as a workaround, you have to define explicitly the default value of at least one of the fields. Paerser handles different sources of data (mainly labels, but also files and env vars), and this diversity of sources induces complexity of the side effects. |
No doubt parsing is a complex task! Would you accept a PR to fix this specifically for the |
Ahh, after testing a bit further, I think I see why this is an issue specifically for plugins. Emtpy maps are only allowed via the type Router struct {
// -- snip --
TLS *RouterTLSConfig `json:"tls,omitempty" toml:"tls,omitempty" yaml:"tls,omitempty" label:"allowEmpty" file:"allowEmpty" kv:"allowEmpty" export:"true"`
} But since a plugin's config is deserialized as a type Configuration struct {
Key map[string]interface{} `json:"key" toml:"key" yaml:"key"`
}
const yamlContent = `key:
emptyMap: {}
`
func TestYaml(t *testing.T) {
configuration := &Configuration{}
err := file.DecodeContent(yamlContent, ".yaml", configuration)
if err != nil {
t.Fatal(err)
}
assertEmptyMap(t, configuration)
}
func assertEmptyMap(t *testing.T, configuration *Configuration) {
t.Helper()
value := configuration.Key["emptyMap"]
assert.IsType(t, "", value) // PASS
assert.IsType(t, make(map[string]interface{}), value) // FAIL
} So, I think this only affects deserialization of |
Hello @ezracelli,
This feature would not make it to our roadmap as we are focused elsewhere. If you or a community member would like to build it, let us know, and we will work with you to ensure you have all the information needed to merge it. We prefer to work with our community members at the beginning of the design process to ensure that we are aligned and can move quickly with the review and merge process. Let us know here or create a PR before you start, and we will work with you there. Don’t forget to check out the contributor docs and link the PR to this issue. |
I think this is the same issue as traefik/traefik#8772, which was unfortunately closed without resolution.
The following traefik dynamic configuration object:
Is deserialized as (copied from traefik's
--log.level=DEBUG
logs):variantA
sub-option is""
(empty string) instead of{}
(empty map) in the JSON serializationpaerser_test.go
Context
I'm writing a traefik plugin which accepts one of a few possible configuration variants, which I was planning on implementing in the "traefik way" by using the pattern of one of many map fields.
For example, traefik dynamic config accepts:
...where "variant" might be
addPrefix
(the type of the middleware); exactly one variant is accepted.In the same vein, I was structuring my plugin's config to be something like:
...where "variant" is one of the few possibilities mentioned above; exactly one variant is accepted. However, I ran into this issue because one of my "variants" doesn't have any sub-options, but the key is still required to be present. I believe this does not affect traefik because all its "variants" have sub-options.
plugin.go
With the following dynamic config (same as above):
...I get the following error message from traefik:
The text was updated successfully, but these errors were encountered: