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