1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"fmt"
"sort"
"strings"
)
// MappingWithEquals is a mapping type that can be converted from a list of
// key[=value] strings.
// For the key with an empty value (`key=`), the mapped value is set to a pointer to `""`.
// For the key without value (`key`), the mapped value is set to nil.
type MappingWithEquals map[string]*string
// NewMappingWithEquals build a new Mapping from a set of KEY=VALUE strings
func NewMappingWithEquals(values []string) MappingWithEquals {
mapping := MappingWithEquals{}
for _, env := range values {
tokens := strings.SplitN(env, "=", 2)
if len(tokens) > 1 {
mapping[tokens[0]] = &tokens[1]
} else {
mapping[env] = nil
}
}
return mapping
}
// OverrideBy update MappingWithEquals with values from another MappingWithEquals
func (m MappingWithEquals) OverrideBy(other MappingWithEquals) MappingWithEquals {
for k, v := range other {
m[k] = v
}
return m
}
// Resolve update a MappingWithEquals for keys without value (`key`, but not `key=`)
func (m MappingWithEquals) Resolve(lookupFn func(string) (string, bool)) MappingWithEquals {
for k, v := range m {
if v == nil {
if value, ok := lookupFn(k); ok {
m[k] = &value
}
}
}
return m
}
// RemoveEmpty excludes keys that are not associated with a value
func (m MappingWithEquals) RemoveEmpty() MappingWithEquals {
for k, v := range m {
if v == nil {
delete(m, k)
}
}
return m
}
func (m MappingWithEquals) ToMapping() Mapping {
o := Mapping{}
for k, v := range m {
if v != nil {
o[k] = *v
}
}
return o
}
func (m *MappingWithEquals) DecodeMapstructure(value interface{}) error {
switch v := value.(type) {
case map[string]interface{}:
mapping := make(MappingWithEquals, len(v))
for k, e := range v {
mapping[k] = mappingValue(e)
}
*m = mapping
case []interface{}:
mapping := make(MappingWithEquals, len(v))
for _, s := range v {
k, e, ok := strings.Cut(fmt.Sprint(s), "=")
if !ok {
mapping[k] = nil
} else {
mapping[k] = mappingValue(e)
}
}
*m = mapping
default:
return fmt.Errorf("unexpected value type %T for mapping", value)
}
return nil
}
// label value can be a string | number | boolean | null
func mappingValue(e interface{}) *string {
if e == nil {
return nil
}
switch v := e.(type) {
case string:
return &v
default:
s := fmt.Sprint(v)
return &s
}
}
// Mapping is a mapping type that can be converted from a list of
// key[=value] strings.
// For the key with an empty value (`key=`), or key without value (`key`), the
// mapped value is set to an empty string `""`.
type Mapping map[string]string
// NewMapping build a new Mapping from a set of KEY=VALUE strings
func NewMapping(values []string) Mapping {
mapping := Mapping{}
for _, value := range values {
parts := strings.SplitN(value, "=", 2)
key := parts[0]
switch {
case len(parts) == 1:
mapping[key] = ""
default:
mapping[key] = parts[1]
}
}
return mapping
}
// convert values into a set of KEY=VALUE strings
func (m Mapping) Values() []string {
values := make([]string, 0, len(m))
for k, v := range m {
values = append(values, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(values)
return values
}
// ToMappingWithEquals converts Mapping into a MappingWithEquals with pointer references
func (m Mapping) ToMappingWithEquals() MappingWithEquals {
mapping := MappingWithEquals{}
for k, v := range m {
v := v
mapping[k] = &v
}
return mapping
}
func (m Mapping) Resolve(s string) (string, bool) {
v, ok := m[s]
return v, ok
}
func (m Mapping) Clone() Mapping {
clone := Mapping{}
for k, v := range m {
clone[k] = v
}
return clone
}
// Merge adds all values from second mapping which are not already defined
func (m Mapping) Merge(o Mapping) Mapping {
for k, v := range o {
if _, set := m[k]; !set {
m[k] = v
}
}
return m
}
func (m *Mapping) DecodeMapstructure(value interface{}) error {
switch v := value.(type) {
case map[string]interface{}:
mapping := make(Mapping, len(v))
for k, e := range v {
if e == nil {
e = ""
}
mapping[k] = fmt.Sprint(e)
}
*m = mapping
case []interface{}:
*m = decodeMapping(v, "=")
default:
return fmt.Errorf("unexpected value type %T for mapping", value)
}
return nil
}
// Generate a mapping by splitting strings at any of seps, which will be tried
// in-order for each input string. (For example, to allow the preferred 'host=ip'
// in 'extra_hosts', as well as 'host:ip' for backwards compatibility.)
func decodeMapping(v []interface{}, seps ...string) map[string]string {
mapping := make(Mapping, len(v))
for _, s := range v {
for i, sep := range seps {
k, e, ok := strings.Cut(fmt.Sprint(s), sep)
if ok {
// Mapping found with this separator, stop here.
mapping[k] = e
break
} else if i == len(seps)-1 {
// No more separators to try, map to empty string.
mapping[k] = ""
}
}
}
return mapping
}
|