Skip to content

Commit 74254c8

Browse files
committed
marshal error messages were enhanced
1 parent 974fa12 commit 74254c8

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

marshal.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ type Unmarshaler interface {
110110
// duration | time.Duration |
111111
// duration | gocql.Duration |
112112
// duration | string | parsed with time.ParseDuration
113+
//
114+
// The marshal/unmarshal error provides a list of supported types when an unsupported type is attempted.
113115
func Marshal(info TypeInfo, value interface{}) ([]byte, error) {
114116
if info.Version() < protoVersion1 {
115117
panic("protocol version not set")
@@ -333,7 +335,7 @@ func marshalVarchar(info TypeInfo, value interface{}) ([]byte, error) {
333335
case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
334336
return rv.Bytes(), nil
335337
}
336-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
338+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, string, []byte, UnsetValue.", value, info)
337339
}
338340

339341
func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error {
@@ -372,7 +374,7 @@ func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error {
372374
rv.SetBytes(dataCopy)
373375
return nil
374376
}
375-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
377+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *string, *[]byte", info, value)
376378
}
377379

378380
func marshalSmallInt(info TypeInfo, value interface{}) ([]byte, error) {
@@ -450,7 +452,7 @@ func marshalSmallInt(info TypeInfo, value interface{}) ([]byte, error) {
450452
}
451453
}
452454

453-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
455+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, int16, uint16, int8, uint8, int, uint, int32, uint32, int64, uint64, string, UnsetValue.", value, info)
454456
}
455457

456458
func marshalTinyInt(info TypeInfo, value interface{}) ([]byte, error) {
@@ -534,7 +536,7 @@ func marshalTinyInt(info TypeInfo, value interface{}) ([]byte, error) {
534536
}
535537
}
536538

537-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
539+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string, UnsetValue.", value, info)
538540
}
539541

540542
func marshalInt(info TypeInfo, value interface{}) ([]byte, error) {
@@ -606,7 +608,7 @@ func marshalInt(info TypeInfo, value interface{}) ([]byte, error) {
606608
}
607609
}
608610

609-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
611+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string, UnsetValue.", value, info)
610612
}
611613

612614
func encInt(x int32) []byte {
@@ -696,7 +698,7 @@ func marshalBigInt(info TypeInfo, value interface{}) ([]byte, error) {
696698
}
697699
return encBigInt(int64(v)), nil
698700
}
699-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
701+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: big.Int, Marshaler, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string, UnsetValue.", value, info)
700702
}
701703

702704
func encBigInt(x int64) []byte {
@@ -1006,7 +1008,7 @@ func unmarshalIntlike(info TypeInfo, int64Val int64, data []byte, value interfac
10061008
rv.SetUint(uint64(int64Val) & 0xff)
10071009
return nil
10081010
}
1009-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1011+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: big.Int, Marshaler, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string.", info, value)
10101012
}
10111013

10121014
func decBigInt(data []byte) int64 {
@@ -1038,7 +1040,7 @@ func marshalBool(info TypeInfo, value interface{}) ([]byte, error) {
10381040
case reflect.Bool:
10391041
return encBool(rv.Bool()), nil
10401042
}
1041-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1043+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, bool, UnsetValue.", value, info)
10421044
}
10431045

10441046
func encBool(v bool) []byte {
@@ -1066,7 +1068,7 @@ func unmarshalBool(info TypeInfo, data []byte, value interface{}) error {
10661068
rv.SetBool(decBool(data))
10671069
return nil
10681070
}
1069-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1071+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *bool.", info, value)
10701072
}
10711073

10721074
func decBool(v []byte) bool {
@@ -1095,7 +1097,7 @@ func marshalFloat(info TypeInfo, value interface{}) ([]byte, error) {
10951097
case reflect.Float32:
10961098
return encInt(int32(math.Float32bits(float32(rv.Float())))), nil
10971099
}
1098-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1100+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, float32, UnsetValue.", value, info)
10991101
}
11001102

11011103
func unmarshalFloat(info TypeInfo, data []byte, value interface{}) error {
@@ -1116,7 +1118,7 @@ func unmarshalFloat(info TypeInfo, data []byte, value interface{}) error {
11161118
rv.SetFloat(float64(math.Float32frombits(uint32(decInt(data)))))
11171119
return nil
11181120
}
1119-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1121+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *float32, UnsetValue.", info, value)
11201122
}
11211123

11221124
func marshalDouble(info TypeInfo, value interface{}) ([]byte, error) {
@@ -1136,7 +1138,7 @@ func marshalDouble(info TypeInfo, value interface{}) ([]byte, error) {
11361138
case reflect.Float64:
11371139
return encBigInt(int64(math.Float64bits(rv.Float()))), nil
11381140
}
1139-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1141+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, float64, UnsetValue.", value, info)
11401142
}
11411143

11421144
func unmarshalDouble(info TypeInfo, data []byte, value interface{}) error {
@@ -1157,7 +1159,7 @@ func unmarshalDouble(info TypeInfo, data []byte, value interface{}) error {
11571159
rv.SetFloat(math.Float64frombits(uint64(decBigInt(data))))
11581160
return nil
11591161
}
1160-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1162+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *float64.", info, value)
11611163
}
11621164

11631165
func marshalDecimal(info TypeInfo, value interface{}) ([]byte, error) {
@@ -1181,7 +1183,7 @@ func marshalDecimal(info TypeInfo, value interface{}) ([]byte, error) {
11811183
copy(buf[4:], unscaled)
11821184
return buf, nil
11831185
}
1184-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1186+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, inf.Dec, UnsetValue.", value, info)
11851187
}
11861188

11871189
func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error {
@@ -1197,7 +1199,7 @@ func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error {
11971199
*v = *inf.NewDecBig(unscaled, inf.Scale(scale))
11981200
return nil
11991201
}
1200-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1202+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *inf.Dec.", info, value)
12011203
}
12021204

12031205
// decBigInt2C sets the value of n to the big-endian two's complement
@@ -1261,7 +1263,7 @@ func marshalTime(info TypeInfo, value interface{}) ([]byte, error) {
12611263
case reflect.Int64:
12621264
return encBigInt(rv.Int()), nil
12631265
}
1264-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1266+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, int64, time.Duration, UnsetValue.", value, info)
12651267
}
12661268

12671269
func marshalTimestamp(info TypeInfo, value interface{}) ([]byte, error) {
@@ -1289,7 +1291,7 @@ func marshalTimestamp(info TypeInfo, value interface{}) ([]byte, error) {
12891291
case reflect.Int64:
12901292
return encBigInt(rv.Int()), nil
12911293
}
1292-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1294+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, int64, time.Time, UnsetValue.", value, info)
12931295
}
12941296

12951297
func unmarshalTime(info TypeInfo, data []byte, value interface{}) error {
@@ -1314,7 +1316,7 @@ func unmarshalTime(info TypeInfo, data []byte, value interface{}) error {
13141316
rv.SetInt(decBigInt(data))
13151317
return nil
13161318
}
1317-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1319+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *int64, *time.Duration.", info, value)
13181320
}
13191321

13201322
func unmarshalTimestamp(info TypeInfo, data []byte, value interface{}) error {
@@ -1346,7 +1348,7 @@ func unmarshalTimestamp(info TypeInfo, data []byte, value interface{}) error {
13461348
rv.SetInt(decBigInt(data))
13471349
return nil
13481350
}
1349-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1351+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *int64, *time.Time.", info, value)
13501352
}
13511353

13521354
const millisecondsInADay int64 = 24 * 60 * 60 * 1000
@@ -1392,7 +1394,7 @@ func marshalDate(info TypeInfo, value interface{}) ([]byte, error) {
13921394
if value == nil {
13931395
return nil, nil
13941396
}
1395-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1397+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, int64, time.Time, *time.Time, string, UnsetValue.", value, info)
13961398
}
13971399

13981400
func unmarshalDate(info TypeInfo, data []byte, value interface{}) error {
@@ -1420,7 +1422,7 @@ func unmarshalDate(info TypeInfo, data []byte, value interface{}) error {
14201422
*v = time.UnixMilli(timestamp).In(time.UTC).Format("2006-01-02")
14211423
return nil
14221424
}
1423-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1425+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *time.Time, *string.", info, value)
14241426
}
14251427

14261428
func marshalDuration(info TypeInfo, value interface{}) ([]byte, error) {
@@ -1452,7 +1454,7 @@ func marshalDuration(info TypeInfo, value interface{}) ([]byte, error) {
14521454
case reflect.Int64:
14531455
return encBigInt(rv.Int()), nil
14541456
}
1455-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1457+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, int64, time.Duration, string, Duration, UnsetValue.", value, info)
14561458
}
14571459

14581460
func unmarshalDuration(info TypeInfo, data []byte, value interface{}) error {
@@ -1479,7 +1481,7 @@ func unmarshalDuration(info TypeInfo, data []byte, value interface{}) error {
14791481
}
14801482
return nil
14811483
}
1482-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1484+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *Duration.", info, value)
14831485
}
14841486

14851487
func decVints(data []byte) (int32, int32, int64, error) {
@@ -1627,7 +1629,7 @@ func marshalList(info TypeInfo, value interface{}) ([]byte, error) {
16271629
return marshalList(listInfo, keys)
16281630
}
16291631
}
1630-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1632+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: slice, array, map[]struct.", value, info)
16311633
}
16321634

16331635
func readCollectionSize(info CollectionType, data []byte) (size, read int, err error) {
@@ -1706,7 +1708,7 @@ func unmarshalList(info TypeInfo, data []byte, value interface{}) error {
17061708
}
17071709
return nil
17081710
}
1709-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
1711+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: *slice, *array.", info, value)
17101712
}
17111713

17121714
func marshalMap(info TypeInfo, value interface{}) ([]byte, error) {
@@ -1870,7 +1872,7 @@ func marshalUUID(info TypeInfo, value interface{}) ([]byte, error) {
18701872
return nil, nil
18711873
}
18721874

1873-
return nil, marshalErrorf("can not marshal %T into %s", value, info)
1875+
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: UUID, [16]byte, string, UnsetValue.", value, info)
18741876
}
18751877

18761878
func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error {
@@ -1883,7 +1885,7 @@ func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error {
18831885
case *UUID:
18841886
*v = UUID{}
18851887
default:
1886-
return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
1888+
return unmarshalErrorf("can not unmarshal X %s into %T. Accepted types: *UUID, *[]byte, *string.", info, value)
18871889
}
18881890

18891891
return nil
@@ -1915,7 +1917,7 @@ func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error {
19151917
*v = u[:]
19161918
return nil
19171919
}
1918-
return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
1920+
return unmarshalErrorf("can not unmarshal X %s into %T. Accepted types: *UUID, *[]byte, *string.", info, value)
19191921
}
19201922

19211923
func unmarshalTimeUUID(info TypeInfo, data []byte, value interface{}) error {
@@ -1965,7 +1967,7 @@ func marshalInet(info TypeInfo, value interface{}) ([]byte, error) {
19651967
return nil, nil
19661968
}
19671969

1968-
return nil, marshalErrorf("cannot marshal %T into %s", value, info)
1970+
return nil, marshalErrorf("cannot marshal %T into %s. Accepted types: net.IP, string.", value, info)
19691971
}
19701972

19711973
func unmarshalInet(info TypeInfo, data []byte, value interface{}) error {
@@ -1997,7 +1999,7 @@ func unmarshalInet(info TypeInfo, data []byte, value interface{}) error {
19971999
*v = ip.String()
19982000
return nil
19992001
}
2000-
return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
2002+
return unmarshalErrorf("cannot unmarshal %s into %T. Accepted types: Unmarshaler, *net.IP, *string.", info, value)
20012003
}
20022004

20032005
func marshalTuple(info TypeInfo, value interface{}) ([]byte, error) {
@@ -2088,7 +2090,7 @@ func marshalTuple(info TypeInfo, value interface{}) ([]byte, error) {
20882090
return buf, nil
20892091
}
20902092

2091-
return nil, marshalErrorf("cannot marshal %T into %s", value, tuple)
2093+
return nil, marshalErrorf("cannot marshal %T into %s. Accepted types: struct, []interface{}, array, slice, UnsetValue.", value, tuple)
20922094
}
20932095

20942096
func readBytes(p []byte) ([]byte, []byte) {
@@ -2208,7 +2210,7 @@ func unmarshalTuple(info TypeInfo, data []byte, value interface{}) error {
22082210
return nil
22092211
}
22102212

2211-
return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
2213+
return unmarshalErrorf("cannot unmarshal %s into %T. Accepted types: *struct, []interface{}, *array, *slice, Unmarshaler.", info, value)
22122214
}
22132215

22142216
// UDTMarshaler is an interface which should be implemented by users wishing to
@@ -2280,7 +2282,7 @@ func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
22802282
}
22812283

22822284
if k.Kind() != reflect.Struct || !k.IsValid() {
2283-
return nil, marshalErrorf("cannot marshal %T into %s", value, info)
2285+
return nil, marshalErrorf("cannot marshal %T into %s. Accepted types: Marshaler, UDTMarshaler, map[string]interface{}, struct, UnsetValue.", value, info)
22842286
}
22852287

22862288
fields := make(map[string]reflect.Value)
@@ -2349,7 +2351,7 @@ func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
23492351
rv = rv.Elem()
23502352
t := rv.Type()
23512353
if t.Kind() != reflect.Map {
2352-
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
2354+
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, UDTUnmarshaler, *map[string]interface{}, struct.", info, value)
23532355
} else if data == nil {
23542356
rv.Set(reflect.Zero(t))
23552357
return nil
@@ -2392,7 +2394,7 @@ func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
23922394
}
23932395
k := rv.Elem()
23942396
if k.Kind() != reflect.Struct || !k.IsValid() {
2395-
return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
2397+
return unmarshalErrorf("cannot unmarshal %s into %T. Accepted types: Unmarshaler, UDTUnmarshaler, *map[string]interface{}, *struct.", info, value)
23962398
}
23972399

23982400
if len(data) == 0 {

0 commit comments

Comments
 (0)