fromvalue.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright 2019 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 schema
  14. import (
  15. "sigs.k8s.io/structured-merge-diff/value"
  16. )
  17. // TypeRefFromValue creates an inlined type from a value v
  18. func TypeRefFromValue(v value.Value) TypeRef {
  19. atom := atomFor(v)
  20. return TypeRef{
  21. Inlined: atom,
  22. }
  23. }
  24. func atomFor(v value.Value) Atom {
  25. switch {
  26. // Untyped cases (handled at the bottom of this function)
  27. case v.Null:
  28. case v.ListValue != nil:
  29. case v.FloatValue != nil:
  30. case v.IntValue != nil:
  31. case v.StringValue != nil:
  32. case v.BooleanValue != nil:
  33. // Recursive case
  34. case v.MapValue != nil:
  35. s := Struct{}
  36. for i := range v.MapValue.Items {
  37. child := v.MapValue.Items[i]
  38. field := StructField{
  39. Name: child.Name,
  40. Type: TypeRef{
  41. Inlined: atomFor(child.Value),
  42. },
  43. }
  44. s.Fields = append(s.Fields, field)
  45. }
  46. return Atom{Struct: &s}
  47. }
  48. return Atom{Untyped: &Untyped{}}
  49. }