null.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. // Package null is used to represent values where the 0 value is significant
  4. // This pattern is common in ECMAScript, this allows us to maintain a matching API
  5. package null
  6. // Bool is used to represent a bool that may be null
  7. type Bool struct {
  8. Valid bool
  9. Bool bool
  10. }
  11. // NewBool turns a bool into a valid null.Bool
  12. func NewBool(value bool) Bool {
  13. return Bool{Valid: true, Bool: value}
  14. }
  15. // Byte is used to represent a byte that may be null
  16. type Byte struct {
  17. Valid bool
  18. Byte byte
  19. }
  20. // NewByte turns a byte into a valid null.Byte
  21. func NewByte(value byte) Byte {
  22. return Byte{Valid: true, Byte: value}
  23. }
  24. // Complex128 is used to represent a complex128 that may be null
  25. type Complex128 struct {
  26. Valid bool
  27. Complex128 complex128
  28. }
  29. // NewComplex128 turns a complex128 into a valid null.Complex128
  30. func NewComplex128(value complex128) Complex128 {
  31. return Complex128{Valid: true, Complex128: value}
  32. }
  33. // Complex64 is used to represent a complex64 that may be null
  34. type Complex64 struct {
  35. Valid bool
  36. Complex64 complex64
  37. }
  38. // NewComplex64 turns a complex64 into a valid null.Complex64
  39. func NewComplex64(value complex64) Complex64 {
  40. return Complex64{Valid: true, Complex64: value}
  41. }
  42. // Float32 is used to represent a float32 that may be null
  43. type Float32 struct {
  44. Valid bool
  45. Float32 float32
  46. }
  47. // NewFloat32 turns a float32 into a valid null.Float32
  48. func NewFloat32(value float32) Float32 {
  49. return Float32{Valid: true, Float32: value}
  50. }
  51. // Float64 is used to represent a float64 that may be null
  52. type Float64 struct {
  53. Valid bool
  54. Float64 float64
  55. }
  56. // NewFloat64 turns a float64 into a valid null.Float64
  57. func NewFloat64(value float64) Float64 {
  58. return Float64{Valid: true, Float64: value}
  59. }
  60. // Int is used to represent a int that may be null
  61. type Int struct {
  62. Valid bool
  63. Int int
  64. }
  65. // NewInt turns a int into a valid null.Int
  66. func NewInt(value int) Int {
  67. return Int{Valid: true, Int: value}
  68. }
  69. // Int16 is used to represent a int16 that may be null
  70. type Int16 struct {
  71. Valid bool
  72. Int16 int16
  73. }
  74. // NewInt16 turns a int16 into a valid null.Int16
  75. func NewInt16(value int16) Int16 {
  76. return Int16{Valid: true, Int16: value}
  77. }
  78. // Int32 is used to represent a int32 that may be null
  79. type Int32 struct {
  80. Valid bool
  81. Int32 int32
  82. }
  83. // NewInt32 turns a int32 into a valid null.Int32
  84. func NewInt32(value int32) Int32 {
  85. return Int32{Valid: true, Int32: value}
  86. }
  87. // Int64 is used to represent a int64 that may be null
  88. type Int64 struct {
  89. Valid bool
  90. Int64 int64
  91. }
  92. // NewInt64 turns a int64 into a valid null.Int64
  93. func NewInt64(value int64) Int64 {
  94. return Int64{Valid: true, Int64: value}
  95. }
  96. // Int8 is used to represent a int8 that may be null
  97. type Int8 struct {
  98. Valid bool
  99. Int8 int8
  100. }
  101. // NewInt8 turns a int8 into a valid null.Int8
  102. func NewInt8(value int8) Int8 {
  103. return Int8{Valid: true, Int8: value}
  104. }
  105. // Rune is used to represent a rune that may be null
  106. type Rune struct {
  107. Valid bool
  108. Rune rune
  109. }
  110. // NewRune turns a rune into a valid null.Rune
  111. func NewRune(value rune) Rune {
  112. return Rune{Valid: true, Rune: value}
  113. }
  114. // String is used to represent a string that may be null
  115. type String struct {
  116. Valid bool
  117. String string
  118. }
  119. // NewString turns a string into a valid null.String
  120. func NewString(value string) String {
  121. return String{Valid: true, String: value}
  122. }
  123. // Uint is used to represent a uint that may be null
  124. type Uint struct {
  125. Valid bool
  126. Uint uint
  127. }
  128. // NewUint turns a uint into a valid null.Uint
  129. func NewUint(value uint) Uint {
  130. return Uint{Valid: true, Uint: value}
  131. }
  132. // Uint16 is used to represent a uint16 that may be null
  133. type Uint16 struct {
  134. Valid bool
  135. Uint16 uint16
  136. }
  137. // NewUint16 turns a uint16 into a valid null.Uint16
  138. func NewUint16(value uint16) Uint16 {
  139. return Uint16{Valid: true, Uint16: value}
  140. }
  141. // Uint32 is used to represent a uint32 that may be null
  142. type Uint32 struct {
  143. Valid bool
  144. Uint32 uint32
  145. }
  146. // NewUint32 turns a uint32 into a valid null.Uint32
  147. func NewUint32(value uint32) Uint32 {
  148. return Uint32{Valid: true, Uint32: value}
  149. }
  150. // Uint64 is used to represent a uint64 that may be null
  151. type Uint64 struct {
  152. Valid bool
  153. Uint64 uint64
  154. }
  155. // NewUint64 turns a uint64 into a valid null.Uint64
  156. func NewUint64(value uint64) Uint64 {
  157. return Uint64{Valid: true, Uint64: value}
  158. }
  159. // Uint8 is used to represent a uint8 that may be null
  160. type Uint8 struct {
  161. Valid bool
  162. Uint8 uint8
  163. }
  164. // NewUint8 turns a uint8 into a valid null.Uint8
  165. func NewUint8(value uint8) Uint8 {
  166. return Uint8{Valid: true, Uint8: value}
  167. }