Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Custom Marshaller can't be for the file #383

Closed
joe-kimmel-vmw opened this issue Feb 21, 2023 · 0 comments
Closed

Custom Marshaller can't be for the file #383

joe-kimmel-vmw opened this issue Feb 21, 2023 · 0 comments

Comments

@joe-kimmel-vmw
Copy link

joe-kimmel-vmw commented Feb 21, 2023

Hi -

I have this code:

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.

@joe-kimmel-vmw joe-kimmel-vmw changed the title Custom Marshaller is mysterious Custom Marshaller can't be for the file Feb 22, 2023
@arp242 arp242 closed this as completed in d56d9f6 May 19, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant