-
-
Notifications
You must be signed in to change notification settings - Fork 528
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
add Marshaler interface #137
Conversation
@BurntSushi ping |
@@ -114,6 +120,9 @@ func (enc *Encoder) encode(key Key, rv reflect.Value) { | |||
case time.Time, TextMarshaler: | |||
enc.keyEqElement(key, rv) | |||
return | |||
|
|||
case Marshaler: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a user-defined type is a Marshaler and a TextMarshaler, the former should take precedence.
@cespare I've force pushed changes with Marshaler precedence. |
@BurntSushi @cespare ping |
@BurntSushi @cespare any reaction? |
@kovetskiy sorry, been busy. I'll try to take a look this weekend. |
@cespare @BurntSushi ping |
1 similar comment
@cespare @BurntSushi ping |
jeez @kovetskiy it must hurt lol |
@tucnak yeah, such maintainers |
@@ -537,6 +551,21 @@ func encPanic(err error) { | |||
panic(tomlEncodeError{err}) | |||
} | |||
|
|||
func marshalDecode(rv reflect.Value) reflect.Value { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're already calling rv.Interface().(Marshaler)
in the caller; pass in a Marshaler
rather than a reflect.Value
and avoid the duplicate work.
I was hoping that adding Marshaler could be a solution for #64, #75, and other cases where there's no way to output a particular encoded value. For example, I was thinking that if you have a MarshalTOML method like func (T) MarshalTOML() ([]byte, error) {
return []byte(`# hello
a = """
1
2
3"""
b = '\n'`), nil
} then that would yield a TOML encoding with a comment, a multi-line string, and a literal string: # hello
a = """
1
2
3"""
b = '\n' but since your approach is to decode and then encode the value, we instead get a = "1\n2\n3"
b = "\\n" I agree that we need to decode the value to check that it's valid TOML, but I think we should emit the original encoding. WDYT? |
@kovetskiy Any plans to address the comments here? |
Hi @BurntSushi, I've noticed that there is no nice way to handle Marshal/Encode operation unless implementing
encoding.TextMarshaler
interface, which can't be used if struct should be also marshalable to JSON (json libraries also looks forencoding.TextMarshaler
),so I've added
Marshaler
interface with following signature:Also I've added various tests for this feautre.