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

How can I use []string and []someStruct at the same field? #974

Open
assisken opened this issue Nov 27, 2024 · 0 comments
Open

How can I use []string and []someStruct at the same field? #974

assisken opened this issue Nov 27, 2024 · 0 comments

Comments

@assisken
Copy link

Describe the bug
I wanna use []string or []someStruct as the same field to hide some complex fields from users when they not need them.
How can I make described behaviour like in example below?

To Reproduce
Example toml file

foo = ["a", "b"]
# the same as
foo = [{name = "a", value = "a"}, {name = "b", value = "b"}]
type config struct {
	Foo []fooConfig `toml:"foo"`
}

type fooConfig struct {
	Name  string `toml:"name"`
	Value string `toml:"value"`
}

func (c *fooConfig) UnmarshalText(b []byte) error {
	data := string(b)
	if data == "" { return nil } // passes when table

	c.Name = data
	c.Value = data

	return nil
}

func main() {
	var foo config

	data := []byte(`foo = ["a", "b", "c"]`)
	if toml.Unmarshal(data, &foo) != nil { log.Fatal() }
	fmt.Println(foo)  // as expected: {[{a a} {b b} {c c}]}

	data = []byte(`foo = [{name = "a", value = "a"}, {name = "b", value = "b"}]`)
	if toml.Unmarshal(data, &foo) != nil { log.Fatal() }
	fmt.Println(foo)  // just default values: {[{ } { }]}
}

Expected behavior

  • For foo = ["a", "b"] out put must be {[{a a} {b b} {c c}]} (works for now)
  • For foo = [{name = "a", value = "a"}, {name = "b", value = "b"}] the same {[{a a} {b b} {c c}]} (does not work)

Versions

  • go-toml: version v2.2.3
  • go: 1.23
  • operating system: Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant