You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"bytes"
"fmt"
"github.com/BurntSushi/toml"
)
type Noun struct{ S string }
func (n Noun) MarshalTOML() ([]byte, error) {
return []byte("\"" + n.S + "\""), nil
}
func main() {
var buf bytes.Buffer
type x struct {
N Noun
}
thing := Noun{S: "hello everyone"}
if err := toml.NewEncoder(&buf).Encode(x{N: thing}); err != nil {
fmt.Println("got err: ", err)
} else {
fmt.Println("successfully marshalled a struct containing a struct with a custom marshaller")
fmt.Println(buf.String())
}
var b2 bytes.Buffer
if err := toml.NewEncoder(&b2).Encode(thing); err != nil {
fmt.Println("but you can't directly marshall a struct with a custom marshaller and i don't know why")
fmt.Println("got err: ", err)
} else {
fmt.Println("successfully marshalled a struct containing a struct with a custom marshaller")
fmt.Println(b2.String())
}
}
Maybe obviously, I'm trying to make a custom marshaller, but basically the logic for that marshaller is at the level of a file -- I want to put a version check in for the file "if they were using v 0.3 use this marshaller, if 0.4 use this other marshalling logic" but I can't see how to do that without running into the ErrNoKey errors.
The text was updated successfully, but these errors were encountered:
joe-kimmel-vmw
changed the title
Custom Marshaller is mysterious
Custom Marshaller can't be for the file
Feb 22, 2023
Hi -
I have this code:
Maybe obviously, I'm trying to make a custom marshaller, but basically the logic for that marshaller is at the level of a file -- I want to put a version check in for the file "if they were using v 0.3 use this marshaller, if 0.4 use this other marshalling logic" but I can't see how to do that without running into the ErrNoKey errors.
The text was updated successfully, but these errors were encountered: