1 /* 2 Copyright 2018 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 internal 18 19 import ( 20 "bytes" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 24 "sigs.k8s.io/structured-merge-diff/v4/fieldpath" 25 ) 26 27 // EmptyFields represents a set with no paths 28 // It looks like metav1.Fields{Raw: []byte("{}")} 29 var EmptyFields = func() metav1.FieldsV1 { 30 f, err := SetToFields(*fieldpath.NewSet()) 31 if err != nil { 32 panic("should never happen") 33 } 34 return f 35 }() 36 37 // FieldsToSet creates a set paths from an input trie of fields 38 func FieldsToSet(f metav1.FieldsV1) (s fieldpath.Set, err error) { 39 err = s.FromJSON(bytes.NewReader(f.Raw)) 40 return s, err 41 } 42 43 // SetToFields creates a trie of fields from an input set of paths 44 func SetToFields(s fieldpath.Set) (f metav1.FieldsV1, err error) { 45 f.Raw, err = s.ToJSON() 46 return f, err 47 } 48