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
For example, take a look at docker-compose file. build can be a string or a struct, and that's perfectly valid. To unmarshal such a situtation, I thought I could try to unmarshal a string before, and then a struct if the former errored out. Unfortunately I found out that TypeError exists, but it's internal:
I managed to parse my stuff correctly using a struct with 2 fields: one string and one struct.
Then I implemented the InterfaceUnmarshaler interface for it, something like that (ironically, #446 was pretty much the documentation I was missing to understand how to do this):
typeA {
Namestring
}
typeDatastruct {
AABstring
}
func (data*Data) UnmarshalYAML(unmarshalfunc(interface{}) error) error {
// Try to unmarshal as a stringerrUnmarshalAsString:=unmarshal(&data.B)
iferrUnmarshalAsString==nil {
// Successreturnnil
}
// Try to unmarshal as a maperrUnmarshalAsStruct:=unmarshal(&data.A)
iferrUnmarshalAsStruct!=nil {
// Both tries failederr:=fmt.Errorf("Failed to unmarshal \"data\" as either a string or as struct A:\n %w\n %w\n",
errUnmarshalAsString, errUnmarshalAsStruct)
log.Print(err)
returnerr
}
// Successreturnnil
}
For example, take a look at docker-compose file.
build
can be a string or a struct, and that's perfectly valid. To unmarshal such a situtation, I thought I could try to unmarshal a string before, and then a struct if the former errored out. Unfortunately I found out thatTypeError
exists, but it's internal:https://github.com/goccy/go-yaml/blob/v1.9.8/internal/errors/error.go#L225
Could you expose it, or suggest me an alternative way to unmarshal a value which may be of two different types?
The text was updated successfully, but these errors were encountered: