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
Looking for anyone's input on this. I don't think it's a bug necessarily (user error more than likely), but I'm doing what I understand to be the intended behavior based on documentation and the examples I can find.
I need a custom unmarshaler for my application. I'd like to use the yaml.Node.Decode method to partially/mostly unpack a Go structure. This should throw an error (but still unpack most fields) then do custom/special handling on the field that it chokes on. The problem is that I'm running into a stack overflow error with the decode method.
There is an "easy" if difficult-to-maintain solution to unpack into a map[string]interface{} first and then manually type-assert and copy each field over (seen a couple examples of this around the internet), however of ~10 fields total, 9 should work fine with the standard decoder, so this is a non-ideal solution.
The following toy example code reproduces my issue. This seems like a reasonably common use case, so please let me know if I've fundamentally misunderstood something about how this is supposed to work (or if the non-optimal, easy solution is the only solution for the time being.)
Thanks in advance!
package main
import (
"gopkg.in/yaml.v3""log""fmt"
)
typeDataStructstruct {
Valstring
}
func (d*DataStruct) UnmarshalYAML(v*yaml.Node) error {
// Decode as much as possible (should throw error!)err:=v.Decode(d)
// TODO: Verify error is what we expected and then handle special// unmarshalingreturnerr
}
funcmain() {
d:=&DataStruct{}
s:=`val: sometestdata`err:=yaml.Unmarshal([]byte(s), d)
iferr!=nil {
log.Fatal(err)
}
fmt.Println(d)
}
If you comment out the UnmarshalYAML method, this example works as intended, but does not give me the opporunity to "intercept" the handling of certain fields.
Here's the stack overflow error (minus all of the goroutine panics):
FWIW, I also sort of get why this is failing, but also feel like wanting to leverage the default decoder for most fields within a structure while adding or modifying behavior for certain fields is a pretty common not entirely unreasonable use case, which makes me think that I've fundamentally misunderstood something about how the decoding workflow is supposed to happen, or maybe this should be feature? Happy to be told I'm way off base here. 😅
Hi,
Looking for anyone's input on this. I don't think it's a bug necessarily (user error more than likely), but I'm doing what I understand to be the intended behavior based on documentation and the examples I can find.
I need a custom unmarshaler for my application. I'd like to use the
yaml.Node.Decode
method to partially/mostly unpack a Go structure. This should throw an error (but still unpack most fields) then do custom/special handling on the field that it chokes on. The problem is that I'm running into a stack overflow error with the decode method.There is an "easy" if difficult-to-maintain solution to unpack into a
map[string]interface{}
first and then manually type-assert and copy each field over (seen a couple examples of this around the internet), however of ~10 fields total, 9 should work fine with the standard decoder, so this is a non-ideal solution.The following toy example code reproduces my issue. This seems like a reasonably common use case, so please let me know if I've fundamentally misunderstood something about how this is supposed to work (or if the non-optimal, easy solution is the only solution for the time being.)
Thanks in advance!
If you comment out the UnmarshalYAML method, this example works as intended, but does not give me the opporunity to "intercept" the handling of certain fields.
Here's the stack overflow error (minus all of the goroutine panics):
The text was updated successfully, but these errors were encountered: