|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP 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 dynamicrestmapper |
| 18 | + |
| 19 | +import ( |
| 20 | + "slices" |
| 21 | +) |
| 22 | + |
| 23 | +// This file adds mutable methods to our fork of upstream's DefaultRESTMapper. |
| 24 | + |
| 25 | +func (m *DefaultRESTMapper) empty() bool { |
| 26 | + // If one of the maps is empty, all of the maps are empty. |
| 27 | + return len(m.resourceToKind) == 0 |
| 28 | +} |
| 29 | + |
| 30 | +func (m *DefaultRESTMapper) add(typeMeta gvkr) { |
| 31 | + kind := typeMeta.groupVersionKind() |
| 32 | + singular := typeMeta.groupVersionResourceSingular() |
| 33 | + plural := typeMeta.groupVersionResourcePlural() |
| 34 | + |
| 35 | + m.singularToPlural[singular] = plural |
| 36 | + m.pluralToSingular[plural] = singular |
| 37 | + |
| 38 | + m.resourceToKind[singular] = kind |
| 39 | + m.resourceToKind[plural] = kind |
| 40 | + |
| 41 | + m.kindToPluralResource[kind] = plural |
| 42 | +} |
| 43 | + |
| 44 | +func (m *DefaultRESTMapper) remove(typeMeta gvkr) { |
| 45 | + kind := typeMeta.groupVersionKind() |
| 46 | + singular := m.kindToPluralResource[kind] |
| 47 | + plural := m.singularToPlural[typeMeta.groupVersionResourceSingular()] |
| 48 | + |
| 49 | + delete(m.kindToPluralResource, kind) |
| 50 | + delete(m.kindToScope, kind) |
| 51 | + delete(m.singularToPlural, singular) |
| 52 | + delete(m.pluralToSingular, plural) |
| 53 | + delete(m.resourceToKind, plural) |
| 54 | +} |
| 55 | + |
| 56 | +func (m *DefaultRESTMapper) apply(toRemove []gvkr, toAdd []gvkr) { |
| 57 | + if slices.Equal(toRemove, toAdd) { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + for i := range toRemove { |
| 62 | + m.remove(toRemove[i]) |
| 63 | + } |
| 64 | + |
| 65 | + for i := range toAdd { |
| 66 | + m.add(toAdd[i]) |
| 67 | + } |
| 68 | +} |
0 commit comments