value.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 value
  14. import (
  15. "fmt"
  16. "strings"
  17. )
  18. // A Value is an object; it corresponds to an 'atom' in the schema.
  19. type Value struct {
  20. // Exactly one of the below must be set.
  21. FloatValue *Float
  22. IntValue *Int
  23. StringValue *String
  24. BooleanValue *Boolean
  25. ListValue *List
  26. MapValue *Map
  27. Null bool // represents an explicit `"foo" = null`
  28. }
  29. type Int int64
  30. type Float float64
  31. type String string
  32. type Boolean bool
  33. // Field is an individual key-value pair.
  34. type Field struct {
  35. Name string
  36. Value Value
  37. }
  38. // List is a list of items.
  39. type List struct {
  40. Items []Value
  41. }
  42. // Map is a map of key-value pairs. It represents both structs and maps. We use
  43. // a list and a go-language map to preserve order.
  44. //
  45. // Set and Get helpers are provided.
  46. type Map struct {
  47. Items []Field
  48. // may be nil; lazily constructed.
  49. // TODO: Direct modifications to Items above will cause serious problems.
  50. index map[string]*Field
  51. }
  52. // Get returns the (Field, true) or (nil, false) if it is not present
  53. func (m *Map) Get(key string) (*Field, bool) {
  54. if m.index == nil {
  55. m.index = map[string]*Field{}
  56. for i := range m.Items {
  57. f := &m.Items[i]
  58. m.index[f.Name] = f
  59. }
  60. }
  61. f, ok := m.index[key]
  62. return f, ok
  63. }
  64. // Set inserts or updates the given item.
  65. func (m *Map) Set(key string, value Value) {
  66. if f, ok := m.Get(key); ok {
  67. f.Value = value
  68. return
  69. }
  70. m.Items = append(m.Items, Field{Name: key, Value: value})
  71. m.index = nil // Since the append might have reallocated
  72. }
  73. // StringValue returns s as a scalar string Value.
  74. func StringValue(s string) Value {
  75. s2 := String(s)
  76. return Value{StringValue: &s2}
  77. }
  78. // IntValue returns i as a scalar numeric (integer) Value.
  79. func IntValue(i int) Value {
  80. i2 := Int(i)
  81. return Value{IntValue: &i2}
  82. }
  83. // FloatValue returns f as a scalar numeric (float) Value.
  84. func FloatValue(f float64) Value {
  85. f2 := Float(f)
  86. return Value{FloatValue: &f2}
  87. }
  88. // BooleanValue returns b as a scalar boolean Value.
  89. func BooleanValue(b bool) Value {
  90. b2 := Boolean(b)
  91. return Value{BooleanValue: &b2}
  92. }
  93. // String returns a human-readable representation of the value.
  94. func (v Value) String() string {
  95. switch {
  96. case v.FloatValue != nil:
  97. return fmt.Sprintf("%v", *v.FloatValue)
  98. case v.IntValue != nil:
  99. return fmt.Sprintf("%v", *v.IntValue)
  100. case v.StringValue != nil:
  101. return fmt.Sprintf("%q", *v.StringValue)
  102. case v.BooleanValue != nil:
  103. return fmt.Sprintf("%v", *v.BooleanValue)
  104. case v.ListValue != nil:
  105. strs := []string{}
  106. for _, item := range v.ListValue.Items {
  107. strs = append(strs, item.String())
  108. }
  109. return "[" + strings.Join(strs, ",") + "]"
  110. case v.MapValue != nil:
  111. strs := []string{}
  112. for _, i := range v.MapValue.Items {
  113. strs = append(strs, fmt.Sprintf("%v=%v", i.Name, i.Value))
  114. }
  115. return "{" + strings.Join(strs, ";") + "}"
  116. default:
  117. fallthrough
  118. case v.Null == true:
  119. return "null"
  120. }
  121. }