fields.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package internal
  14. import (
  15. "bytes"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
  18. )
  19. // EmptyFields represents a set with no paths
  20. // It looks like metav1.Fields{Raw: []byte("{}")}
  21. var EmptyFields = func() metav1.FieldsV1 {
  22. f, err := SetToFields(*fieldpath.NewSet())
  23. if err != nil {
  24. panic("should never happen")
  25. }
  26. return f
  27. }()
  28. // FieldsToSet creates a set paths from an input trie of fields
  29. func FieldsToSet(f metav1.FieldsV1) (s fieldpath.Set, err error) {
  30. err = s.FromJSON(bytes.NewReader(f.Raw))
  31. return s, err
  32. }
  33. // SetToFields creates a trie of fields from an input set of paths
  34. func SetToFields(s fieldpath.Set) (f metav1.FieldsV1, err error) {
  35. f.Raw, err = s.ToJSON()
  36. return f, err
  37. }