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

when Set() a struct tagged with flow, format break. #1927

Open
3 tasks done
minikocha opened this issue Sep 26, 2024 · 0 comments
Open
3 tasks done

when Set() a struct tagged with flow, format break. #1927

minikocha opened this issue Sep 26, 2024 · 0 comments
Labels
kind/bug Something isn't working

Comments

@minikocha
Copy link

minikocha commented Sep 26, 2024

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.
  • I am not looking for support or already pursued the available support channels without success.
  • I have checked the troubleshooting guide for my problem, without success.

Viper Version

1.19.0

Go Version

1.23.1

Config Source

Manual set

Format

YAML

Repl.it link

No response

Code reproducing the issue

package main

import (
	"github.com/spf13/viper"
)

type Something struct {
	Id   int    `yaml:"Id,flow"`
	Name string `yaml:"Name,flow"`
}

func main() {
	s := []Something{{Id: 1, Name: "Foo"}, {Id: 2, Name: "Bar"}, {Id: 3, Name: "Baz"}}

	viper.Set("something", s)
	if err := viper.WriteConfigAs("config.yaml"); err != nil {
		panic(err)
	}
}

Expected Behavior

I thought config.yaml would be written like this:

something:
    - id: 1
      name: Foo
    - id: 2
      name: Bar
    - id: 3
      name: Baz

Actual Behavior

But it was actually written as follows:

something:
    - Id: 1
      Name: Foo
    - {Id: 2, Name: Bar}
    - {Id: 3, Name: Baz}

Steps To Reproduce

No response

Additional Information

When untagged it worked as expected.

main.go

package main

import (
	"github.com/spf13/viper"
)

type Something struct {
	Id   int
	Name string
}

func main() {
	s := []Something{{Id: 1, Name: "Foo"}, {Id: 2, Name: "Bar"}, {Id: 3, Name: "Baz"}}

	viper.Set("something", s)
	if err := viper.WriteConfigAs("config.yaml"); err != nil {
		panic(err)
	}
}

config.yaml

something:
    - id: 1
      name: Foo
    - id: 2
      name: Bar
    - id: 3
      name: Baz
@minikocha minikocha added the kind/bug Something isn't working label Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant