Skip to content
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

MarshalYAML doesn't call MarshalYAML on returned value #134

Open
rogpeppe opened this issue Oct 16, 2015 · 9 comments
Open

MarshalYAML doesn't call MarshalYAML on returned value #134

rogpeppe opened this issue Oct 16, 2015 · 9 comments

Comments

@rogpeppe
Copy link
Contributor

From reading the docs, I'd expect the following example to print "something",
but it prints "y": 99 instead.

package main

import (
    "fmt"
    "log"
    "gopkg.in/yaml.v2"
)

func main() {
    t := T{99}
    data, err := yaml.Marshal(t)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%s\n", data)
}

type T struct {
    X int
}

func (t T) MarshalYAML() (interface{}, error) {
    return S{t.X}, nil
}

type S struct {
    Y int
}

func (s S) MarshalYAML() (interface{}, error) {
    return "something", nil
}
@niemeyer
Copy link
Contributor

Interested in offering a patch for this?

@sigmonsays
Copy link

I am interested in getting this fixed too. +1

@rogpeppe
Copy link
Contributor Author

I'd love to do a patch but there are at least 10 things that I need to do more than this that I know no-one else is ever going to do... Perhaps on a flight sometime, unless I find this bug blocking some other task.

@josephbuchma
Copy link

Why MarshalYAML method of Marshaler interface returns interface{} rather than []byte like in encoding/json ?

@ghost
Copy link

ghost commented Apr 3, 2019

We ran into this. Is this something you'd consider a bug or just inaccurate documentation?

@niemeyer
Copy link
Contributor

niemeyer commented Apr 3, 2019

The expectation sounds reasonable, so tastes more like a bug than desired behavior.

I'm about to release v3 publicly, which is a good chance to get this fixed as it might break compatibility otherwise. I'll look into this.

niemeyer added a commit that referenced this issue Apr 3, 2019
@niemeyer
Copy link
Contributor

niemeyer commented Apr 3, 2019

This is fixed in v3. Will close the issue once v3 is announced.

@metux
Copy link

metux commented Oct 19, 2023

According to the source, this should be solved at least in v3.
Shall the issue be marked resolved ?

@arykalin
Copy link

arykalin commented Apr 9, 2024

I'm using v3 and MarshalYAML is still no called.
UPD: sorry. MarshalYAML is not working only if use pointer receiver. Without pointer it's working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants