Preflight Checklist

  • [X] I have searched the issue tracker for an issue that matches the one I want to file, without success.
  • [X] I am not looking for support or already pursued the available support channels without success.
  • [X] 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

Comment From: github-actions[bot]

Issues with no activity for 30 days are marked stale and subject to being closed.