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

The array has been modified to an object after we call viper.Set() #1961

Open
3 tasks done
sekzerd opened this issue Dec 13, 2024 · 0 comments
Open
3 tasks done

The array has been modified to an object after we call viper.Set() #1961

sekzerd opened this issue Dec 13, 2024 · 0 comments
Labels
kind/bug Something isn't working

Comments

@sekzerd
Copy link

sekzerd commented Dec 13, 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.3

Config Source

Files

Format

JSON

Repl.it link

No response

Code reproducing the issue

package main

import (
	"fmt"
	"io"
	"os"
)
import "github.com/spf13/viper"

func printConfig() {
	f, err := os.OpenFile("./config.json", os.O_RDONLY, os.ModePerm)
	if err != nil {
		panic(err)
	}
	data, err := io.ReadAll(f)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(data))
}

func main() {
	printConfig()
	viper.SetConfigName("config")
	viper.SetConfigType("json")
	viper.AddConfigPath(".")
	err := viper.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("fatal error config file: %w", err))
	}
	viper.Set("person.0.detail.age", 29)
	err = viper.WriteConfig()
	if err != nil {
		panic(fmt.Errorf("fatal error config file: %w", err))
	}
	printConfig()
}

Expected Behavior

source config json file

{
  "person": [
    {
      "detail": {
        "age": 18
      }
    }
  ]
}

after call function

viper.Set("person.0.detail.age", 29)

this config file should be

{
  "person": [
    {
      "detail": {
        "age": 29
      }
    }
  ]
}

Actual Behavior

source config json file

{
  "person": [
    {
      "detail": {
        "age": 18
      }
    }
  ]
}

after call function

viper.Set("person.0.detail.age", 29)

this config file was changed like this

{
  "person": {
    "0": {
      "detail": {
        "age": 29
      }
    }
  }
}

Steps To Reproduce

1.create config.json in current project directory and input content

{
 "person": [
   {
     "detail": {
       "age": 18
     }
   }
 ]
}

2.go run main.go

3.watch the config.json data

Additional Information

In some programming languages like JavaScript, maybe person[0] is same to person["0"], but the changed of 'data struct' maybe cause confused issues.

@sekzerd sekzerd added the kind/bug Something isn't working label Dec 13, 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