-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
v3: Feature Request: Decoder/Encoder Options #639
Comments
Continuing to convert the code from v2 to v3, I found a subtle bug that might hit other people. Unmarshal and Decode handle empty buffers differently. yaml.Unmarshal([]byte{}, &out) == nil yaml.NewDecoder(bytes.NewReader([]byte{})).Decode(&out) == io.EOF Naively replacing: if err := yaml.UnmarshalStrict(b, &out); err != nil { /* ... */ } with: dec := yaml.NewDecoder(bytes.NewReader(b))
dec.KnownFields(true)
if err := dec.Decode(&out); err != nil { /* ... */ } won't work. To maintain the existing behavior, you also need to check for io.EOF: dec := yaml.NewDecoder(bytes.NewReader(b))
dec.KnownFields(true)
if err := dec.Decode(&out); err != nil && err != io.EOF { /* ... */ } |
Great idea, i'd like to have it |
Maybe restoring |
This was referenced Jun 7, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was trying to upgrade a project from v2 to v3 and found liberal uses of
yaml.UmarshalStrict
which is no longer available in v3. I assume the removal of that function was a conscious decision andUnmarshalStrict
is being avoided along withMarshalIndent
.Instead of having to wrap the bytes with a
bytes.Reader
, create aDecoder
, calldec.KnownFields(true)
, thenDecode
, I think it would be more ergonomic to have options for Marshal/Unmarshal and/or NewDecoder/NewEncoder.This would allow:
Similar for Marshal and SetIndent.
Or just add
UnmarshalStrict
andMarshalIndent
functions :)The text was updated successfully, but these errors were encountered: