spf13 / viper

Go configuration with fangs
MIT License
27.03k stars 2.01k forks source link

Case sensitive keys #1014

Open sagikazarmark opened 3 years ago

sagikazarmark commented 3 years ago

This issue will track (probably) the most requested feature for Viper: case sensitivity.

Every other issue will be closed in favor of this one.

We know this is something that the community wants (although based on feedbacks, it might not be a majority), so it's being considered for Viper v2. It doesn't mean it will be implemented though.

Please see for more details https://github.com/spf13/viper#does-viper-support-case-sensitive-keys

aaronbuchwald commented 3 years ago

What is the timeline for a decision on incorporating case sensitivity into v2 as well as the release of v2?

li1234yun commented 3 years ago

Maybe key case sensitive can be viper config param, maybe like WithCaseSensitive etc. When WithCaseSensitive == true, all viper operation is case sensitive, or not.

li1234yun commented 3 years ago

I want to mention a little bit about the benefits of case sensitivity. Because the structure public fields of golang start with uppercase, when the read configuration is consistent with the structure fields, we can directly use the configuration structure for deserialization, which saves a lot of time and avoids some unnecessary intermediate conversion.

sagikazarmark commented 3 years ago

Unmarshal is case insensitive, so it should work either way.

li1234yun commented 3 years ago

Unmarshal is case insensitive, so it should work either way.

Thanks!

WeiChieh-Chen commented 3 years ago

Maybe I'm wrong, but because of the automatic function here, I have to take on additional processing work. And usually, the configuration files are set by themselves, which means that these format designs are useful to users. Is this really right ?

boatrainlsz commented 1 year ago

it's been 2 years

jeven2016 commented 1 year ago

God.... no fix .... it's 2022...

routes:
  # request path: /api/iam/**
  - serviceName: iam
    url: http://www.baidu.com/ # the url of backend service
    filters:
      - SetHeader: Host=www.test.com, X-Forwared-Host=www.test.com

the map key "SetHeader" will be "setheader" after called the method "viper.Unmarshal()"

link-duan commented 1 year ago

fix this please

dreamlu commented 1 year ago

https://github.com/spf13/viper/commit/2cb5cbf43cc6d8b777a6f1baae164e3853c8430d why delete it?

jim3ma commented 1 year ago

There are many issues about case sensitive, why it is not end till now ?

meln5674 commented 1 year ago

If this is not going to be fixed, then viper.BindPFlag() on a pflag.StringToString[Var] or calling viper.Unmarshal() on a struct with map fields should both panic, because those are both guaranteed not to do what the user intended.

sngyai commented 1 year ago

Any progress?

chayuto commented 1 year ago

i want this fix too!

when unmarshal map[string]string

this_is_cap_key:TEST VAL
kayvonkhosrowpour commented 1 year ago

This is super frustrating but I did find an undesirable workaround. If you create a custom "alias" type, viper seems to load the string case sensitive:

type ExampleKey string

And then in your config

type Config struct {
    MyMap map[ExampleKey]string `mapstructure:"myMap"`
}

And an example config

myMap:
  keyA: "1234"
  keya: "4567"

But then comes the challenge of how to reference the map keys by an ExampleKey, since the hash of the custom type is not the string itself...

win5do commented 1 year ago

Implicit case conversions in config files cause a lot of trouble, we dropped the viper library for this reason.

jazanne commented 1 year ago

@win5do curious what you switched to? this issue is causing me headaches as well...

win5do commented 1 year ago

@win5do curious what you switched to? this issue is causing me headaches as well...

@jazanne Just use go-yaml/yaml directly. It is not too difficult to support functions such as config file reading and watching.

koh-osug commented 1 year ago

Would be nice to have this implemented. I had to migrate to koanf which fits better my needs with this issue fixed.

PaleNeutron commented 1 year ago

3 years passed... unbelievable.

I have to make a lot of effort migrating to koanf, just for case.

