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

Feature Request: Case Sensitive Keys #1013

Closed
aaronbuchwald opened this issue Nov 3, 2020 · 5 comments
Closed

Feature Request: Case Sensitive Keys #1013

aaronbuchwald opened this issue Nov 3, 2020 · 5 comments
Labels
resolution/duplicate This issue or pull request already exists

Comments

@aaronbuchwald
Copy link

Case sensitive keys are useful in many contexts, but they are not currently possible using viper.

@github-actions
Copy link

github-actions bot commented Nov 3, 2020

👋 Thanks for reporting!

A maintainer will take a look at your issue shortly. 👀

In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news,
either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️

@aaronbuchwald
Copy link
Author

Actually, even in the case where I call Get on an individual key which maps to a JSON object, it still makes the keys of that JSON object lower case as well.

{"key": {"CaseSensitive": {"key":"value"}}}

Is there any way to get the value of the "CaseSensitive" key in this example using viper?

@chenxxpro
Copy link

See 2cb5cbf

@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
@dreamlu
Copy link

dreamlu commented Dec 13, 2022

2cb5cbf
why delete it? it's useful
so i use a messy method to solve it:
source

func (c *Yaml) Unmarshal(v any) {
	mp := c.AllSettings()
	c.yamlUnmarshal(mp, v)
}

func (c *Yaml) UnmarshalKey(key string, v any) {
	if mp := c.Get(key); mp != nil {
		c.yamlUnmarshal(mp.(map[string]any), v)
	}
}

func (c *Yaml) yamlUnmarshal(viper map[string]any, v any) {
	var (
		typ = mr.TrueTypeof(v)
		val = mr.TrueValueOf(v)
	)
	c.yamlViper(viper, typ, val)
	bs, err := yaml.Marshal(viper)
	if err != nil {
		panic(err)
	}
	err = yaml.Unmarshal(bs, v)
	if err != nil {
		panic(err)
	}
}

func (c *Yaml) yamlViper(viper map[string]any, typ reflect.Type, v reflect.Value) {
	var (
		tn = typ.NumField()
	)
	for i := 0; i < tn; i++ {
		field := typ.Field(i)
		val := v.Field(i)
		if field.Anonymous {
			c.yamlViper(viper, field.Type, val)
			continue
		}
		tv := field.Tag.Get(cons.Yaml)
		key := tv
		if tv == "" {
			tv = strings.ToLower(field.Name)
			key = util.HumpToLine(field.Name)
		}
		tv = strings.ToLower(tv)
		if vs := viper[tv]; vs != nil {
			switch field.Type.Kind() {
			case reflect.Struct:
				c.yamlViper(vs.(map[string]any), field.Type, val)
			default:
				delete(viper, tv)
				viper[key] = vs
			}
		}
	}
}

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

4 participants