github.com/gocql/gocql@v1.6.0/marshal.go (about) 1 // Copyright (c) 2012 The gocql Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gocql 6 7 import ( 8 "bytes" 9 "encoding/binary" 10 "errors" 11 "fmt" 12 "math" 13 "math/big" 14 "math/bits" 15 "net" 16 "reflect" 17 "strconv" 18 "strings" 19 "time" 20 21 "gopkg.in/inf.v0" 22 ) 23 24 var ( 25 bigOne = big.NewInt(1) 26 emptyValue reflect.Value 27 ) 28 29 var ( 30 ErrorUDTUnavailable = errors.New("UDT are not available on protocols less than 3, please update config") 31 ) 32 33 // Marshaler is the interface implemented by objects that can marshal 34 // themselves into values understood by Cassandra. 35 type Marshaler interface { 36 MarshalCQL(info TypeInfo) ([]byte, error) 37 } 38 39 // Unmarshaler is the interface implemented by objects that can unmarshal 40 // a Cassandra specific description of themselves. 41 type Unmarshaler interface { 42 UnmarshalCQL(info TypeInfo, data []byte) error 43 } 44 45 // Marshal returns the CQL encoding of the value for the Cassandra 46 // internal type described by the info parameter. 47 // 48 // nil is serialized as CQL null. 49 // If value implements Marshaler, its MarshalCQL method is called to marshal the data. 50 // If value is a pointer, the pointed-to value is marshaled. 51 // 52 // Supported conversions are as follows, other type combinations may be added in the future: 53 // 54 // CQL type | Go type (value) | Note 55 // varchar, ascii, blob, text | string, []byte | 56 // boolean | bool | 57 // tinyint, smallint, int | integer types | 58 // tinyint, smallint, int | string | formatted as base 10 number 59 // bigint, counter | integer types | 60 // bigint, counter | big.Int | 61 // bigint, counter | string | formatted as base 10 number 62 // float | float32 | 63 // double | float64 | 64 // decimal | inf.Dec | 65 // time | int64 | nanoseconds since start of day 66 // time | time.Duration | duration since start of day 67 // timestamp | int64 | milliseconds since Unix epoch 68 // timestamp | time.Time | 69 // list, set | slice, array | 70 // list, set | map[X]struct{} | 71 // map | map[X]Y | 72 // uuid, timeuuid | gocql.UUID | 73 // uuid, timeuuid | [16]byte | raw UUID bytes 74 // uuid, timeuuid | []byte | raw UUID bytes, length must be 16 bytes 75 // uuid, timeuuid | string | hex representation, see ParseUUID 76 // varint | integer types | 77 // varint | big.Int | 78 // varint | string | value of number in decimal notation 79 // inet | net.IP | 80 // inet | string | IPv4 or IPv6 address string 81 // tuple | slice, array | 82 // tuple | struct | fields are marshaled in order of declaration 83 // user-defined type | gocql.UDTMarshaler | MarshalUDT is called 84 // user-defined type | map[string]interface{} | 85 // user-defined type | struct | struct fields' cql tags are used for column names 86 // date | int64 | milliseconds since Unix epoch to start of day (in UTC) 87 // date | time.Time | start of day (in UTC) 88 // date | string | parsed using "2006-01-02" format 89 // duration | int64 | duration in nanoseconds 90 // duration | time.Duration | 91 // duration | gocql.Duration | 92 // duration | string | parsed with time.ParseDuration 93 func Marshal(info TypeInfo, value interface{}) ([]byte, error) { 94 if info.Version() < protoVersion1 { 95 panic("protocol version not set") 96 } 97 98 if valueRef := reflect.ValueOf(value); valueRef.Kind() == reflect.Ptr { 99 if valueRef.IsNil() { 100 return nil, nil 101 } else if v, ok := value.(Marshaler); ok { 102 return v.MarshalCQL(info) 103 } else { 104 return Marshal(info, valueRef.Elem().Interface()) 105 } 106 } 107 108 if v, ok := value.(Marshaler); ok { 109 return v.MarshalCQL(info) 110 } 111 112 switch info.Type() { 113 case TypeVarchar, TypeAscii, TypeBlob, TypeText: 114 return marshalVarchar(info, value) 115 case TypeBoolean: 116 return marshalBool(info, value) 117 case TypeTinyInt: 118 return marshalTinyInt(info, value) 119 case TypeSmallInt: 120 return marshalSmallInt(info, value) 121 case TypeInt: 122 return marshalInt(info, value) 123 case TypeBigInt, TypeCounter: 124 return marshalBigInt(info, value) 125 case TypeFloat: 126 return marshalFloat(info, value) 127 case TypeDouble: 128 return marshalDouble(info, value) 129 case TypeDecimal: 130 return marshalDecimal(info, value) 131 case TypeTime: 132 return marshalTime(info, value) 133 case TypeTimestamp: 134 return marshalTimestamp(info, value) 135 case TypeList, TypeSet: 136 return marshalList(info, value) 137 case TypeMap: 138 return marshalMap(info, value) 139 case TypeUUID, TypeTimeUUID: 140 return marshalUUID(info, value) 141 case TypeVarint: 142 return marshalVarint(info, value) 143 case TypeInet: 144 return marshalInet(info, value) 145 case TypeTuple: 146 return marshalTuple(info, value) 147 case TypeUDT: 148 return marshalUDT(info, value) 149 case TypeDate: 150 return marshalDate(info, value) 151 case TypeDuration: 152 return marshalDuration(info, value) 153 } 154 155 // detect protocol 2 UDT 156 if strings.HasPrefix(info.Custom(), "org.apache.cassandra.db.marshal.UserType") && info.Version() < 3 { 157 return nil, ErrorUDTUnavailable 158 } 159 160 // TODO(tux21b): add the remaining types 161 return nil, fmt.Errorf("can not marshal %T into %s", value, info) 162 } 163 164 // Unmarshal parses the CQL encoded data based on the info parameter that 165 // describes the Cassandra internal data type and stores the result in the 166 // value pointed by value. 167 // 168 // If value implements Unmarshaler, it's UnmarshalCQL method is called to 169 // unmarshal the data. 170 // If value is a pointer to pointer, it is set to nil if the CQL value is 171 // null. Otherwise, nulls are unmarshalled as zero value. 172 // 173 // Supported conversions are as follows, other type combinations may be added in the future: 174 // 175 // CQL type | Go type (value) | Note 176 // varchar, ascii, blob, text | *string | 177 // varchar, ascii, blob, text | *[]byte | non-nil buffer is reused 178 // bool | *bool | 179 // tinyint, smallint, int, bigint, counter | *integer types | 180 // tinyint, smallint, int, bigint, counter | *big.Int | 181 // tinyint, smallint, int, bigint, counter | *string | formatted as base 10 number 182 // float | *float32 | 183 // double | *float64 | 184 // decimal | *inf.Dec | 185 // time | *int64 | nanoseconds since start of day 186 // time | *time.Duration | 187 // timestamp | *int64 | milliseconds since Unix epoch 188 // timestamp | *time.Time | 189 // list, set | *slice, *array | 190 // map | *map[X]Y | 191 // uuid, timeuuid | *string | see UUID.String 192 // uuid, timeuuid | *[]byte | raw UUID bytes 193 // uuid, timeuuid | *gocql.UUID | 194 // timeuuid | *time.Time | timestamp of the UUID 195 // inet | *net.IP | 196 // inet | *string | IPv4 or IPv6 address string 197 // tuple | *slice, *array | 198 // tuple | *struct | struct fields are set in order of declaration 199 // user-defined types | gocql.UDTUnmarshaler | UnmarshalUDT is called 200 // user-defined types | *map[string]interface{} | 201 // user-defined types | *struct | cql tag is used to determine field name 202 // date | *time.Time | time of beginning of the day (in UTC) 203 // date | *string | formatted with 2006-01-02 format 204 // duration | *gocql.Duration | 205 func Unmarshal(info TypeInfo, data []byte, value interface{}) error { 206 if v, ok := value.(Unmarshaler); ok { 207 return v.UnmarshalCQL(info, data) 208 } 209 210 if isNullableValue(value) { 211 return unmarshalNullable(info, data, value) 212 } 213 214 switch info.Type() { 215 case TypeVarchar, TypeAscii, TypeBlob, TypeText: 216 return unmarshalVarchar(info, data, value) 217 case TypeBoolean: 218 return unmarshalBool(info, data, value) 219 case TypeInt: 220 return unmarshalInt(info, data, value) 221 case TypeBigInt, TypeCounter: 222 return unmarshalBigInt(info, data, value) 223 case TypeVarint: 224 return unmarshalVarint(info, data, value) 225 case TypeSmallInt: 226 return unmarshalSmallInt(info, data, value) 227 case TypeTinyInt: 228 return unmarshalTinyInt(info, data, value) 229 case TypeFloat: 230 return unmarshalFloat(info, data, value) 231 case TypeDouble: 232 return unmarshalDouble(info, data, value) 233 case TypeDecimal: 234 return unmarshalDecimal(info, data, value) 235 case TypeTime: 236 return unmarshalTime(info, data, value) 237 case TypeTimestamp: 238 return unmarshalTimestamp(info, data, value) 239 case TypeList, TypeSet: 240 return unmarshalList(info, data, value) 241 case TypeMap: 242 return unmarshalMap(info, data, value) 243 case TypeTimeUUID: 244 return unmarshalTimeUUID(info, data, value) 245 case TypeUUID: 246 return unmarshalUUID(info, data, value) 247 case TypeInet: 248 return unmarshalInet(info, data, value) 249 case TypeTuple: 250 return unmarshalTuple(info, data, value) 251 case TypeUDT: 252 return unmarshalUDT(info, data, value) 253 case TypeDate: 254 return unmarshalDate(info, data, value) 255 case TypeDuration: 256 return unmarshalDuration(info, data, value) 257 } 258 259 // detect protocol 2 UDT 260 if strings.HasPrefix(info.Custom(), "org.apache.cassandra.db.marshal.UserType") && info.Version() < 3 { 261 return ErrorUDTUnavailable 262 } 263 264 // TODO(tux21b): add the remaining types 265 return fmt.Errorf("can not unmarshal %s into %T", info, value) 266 } 267 268 func isNullableValue(value interface{}) bool { 269 v := reflect.ValueOf(value) 270 return v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Ptr 271 } 272 273 func isNullData(info TypeInfo, data []byte) bool { 274 return data == nil 275 } 276 277 func unmarshalNullable(info TypeInfo, data []byte, value interface{}) error { 278 valueRef := reflect.ValueOf(value) 279 280 if isNullData(info, data) { 281 nilValue := reflect.Zero(valueRef.Type().Elem()) 282 valueRef.Elem().Set(nilValue) 283 return nil 284 } 285 286 newValue := reflect.New(valueRef.Type().Elem().Elem()) 287 valueRef.Elem().Set(newValue) 288 return Unmarshal(info, data, newValue.Interface()) 289 } 290 291 func marshalVarchar(info TypeInfo, value interface{}) ([]byte, error) { 292 switch v := value.(type) { 293 case Marshaler: 294 return v.MarshalCQL(info) 295 case unsetColumn: 296 return nil, nil 297 case string: 298 return []byte(v), nil 299 case []byte: 300 return v, nil 301 } 302 303 if value == nil { 304 return nil, nil 305 } 306 307 rv := reflect.ValueOf(value) 308 t := rv.Type() 309 k := t.Kind() 310 switch { 311 case k == reflect.String: 312 return []byte(rv.String()), nil 313 case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8: 314 return rv.Bytes(), nil 315 } 316 return nil, marshalErrorf("can not marshal %T into %s", value, info) 317 } 318 319 func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error { 320 switch v := value.(type) { 321 case Unmarshaler: 322 return v.UnmarshalCQL(info, data) 323 case *string: 324 *v = string(data) 325 return nil 326 case *[]byte: 327 if data != nil { 328 *v = append((*v)[:0], data...) 329 } else { 330 *v = nil 331 } 332 return nil 333 } 334 335 rv := reflect.ValueOf(value) 336 if rv.Kind() != reflect.Ptr { 337 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 338 } 339 rv = rv.Elem() 340 t := rv.Type() 341 k := t.Kind() 342 switch { 343 case k == reflect.String: 344 rv.SetString(string(data)) 345 return nil 346 case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8: 347 var dataCopy []byte 348 if data != nil { 349 dataCopy = make([]byte, len(data)) 350 copy(dataCopy, data) 351 } 352 rv.SetBytes(dataCopy) 353 return nil 354 } 355 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 356 } 357 358 func marshalSmallInt(info TypeInfo, value interface{}) ([]byte, error) { 359 switch v := value.(type) { 360 case Marshaler: 361 return v.MarshalCQL(info) 362 case unsetColumn: 363 return nil, nil 364 case int16: 365 return encShort(v), nil 366 case uint16: 367 return encShort(int16(v)), nil 368 case int8: 369 return encShort(int16(v)), nil 370 case uint8: 371 return encShort(int16(v)), nil 372 case int: 373 if v > math.MaxInt16 || v < math.MinInt16 { 374 return nil, marshalErrorf("marshal smallint: value %d out of range", v) 375 } 376 return encShort(int16(v)), nil 377 case int32: 378 if v > math.MaxInt16 || v < math.MinInt16 { 379 return nil, marshalErrorf("marshal smallint: value %d out of range", v) 380 } 381 return encShort(int16(v)), nil 382 case int64: 383 if v > math.MaxInt16 || v < math.MinInt16 { 384 return nil, marshalErrorf("marshal smallint: value %d out of range", v) 385 } 386 return encShort(int16(v)), nil 387 case uint: 388 if v > math.MaxUint16 { 389 return nil, marshalErrorf("marshal smallint: value %d out of range", v) 390 } 391 return encShort(int16(v)), nil 392 case uint32: 393 if v > math.MaxUint16 { 394 return nil, marshalErrorf("marshal smallint: value %d out of range", v) 395 } 396 return encShort(int16(v)), nil 397 case uint64: 398 if v > math.MaxUint16 { 399 return nil, marshalErrorf("marshal smallint: value %d out of range", v) 400 } 401 return encShort(int16(v)), nil 402 case string: 403 n, err := strconv.ParseInt(v, 10, 16) 404 if err != nil { 405 return nil, marshalErrorf("can not marshal %T into %s: %v", value, info, err) 406 } 407 return encShort(int16(n)), nil 408 } 409 410 if value == nil { 411 return nil, nil 412 } 413 414 switch rv := reflect.ValueOf(value); rv.Type().Kind() { 415 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8: 416 v := rv.Int() 417 if v > math.MaxInt16 || v < math.MinInt16 { 418 return nil, marshalErrorf("marshal smallint: value %d out of range", v) 419 } 420 return encShort(int16(v)), nil 421 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8: 422 v := rv.Uint() 423 if v > math.MaxUint16 { 424 return nil, marshalErrorf("marshal smallint: value %d out of range", v) 425 } 426 return encShort(int16(v)), nil 427 case reflect.Ptr: 428 if rv.IsNil() { 429 return nil, nil 430 } 431 } 432 433 return nil, marshalErrorf("can not marshal %T into %s", value, info) 434 } 435 436 func marshalTinyInt(info TypeInfo, value interface{}) ([]byte, error) { 437 switch v := value.(type) { 438 case Marshaler: 439 return v.MarshalCQL(info) 440 case unsetColumn: 441 return nil, nil 442 case int8: 443 return []byte{byte(v)}, nil 444 case uint8: 445 return []byte{byte(v)}, nil 446 case int16: 447 if v > math.MaxInt8 || v < math.MinInt8 { 448 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 449 } 450 return []byte{byte(v)}, nil 451 case uint16: 452 if v > math.MaxUint8 { 453 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 454 } 455 return []byte{byte(v)}, nil 456 case int: 457 if v > math.MaxInt8 || v < math.MinInt8 { 458 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 459 } 460 return []byte{byte(v)}, nil 461 case int32: 462 if v > math.MaxInt8 || v < math.MinInt8 { 463 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 464 } 465 return []byte{byte(v)}, nil 466 case int64: 467 if v > math.MaxInt8 || v < math.MinInt8 { 468 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 469 } 470 return []byte{byte(v)}, nil 471 case uint: 472 if v > math.MaxUint8 { 473 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 474 } 475 return []byte{byte(v)}, nil 476 case uint32: 477 if v > math.MaxUint8 { 478 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 479 } 480 return []byte{byte(v)}, nil 481 case uint64: 482 if v > math.MaxUint8 { 483 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 484 } 485 return []byte{byte(v)}, nil 486 case string: 487 n, err := strconv.ParseInt(v, 10, 8) 488 if err != nil { 489 return nil, marshalErrorf("can not marshal %T into %s: %v", value, info, err) 490 } 491 return []byte{byte(n)}, nil 492 } 493 494 if value == nil { 495 return nil, nil 496 } 497 498 switch rv := reflect.ValueOf(value); rv.Type().Kind() { 499 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8: 500 v := rv.Int() 501 if v > math.MaxInt8 || v < math.MinInt8 { 502 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 503 } 504 return []byte{byte(v)}, nil 505 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8: 506 v := rv.Uint() 507 if v > math.MaxUint8 { 508 return nil, marshalErrorf("marshal tinyint: value %d out of range", v) 509 } 510 return []byte{byte(v)}, nil 511 case reflect.Ptr: 512 if rv.IsNil() { 513 return nil, nil 514 } 515 } 516 517 return nil, marshalErrorf("can not marshal %T into %s", value, info) 518 } 519 520 func marshalInt(info TypeInfo, value interface{}) ([]byte, error) { 521 switch v := value.(type) { 522 case Marshaler: 523 return v.MarshalCQL(info) 524 case unsetColumn: 525 return nil, nil 526 case int: 527 if v > math.MaxInt32 || v < math.MinInt32 { 528 return nil, marshalErrorf("marshal int: value %d out of range", v) 529 } 530 return encInt(int32(v)), nil 531 case uint: 532 if v > math.MaxUint32 { 533 return nil, marshalErrorf("marshal int: value %d out of range", v) 534 } 535 return encInt(int32(v)), nil 536 case int64: 537 if v > math.MaxInt32 || v < math.MinInt32 { 538 return nil, marshalErrorf("marshal int: value %d out of range", v) 539 } 540 return encInt(int32(v)), nil 541 case uint64: 542 if v > math.MaxUint32 { 543 return nil, marshalErrorf("marshal int: value %d out of range", v) 544 } 545 return encInt(int32(v)), nil 546 case int32: 547 return encInt(v), nil 548 case uint32: 549 return encInt(int32(v)), nil 550 case int16: 551 return encInt(int32(v)), nil 552 case uint16: 553 return encInt(int32(v)), nil 554 case int8: 555 return encInt(int32(v)), nil 556 case uint8: 557 return encInt(int32(v)), nil 558 case string: 559 i, err := strconv.ParseInt(v, 10, 32) 560 if err != nil { 561 return nil, marshalErrorf("can not marshal string to int: %s", err) 562 } 563 return encInt(int32(i)), nil 564 } 565 566 if value == nil { 567 return nil, nil 568 } 569 570 switch rv := reflect.ValueOf(value); rv.Type().Kind() { 571 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8: 572 v := rv.Int() 573 if v > math.MaxInt32 || v < math.MinInt32 { 574 return nil, marshalErrorf("marshal int: value %d out of range", v) 575 } 576 return encInt(int32(v)), nil 577 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8: 578 v := rv.Uint() 579 if v > math.MaxInt32 { 580 return nil, marshalErrorf("marshal int: value %d out of range", v) 581 } 582 return encInt(int32(v)), nil 583 case reflect.Ptr: 584 if rv.IsNil() { 585 return nil, nil 586 } 587 } 588 589 return nil, marshalErrorf("can not marshal %T into %s", value, info) 590 } 591 592 func encInt(x int32) []byte { 593 return []byte{byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)} 594 } 595 596 func decInt(x []byte) int32 { 597 if len(x) != 4 { 598 return 0 599 } 600 return int32(x[0])<<24 | int32(x[1])<<16 | int32(x[2])<<8 | int32(x[3]) 601 } 602 603 func encShort(x int16) []byte { 604 p := make([]byte, 2) 605 p[0] = byte(x >> 8) 606 p[1] = byte(x) 607 return p 608 } 609 610 func decShort(p []byte) int16 { 611 if len(p) != 2 { 612 return 0 613 } 614 return int16(p[0])<<8 | int16(p[1]) 615 } 616 617 func decTiny(p []byte) int8 { 618 if len(p) != 1 { 619 return 0 620 } 621 return int8(p[0]) 622 } 623 624 func marshalBigInt(info TypeInfo, value interface{}) ([]byte, error) { 625 switch v := value.(type) { 626 case Marshaler: 627 return v.MarshalCQL(info) 628 case unsetColumn: 629 return nil, nil 630 case int: 631 return encBigInt(int64(v)), nil 632 case uint: 633 if uint64(v) > math.MaxInt64 { 634 return nil, marshalErrorf("marshal bigint: value %d out of range", v) 635 } 636 return encBigInt(int64(v)), nil 637 case int64: 638 return encBigInt(v), nil 639 case uint64: 640 return encBigInt(int64(v)), nil 641 case int32: 642 return encBigInt(int64(v)), nil 643 case uint32: 644 return encBigInt(int64(v)), nil 645 case int16: 646 return encBigInt(int64(v)), nil 647 case uint16: 648 return encBigInt(int64(v)), nil 649 case int8: 650 return encBigInt(int64(v)), nil 651 case uint8: 652 return encBigInt(int64(v)), nil 653 case big.Int: 654 return encBigInt2C(&v), nil 655 case string: 656 i, err := strconv.ParseInt(value.(string), 10, 64) 657 if err != nil { 658 return nil, marshalErrorf("can not marshal string to bigint: %s", err) 659 } 660 return encBigInt(i), nil 661 } 662 663 if value == nil { 664 return nil, nil 665 } 666 667 rv := reflect.ValueOf(value) 668 switch rv.Type().Kind() { 669 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8: 670 v := rv.Int() 671 return encBigInt(v), nil 672 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8: 673 v := rv.Uint() 674 if v > math.MaxInt64 { 675 return nil, marshalErrorf("marshal bigint: value %d out of range", v) 676 } 677 return encBigInt(int64(v)), nil 678 } 679 return nil, marshalErrorf("can not marshal %T into %s", value, info) 680 } 681 682 func encBigInt(x int64) []byte { 683 return []byte{byte(x >> 56), byte(x >> 48), byte(x >> 40), byte(x >> 32), 684 byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)} 685 } 686 687 func bytesToInt64(data []byte) (ret int64) { 688 for i := range data { 689 ret |= int64(data[i]) << (8 * uint(len(data)-i-1)) 690 } 691 return ret 692 } 693 694 func bytesToUint64(data []byte) (ret uint64) { 695 for i := range data { 696 ret |= uint64(data[i]) << (8 * uint(len(data)-i-1)) 697 } 698 return ret 699 } 700 701 func unmarshalBigInt(info TypeInfo, data []byte, value interface{}) error { 702 return unmarshalIntlike(info, decBigInt(data), data, value) 703 } 704 705 func unmarshalInt(info TypeInfo, data []byte, value interface{}) error { 706 return unmarshalIntlike(info, int64(decInt(data)), data, value) 707 } 708 709 func unmarshalSmallInt(info TypeInfo, data []byte, value interface{}) error { 710 return unmarshalIntlike(info, int64(decShort(data)), data, value) 711 } 712 713 func unmarshalTinyInt(info TypeInfo, data []byte, value interface{}) error { 714 return unmarshalIntlike(info, int64(decTiny(data)), data, value) 715 } 716 717 func unmarshalVarint(info TypeInfo, data []byte, value interface{}) error { 718 switch v := value.(type) { 719 case *big.Int: 720 return unmarshalIntlike(info, 0, data, value) 721 case *uint64: 722 if len(data) == 9 && data[0] == 0 { 723 *v = bytesToUint64(data[1:]) 724 return nil 725 } 726 } 727 728 if len(data) > 8 { 729 return unmarshalErrorf("unmarshal int: varint value %v out of range for %T (use big.Int)", data, value) 730 } 731 732 int64Val := bytesToInt64(data) 733 if len(data) > 0 && len(data) < 8 && data[0]&0x80 > 0 { 734 int64Val -= (1 << uint(len(data)*8)) 735 } 736 return unmarshalIntlike(info, int64Val, data, value) 737 } 738 739 func marshalVarint(info TypeInfo, value interface{}) ([]byte, error) { 740 var ( 741 retBytes []byte 742 err error 743 ) 744 745 switch v := value.(type) { 746 case unsetColumn: 747 return nil, nil 748 case uint64: 749 if v > uint64(math.MaxInt64) { 750 retBytes = make([]byte, 9) 751 binary.BigEndian.PutUint64(retBytes[1:], v) 752 } else { 753 retBytes = make([]byte, 8) 754 binary.BigEndian.PutUint64(retBytes, v) 755 } 756 default: 757 retBytes, err = marshalBigInt(info, value) 758 } 759 760 if err == nil { 761 // trim down to most significant byte 762 i := 0 763 for ; i < len(retBytes)-1; i++ { 764 b0 := retBytes[i] 765 if b0 != 0 && b0 != 0xFF { 766 break 767 } 768 769 b1 := retBytes[i+1] 770 if b0 == 0 && b1 != 0 { 771 if b1&0x80 == 0 { 772 i++ 773 } 774 break 775 } 776 777 if b0 == 0xFF && b1 != 0xFF { 778 if b1&0x80 > 0 { 779 i++ 780 } 781 break 782 } 783 } 784 retBytes = retBytes[i:] 785 } 786 787 return retBytes, err 788 } 789 790 func unmarshalIntlike(info TypeInfo, int64Val int64, data []byte, value interface{}) error { 791 switch v := value.(type) { 792 case *int: 793 if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) { 794 return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v) 795 } 796 *v = int(int64Val) 797 return nil 798 case *uint: 799 unitVal := uint64(int64Val) 800 switch info.Type() { 801 case TypeInt: 802 *v = uint(unitVal) & 0xFFFFFFFF 803 case TypeSmallInt: 804 *v = uint(unitVal) & 0xFFFF 805 case TypeTinyInt: 806 *v = uint(unitVal) & 0xFF 807 default: 808 if ^uint(0) == math.MaxUint32 && (int64Val < 0 || int64Val > math.MaxUint32) { 809 return unmarshalErrorf("unmarshal int: value %d out of range for %T", unitVal, *v) 810 } 811 *v = uint(unitVal) 812 } 813 return nil 814 case *int64: 815 *v = int64Val 816 return nil 817 case *uint64: 818 switch info.Type() { 819 case TypeInt: 820 *v = uint64(int64Val) & 0xFFFFFFFF 821 case TypeSmallInt: 822 *v = uint64(int64Val) & 0xFFFF 823 case TypeTinyInt: 824 *v = uint64(int64Val) & 0xFF 825 default: 826 *v = uint64(int64Val) 827 } 828 return nil 829 case *int32: 830 if int64Val < math.MinInt32 || int64Val > math.MaxInt32 { 831 return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v) 832 } 833 *v = int32(int64Val) 834 return nil 835 case *uint32: 836 switch info.Type() { 837 case TypeInt: 838 *v = uint32(int64Val) & 0xFFFFFFFF 839 case TypeSmallInt: 840 *v = uint32(int64Val) & 0xFFFF 841 case TypeTinyInt: 842 *v = uint32(int64Val) & 0xFF 843 default: 844 if int64Val < 0 || int64Val > math.MaxUint32 { 845 return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v) 846 } 847 *v = uint32(int64Val) & 0xFFFFFFFF 848 } 849 return nil 850 case *int16: 851 if int64Val < math.MinInt16 || int64Val > math.MaxInt16 { 852 return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v) 853 } 854 *v = int16(int64Val) 855 return nil 856 case *uint16: 857 switch info.Type() { 858 case TypeSmallInt: 859 *v = uint16(int64Val) & 0xFFFF 860 case TypeTinyInt: 861 *v = uint16(int64Val) & 0xFF 862 default: 863 if int64Val < 0 || int64Val > math.MaxUint16 { 864 return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v) 865 } 866 *v = uint16(int64Val) & 0xFFFF 867 } 868 return nil 869 case *int8: 870 if int64Val < math.MinInt8 || int64Val > math.MaxInt8 { 871 return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v) 872 } 873 *v = int8(int64Val) 874 return nil 875 case *uint8: 876 if info.Type() != TypeTinyInt && (int64Val < 0 || int64Val > math.MaxUint8) { 877 return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v) 878 } 879 *v = uint8(int64Val) & 0xFF 880 return nil 881 case *big.Int: 882 decBigInt2C(data, v) 883 return nil 884 case *string: 885 *v = strconv.FormatInt(int64Val, 10) 886 return nil 887 } 888 889 rv := reflect.ValueOf(value) 890 if rv.Kind() != reflect.Ptr { 891 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 892 } 893 rv = rv.Elem() 894 895 switch rv.Type().Kind() { 896 case reflect.Int: 897 if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) { 898 return unmarshalErrorf("unmarshal int: value %d out of range", int64Val) 899 } 900 rv.SetInt(int64Val) 901 return nil 902 case reflect.Int64: 903 rv.SetInt(int64Val) 904 return nil 905 case reflect.Int32: 906 if int64Val < math.MinInt32 || int64Val > math.MaxInt32 { 907 return unmarshalErrorf("unmarshal int: value %d out of range", int64Val) 908 } 909 rv.SetInt(int64Val) 910 return nil 911 case reflect.Int16: 912 if int64Val < math.MinInt16 || int64Val > math.MaxInt16 { 913 return unmarshalErrorf("unmarshal int: value %d out of range", int64Val) 914 } 915 rv.SetInt(int64Val) 916 return nil 917 case reflect.Int8: 918 if int64Val < math.MinInt8 || int64Val > math.MaxInt8 { 919 return unmarshalErrorf("unmarshal int: value %d out of range", int64Val) 920 } 921 rv.SetInt(int64Val) 922 return nil 923 case reflect.Uint: 924 unitVal := uint64(int64Val) 925 switch info.Type() { 926 case TypeInt: 927 rv.SetUint(unitVal & 0xFFFFFFFF) 928 case TypeSmallInt: 929 rv.SetUint(unitVal & 0xFFFF) 930 case TypeTinyInt: 931 rv.SetUint(unitVal & 0xFF) 932 default: 933 if ^uint(0) == math.MaxUint32 && (int64Val < 0 || int64Val > math.MaxUint32) { 934 return unmarshalErrorf("unmarshal int: value %d out of range for %s", unitVal, rv.Type()) 935 } 936 rv.SetUint(unitVal) 937 } 938 return nil 939 case reflect.Uint64: 940 unitVal := uint64(int64Val) 941 switch info.Type() { 942 case TypeInt: 943 rv.SetUint(unitVal & 0xFFFFFFFF) 944 case TypeSmallInt: 945 rv.SetUint(unitVal & 0xFFFF) 946 case TypeTinyInt: 947 rv.SetUint(unitVal & 0xFF) 948 default: 949 rv.SetUint(unitVal) 950 } 951 return nil 952 case reflect.Uint32: 953 unitVal := uint64(int64Val) 954 switch info.Type() { 955 case TypeInt: 956 rv.SetUint(unitVal & 0xFFFFFFFF) 957 case TypeSmallInt: 958 rv.SetUint(unitVal & 0xFFFF) 959 case TypeTinyInt: 960 rv.SetUint(unitVal & 0xFF) 961 default: 962 if int64Val < 0 || int64Val > math.MaxUint32 { 963 return unmarshalErrorf("unmarshal int: value %d out of range for %s", int64Val, rv.Type()) 964 } 965 rv.SetUint(unitVal & 0xFFFFFFFF) 966 } 967 return nil 968 case reflect.Uint16: 969 unitVal := uint64(int64Val) 970 switch info.Type() { 971 case TypeSmallInt: 972 rv.SetUint(unitVal & 0xFFFF) 973 case TypeTinyInt: 974 rv.SetUint(unitVal & 0xFF) 975 default: 976 if int64Val < 0 || int64Val > math.MaxUint16 { 977 return unmarshalErrorf("unmarshal int: value %d out of range for %s", int64Val, rv.Type()) 978 } 979 rv.SetUint(unitVal & 0xFFFF) 980 } 981 return nil 982 case reflect.Uint8: 983 if info.Type() != TypeTinyInt && (int64Val < 0 || int64Val > math.MaxUint8) { 984 return unmarshalErrorf("unmarshal int: value %d out of range for %s", int64Val, rv.Type()) 985 } 986 rv.SetUint(uint64(int64Val) & 0xff) 987 return nil 988 } 989 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 990 } 991 992 func decBigInt(data []byte) int64 { 993 if len(data) != 8 { 994 return 0 995 } 996 return int64(data[0])<<56 | int64(data[1])<<48 | 997 int64(data[2])<<40 | int64(data[3])<<32 | 998 int64(data[4])<<24 | int64(data[5])<<16 | 999 int64(data[6])<<8 | int64(data[7]) 1000 } 1001 1002 func marshalBool(info TypeInfo, value interface{}) ([]byte, error) { 1003 switch v := value.(type) { 1004 case Marshaler: 1005 return v.MarshalCQL(info) 1006 case unsetColumn: 1007 return nil, nil 1008 case bool: 1009 return encBool(v), nil 1010 } 1011 1012 if value == nil { 1013 return nil, nil 1014 } 1015 1016 rv := reflect.ValueOf(value) 1017 switch rv.Type().Kind() { 1018 case reflect.Bool: 1019 return encBool(rv.Bool()), nil 1020 } 1021 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1022 } 1023 1024 func encBool(v bool) []byte { 1025 if v { 1026 return []byte{1} 1027 } 1028 return []byte{0} 1029 } 1030 1031 func unmarshalBool(info TypeInfo, data []byte, value interface{}) error { 1032 switch v := value.(type) { 1033 case Unmarshaler: 1034 return v.UnmarshalCQL(info, data) 1035 case *bool: 1036 *v = decBool(data) 1037 return nil 1038 } 1039 rv := reflect.ValueOf(value) 1040 if rv.Kind() != reflect.Ptr { 1041 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 1042 } 1043 rv = rv.Elem() 1044 switch rv.Type().Kind() { 1045 case reflect.Bool: 1046 rv.SetBool(decBool(data)) 1047 return nil 1048 } 1049 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1050 } 1051 1052 func decBool(v []byte) bool { 1053 if len(v) == 0 { 1054 return false 1055 } 1056 return v[0] != 0 1057 } 1058 1059 func marshalFloat(info TypeInfo, value interface{}) ([]byte, error) { 1060 switch v := value.(type) { 1061 case Marshaler: 1062 return v.MarshalCQL(info) 1063 case unsetColumn: 1064 return nil, nil 1065 case float32: 1066 return encInt(int32(math.Float32bits(v))), nil 1067 } 1068 1069 if value == nil { 1070 return nil, nil 1071 } 1072 1073 rv := reflect.ValueOf(value) 1074 switch rv.Type().Kind() { 1075 case reflect.Float32: 1076 return encInt(int32(math.Float32bits(float32(rv.Float())))), nil 1077 } 1078 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1079 } 1080 1081 func unmarshalFloat(info TypeInfo, data []byte, value interface{}) error { 1082 switch v := value.(type) { 1083 case Unmarshaler: 1084 return v.UnmarshalCQL(info, data) 1085 case *float32: 1086 *v = math.Float32frombits(uint32(decInt(data))) 1087 return nil 1088 } 1089 rv := reflect.ValueOf(value) 1090 if rv.Kind() != reflect.Ptr { 1091 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 1092 } 1093 rv = rv.Elem() 1094 switch rv.Type().Kind() { 1095 case reflect.Float32: 1096 rv.SetFloat(float64(math.Float32frombits(uint32(decInt(data))))) 1097 return nil 1098 } 1099 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1100 } 1101 1102 func marshalDouble(info TypeInfo, value interface{}) ([]byte, error) { 1103 switch v := value.(type) { 1104 case Marshaler: 1105 return v.MarshalCQL(info) 1106 case unsetColumn: 1107 return nil, nil 1108 case float64: 1109 return encBigInt(int64(math.Float64bits(v))), nil 1110 } 1111 if value == nil { 1112 return nil, nil 1113 } 1114 rv := reflect.ValueOf(value) 1115 switch rv.Type().Kind() { 1116 case reflect.Float64: 1117 return encBigInt(int64(math.Float64bits(rv.Float()))), nil 1118 } 1119 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1120 } 1121 1122 func unmarshalDouble(info TypeInfo, data []byte, value interface{}) error { 1123 switch v := value.(type) { 1124 case Unmarshaler: 1125 return v.UnmarshalCQL(info, data) 1126 case *float64: 1127 *v = math.Float64frombits(uint64(decBigInt(data))) 1128 return nil 1129 } 1130 rv := reflect.ValueOf(value) 1131 if rv.Kind() != reflect.Ptr { 1132 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 1133 } 1134 rv = rv.Elem() 1135 switch rv.Type().Kind() { 1136 case reflect.Float64: 1137 rv.SetFloat(math.Float64frombits(uint64(decBigInt(data)))) 1138 return nil 1139 } 1140 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1141 } 1142 1143 func marshalDecimal(info TypeInfo, value interface{}) ([]byte, error) { 1144 if value == nil { 1145 return nil, nil 1146 } 1147 1148 switch v := value.(type) { 1149 case Marshaler: 1150 return v.MarshalCQL(info) 1151 case unsetColumn: 1152 return nil, nil 1153 case inf.Dec: 1154 unscaled := encBigInt2C(v.UnscaledBig()) 1155 if unscaled == nil { 1156 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1157 } 1158 1159 buf := make([]byte, 4+len(unscaled)) 1160 copy(buf[0:4], encInt(int32(v.Scale()))) 1161 copy(buf[4:], unscaled) 1162 return buf, nil 1163 } 1164 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1165 } 1166 1167 func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error { 1168 switch v := value.(type) { 1169 case Unmarshaler: 1170 return v.UnmarshalCQL(info, data) 1171 case *inf.Dec: 1172 if len(data) < 4 { 1173 return unmarshalErrorf("inf.Dec needs at least 4 bytes, while value has only %d", len(data)) 1174 } 1175 scale := decInt(data[0:4]) 1176 unscaled := decBigInt2C(data[4:], nil) 1177 *v = *inf.NewDecBig(unscaled, inf.Scale(scale)) 1178 return nil 1179 } 1180 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1181 } 1182 1183 // decBigInt2C sets the value of n to the big-endian two's complement 1184 // value stored in the given data. If data[0]&80 != 0, the number 1185 // is negative. If data is empty, the result will be 0. 1186 func decBigInt2C(data []byte, n *big.Int) *big.Int { 1187 if n == nil { 1188 n = new(big.Int) 1189 } 1190 n.SetBytes(data) 1191 if len(data) > 0 && data[0]&0x80 > 0 { 1192 n.Sub(n, new(big.Int).Lsh(bigOne, uint(len(data))*8)) 1193 } 1194 return n 1195 } 1196 1197 // encBigInt2C returns the big-endian two's complement 1198 // form of n. 1199 func encBigInt2C(n *big.Int) []byte { 1200 switch n.Sign() { 1201 case 0: 1202 return []byte{0} 1203 case 1: 1204 b := n.Bytes() 1205 if b[0]&0x80 > 0 { 1206 b = append([]byte{0}, b...) 1207 } 1208 return b 1209 case -1: 1210 length := uint(n.BitLen()/8+1) * 8 1211 b := new(big.Int).Add(n, new(big.Int).Lsh(bigOne, length)).Bytes() 1212 // When the most significant bit is on a byte 1213 // boundary, we can get some extra significant 1214 // bits, so strip them off when that happens. 1215 if len(b) >= 2 && b[0] == 0xff && b[1]&0x80 != 0 { 1216 b = b[1:] 1217 } 1218 return b 1219 } 1220 return nil 1221 } 1222 1223 func marshalTime(info TypeInfo, value interface{}) ([]byte, error) { 1224 switch v := value.(type) { 1225 case Marshaler: 1226 return v.MarshalCQL(info) 1227 case unsetColumn: 1228 return nil, nil 1229 case int64: 1230 return encBigInt(v), nil 1231 case time.Duration: 1232 return encBigInt(v.Nanoseconds()), nil 1233 } 1234 1235 if value == nil { 1236 return nil, nil 1237 } 1238 1239 rv := reflect.ValueOf(value) 1240 switch rv.Type().Kind() { 1241 case reflect.Int64: 1242 return encBigInt(rv.Int()), nil 1243 } 1244 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1245 } 1246 1247 func marshalTimestamp(info TypeInfo, value interface{}) ([]byte, error) { 1248 switch v := value.(type) { 1249 case Marshaler: 1250 return v.MarshalCQL(info) 1251 case unsetColumn: 1252 return nil, nil 1253 case int64: 1254 return encBigInt(v), nil 1255 case time.Time: 1256 if v.IsZero() { 1257 return []byte{}, nil 1258 } 1259 x := int64(v.UTC().Unix()*1e3) + int64(v.UTC().Nanosecond()/1e6) 1260 return encBigInt(x), nil 1261 } 1262 1263 if value == nil { 1264 return nil, nil 1265 } 1266 1267 rv := reflect.ValueOf(value) 1268 switch rv.Type().Kind() { 1269 case reflect.Int64: 1270 return encBigInt(rv.Int()), nil 1271 } 1272 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1273 } 1274 1275 func unmarshalTime(info TypeInfo, data []byte, value interface{}) error { 1276 switch v := value.(type) { 1277 case Unmarshaler: 1278 return v.UnmarshalCQL(info, data) 1279 case *int64: 1280 *v = decBigInt(data) 1281 return nil 1282 case *time.Duration: 1283 *v = time.Duration(decBigInt(data)) 1284 return nil 1285 } 1286 1287 rv := reflect.ValueOf(value) 1288 if rv.Kind() != reflect.Ptr { 1289 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 1290 } 1291 rv = rv.Elem() 1292 switch rv.Type().Kind() { 1293 case reflect.Int64: 1294 rv.SetInt(decBigInt(data)) 1295 return nil 1296 } 1297 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1298 } 1299 1300 func unmarshalTimestamp(info TypeInfo, data []byte, value interface{}) error { 1301 switch v := value.(type) { 1302 case Unmarshaler: 1303 return v.UnmarshalCQL(info, data) 1304 case *int64: 1305 *v = decBigInt(data) 1306 return nil 1307 case *time.Time: 1308 if len(data) == 0 { 1309 *v = time.Time{} 1310 return nil 1311 } 1312 x := decBigInt(data) 1313 sec := x / 1000 1314 nsec := (x - sec*1000) * 1000000 1315 *v = time.Unix(sec, nsec).In(time.UTC) 1316 return nil 1317 } 1318 1319 rv := reflect.ValueOf(value) 1320 if rv.Kind() != reflect.Ptr { 1321 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 1322 } 1323 rv = rv.Elem() 1324 switch rv.Type().Kind() { 1325 case reflect.Int64: 1326 rv.SetInt(decBigInt(data)) 1327 return nil 1328 } 1329 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1330 } 1331 1332 const millisecondsInADay int64 = 24 * 60 * 60 * 1000 1333 1334 func marshalDate(info TypeInfo, value interface{}) ([]byte, error) { 1335 var timestamp int64 1336 switch v := value.(type) { 1337 case Marshaler: 1338 return v.MarshalCQL(info) 1339 case unsetColumn: 1340 return nil, nil 1341 case int64: 1342 timestamp = v 1343 x := timestamp/millisecondsInADay + int64(1<<31) 1344 return encInt(int32(x)), nil 1345 case time.Time: 1346 if v.IsZero() { 1347 return []byte{}, nil 1348 } 1349 timestamp = int64(v.UTC().Unix()*1e3) + int64(v.UTC().Nanosecond()/1e6) 1350 x := timestamp/millisecondsInADay + int64(1<<31) 1351 return encInt(int32(x)), nil 1352 case *time.Time: 1353 if v.IsZero() { 1354 return []byte{}, nil 1355 } 1356 timestamp = int64(v.UTC().Unix()*1e3) + int64(v.UTC().Nanosecond()/1e6) 1357 x := timestamp/millisecondsInADay + int64(1<<31) 1358 return encInt(int32(x)), nil 1359 case string: 1360 if v == "" { 1361 return []byte{}, nil 1362 } 1363 t, err := time.Parse("2006-01-02", v) 1364 if err != nil { 1365 return nil, marshalErrorf("can not marshal %T into %s, date layout must be '2006-01-02'", value, info) 1366 } 1367 timestamp = int64(t.UTC().Unix()*1e3) + int64(t.UTC().Nanosecond()/1e6) 1368 x := timestamp/millisecondsInADay + int64(1<<31) 1369 return encInt(int32(x)), nil 1370 } 1371 1372 if value == nil { 1373 return nil, nil 1374 } 1375 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1376 } 1377 1378 func unmarshalDate(info TypeInfo, data []byte, value interface{}) error { 1379 switch v := value.(type) { 1380 case Unmarshaler: 1381 return v.UnmarshalCQL(info, data) 1382 case *time.Time: 1383 if len(data) == 0 { 1384 *v = time.Time{} 1385 return nil 1386 } 1387 var origin uint32 = 1 << 31 1388 var current uint32 = binary.BigEndian.Uint32(data) 1389 timestamp := (int64(current) - int64(origin)) * millisecondsInADay 1390 *v = time.UnixMilli(timestamp).In(time.UTC) 1391 return nil 1392 case *string: 1393 if len(data) == 0 { 1394 *v = "" 1395 return nil 1396 } 1397 var origin uint32 = 1 << 31 1398 var current uint32 = binary.BigEndian.Uint32(data) 1399 timestamp := (int64(current) - int64(origin)) * millisecondsInADay 1400 *v = time.UnixMilli(timestamp).In(time.UTC).Format("2006-01-02") 1401 return nil 1402 } 1403 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1404 } 1405 1406 func marshalDuration(info TypeInfo, value interface{}) ([]byte, error) { 1407 switch v := value.(type) { 1408 case Marshaler: 1409 return v.MarshalCQL(info) 1410 case unsetColumn: 1411 return nil, nil 1412 case int64: 1413 return encVints(0, 0, v), nil 1414 case time.Duration: 1415 return encVints(0, 0, v.Nanoseconds()), nil 1416 case string: 1417 d, err := time.ParseDuration(v) 1418 if err != nil { 1419 return nil, err 1420 } 1421 return encVints(0, 0, d.Nanoseconds()), nil 1422 case Duration: 1423 return encVints(v.Months, v.Days, v.Nanoseconds), nil 1424 } 1425 1426 if value == nil { 1427 return nil, nil 1428 } 1429 1430 rv := reflect.ValueOf(value) 1431 switch rv.Type().Kind() { 1432 case reflect.Int64: 1433 return encBigInt(rv.Int()), nil 1434 } 1435 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1436 } 1437 1438 func unmarshalDuration(info TypeInfo, data []byte, value interface{}) error { 1439 switch v := value.(type) { 1440 case Unmarshaler: 1441 return v.UnmarshalCQL(info, data) 1442 case *Duration: 1443 if len(data) == 0 { 1444 *v = Duration{ 1445 Months: 0, 1446 Days: 0, 1447 Nanoseconds: 0, 1448 } 1449 return nil 1450 } 1451 months, days, nanos, err := decVints(data) 1452 if err != nil { 1453 return unmarshalErrorf("failed to unmarshal %s into %T: %s", info, value, err.Error()) 1454 } 1455 *v = Duration{ 1456 Months: months, 1457 Days: days, 1458 Nanoseconds: nanos, 1459 } 1460 return nil 1461 } 1462 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1463 } 1464 1465 func decVints(data []byte) (int32, int32, int64, error) { 1466 month, i, err := decVint(data, 0) 1467 if err != nil { 1468 return 0, 0, 0, fmt.Errorf("failed to extract month: %s", err.Error()) 1469 } 1470 days, i, err := decVint(data, i) 1471 if err != nil { 1472 return 0, 0, 0, fmt.Errorf("failed to extract days: %s", err.Error()) 1473 } 1474 nanos, _, err := decVint(data, i) 1475 if err != nil { 1476 return 0, 0, 0, fmt.Errorf("failed to extract nanoseconds: %s", err.Error()) 1477 } 1478 return int32(month), int32(days), nanos, err 1479 } 1480 1481 func decVint(data []byte, start int) (int64, int, error) { 1482 if len(data) <= start { 1483 return 0, 0, errors.New("unexpected eof") 1484 } 1485 firstByte := data[start] 1486 if firstByte&0x80 == 0 { 1487 return decIntZigZag(uint64(firstByte)), start + 1, nil 1488 } 1489 numBytes := bits.LeadingZeros32(uint32(^firstByte)) - 24 1490 ret := uint64(firstByte & (0xff >> uint(numBytes))) 1491 if len(data) < start+numBytes+1 { 1492 return 0, 0, fmt.Errorf("data expect to have %d bytes, but it has only %d", start+numBytes+1, len(data)) 1493 } 1494 for i := start; i < start+numBytes; i++ { 1495 ret <<= 8 1496 ret |= uint64(data[i+1] & 0xff) 1497 } 1498 return decIntZigZag(ret), start + numBytes + 1, nil 1499 } 1500 1501 func decIntZigZag(n uint64) int64 { 1502 return int64((n >> 1) ^ -(n & 1)) 1503 } 1504 1505 func encIntZigZag(n int64) uint64 { 1506 return uint64((n >> 63) ^ (n << 1)) 1507 } 1508 1509 func encVints(months int32, seconds int32, nanos int64) []byte { 1510 buf := append(encVint(int64(months)), encVint(int64(seconds))...) 1511 return append(buf, encVint(nanos)...) 1512 } 1513 1514 func encVint(v int64) []byte { 1515 vEnc := encIntZigZag(v) 1516 lead0 := bits.LeadingZeros64(vEnc) 1517 numBytes := (639 - lead0*9) >> 6 1518 1519 // It can be 1 or 0 is v ==0 1520 if numBytes <= 1 { 1521 return []byte{byte(vEnc)} 1522 } 1523 extraBytes := numBytes - 1 1524 var buf = make([]byte, numBytes) 1525 for i := extraBytes; i >= 0; i-- { 1526 buf[i] = byte(vEnc) 1527 vEnc >>= 8 1528 } 1529 buf[0] |= byte(^(0xff >> uint(extraBytes))) 1530 return buf 1531 } 1532 1533 func writeCollectionSize(info CollectionType, n int, buf *bytes.Buffer) error { 1534 if info.proto > protoVersion2 { 1535 if n > math.MaxInt32 { 1536 return marshalErrorf("marshal: collection too large") 1537 } 1538 1539 buf.WriteByte(byte(n >> 24)) 1540 buf.WriteByte(byte(n >> 16)) 1541 buf.WriteByte(byte(n >> 8)) 1542 buf.WriteByte(byte(n)) 1543 } else { 1544 if n > math.MaxUint16 { 1545 return marshalErrorf("marshal: collection too large") 1546 } 1547 1548 buf.WriteByte(byte(n >> 8)) 1549 buf.WriteByte(byte(n)) 1550 } 1551 1552 return nil 1553 } 1554 1555 func marshalList(info TypeInfo, value interface{}) ([]byte, error) { 1556 listInfo, ok := info.(CollectionType) 1557 if !ok { 1558 return nil, marshalErrorf("marshal: can not marshal non collection type into list") 1559 } 1560 1561 if value == nil { 1562 return nil, nil 1563 } else if _, ok := value.(unsetColumn); ok { 1564 return nil, nil 1565 } 1566 1567 rv := reflect.ValueOf(value) 1568 t := rv.Type() 1569 k := t.Kind() 1570 if k == reflect.Slice && rv.IsNil() { 1571 return nil, nil 1572 } 1573 1574 switch k { 1575 case reflect.Slice, reflect.Array: 1576 buf := &bytes.Buffer{} 1577 n := rv.Len() 1578 1579 if err := writeCollectionSize(listInfo, n, buf); err != nil { 1580 return nil, err 1581 } 1582 1583 for i := 0; i < n; i++ { 1584 item, err := Marshal(listInfo.Elem, rv.Index(i).Interface()) 1585 if err != nil { 1586 return nil, err 1587 } 1588 itemLen := len(item) 1589 // Set the value to null for supported protocols 1590 if item == nil && listInfo.proto > protoVersion2 { 1591 itemLen = -1 1592 } 1593 if err := writeCollectionSize(listInfo, itemLen, buf); err != nil { 1594 return nil, err 1595 } 1596 buf.Write(item) 1597 } 1598 return buf.Bytes(), nil 1599 case reflect.Map: 1600 elem := t.Elem() 1601 if elem.Kind() == reflect.Struct && elem.NumField() == 0 { 1602 rkeys := rv.MapKeys() 1603 keys := make([]interface{}, len(rkeys)) 1604 for i := 0; i < len(keys); i++ { 1605 keys[i] = rkeys[i].Interface() 1606 } 1607 return marshalList(listInfo, keys) 1608 } 1609 } 1610 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1611 } 1612 1613 func readCollectionSize(info CollectionType, data []byte) (size, read int, err error) { 1614 if info.proto > protoVersion2 { 1615 if len(data) < 4 { 1616 return 0, 0, unmarshalErrorf("unmarshal list: unexpected eof") 1617 } 1618 size = int(int32(data[0])<<24 | int32(data[1])<<16 | int32(data[2])<<8 | int32(data[3])) 1619 read = 4 1620 } else { 1621 if len(data) < 2 { 1622 return 0, 0, unmarshalErrorf("unmarshal list: unexpected eof") 1623 } 1624 size = int(data[0])<<8 | int(data[1]) 1625 read = 2 1626 } 1627 return 1628 } 1629 1630 func unmarshalList(info TypeInfo, data []byte, value interface{}) error { 1631 listInfo, ok := info.(CollectionType) 1632 if !ok { 1633 return unmarshalErrorf("unmarshal: can not unmarshal none collection type into list") 1634 } 1635 1636 rv := reflect.ValueOf(value) 1637 if rv.Kind() != reflect.Ptr { 1638 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 1639 } 1640 rv = rv.Elem() 1641 t := rv.Type() 1642 k := t.Kind() 1643 1644 switch k { 1645 case reflect.Slice, reflect.Array: 1646 if data == nil { 1647 if k == reflect.Array { 1648 return unmarshalErrorf("unmarshal list: can not store nil in array value") 1649 } 1650 if rv.IsNil() { 1651 return nil 1652 } 1653 rv.Set(reflect.Zero(t)) 1654 return nil 1655 } 1656 n, p, err := readCollectionSize(listInfo, data) 1657 if err != nil { 1658 return err 1659 } 1660 data = data[p:] 1661 if k == reflect.Array { 1662 if rv.Len() != n { 1663 return unmarshalErrorf("unmarshal list: array with wrong size") 1664 } 1665 } else { 1666 rv.Set(reflect.MakeSlice(t, n, n)) 1667 } 1668 for i := 0; i < n; i++ { 1669 m, p, err := readCollectionSize(listInfo, data) 1670 if err != nil { 1671 return err 1672 } 1673 data = data[p:] 1674 // In case m < 0, the value is null, and unmarshalData should be nil. 1675 var unmarshalData []byte 1676 if m >= 0 { 1677 if len(data) < m { 1678 return unmarshalErrorf("unmarshal list: unexpected eof") 1679 } 1680 unmarshalData = data[:m] 1681 data = data[m:] 1682 } 1683 if err := Unmarshal(listInfo.Elem, unmarshalData, rv.Index(i).Addr().Interface()); err != nil { 1684 return err 1685 } 1686 } 1687 return nil 1688 } 1689 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1690 } 1691 1692 func marshalMap(info TypeInfo, value interface{}) ([]byte, error) { 1693 mapInfo, ok := info.(CollectionType) 1694 if !ok { 1695 return nil, marshalErrorf("marshal: can not marshal none collection type into map") 1696 } 1697 1698 if value == nil { 1699 return nil, nil 1700 } else if _, ok := value.(unsetColumn); ok { 1701 return nil, nil 1702 } 1703 1704 rv := reflect.ValueOf(value) 1705 1706 t := rv.Type() 1707 if t.Kind() != reflect.Map { 1708 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1709 } 1710 1711 if rv.IsNil() { 1712 return nil, nil 1713 } 1714 1715 buf := &bytes.Buffer{} 1716 n := rv.Len() 1717 1718 if err := writeCollectionSize(mapInfo, n, buf); err != nil { 1719 return nil, err 1720 } 1721 1722 keys := rv.MapKeys() 1723 for _, key := range keys { 1724 item, err := Marshal(mapInfo.Key, key.Interface()) 1725 if err != nil { 1726 return nil, err 1727 } 1728 itemLen := len(item) 1729 // Set the key to null for supported protocols 1730 if item == nil && mapInfo.proto > protoVersion2 { 1731 itemLen = -1 1732 } 1733 if err := writeCollectionSize(mapInfo, itemLen, buf); err != nil { 1734 return nil, err 1735 } 1736 buf.Write(item) 1737 1738 item, err = Marshal(mapInfo.Elem, rv.MapIndex(key).Interface()) 1739 if err != nil { 1740 return nil, err 1741 } 1742 itemLen = len(item) 1743 // Set the value to null for supported protocols 1744 if item == nil && mapInfo.proto > protoVersion2 { 1745 itemLen = -1 1746 } 1747 if err := writeCollectionSize(mapInfo, itemLen, buf); err != nil { 1748 return nil, err 1749 } 1750 buf.Write(item) 1751 } 1752 return buf.Bytes(), nil 1753 } 1754 1755 func unmarshalMap(info TypeInfo, data []byte, value interface{}) error { 1756 mapInfo, ok := info.(CollectionType) 1757 if !ok { 1758 return unmarshalErrorf("unmarshal: can not unmarshal none collection type into map") 1759 } 1760 1761 rv := reflect.ValueOf(value) 1762 if rv.Kind() != reflect.Ptr { 1763 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 1764 } 1765 rv = rv.Elem() 1766 t := rv.Type() 1767 if t.Kind() != reflect.Map { 1768 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 1769 } 1770 if data == nil { 1771 rv.Set(reflect.Zero(t)) 1772 return nil 1773 } 1774 n, p, err := readCollectionSize(mapInfo, data) 1775 if err != nil { 1776 return err 1777 } 1778 if n < 0 { 1779 return unmarshalErrorf("negative map size %d", n) 1780 } 1781 rv.Set(reflect.MakeMapWithSize(t, n)) 1782 data = data[p:] 1783 for i := 0; i < n; i++ { 1784 m, p, err := readCollectionSize(mapInfo, data) 1785 if err != nil { 1786 return err 1787 } 1788 data = data[p:] 1789 key := reflect.New(t.Key()) 1790 // In case m < 0, the key is null, and unmarshalData should be nil. 1791 var unmarshalData []byte 1792 if m >= 0 { 1793 if len(data) < m { 1794 return unmarshalErrorf("unmarshal map: unexpected eof") 1795 } 1796 unmarshalData = data[:m] 1797 data = data[m:] 1798 } 1799 if err := Unmarshal(mapInfo.Key, unmarshalData, key.Interface()); err != nil { 1800 return err 1801 } 1802 1803 m, p, err = readCollectionSize(mapInfo, data) 1804 if err != nil { 1805 return err 1806 } 1807 data = data[p:] 1808 val := reflect.New(t.Elem()) 1809 1810 // In case m < 0, the value is null, and unmarshalData should be nil. 1811 unmarshalData = nil 1812 if m >= 0 { 1813 if len(data) < m { 1814 return unmarshalErrorf("unmarshal map: unexpected eof") 1815 } 1816 unmarshalData = data[:m] 1817 data = data[m:] 1818 } 1819 if err := Unmarshal(mapInfo.Elem, unmarshalData, val.Interface()); err != nil { 1820 return err 1821 } 1822 1823 rv.SetMapIndex(key.Elem(), val.Elem()) 1824 } 1825 return nil 1826 } 1827 1828 func marshalUUID(info TypeInfo, value interface{}) ([]byte, error) { 1829 switch val := value.(type) { 1830 case unsetColumn: 1831 return nil, nil 1832 case UUID: 1833 return val.Bytes(), nil 1834 case [16]byte: 1835 return val[:], nil 1836 case []byte: 1837 if len(val) != 16 { 1838 return nil, marshalErrorf("can not marshal []byte %d bytes long into %s, must be exactly 16 bytes long", len(val), info) 1839 } 1840 return val, nil 1841 case string: 1842 b, err := ParseUUID(val) 1843 if err != nil { 1844 return nil, err 1845 } 1846 return b[:], nil 1847 } 1848 1849 if value == nil { 1850 return nil, nil 1851 } 1852 1853 return nil, marshalErrorf("can not marshal %T into %s", value, info) 1854 } 1855 1856 func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error { 1857 if len(data) == 0 { 1858 switch v := value.(type) { 1859 case *string: 1860 *v = "" 1861 case *[]byte: 1862 *v = nil 1863 case *UUID: 1864 *v = UUID{} 1865 default: 1866 return unmarshalErrorf("can not unmarshal X %s into %T", info, value) 1867 } 1868 1869 return nil 1870 } 1871 1872 if len(data) != 16 { 1873 return unmarshalErrorf("unable to parse UUID: UUIDs must be exactly 16 bytes long") 1874 } 1875 1876 switch v := value.(type) { 1877 case *[16]byte: 1878 copy((*v)[:], data) 1879 return nil 1880 case *UUID: 1881 copy((*v)[:], data) 1882 return nil 1883 } 1884 1885 u, err := UUIDFromBytes(data) 1886 if err != nil { 1887 return unmarshalErrorf("unable to parse UUID: %s", err) 1888 } 1889 1890 switch v := value.(type) { 1891 case *string: 1892 *v = u.String() 1893 return nil 1894 case *[]byte: 1895 *v = u[:] 1896 return nil 1897 } 1898 return unmarshalErrorf("can not unmarshal X %s into %T", info, value) 1899 } 1900 1901 func unmarshalTimeUUID(info TypeInfo, data []byte, value interface{}) error { 1902 switch v := value.(type) { 1903 case Unmarshaler: 1904 return v.UnmarshalCQL(info, data) 1905 case *time.Time: 1906 id, err := UUIDFromBytes(data) 1907 if err != nil { 1908 return err 1909 } else if id.Version() != 1 { 1910 return unmarshalErrorf("invalid timeuuid") 1911 } 1912 *v = id.Time() 1913 return nil 1914 default: 1915 return unmarshalUUID(info, data, value) 1916 } 1917 } 1918 1919 func marshalInet(info TypeInfo, value interface{}) ([]byte, error) { 1920 // we return either the 4 or 16 byte representation of an 1921 // ip address here otherwise the db value will be prefixed 1922 // with the remaining byte values e.g. ::ffff:127.0.0.1 and not 127.0.0.1 1923 switch val := value.(type) { 1924 case unsetColumn: 1925 return nil, nil 1926 case net.IP: 1927 t := val.To4() 1928 if t == nil { 1929 return val.To16(), nil 1930 } 1931 return t, nil 1932 case string: 1933 b := net.ParseIP(val) 1934 if b != nil { 1935 t := b.To4() 1936 if t == nil { 1937 return b.To16(), nil 1938 } 1939 return t, nil 1940 } 1941 return nil, marshalErrorf("cannot marshal. invalid ip string %s", val) 1942 } 1943 1944 if value == nil { 1945 return nil, nil 1946 } 1947 1948 return nil, marshalErrorf("cannot marshal %T into %s", value, info) 1949 } 1950 1951 func unmarshalInet(info TypeInfo, data []byte, value interface{}) error { 1952 switch v := value.(type) { 1953 case Unmarshaler: 1954 return v.UnmarshalCQL(info, data) 1955 case *net.IP: 1956 if x := len(data); !(x == 4 || x == 16) { 1957 return unmarshalErrorf("cannot unmarshal %s into %T: invalid sized IP: got %d bytes not 4 or 16", info, value, x) 1958 } 1959 buf := copyBytes(data) 1960 ip := net.IP(buf) 1961 if v4 := ip.To4(); v4 != nil { 1962 *v = v4 1963 return nil 1964 } 1965 *v = ip 1966 return nil 1967 case *string: 1968 if len(data) == 0 { 1969 *v = "" 1970 return nil 1971 } 1972 ip := net.IP(data) 1973 if v4 := ip.To4(); v4 != nil { 1974 *v = v4.String() 1975 return nil 1976 } 1977 *v = ip.String() 1978 return nil 1979 } 1980 return unmarshalErrorf("cannot unmarshal %s into %T", info, value) 1981 } 1982 1983 func marshalTuple(info TypeInfo, value interface{}) ([]byte, error) { 1984 tuple := info.(TupleTypeInfo) 1985 switch v := value.(type) { 1986 case unsetColumn: 1987 return nil, unmarshalErrorf("Invalid request: UnsetValue is unsupported for tuples") 1988 case []interface{}: 1989 if len(v) != len(tuple.Elems) { 1990 return nil, unmarshalErrorf("cannont marshal tuple: wrong number of elements") 1991 } 1992 1993 var buf []byte 1994 for i, elem := range v { 1995 if elem == nil { 1996 buf = appendInt(buf, int32(-1)) 1997 continue 1998 } 1999 2000 data, err := Marshal(tuple.Elems[i], elem) 2001 if err != nil { 2002 return nil, err 2003 } 2004 2005 n := len(data) 2006 buf = appendInt(buf, int32(n)) 2007 buf = append(buf, data...) 2008 } 2009 2010 return buf, nil 2011 } 2012 2013 rv := reflect.ValueOf(value) 2014 t := rv.Type() 2015 k := t.Kind() 2016 2017 switch k { 2018 case reflect.Struct: 2019 if v := t.NumField(); v != len(tuple.Elems) { 2020 return nil, marshalErrorf("can not marshal tuple into struct %v, not enough fields have %d need %d", t, v, len(tuple.Elems)) 2021 } 2022 2023 var buf []byte 2024 for i, elem := range tuple.Elems { 2025 field := rv.Field(i) 2026 2027 if field.Kind() == reflect.Ptr && field.IsNil() { 2028 buf = appendInt(buf, int32(-1)) 2029 continue 2030 } 2031 2032 data, err := Marshal(elem, field.Interface()) 2033 if err != nil { 2034 return nil, err 2035 } 2036 2037 n := len(data) 2038 buf = appendInt(buf, int32(n)) 2039 buf = append(buf, data...) 2040 } 2041 2042 return buf, nil 2043 case reflect.Slice, reflect.Array: 2044 size := rv.Len() 2045 if size != len(tuple.Elems) { 2046 return nil, marshalErrorf("can not marshal tuple into %v of length %d need %d elements", k, size, len(tuple.Elems)) 2047 } 2048 2049 var buf []byte 2050 for i, elem := range tuple.Elems { 2051 item := rv.Index(i) 2052 2053 if item.Kind() == reflect.Ptr && item.IsNil() { 2054 buf = appendInt(buf, int32(-1)) 2055 continue 2056 } 2057 2058 data, err := Marshal(elem, item.Interface()) 2059 if err != nil { 2060 return nil, err 2061 } 2062 2063 n := len(data) 2064 buf = appendInt(buf, int32(n)) 2065 buf = append(buf, data...) 2066 } 2067 2068 return buf, nil 2069 } 2070 2071 return nil, marshalErrorf("cannot marshal %T into %s", value, tuple) 2072 } 2073 2074 func readBytes(p []byte) ([]byte, []byte) { 2075 // TODO: really should use a framer 2076 size := readInt(p) 2077 p = p[4:] 2078 if size < 0 { 2079 return nil, p 2080 } 2081 return p[:size], p[size:] 2082 } 2083 2084 // currently only support unmarshal into a list of values, this makes it possible 2085 // to support tuples without changing the query API. In the future this can be extend 2086 // to allow unmarshalling into custom tuple types. 2087 func unmarshalTuple(info TypeInfo, data []byte, value interface{}) error { 2088 if v, ok := value.(Unmarshaler); ok { 2089 return v.UnmarshalCQL(info, data) 2090 } 2091 2092 tuple := info.(TupleTypeInfo) 2093 switch v := value.(type) { 2094 case []interface{}: 2095 for i, elem := range tuple.Elems { 2096 // each element inside data is a [bytes] 2097 var p []byte 2098 if len(data) >= 4 { 2099 p, data = readBytes(data) 2100 } 2101 err := Unmarshal(elem, p, v[i]) 2102 if err != nil { 2103 return err 2104 } 2105 } 2106 2107 return nil 2108 } 2109 2110 rv := reflect.ValueOf(value) 2111 if rv.Kind() != reflect.Ptr { 2112 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 2113 } 2114 2115 rv = rv.Elem() 2116 t := rv.Type() 2117 k := t.Kind() 2118 2119 switch k { 2120 case reflect.Struct: 2121 if v := t.NumField(); v != len(tuple.Elems) { 2122 return unmarshalErrorf("can not unmarshal tuple into struct %v, not enough fields have %d need %d", t, v, len(tuple.Elems)) 2123 } 2124 2125 for i, elem := range tuple.Elems { 2126 var p []byte 2127 if len(data) >= 4 { 2128 p, data = readBytes(data) 2129 } 2130 2131 v, err := elem.NewWithError() 2132 if err != nil { 2133 return err 2134 } 2135 if err := Unmarshal(elem, p, v); err != nil { 2136 return err 2137 } 2138 2139 switch rv.Field(i).Kind() { 2140 case reflect.Ptr: 2141 if p != nil { 2142 rv.Field(i).Set(reflect.ValueOf(v)) 2143 } else { 2144 rv.Field(i).Set(reflect.Zero(reflect.TypeOf(v))) 2145 } 2146 default: 2147 rv.Field(i).Set(reflect.ValueOf(v).Elem()) 2148 } 2149 } 2150 2151 return nil 2152 case reflect.Slice, reflect.Array: 2153 if k == reflect.Array { 2154 size := rv.Len() 2155 if size != len(tuple.Elems) { 2156 return unmarshalErrorf("can not unmarshal tuple into array of length %d need %d elements", size, len(tuple.Elems)) 2157 } 2158 } else { 2159 rv.Set(reflect.MakeSlice(t, len(tuple.Elems), len(tuple.Elems))) 2160 } 2161 2162 for i, elem := range tuple.Elems { 2163 var p []byte 2164 if len(data) >= 4 { 2165 p, data = readBytes(data) 2166 } 2167 2168 v, err := elem.NewWithError() 2169 if err != nil { 2170 return err 2171 } 2172 if err := Unmarshal(elem, p, v); err != nil { 2173 return err 2174 } 2175 2176 switch rv.Index(i).Kind() { 2177 case reflect.Ptr: 2178 if p != nil { 2179 rv.Index(i).Set(reflect.ValueOf(v)) 2180 } else { 2181 rv.Index(i).Set(reflect.Zero(reflect.TypeOf(v))) 2182 } 2183 default: 2184 rv.Index(i).Set(reflect.ValueOf(v).Elem()) 2185 } 2186 } 2187 2188 return nil 2189 } 2190 2191 return unmarshalErrorf("cannot unmarshal %s into %T", info, value) 2192 } 2193 2194 // UDTMarshaler is an interface which should be implemented by users wishing to 2195 // handle encoding UDT types to sent to Cassandra. Note: due to current implentations 2196 // methods defined for this interface must be value receivers not pointer receivers. 2197 type UDTMarshaler interface { 2198 // MarshalUDT will be called for each field in the the UDT returned by Cassandra, 2199 // the implementor should marshal the type to return by for example calling 2200 // Marshal. 2201 MarshalUDT(name string, info TypeInfo) ([]byte, error) 2202 } 2203 2204 // UDTUnmarshaler should be implemented by users wanting to implement custom 2205 // UDT unmarshaling. 2206 type UDTUnmarshaler interface { 2207 // UnmarshalUDT will be called for each field in the UDT return by Cassandra, 2208 // the implementor should unmarshal the data into the value of their chosing, 2209 // for example by calling Unmarshal. 2210 UnmarshalUDT(name string, info TypeInfo, data []byte) error 2211 } 2212 2213 func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) { 2214 udt := info.(UDTTypeInfo) 2215 2216 switch v := value.(type) { 2217 case Marshaler: 2218 return v.MarshalCQL(info) 2219 case unsetColumn: 2220 return nil, unmarshalErrorf("invalid request: UnsetValue is unsupported for user defined types") 2221 case UDTMarshaler: 2222 var buf []byte 2223 for _, e := range udt.Elements { 2224 data, err := v.MarshalUDT(e.Name, e.Type) 2225 if err != nil { 2226 return nil, err 2227 } 2228 2229 buf = appendBytes(buf, data) 2230 } 2231 2232 return buf, nil 2233 case map[string]interface{}: 2234 var buf []byte 2235 for _, e := range udt.Elements { 2236 val, ok := v[e.Name] 2237 2238 var data []byte 2239 2240 if ok { 2241 var err error 2242 data, err = Marshal(e.Type, val) 2243 if err != nil { 2244 return nil, err 2245 } 2246 } 2247 2248 buf = appendBytes(buf, data) 2249 } 2250 2251 return buf, nil 2252 } 2253 2254 k := reflect.ValueOf(value) 2255 if k.Kind() == reflect.Ptr { 2256 if k.IsNil() { 2257 return nil, marshalErrorf("cannot marshal %T into %s", value, info) 2258 } 2259 k = k.Elem() 2260 } 2261 2262 if k.Kind() != reflect.Struct || !k.IsValid() { 2263 return nil, marshalErrorf("cannot marshal %T into %s", value, info) 2264 } 2265 2266 fields := make(map[string]reflect.Value) 2267 t := reflect.TypeOf(value) 2268 for i := 0; i < t.NumField(); i++ { 2269 sf := t.Field(i) 2270 2271 if tag := sf.Tag.Get("cql"); tag != "" { 2272 fields[tag] = k.Field(i) 2273 } 2274 } 2275 2276 var buf []byte 2277 for _, e := range udt.Elements { 2278 f, ok := fields[e.Name] 2279 if !ok { 2280 f = k.FieldByName(e.Name) 2281 } 2282 2283 var data []byte 2284 if f.IsValid() && f.CanInterface() { 2285 var err error 2286 data, err = Marshal(e.Type, f.Interface()) 2287 if err != nil { 2288 return nil, err 2289 } 2290 } 2291 2292 buf = appendBytes(buf, data) 2293 } 2294 2295 return buf, nil 2296 } 2297 2298 func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error { 2299 switch v := value.(type) { 2300 case Unmarshaler: 2301 return v.UnmarshalCQL(info, data) 2302 case UDTUnmarshaler: 2303 udt := info.(UDTTypeInfo) 2304 2305 for id, e := range udt.Elements { 2306 if len(data) == 0 { 2307 return nil 2308 } 2309 if len(data) < 4 { 2310 return unmarshalErrorf("can not unmarshal %s: field [%d]%s: unexpected eof", info, id, e.Name) 2311 } 2312 2313 var p []byte 2314 p, data = readBytes(data) 2315 if err := v.UnmarshalUDT(e.Name, e.Type, p); err != nil { 2316 return err 2317 } 2318 } 2319 2320 return nil 2321 case *map[string]interface{}: 2322 udt := info.(UDTTypeInfo) 2323 2324 rv := reflect.ValueOf(value) 2325 if rv.Kind() != reflect.Ptr { 2326 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 2327 } 2328 2329 rv = rv.Elem() 2330 t := rv.Type() 2331 if t.Kind() != reflect.Map { 2332 return unmarshalErrorf("can not unmarshal %s into %T", info, value) 2333 } else if data == nil { 2334 rv.Set(reflect.Zero(t)) 2335 return nil 2336 } 2337 2338 rv.Set(reflect.MakeMap(t)) 2339 m := *v 2340 2341 for id, e := range udt.Elements { 2342 if len(data) == 0 { 2343 return nil 2344 } 2345 if len(data) < 4 { 2346 return unmarshalErrorf("can not unmarshal %s: field [%d]%s: unexpected eof", info, id, e.Name) 2347 } 2348 2349 valType, err := goType(e.Type) 2350 if err != nil { 2351 return unmarshalErrorf("can not unmarshal %s: %v", info, err) 2352 } 2353 2354 val := reflect.New(valType) 2355 2356 var p []byte 2357 p, data = readBytes(data) 2358 2359 if err := Unmarshal(e.Type, p, val.Interface()); err != nil { 2360 return err 2361 } 2362 2363 m[e.Name] = val.Elem().Interface() 2364 } 2365 2366 return nil 2367 } 2368 2369 rv := reflect.ValueOf(value) 2370 if rv.Kind() != reflect.Ptr { 2371 return unmarshalErrorf("can not unmarshal into non-pointer %T", value) 2372 } 2373 k := rv.Elem() 2374 if k.Kind() != reflect.Struct || !k.IsValid() { 2375 return unmarshalErrorf("cannot unmarshal %s into %T", info, value) 2376 } 2377 2378 if len(data) == 0 { 2379 if k.CanSet() { 2380 k.Set(reflect.Zero(k.Type())) 2381 } 2382 2383 return nil 2384 } 2385 2386 t := k.Type() 2387 fields := make(map[string]reflect.Value, t.NumField()) 2388 for i := 0; i < t.NumField(); i++ { 2389 sf := t.Field(i) 2390 2391 if tag := sf.Tag.Get("cql"); tag != "" { 2392 fields[tag] = k.Field(i) 2393 } 2394 } 2395 2396 udt := info.(UDTTypeInfo) 2397 for id, e := range udt.Elements { 2398 if len(data) == 0 { 2399 return nil 2400 } 2401 if len(data) < 4 { 2402 // UDT def does not match the column value 2403 return unmarshalErrorf("can not unmarshal %s: field [%d]%s: unexpected eof", info, id, e.Name) 2404 } 2405 2406 var p []byte 2407 p, data = readBytes(data) 2408 2409 f, ok := fields[e.Name] 2410 if !ok { 2411 f = k.FieldByName(e.Name) 2412 if f == emptyValue { 2413 // skip fields which exist in the UDT but not in 2414 // the struct passed in 2415 continue 2416 } 2417 } 2418 2419 if !f.IsValid() || !f.CanAddr() { 2420 return unmarshalErrorf("cannot unmarshal %s into %T: field %v is not valid", info, value, e.Name) 2421 } 2422 2423 fk := f.Addr().Interface() 2424 if err := Unmarshal(e.Type, p, fk); err != nil { 2425 return err 2426 } 2427 } 2428 2429 return nil 2430 } 2431 2432 // TypeInfo describes a Cassandra specific data type. 2433 type TypeInfo interface { 2434 Type() Type 2435 Version() byte 2436 Custom() string 2437 2438 // New creates a pointer to an empty version of whatever type 2439 // is referenced by the TypeInfo receiver. 2440 // 2441 // If there is no corresponding Go type for the CQL type, New panics. 2442 // 2443 // Deprecated: Use NewWithError instead. 2444 New() interface{} 2445 2446 // NewWithError creates a pointer to an empty version of whatever type 2447 // is referenced by the TypeInfo receiver. 2448 // 2449 // If there is no corresponding Go type for the CQL type, NewWithError returns an error. 2450 NewWithError() (interface{}, error) 2451 } 2452 2453 type NativeType struct { 2454 proto byte 2455 typ Type 2456 custom string // only used for TypeCustom 2457 } 2458 2459 func NewNativeType(proto byte, typ Type, custom string) NativeType { 2460 return NativeType{proto, typ, custom} 2461 } 2462 2463 func (t NativeType) NewWithError() (interface{}, error) { 2464 typ, err := goType(t) 2465 if err != nil { 2466 return nil, err 2467 } 2468 return reflect.New(typ).Interface(), nil 2469 } 2470 2471 func (t NativeType) New() interface{} { 2472 val, err := t.NewWithError() 2473 if err != nil { 2474 panic(err.Error()) 2475 } 2476 return val 2477 } 2478 2479 func (s NativeType) Type() Type { 2480 return s.typ 2481 } 2482 2483 func (s NativeType) Version() byte { 2484 return s.proto 2485 } 2486 2487 func (s NativeType) Custom() string { 2488 return s.custom 2489 } 2490 2491 func (s NativeType) String() string { 2492 switch s.typ { 2493 case TypeCustom: 2494 return fmt.Sprintf("%s(%s)", s.typ, s.custom) 2495 default: 2496 return s.typ.String() 2497 } 2498 } 2499 2500 type CollectionType struct { 2501 NativeType 2502 Key TypeInfo // only used for TypeMap 2503 Elem TypeInfo // only used for TypeMap, TypeList and TypeSet 2504 } 2505 2506 func (t CollectionType) NewWithError() (interface{}, error) { 2507 typ, err := goType(t) 2508 if err != nil { 2509 return nil, err 2510 } 2511 return reflect.New(typ).Interface(), nil 2512 } 2513 2514 func (t CollectionType) New() interface{} { 2515 val, err := t.NewWithError() 2516 if err != nil { 2517 panic(err.Error()) 2518 } 2519 return val 2520 } 2521 2522 func (c CollectionType) String() string { 2523 switch c.typ { 2524 case TypeMap: 2525 return fmt.Sprintf("%s(%s, %s)", c.typ, c.Key, c.Elem) 2526 case TypeList, TypeSet: 2527 return fmt.Sprintf("%s(%s)", c.typ, c.Elem) 2528 case TypeCustom: 2529 return fmt.Sprintf("%s(%s)", c.typ, c.custom) 2530 default: 2531 return c.typ.String() 2532 } 2533 } 2534 2535 type TupleTypeInfo struct { 2536 NativeType 2537 Elems []TypeInfo 2538 } 2539 2540 func (t TupleTypeInfo) String() string { 2541 var buf bytes.Buffer 2542 buf.WriteString(fmt.Sprintf("%s(", t.typ)) 2543 for _, elem := range t.Elems { 2544 buf.WriteString(fmt.Sprintf("%s, ", elem)) 2545 } 2546 buf.Truncate(buf.Len() - 2) 2547 buf.WriteByte(')') 2548 return buf.String() 2549 } 2550 2551 func (t TupleTypeInfo) NewWithError() (interface{}, error) { 2552 typ, err := goType(t) 2553 if err != nil { 2554 return nil, err 2555 } 2556 return reflect.New(typ).Interface(), nil 2557 } 2558 2559 func (t TupleTypeInfo) New() interface{} { 2560 val, err := t.NewWithError() 2561 if err != nil { 2562 panic(err.Error()) 2563 } 2564 return val 2565 } 2566 2567 type UDTField struct { 2568 Name string 2569 Type TypeInfo 2570 } 2571 2572 type UDTTypeInfo struct { 2573 NativeType 2574 KeySpace string 2575 Name string 2576 Elements []UDTField 2577 } 2578 2579 func (u UDTTypeInfo) NewWithError() (interface{}, error) { 2580 typ, err := goType(u) 2581 if err != nil { 2582 return nil, err 2583 } 2584 return reflect.New(typ).Interface(), nil 2585 } 2586 2587 func (u UDTTypeInfo) New() interface{} { 2588 val, err := u.NewWithError() 2589 if err != nil { 2590 panic(err.Error()) 2591 } 2592 return val 2593 } 2594 2595 func (u UDTTypeInfo) String() string { 2596 buf := &bytes.Buffer{} 2597 2598 fmt.Fprintf(buf, "%s.%s{", u.KeySpace, u.Name) 2599 first := true 2600 for _, e := range u.Elements { 2601 if !first { 2602 fmt.Fprint(buf, ",") 2603 } else { 2604 first = false 2605 } 2606 2607 fmt.Fprintf(buf, "%s=%v", e.Name, e.Type) 2608 } 2609 fmt.Fprint(buf, "}") 2610 2611 return buf.String() 2612 } 2613 2614 // String returns a human readable name for the Cassandra datatype 2615 // described by t. 2616 // Type is the identifier of a Cassandra internal datatype. 2617 type Type int 2618 2619 const ( 2620 TypeCustom Type = 0x0000 2621 TypeAscii Type = 0x0001 2622 TypeBigInt Type = 0x0002 2623 TypeBlob Type = 0x0003 2624 TypeBoolean Type = 0x0004 2625 TypeCounter Type = 0x0005 2626 TypeDecimal Type = 0x0006 2627 TypeDouble Type = 0x0007 2628 TypeFloat Type = 0x0008 2629 TypeInt Type = 0x0009 2630 TypeText Type = 0x000A 2631 TypeTimestamp Type = 0x000B 2632 TypeUUID Type = 0x000C 2633 TypeVarchar Type = 0x000D 2634 TypeVarint Type = 0x000E 2635 TypeTimeUUID Type = 0x000F 2636 TypeInet Type = 0x0010 2637 TypeDate Type = 0x0011 2638 TypeTime Type = 0x0012 2639 TypeSmallInt Type = 0x0013 2640 TypeTinyInt Type = 0x0014 2641 TypeDuration Type = 0x0015 2642 TypeList Type = 0x0020 2643 TypeMap Type = 0x0021 2644 TypeSet Type = 0x0022 2645 TypeUDT Type = 0x0030 2646 TypeTuple Type = 0x0031 2647 ) 2648 2649 // String returns the name of the identifier. 2650 func (t Type) String() string { 2651 switch t { 2652 case TypeCustom: 2653 return "custom" 2654 case TypeAscii: 2655 return "ascii" 2656 case TypeBigInt: 2657 return "bigint" 2658 case TypeBlob: 2659 return "blob" 2660 case TypeBoolean: 2661 return "boolean" 2662 case TypeCounter: 2663 return "counter" 2664 case TypeDecimal: 2665 return "decimal" 2666 case TypeDouble: 2667 return "double" 2668 case TypeFloat: 2669 return "float" 2670 case TypeInt: 2671 return "int" 2672 case TypeText: 2673 return "text" 2674 case TypeTimestamp: 2675 return "timestamp" 2676 case TypeUUID: 2677 return "uuid" 2678 case TypeVarchar: 2679 return "varchar" 2680 case TypeTimeUUID: 2681 return "timeuuid" 2682 case TypeInet: 2683 return "inet" 2684 case TypeDate: 2685 return "date" 2686 case TypeDuration: 2687 return "duration" 2688 case TypeTime: 2689 return "time" 2690 case TypeSmallInt: 2691 return "smallint" 2692 case TypeTinyInt: 2693 return "tinyint" 2694 case TypeList: 2695 return "list" 2696 case TypeMap: 2697 return "map" 2698 case TypeSet: 2699 return "set" 2700 case TypeVarint: 2701 return "varint" 2702 case TypeTuple: 2703 return "tuple" 2704 default: 2705 return fmt.Sprintf("unknown_type_%d", t) 2706 } 2707 } 2708 2709 type MarshalError string 2710 2711 func (m MarshalError) Error() string { 2712 return string(m) 2713 } 2714 2715 func marshalErrorf(format string, args ...interface{}) MarshalError { 2716 return MarshalError(fmt.Sprintf(format, args...)) 2717 } 2718 2719 type UnmarshalError string 2720 2721 func (m UnmarshalError) Error() string { 2722 return string(m) 2723 } 2724 2725 func unmarshalErrorf(format string, args ...interface{}) UnmarshalError { 2726 return UnmarshalError(fmt.Sprintf(format, args...)) 2727 }