github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/value/nullable.go (about) 1 package value 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3/internal/types" 8 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring" 9 ) 10 11 func NullableBoolValue(v *bool) Value { 12 if v == nil { 13 return NullValue(types.Bool) 14 } 15 16 return OptionalValue(BoolValue(*v)) 17 } 18 19 func NullableInt8Value(v *int8) Value { 20 if v == nil { 21 return NullValue(types.Int8) 22 } 23 24 return OptionalValue(Int8Value(*v)) 25 } 26 27 func NullableInt16Value(v *int16) Value { 28 if v == nil { 29 return NullValue(types.Int16) 30 } 31 32 return OptionalValue(Int16Value(*v)) 33 } 34 35 func NullableInt32Value(v *int32) Value { 36 if v == nil { 37 return NullValue(types.Int32) 38 } 39 40 return OptionalValue(Int32Value(*v)) 41 } 42 43 func NullableInt64Value(v *int64) Value { 44 if v == nil { 45 return NullValue(types.Int64) 46 } 47 48 return OptionalValue(Int64Value(*v)) 49 } 50 51 func NullableUint8Value(v *uint8) Value { 52 if v == nil { 53 return NullValue(types.Uint8) 54 } 55 56 return OptionalValue(Uint8Value(*v)) 57 } 58 59 func NullableUint16Value(v *uint16) Value { 60 if v == nil { 61 return NullValue(types.Uint16) 62 } 63 64 return OptionalValue(Uint16Value(*v)) 65 } 66 67 func NullableUint32Value(v *uint32) Value { 68 if v == nil { 69 return NullValue(types.Uint32) 70 } 71 72 return OptionalValue(Uint32Value(*v)) 73 } 74 75 func NullableUint64Value(v *uint64) Value { 76 if v == nil { 77 return NullValue(types.Uint64) 78 } 79 80 return OptionalValue(Uint64Value(*v)) 81 } 82 83 func NullableFloatValue(v *float32) Value { 84 if v == nil { 85 return NullValue(types.Float) 86 } 87 88 return OptionalValue(FloatValue(*v)) 89 } 90 91 func NullableDoubleValue(v *float64) Value { 92 if v == nil { 93 return NullValue(types.Double) 94 } 95 96 return OptionalValue(DoubleValue(*v)) 97 } 98 99 func NullableDateValue(v *uint32) Value { 100 if v == nil { 101 return NullValue(types.Date) 102 } 103 104 return OptionalValue(DateValue(*v)) 105 } 106 107 func NullableDateValueFromTime(v *time.Time) Value { 108 if v == nil { 109 return NullValue(types.Date) 110 } 111 112 return OptionalValue(DateValueFromTime(*v)) 113 } 114 115 func NullableDatetimeValue(v *uint32) Value { 116 if v == nil { 117 return NullValue(types.Datetime) 118 } 119 120 return OptionalValue(DatetimeValue(*v)) 121 } 122 123 func NullableDatetimeValueFromTime(v *time.Time) Value { 124 if v == nil { 125 return NullValue(types.Datetime) 126 } 127 128 return OptionalValue(DatetimeValueFromTime(*v)) 129 } 130 131 func NullableTzDateValue(v *string) Value { 132 if v == nil { 133 return NullValue(types.TzDate) 134 } 135 136 return OptionalValue(TzDateValue(*v)) 137 } 138 139 func NullableTzDateValueFromTime(v *time.Time) Value { 140 if v == nil { 141 return NullValue(types.TzDate) 142 } 143 144 return OptionalValue(TzDateValueFromTime(*v)) 145 } 146 147 func NullableTzDatetimeValue(v *string) Value { 148 if v == nil { 149 return NullValue(types.TzDatetime) 150 } 151 152 return OptionalValue(TzDatetimeValue(*v)) 153 } 154 155 func NullableTzDatetimeValueFromTime(v *time.Time) Value { 156 if v == nil { 157 return NullValue(types.TzDatetime) 158 } 159 160 return OptionalValue(TzDatetimeValueFromTime(*v)) 161 } 162 163 func NullableTimestampValue(v *uint64) Value { 164 if v == nil { 165 return NullValue(types.Timestamp) 166 } 167 168 return OptionalValue(TimestampValue(*v)) 169 } 170 171 func NullableTimestampValueFromTime(v *time.Time) Value { 172 if v == nil { 173 return NullValue(types.Timestamp) 174 } 175 176 return OptionalValue(TimestampValueFromTime(*v)) 177 } 178 179 func NullableTzTimestampValue(v *string) Value { 180 if v == nil { 181 return NullValue(types.TzTimestamp) 182 } 183 184 return OptionalValue(TzTimestampValue(*v)) 185 } 186 187 func NullableTzTimestampValueFromTime(v *time.Time) Value { 188 if v == nil { 189 return NullValue(types.TzTimestamp) 190 } 191 192 return OptionalValue(TzTimestampValueFromTime(*v)) 193 } 194 195 func NullableIntervalValueFromMicroseconds(v *int64) Value { 196 if v == nil { 197 return NullValue(types.Interval) 198 } 199 200 return OptionalValue(IntervalValue(*v)) 201 } 202 203 func NullableIntervalValueFromDuration(v *time.Duration) Value { 204 if v == nil { 205 return NullValue(types.Interval) 206 } 207 208 return OptionalValue(IntervalValueFromDuration(*v)) 209 } 210 211 func NullableBytesValue(v *[]byte) Value { 212 if v == nil { 213 return NullValue(types.Bytes) 214 } 215 216 return OptionalValue(BytesValue(*v)) 217 } 218 219 func NullableBytesValueFromString(v *string) Value { 220 if v == nil { 221 return NullValue(types.Bytes) 222 } 223 224 return OptionalValue(BytesValue(xstring.ToBytes(*v))) 225 } 226 227 func NullableTextValue(v *string) Value { 228 if v == nil { 229 return NullValue(types.Text) 230 } 231 232 return OptionalValue(TextValue(*v)) 233 } 234 235 func NullableYSONValue(v *string) Value { 236 if v == nil { 237 return NullValue(types.YSON) 238 } 239 240 return OptionalValue(YSONValue(xstring.ToBytes(*v))) 241 } 242 243 func NullableYSONValueFromBytes(v *[]byte) Value { 244 if v == nil { 245 return NullValue(types.YSON) 246 } 247 248 return OptionalValue(YSONValue(*v)) 249 } 250 251 func NullableJSONValue(v *string) Value { 252 if v == nil { 253 return NullValue(types.JSON) 254 } 255 256 return OptionalValue(JSONValue(*v)) 257 } 258 259 func NullableJSONValueFromBytes(v *[]byte) Value { 260 if v == nil { 261 return NullValue(types.JSON) 262 } 263 264 return OptionalValue(JSONValue(xstring.FromBytes(*v))) 265 } 266 267 func NullableUUIDValue(v *[16]byte) Value { 268 if v == nil { 269 return NullValue(types.UUID) 270 } 271 272 return OptionalValue(UUIDValue(*v)) 273 } 274 275 func NullableJSONDocumentValue(v *string) Value { 276 if v == nil { 277 return NullValue(types.JSONDocument) 278 } 279 280 return OptionalValue(JSONDocumentValue(*v)) 281 } 282 283 func NullableJSONDocumentValueFromBytes(v *[]byte) Value { 284 if v == nil { 285 return NullValue(types.JSONDocument) 286 } 287 288 return OptionalValue(JSONDocumentValue(xstring.FromBytes(*v))) 289 } 290 291 func NullableDyNumberValue(v *string) Value { 292 if v == nil { 293 return NullValue(types.DyNumber) 294 } 295 296 return OptionalValue(DyNumberValue(*v)) 297 } 298 299 // Nullable makes optional value from nullable type 300 // Warning: type interface will be replaced in the future with typed parameters pattern from go1.18 301 // 302 //nolint:gocyclo 303 func Nullable(t types.Type, v interface{}) Value { 304 switch t { 305 case types.Bool: 306 return NullableBoolValue(v.(*bool)) 307 case types.Int8: 308 return NullableInt8Value(v.(*int8)) 309 case types.Uint8: 310 return NullableUint8Value(v.(*uint8)) 311 case types.Int16: 312 return NullableInt16Value(v.(*int16)) 313 case types.Uint16: 314 return NullableUint16Value(v.(*uint16)) 315 case types.Int32: 316 return NullableInt32Value(v.(*int32)) 317 case types.Uint32: 318 return NullableUint32Value(v.(*uint32)) 319 case types.Int64: 320 return NullableInt64Value(v.(*int64)) 321 case types.Uint64: 322 return NullableUint64Value(v.(*uint64)) 323 case types.Float: 324 return NullableFloatValue(v.(*float32)) 325 case types.Double: 326 return NullableDoubleValue(v.(*float64)) 327 case types.Date: 328 switch tt := v.(type) { 329 case *uint32: 330 return NullableDateValue(tt) 331 case *time.Time: 332 return NullableDateValueFromTime(tt) 333 default: 334 panic(fmt.Sprintf("unsupported type conversion from %T to TypeDate", tt)) 335 } 336 case types.Datetime: 337 switch tt := v.(type) { 338 case *uint32: 339 return NullableDatetimeValue(tt) 340 case *time.Time: 341 return NullableDatetimeValueFromTime(tt) 342 default: 343 panic(fmt.Sprintf("unsupported type conversion from %T to TypeDatetime", tt)) 344 } 345 case types.Timestamp: 346 switch tt := v.(type) { 347 case *uint64: 348 return NullableTimestampValue(tt) 349 case *time.Time: 350 return NullableTimestampValueFromTime(tt) 351 default: 352 panic(fmt.Sprintf("unsupported type conversion from %T to TypeTimestamp", tt)) 353 } 354 case types.Interval: 355 switch tt := v.(type) { 356 case *int64: 357 return NullableIntervalValueFromMicroseconds(tt) 358 case *time.Duration: 359 return NullableIntervalValueFromDuration(tt) 360 default: 361 panic(fmt.Sprintf("unsupported type conversion from %T to TypeInterval", tt)) 362 } 363 case types.TzDate: 364 switch tt := v.(type) { 365 case *string: 366 return NullableTzDateValue(tt) 367 case *time.Time: 368 return NullableTzDateValueFromTime(tt) 369 default: 370 panic(fmt.Sprintf("unsupported type conversion from %T to TypeTzDate", tt)) 371 } 372 case types.TzDatetime: 373 switch tt := v.(type) { 374 case *string: 375 return NullableTzDatetimeValue(tt) 376 case *time.Time: 377 return NullableTzDatetimeValueFromTime(tt) 378 default: 379 panic(fmt.Sprintf("unsupported type conversion from %T to TypeTzDatetime", tt)) 380 } 381 case types.TzTimestamp: 382 switch tt := v.(type) { 383 case *string: 384 return NullableTzTimestampValue(tt) 385 case *time.Time: 386 return NullableTzTimestampValueFromTime(tt) 387 default: 388 panic(fmt.Sprintf("unsupported type conversion from %T to TypeTzTimestamp", tt)) 389 } 390 case types.Bytes: 391 switch tt := v.(type) { 392 case *[]byte: 393 return NullableBytesValue(tt) 394 case *string: 395 return NullableBytesValueFromString(tt) 396 default: 397 panic(fmt.Sprintf("unsupported type conversion from %T to TypeBytes", tt)) 398 } 399 case types.Text: 400 switch tt := v.(type) { 401 case *string: 402 return NullableTextValue(tt) 403 default: 404 panic(fmt.Sprintf("unsupported type conversion from %T to TypeText", tt)) 405 } 406 case types.YSON: 407 switch tt := v.(type) { 408 case *string: 409 return NullableYSONValue(tt) 410 case *[]byte: 411 return NullableYSONValueFromBytes(tt) 412 default: 413 panic(fmt.Sprintf("unsupported type conversion from %T to TypeYSON", tt)) 414 } 415 case types.JSON: 416 switch tt := v.(type) { 417 case *string: 418 return NullableJSONValue(tt) 419 case *[]byte: 420 return NullableJSONValueFromBytes(tt) 421 default: 422 panic(fmt.Sprintf("unsupported type conversion from %T to TypeJSON", tt)) 423 } 424 case types.UUID: 425 switch tt := v.(type) { 426 case *[16]byte: 427 return NullableUUIDValue(tt) 428 default: 429 panic(fmt.Sprintf("unsupported type conversion from %T to TypeUUID", tt)) 430 } 431 case types.JSONDocument: 432 switch tt := v.(type) { 433 case *string: 434 return NullableJSONDocumentValue(tt) 435 case *[]byte: 436 return NullableJSONDocumentValueFromBytes(tt) 437 default: 438 panic(fmt.Sprintf("unsupported type conversion from %T to TypeJSONDocument", tt)) 439 } 440 case types.DyNumber: 441 switch tt := v.(type) { 442 case *string: 443 return NullableDyNumberValue(tt) 444 default: 445 panic(fmt.Sprintf("unsupported type conversion from %T to TypeDyNumber", tt)) 446 } 447 default: 448 panic(fmt.Sprintf("unsupported type: %T", t)) 449 } 450 }