|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package typed |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "sigs.k8s.io/structured-merge-diff/schema" |
| 23 | + "sigs.k8s.io/structured-merge-diff/value" |
| 24 | +) |
| 25 | + |
| 26 | +func normalizeUnion(w *mergingWalker) error { |
| 27 | + atom, found := w.schema.Resolve(w.typeRef) |
| 28 | + if !found { |
| 29 | + panic(fmt.Sprintf("Unable to resolve schema in normalize union: %v/%v", w.schema, w.typeRef)) |
| 30 | + } |
| 31 | + // Unions can only be in structures, and the struct must not have been removed |
| 32 | + if atom.Struct == nil || atom.Struct.Union == nil || w.out == nil { |
| 33 | + return nil |
| 34 | + } |
| 35 | + |
| 36 | + old := &value.Map{} |
| 37 | + if w.lhs != nil { |
| 38 | + old = w.lhs.MapValue |
| 39 | + } |
| 40 | + return newUnion(atom.Struct.Union).Normalize(old, w.rhs.MapValue, w.out.MapValue) |
| 41 | +} |
| 42 | + |
| 43 | +type discriminated string |
| 44 | +type field string |
| 45 | + |
| 46 | +type discriminatedNames struct { |
| 47 | + f2d map[field]discriminated |
| 48 | + d2f map[discriminated]field |
| 49 | +} |
| 50 | + |
| 51 | +func newDiscriminatedName(f2d map[field]discriminated) discriminatedNames { |
| 52 | + d2f := map[discriminated]field{} |
| 53 | + for key, value := range f2d { |
| 54 | + d2f[value] = key |
| 55 | + } |
| 56 | + return discriminatedNames{ |
| 57 | + f2d: f2d, |
| 58 | + d2f: d2f, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (dn discriminatedNames) toField(d discriminated) field { |
| 63 | + if f, ok := dn.d2f[d]; ok { |
| 64 | + return f |
| 65 | + } |
| 66 | + return field(d) |
| 67 | +} |
| 68 | + |
| 69 | +func (dn discriminatedNames) toDiscriminated(f field) discriminated { |
| 70 | + if d, ok := dn.f2d[f]; ok { |
| 71 | + return d |
| 72 | + } |
| 73 | + return discriminated(f) |
| 74 | +} |
| 75 | + |
| 76 | +type discriminator struct { |
| 77 | + name string |
| 78 | +} |
| 79 | + |
| 80 | +func (d *discriminator) Set(m *value.Map, v discriminated) { |
| 81 | + if d == nil { |
| 82 | + return |
| 83 | + } |
| 84 | + m.Set(d.name, value.StringValue(string(v))) |
| 85 | +} |
| 86 | + |
| 87 | +func (d *discriminator) Get(m *value.Map) discriminated { |
| 88 | + if d == nil || m == nil { |
| 89 | + return "" |
| 90 | + } |
| 91 | + f, ok := m.Get(d.name) |
| 92 | + if !ok { |
| 93 | + return "" |
| 94 | + } |
| 95 | + if f.Value.StringValue == nil { |
| 96 | + return "" |
| 97 | + } |
| 98 | + return discriminated(*f.Value.StringValue) |
| 99 | +} |
| 100 | + |
| 101 | +type fieldsSet map[field]struct{} |
| 102 | + |
| 103 | +// newFieldsSet returns a map of the fields that are part of the union and are set |
| 104 | +// in the given map. |
| 105 | +func newFieldsSet(m *value.Map, fields []field) fieldsSet { |
| 106 | + if m == nil { |
| 107 | + return nil |
| 108 | + } |
| 109 | + set := fieldsSet{} |
| 110 | + for _, f := range fields { |
| 111 | + if _, ok := m.Get(string(f)); ok { |
| 112 | + set.Add(f) |
| 113 | + } |
| 114 | + } |
| 115 | + return set |
| 116 | +} |
| 117 | + |
| 118 | +func (fs fieldsSet) Add(f field) { |
| 119 | + if fs == nil { |
| 120 | + fs = map[field]struct{}{} |
| 121 | + } |
| 122 | + fs[f] = struct{}{} |
| 123 | +} |
| 124 | + |
| 125 | +func (fs fieldsSet) One() *field { |
| 126 | + for f := range fs { |
| 127 | + return &f |
| 128 | + } |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func (fs fieldsSet) Has(f field) bool { |
| 133 | + _, ok := fs[f] |
| 134 | + return ok |
| 135 | +} |
| 136 | + |
| 137 | +func (fs fieldsSet) List() []field { |
| 138 | + fields := []field{} |
| 139 | + for f := range fs { |
| 140 | + fields = append(fields, f) |
| 141 | + } |
| 142 | + return fields |
| 143 | +} |
| 144 | + |
| 145 | +func (fs fieldsSet) Difference(o fieldsSet) fieldsSet { |
| 146 | + n := fieldsSet{} |
| 147 | + for f := range fs { |
| 148 | + if !o.Has(f) { |
| 149 | + n.Add(f) |
| 150 | + } |
| 151 | + } |
| 152 | + return n |
| 153 | +} |
| 154 | + |
| 155 | +type union struct { |
| 156 | + d *discriminator |
| 157 | + dn discriminatedNames |
| 158 | + f []field |
| 159 | +} |
| 160 | + |
| 161 | +func newUnion(su *schema.Union) *union { |
| 162 | + u := &union{} |
| 163 | + if su.Discriminator != nil { |
| 164 | + u.d = &discriminator{name: *su.Discriminator} |
| 165 | + } |
| 166 | + f2d := map[field]discriminated{} |
| 167 | + for _, f := range su.Fields { |
| 168 | + u.f = append(u.f, field(f.FieldName)) |
| 169 | + f2d[field(f.FieldName)] = discriminated(f.DiscriminatedBy) |
| 170 | + } |
| 171 | + u.dn = newDiscriminatedName(f2d) |
| 172 | + return u |
| 173 | +} |
| 174 | + |
| 175 | +// clear removes all the fields in map that are part of the union, but |
| 176 | +// the one we decided to keep. |
| 177 | +func (u *union) clear(m *value.Map, f field) { |
| 178 | + for _, fieldName := range u.f { |
| 179 | + if field(fieldName) != f { |
| 180 | + m.Delete(string(fieldName)) |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +func (u *union) Normalize(old, new, out *value.Map) error { |
| 186 | + os := newFieldsSet(old, u.f) |
| 187 | + ns := newFieldsSet(new, u.f) |
| 188 | + diff := ns.Difference(os) |
| 189 | + |
| 190 | + if len(ns) > 1 && len(diff) != 1 { |
| 191 | + return fmt.Errorf("unable to guess new discriminator: %v", diff) |
| 192 | + } |
| 193 | + |
| 194 | + discriminator := field("") |
| 195 | + if len(ns) == 1 { |
| 196 | + discriminator = *ns.One() |
| 197 | + } else if len(diff) == 1 { |
| 198 | + discriminator = *diff.One() |
| 199 | + } |
| 200 | + |
| 201 | + if u.d.Get(old) != u.d.Get(new) && u.d.Get(new) != "" { |
| 202 | + if len(diff) == 1 && u.d.Get(new) != u.dn.toDiscriminated(discriminator) { |
| 203 | + return fmt.Errorf("discriminator and field changed: %v/%v", discriminator, u.d.Get(new)) |
| 204 | + } |
| 205 | + u.clear(out, u.dn.toField(u.d.Get(new))) |
| 206 | + return nil |
| 207 | + } |
| 208 | + |
| 209 | + if discriminator != "" { |
| 210 | + u.clear(out, discriminator) |
| 211 | + u.d.Set(out, u.dn.toDiscriminated(discriminator)) |
| 212 | + } |
| 213 | + |
| 214 | + return nil |
| 215 | +} |
0 commit comments