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

key case-sensitive #697

Closed
relax-admin opened this issue May 1, 2019 · 4 comments
Closed

key case-sensitive #697

relax-admin opened this issue May 1, 2019 · 4 comments
Labels
resolution/duplicate This issue or pull request already exists

Comments

@relax-admin
Copy link

relax-admin commented May 1, 2019

Sometimes, the key is expected to be case-sensitive, such as an environment variable.Whether the viper can modify the case of the key?Such as the following KAFKA_LISTENERS

exp

  version: "3"
  kafka-server:
    environment:
      KAFKA_LISTENERS: INSIDE://:9092,OUTSIDE://:29092
    image: wurstmeister/kafka

act

  version: "3"
  kafka-server:
    environment:
      kafka_listeners: INSIDE://:9092,OUTSIDE://:29092
    image: wurstmeister/kafka

sample

https://github.com/relax-space/test-viper

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/ghodss/yaml"
	"github.com/spf13/viper"
)

func main() {
	expWrite()
	actWrite()

}

func expWrite() {
	configName := "config-exp"
	viper := viper.New()
	viper.Set("version", "3")
	viper.SetConfigName(configName)
	viper.AddConfigPath(".")

	viper.Set("kafka-server.image", "wurstmeister/kafka")
	viper.Set("kafka-server.environment.KAFKA_LISTENERS", "INSIDE://:9092,OUTSIDE://:29092")

	ymlStr, err := yamlStringSettings(viper)
	if err != nil {
		fmt.Printf("write to %v.yml error:%v\n", configName, err)
		return
	}
	ymlStr = strings.Replace(ymlStr, "kafka_listeners", "KAFKA_LISTENERS", -1)

	if writeFile(configName+".yml", ymlStr); err != nil {
		fmt.Printf("write to %v.yml error:%v", configName, err)
		return
	}
}

func actWrite() {
	viper.Set("version", "3")
	viper.SetConfigName("config-act")
	viper.AddConfigPath(".")

	viper.Set("kafka-server.image", "wurstmeister/kafka")
	viper.Set("kafka-server.environment.KAFKA_LISTENERS", "INSIDE://:9092,OUTSIDE://:29092")
	viper.WriteConfig()
}


func yamlStringSettings(vip *viper.Viper) (ymlString string, err error) {
	c := vip.AllSettings()
	bs, err := yaml.Marshal(c)
	if err != nil {
		return
	}
	ymlString = string(bs)
	return
}

func writeFile(fileName, content string) (err error) {
	out, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
	if err != nil {
		fmt.Println(err)
		return
	}
	_, err = out.WriteString(content)
	return
}

information

https://github.com/wurstmeister/kafka-docker#listener-configuration

@KISSMonX
Copy link

KISSMonX commented May 6, 2019

same error:
dbservers.hybridRW
then to map :
dbservers.hybridrw

@demond2
Copy link

demond2 commented May 6, 2019

Would #635 resolve this?

@ToJen
Copy link

ToJen commented Jun 10, 2019

@demond2 it should

@sagikazarmark
Copy link
Collaborator

Duplicate of #1014

@sagikazarmark sagikazarmark marked this as a duplicate of #1014 Nov 3, 2020
@sagikazarmark sagikazarmark added the resolution/duplicate This issue or pull request already exists label Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution/duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

5 participants