travisnewhouse commented 1 year ago

@sagikazarmark I read the comments on #635. I understand the concern for changing the fundamental behavior of Viper to have case-sensitive keys. I am wondering if there is a possibility for incorporating this functionality as an opt-in Option only available to explicitly created Vipers (i.e., created with viper.NewWithOptions()). Thereby, users are make a conscious choice to accept a new behavior. The behavior of the "global Viper" would not change and remain case-insensitive always. Would you be willing to entertain PR that implements this idea?

maknapik commented 11 months ago

My workaround for 1.12.0 (I checked it on the latest 1.17.0 and it did not work there) is to embed a map in a list:

Config     []map[string]string

Then, it reads keys case sensitive, e.g.:

config:
  - oneTwo: threeFour
  - fiveSix: sevenEight
royalzzz commented 9 months ago

This year is 2024 😎

sagikazarmark commented 9 months ago

@royalzzz indeed it is, good observation. :)

@travisnewhouse I may consider accepting such a PR, but the concern (in addition to the ones voiced in #635) is the added complexity on top of the already overly complex implementation and the potential increase in support requests on the issue tracker (ie. why does AutomaticEnv not work (with case sensitive keys).

travisnewhouse commented 9 months ago

@sagikazarmark I understand your concerns. That's why I proposed a design in #1673 that requires explicit "opt-in" by the user without affecting the default "global Viper" that may be used by many. Please review PR #1673 and let me know your thoughts. Thank you!

c9s commented 8 months ago

oh, please add this feature, this is so critical

EngHabu commented 8 months ago

I'm sure this has been talked about enough (possibly including this proposal) but we, at flyte.org, have been using viper since 2018/2019 and from the get go we realized how messy it'll be to not have types and structure so we built these two libraries:

  1. https://pkg.go.dev/github.com/flyteorg/flyte/flytestdlib/config to allow us to define configs in code structures that automatically get tracked, parsed, that can be sections and nested and that automatically subscribe to be updated when configs are updated.. comes with cobra commands to find and validate config files as well and automatically generates expected ENV VAR names and asks viper to merge it all together
  2. https://pkg.go.dev/github.com/flyteorg/flyte/flytestdlib@v1.10.7/cli/pflags to generate command line pflags from these properties.

With these two we have weathered a lot of miss configurations and allowed us to catch mistakes very early on..

Besides pitching these libraries to viper to adopt/align directions, having these strongly typed classes allowed us to correctly parse out configs using viper and maintain case sensitivity (we create a hook to check if we are trying to unmarshal into a map and instead write configs as slices and do the conversion there)

zsyo commented 7 months ago

I hope this message finds you well. I am writing to provide feedback regarding the default case sensitivity behavior in Viper. I have encountered difficulty in understanding why Viper defaults to not distinguishing between uppercase and lowercase keys. In my opinion, it would be more intuitive for Viper to prioritize case sensitivity as the default behavior, while case insensitivity could be offered as a configurable option.

When configuring Viper, I often find myself intentionally using uppercase keys for a specific purpose. In such cases, I believe there is a deliberate reason behind my decision to use uppercase letters. If the default behavior of Viper were to differentiate between cases, it would align with the expectation that uppercase keys hold significance and should not be treated the same as their lowercase counterparts.

Moreover, considering the physical aspect, typing uppercase letters requires an additional effort of pressing the shift key. If the default behavior of Viper were to ignore case sensitivity, it would essentially render the extra effort unnecessary in many cases. This could lead to potential confusion and frustration when trying to differentiate between keys based on their case.

I kindly request that you consider revisiting the default behavior of Viper and potentially making case sensitivity the default option, with the ability to configure case insensitivity as needed. This adjustment would enhance the usability and clarity of the Viper configuration framework, aligning it more closely with user expectations.

Thank you for your attention to this matter. I appreciate your dedication to continuously improving Viper and providing a seamless experience for its users.