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
I have been playing around with this feature-rich library to see how it operates, but the following puzzled me pretty much and
I dont understand it. It would really help what the underlying pin points are what makes the two examples destinct, wenn encoding and then decoding (which is the problem).
I have the follwing structs:
Example A (with interface)
typeBstruct {
Nint`yaml:"n"`
}
typeAstruct {
Numberint`yaml:"number" Data Interface `yaml:"data"` // here B gets assigned
}
typeInterfaceinterface {}
Example B (Wrapping the interface in a concrete type)
In Example A I can create struct var a A{Data: B{}} and marshal it properly. The unmarshal does not work as
the library throws an error that it cannot assign map[interface{}]interface{} to Interface.
In Example B I can marshal properly and also the unmarshaling works without any error.
Questions
In Example A somehow go-yaml uses reflect to iterate on the fields on A and also it is able to magically marshal Data which is an interface and does not implement UnmarshalYAML. But the unmarshaling does not work, but it works in example B, when it is wrapped in a type Wrapper which implements the UnmarshalYAML function.
I don't get why:
go-yaml cannot figure out in example A that it can unmarshal into Data? (when it could marshal it?) Why is this asymetric behavior, is it due to reflect?
In example B, I am just forwarding the interface Data to the unmarshal function which then works for unmarshaling, which makes me wonder why in this case it works?
Thanks a lot for the help. It would be great to make this example into a documentation/example once this issue resolves in something meaningful. =)
The text was updated successfully, but these errors were encountered:
I have been playing around with this feature-rich library to see how it operates, but the following puzzled me pretty much and
I dont understand it. It would really help what the underlying pin points are what makes the two examples destinct, wenn encoding and then decoding (which is the problem).
I have the follwing structs:
Example A (with interface)
Example B (Wrapping the interface in a concrete type)
Observations in the Marshal and Unmarshal Loop
In Example A I can create struct
var a A{Data: B{}}
and marshal it properly. The unmarshal does not work asthe library throws an error that it cannot assign
map[interface{}]interface{}
toInterface
.In Example B I can marshal properly and also the unmarshaling works without any error.
Questions
go-yaml
usesreflect
to iterate on the fields onA
and also it is able to magically marshalData
which is an interface and does not implement UnmarshalYAML. But the unmarshaling does not work, but it works in example B, when it is wrapped in a typeWrapper
which implements theUnmarshalYAML
function.I don't get why:
go-yaml
cannot figure out in example A that it can unmarshal intoData
? (when it could marshal it?) Why is this asymetric behavior, is it due toreflect
?Data
to theunmarshal
function which then works for unmarshaling, which makes me wonder why in this case it works?Thanks a lot for the help. It would be great to make this example into a documentation/example once this issue resolves in something meaningful. =)
The text was updated successfully, but these errors were encountered: