BurntSushi / toml

TOML parser for Golang with reflection.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Marshaler Should not writeQuoted string

lucasbutn opened this issue · comments

commented

Hi, hope you can understand this issue (if it is really an issue).

In enconde.go Line 210

case Marshaler:
		s, err := v.MarshalTOML()
		if err != nil {
			encPanic(err)
		}
		enc.writeQuoted(string(s))
		return

If the struct implements MarshalToml to output a valid toml, this piece of code is writing the result with "double quotes" which is the same as TextMarshaler, which I don't think this is meant to this.

What I'm trying to do is to Marshall an structure to a single value, and I'm getting a quoted value

In my case if I have:

type WrappedBool struct {
    Value bool
}

And I what to marshall this:

type SomeStruct struct {
       BooleanValue *WrappedBool
}

Without custom marshall:

[SomeStruct]
    [SomeStruct.BooleanValue] = true

With Custom Marshall:

[SomeStruct]
    SomeValue = "true"

What it actually should be

[SomeStruct]
    SomeValue = true

This double quotes are added outside the MarshalTOML() function and according to the interface comments, it should not be like this right?:

// Marshaler is the interface implemented by types that can marshal themselves
// into valid TOML.
type Marshaler interface {
	MarshalTOML() ([]byte, error)
}

Or this is some expected behavior?

Is there is another way to implement what I'm trying to do and I didn't figure it out?

I think this is because I copied this by accident from the already existing TextMarshaler. You should be able to write true rather than "true" in any case. I'll have a more detailed look later.

commented

I think this is because I copied this by accident from the already existing TextMarshaler. You should be able to write true rather than "true" in any case. I'll have a more detailed look later.

Hi, did you had any chance to took at this issue. I'll share here some code to test it

https://go.dev/play/p/WcKntwa2rze

See, I've compared to json marshal to illustrate the expected behaviour

Just run it and you will see what is wrong. MarshalTOML result should no be wrote with quotes. But please confirm

did you had any chance to took at this issue.

No, sorry; I haven't had the time. Not sure if I have in the coming weeks, but I'll be happy to review and merge patches in a timely fashion.

commented

did you had any chance to took at this issue.

No, sorry; I haven't had the time. Not sure if I have in the coming weeks, but I'll be happy to review and merge patches in a timely fashion.

Thanks a lot!
Here is the fix:

#344

Feel free to suggest any changes or ask for extra clarification if required.

My main concern is that this could affect previous implementations of MarshalTOML