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

the returned value of MarshalTOML is not used as TOML in marshalling #414

Closed
cofyc opened this issue Jun 1, 2020 · 3 comments
Closed

the returned value of MarshalTOML is not used as TOML in marshalling #414

cofyc opened this issue Jun 1, 2020 · 3 comments
Assignees
Labels
bug Issues describing a bug in go-toml.

Comments

@cofyc
Copy link

cofyc commented Jun 1, 2020

Describe the bug

the returned value of MarshalTOML is used as a TOML value, not a raw TOML formatted value

To Reproduce

This example shows the difference between JSON/TOML marshaling:

package main

import (
	"encoding/json"
	"fmt"
	"strconv"

	"github.com/pelletier/go-toml"
)

type IntOrString string

func (x *IntOrString) MarshalTOML() ([]byte, error) {
	s := *(*string)(x)
	_, err := strconv.Atoi(s)
	if err != nil {
		return []byte(fmt.Sprintf(`"%s"`, s)), nil
	}
	return []byte(s), nil
}

func (x *IntOrString) MarshalJSON() ([]byte, error) {
	return x.MarshalTOML()
}

type foo struct {
	X *IntOrString
}

func main() {
	xx := IntOrString("111")
	d := foo{
		X: &xx,
	}
	s1, _ := toml.Marshal(d)
	fmt.Printf("toml:\n%s\n", string(s1))
	s2, _ := json.Marshal(d)
	fmt.Printf("json:\n%s\n", string(s2))
}

The current output is:

toml:
X = "111"

json:
{"X":111}

See https://play.golang.org/p/8RbpHT0v86L

Expected behavior

The returned value of MarshalYAML should be used as a valid TOML, otherwise, it can't be used to generate TOML for customized types.

The expected output of the above program:

toml:
X = 111

json:
{"X":111}

Versions

  • go-toml: 1.8.0
  • go: 1.13.9
  • operating system: os neutral

Additional context
Add any other context about the problem here that you think may help to diagnose.

@cofyc cofyc changed the title the returned value of MarshalYAML is not used as TOML in marshalling the returned value of MarshalTOML is not used as TOML in marshalling Jun 1, 2020
@cofyc
Copy link
Author

cofyc commented Jun 1, 2020

cc @pelletier what do you think?

@pelletier pelletier added the bug Issues describing a bug in go-toml. label Jun 1, 2020
@pelletier
Copy link
Owner

Thanks for reporting this! This behavior is not correct, we should mimic encoding/json's.

@pelletier
Copy link
Owner

This should have been fixed in 05bf380.

Let me know if that's not the case!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues describing a bug in go-toml.
Projects
None yet
Development

No branches or pull requests

3 participants