envResolverWithCase returns resolver for environment variables with the specified case-sensitive condition. Expected to be used with `MappingWithEquals.Resolve`. Updates in `environment` may not be reflected.
(environment map[string]string, caseInsensitive bool)
| 35 | // Expected to be used with `MappingWithEquals.Resolve`. |
| 36 | // Updates in `environment` may not be reflected. |
| 37 | func envResolverWithCase(environment map[string]string, caseInsensitive bool) func(string) (string, bool) { |
| 38 | if environment == nil { |
| 39 | return func(s string) (string, bool) { |
| 40 | return "", false |
| 41 | } |
| 42 | } |
| 43 | if !caseInsensitive { |
| 44 | return func(s string) (string, bool) { |
| 45 | v, ok := environment[s] |
| 46 | return v, ok |
| 47 | } |
| 48 | } |
| 49 | // variable names must be treated case-insensitively. |
| 50 | // Resolves in this way: |
| 51 | // * Return the value if its name matches with the passed name case-sensitively. |
| 52 | // * Otherwise, return the value if its lower-cased name matches lower-cased passed name. |
| 53 | // * The value is indefinite if multiple variable matches. |
| 54 | loweredEnvironment := make(map[string]string, len(environment)) |
| 55 | for k, v := range environment { |
| 56 | loweredEnvironment[strings.ToLower(k)] = v |
| 57 | } |
| 58 | return func(s string) (string, bool) { |
| 59 | v, ok := environment[s] |
| 60 | if ok { |
| 61 | return v, ok |
| 62 | } |
| 63 | v, ok = loweredEnvironment[strings.ToLower(s)] |
| 64 | return v, ok |
| 65 | } |
| 66 | } |
no outgoing calls