You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I encode a variable f1 of Foo into some values and I try to decode said values again into another Foo f2 the values are not the same and Decode returns and error schema: invalid path "a" (and 1 other error).
f:=Foo{
X: "test",
Y: Bar{
A: "test",
B: true,
},
}
vals:=map[string][]string{}
iferr:=schema.NewEncoder().Encode(f, vals); err!=nil { // This does not failpanic(err)
}
// vals = map[a:[test] b:[true] x:[test]]varf2Fooiferr:=schema.NewDecoder().Decode(&f2, vals); err!=nil { // This failspanic(err) // panic: schema: invalid path "a" (and 1 other error)
}
This problem appears to only occur with nested structs and probably comes from the fact that Decode requires prefixes for nested values separated with a . and Encode does not add these said prefixes.
vals=map[string][]string{
"x": {"hello"},
"y.a": {"world"},
"y.b": {"true"},
}
varf3Fooiferr:=schema.NewDecoder().Decode(&f3, vals); err!=nil { // This works just finepanic(err)
}
// f3 = {hello {world true}}
Expected Behavior
It should be possible to decode the values that have been encoded with the same library. Therefore Encode should add the period separated prefixes to fields of nested structs.
f:=Foo{
X: "hello",
Y: Bar{
A: "world",
B: true,
},
}
vals:=map[string][]string{}
iferr:=schema.NewEncoder().Encode(f, vals); err!=nil { // This should not failpanic(err)
}
// Should result into // vals = map[y.a:[test] y.b:[true] x:[test]]
Steps To Reproduce
Run the following code:
package main
import"github.com/gorilla/schema"funcmain() {
f:=Foo{
X: "test",
Y: Bar{
A: "test",
B: true,
},
}
vals:=map[string][]string{}
iferr:=schema.NewEncoder().Encode(f, vals); err!=nil { // This does not failpanic(err)
}
// vals = map[a:[test] b:[true] x:[test]]varf2Fooiferr:=schema.NewDecoder().Decode(&f2, vals); err!=nil { // This failspanic(err) // panic: schema: invalid path "a" (and 1 other error)
}
}
Anything else?
No response
The text was updated successfully, but these errors were encountered:
I have created a PR that adds a method ActivateKeySeparation to the Encoder which allows the Encoder to appends the name of each nested struct field to the full key inside the values map. Though I am not sure about the name of the method. Any other suggestions?
Is there an existing issue for this?
Current Behavior
I have the two following structs:
If I encode a variable f1 of Foo into some values and I try to decode said values again into another Foo f2 the values are not the same and
Decode
returns and errorschema: invalid path "a" (and 1 other error)
.This problem appears to only occur with nested structs and probably comes from the fact that
Decode
requires prefixes for nested values separated with a.
andEncode
does not add these said prefixes.Expected Behavior
It should be possible to decode the values that have been encoded with the same library. Therefore
Encode
should add the period separated prefixes to fields of nested structs.Steps To Reproduce
Run the following code:
Anything else?
No response
The text was updated successfully, but these errors were encountered: