From c3dcd986a9c27fcfd225a344fa39a8d1d03def44 Mon Sep 17 00:00:00 2001 From: tengu-alt Date: Wed, 30 Oct 2024 14:02:02 +0200 Subject: [PATCH] marshal error messages were enhanced --- marshal.go | 70 +++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/marshal.go b/marshal.go index 4d0adb923..2d5a65a9a 100644 --- a/marshal.go +++ b/marshal.go @@ -333,7 +333,7 @@ func marshalVarchar(info TypeInfo, value interface{}) ([]byte, error) { case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8: return rv.Bytes(), nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, string, []byte, unsetColumn.", value, info) } func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error { @@ -372,7 +372,7 @@ func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error { rv.SetBytes(dataCopy) return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *string, *[]byte", info, value) } func marshalSmallInt(info TypeInfo, value interface{}) ([]byte, error) { @@ -450,7 +450,7 @@ func marshalSmallInt(info TypeInfo, value interface{}) ([]byte, error) { } } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, int16, uint16, int8, uint8, int, uint, int32, uint32, int64, uint64, string.", value, info) } func marshalTinyInt(info TypeInfo, value interface{}) ([]byte, error) { @@ -534,7 +534,7 @@ func marshalTinyInt(info TypeInfo, value interface{}) ([]byte, error) { } } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string.", value, info) } func marshalInt(info TypeInfo, value interface{}) ([]byte, error) { @@ -606,7 +606,7 @@ func marshalInt(info TypeInfo, value interface{}) ([]byte, error) { } } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string.", value, info) } func encInt(x int32) []byte { @@ -696,7 +696,7 @@ func marshalBigInt(info TypeInfo, value interface{}) ([]byte, error) { } return encBigInt(int64(v)), nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: big.Int, Marshaler, unsetColumn, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string.", value, info) } func encBigInt(x int64) []byte { @@ -1006,7 +1006,7 @@ func unmarshalIntlike(info TypeInfo, int64Val int64, data []byte, value interfac rv.SetUint(uint64(int64Val) & 0xff) return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: big.Int, Marshaler, unsetColumn, int8, uint8, int16, uint16, int, uint, int32, uint32, int64, uint64, string.", info, value) } func decBigInt(data []byte) int64 { @@ -1038,7 +1038,7 @@ func marshalBool(info TypeInfo, value interface{}) ([]byte, error) { case reflect.Bool: return encBool(rv.Bool()), nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, bool.", value, info) } func encBool(v bool) []byte { @@ -1066,7 +1066,7 @@ func unmarshalBool(info TypeInfo, data []byte, value interface{}) error { rv.SetBool(decBool(data)) return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *bool.", info, value) } func decBool(v []byte) bool { @@ -1095,7 +1095,7 @@ func marshalFloat(info TypeInfo, value interface{}) ([]byte, error) { case reflect.Float32: return encInt(int32(math.Float32bits(float32(rv.Float())))), nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, float32.", value, info) } func unmarshalFloat(info TypeInfo, data []byte, value interface{}) error { @@ -1116,7 +1116,7 @@ func unmarshalFloat(info TypeInfo, data []byte, value interface{}) error { rv.SetFloat(float64(math.Float32frombits(uint32(decInt(data))))) return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *float32.", info, value) } func marshalDouble(info TypeInfo, value interface{}) ([]byte, error) { @@ -1136,7 +1136,7 @@ func marshalDouble(info TypeInfo, value interface{}) ([]byte, error) { case reflect.Float64: return encBigInt(int64(math.Float64bits(rv.Float()))), nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, float64.", value, info) } func unmarshalDouble(info TypeInfo, data []byte, value interface{}) error { @@ -1157,7 +1157,7 @@ func unmarshalDouble(info TypeInfo, data []byte, value interface{}) error { rv.SetFloat(math.Float64frombits(uint64(decBigInt(data)))) return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *float64.", info, value) } func marshalDecimal(info TypeInfo, value interface{}) ([]byte, error) { @@ -1181,7 +1181,7 @@ func marshalDecimal(info TypeInfo, value interface{}) ([]byte, error) { copy(buf[4:], unscaled) return buf, nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, inf.Dec.", value, info) } func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error { @@ -1197,7 +1197,7 @@ func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error { *v = *inf.NewDecBig(unscaled, inf.Scale(scale)) return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *inf.Dec.", info, value) } // decBigInt2C sets the value of n to the big-endian two's complement @@ -1261,7 +1261,7 @@ func marshalTime(info TypeInfo, value interface{}) ([]byte, error) { case reflect.Int64: return encBigInt(rv.Int()), nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, int64, time.Duration.", value, info) } func marshalTimestamp(info TypeInfo, value interface{}) ([]byte, error) { @@ -1289,7 +1289,7 @@ func marshalTimestamp(info TypeInfo, value interface{}) ([]byte, error) { case reflect.Int64: return encBigInt(rv.Int()), nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, int64, time.Time.", value, info) } func unmarshalTime(info TypeInfo, data []byte, value interface{}) error { @@ -1314,7 +1314,7 @@ func unmarshalTime(info TypeInfo, data []byte, value interface{}) error { rv.SetInt(decBigInt(data)) return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *int64, *time.Duration.", info, value) } func unmarshalTimestamp(info TypeInfo, data []byte, value interface{}) error { @@ -1346,7 +1346,7 @@ func unmarshalTimestamp(info TypeInfo, data []byte, value interface{}) error { rv.SetInt(decBigInt(data)) return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *int64, *time.Time.", info, value) } const millisecondsInADay int64 = 24 * 60 * 60 * 1000 @@ -1392,7 +1392,7 @@ func marshalDate(info TypeInfo, value interface{}) ([]byte, error) { if value == nil { return nil, nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, int64, time.Time, *time.Time, string.", value, info) } func unmarshalDate(info TypeInfo, data []byte, value interface{}) error { @@ -1420,7 +1420,7 @@ func unmarshalDate(info TypeInfo, data []byte, value interface{}) error { *v = time.UnixMilli(timestamp).In(time.UTC).Format("2006-01-02") return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *time.Time, *string.", info, value) } func marshalDuration(info TypeInfo, value interface{}) ([]byte, error) { @@ -1452,7 +1452,7 @@ func marshalDuration(info TypeInfo, value interface{}) ([]byte, error) { case reflect.Int64: return encBigInt(rv.Int()), nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, unsetColumn, int64, time.Duration, string, Duration.", value, info) } func unmarshalDuration(info TypeInfo, data []byte, value interface{}) error { @@ -1479,7 +1479,7 @@ func unmarshalDuration(info TypeInfo, data []byte, value interface{}) error { } return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, *Duration.", info, value) } func decVints(data []byte) (int32, int32, int64, error) { @@ -1627,7 +1627,7 @@ func marshalList(info TypeInfo, value interface{}) ([]byte, error) { return marshalList(listInfo, keys) } } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: slice, array, map[]struct.", value, info) } func readCollectionSize(info CollectionType, data []byte) (size, read int, err error) { @@ -1706,7 +1706,7 @@ func unmarshalList(info TypeInfo, data []byte, value interface{}) error { } return nil } - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: slice, array.", info, value) } func marshalMap(info TypeInfo, value interface{}) ([]byte, error) { @@ -1870,7 +1870,7 @@ func marshalUUID(info TypeInfo, value interface{}) ([]byte, error) { return nil, nil } - return nil, marshalErrorf("can not marshal %T into %s", value, info) + return nil, marshalErrorf("can not marshal %T into %s. Accepted types: unsetColumn, UUID, [16]byte, string.", value, info) } func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error { @@ -1883,7 +1883,7 @@ func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error { case *UUID: *v = UUID{} default: - return unmarshalErrorf("can not unmarshal X %s into %T", info, value) + return unmarshalErrorf("can not unmarshal X %s into %T. Accepted types: *UUID, *[]byte, *string.", info, value) } return nil @@ -1915,7 +1915,7 @@ func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error { *v = u[:] return nil } - return unmarshalErrorf("can not unmarshal X %s into %T", info, value) + return unmarshalErrorf("can not unmarshal X %s into %T. Accepted types: *UUID, *[]byte, *string.", info, value) } func unmarshalTimeUUID(info TypeInfo, data []byte, value interface{}) error { @@ -1965,7 +1965,7 @@ func marshalInet(info TypeInfo, value interface{}) ([]byte, error) { return nil, nil } - return nil, marshalErrorf("cannot marshal %T into %s", value, info) + return nil, marshalErrorf("cannot marshal %T into %s. Accepted types: unsetColumn, net.IP, string.", value, info) } func unmarshalInet(info TypeInfo, data []byte, value interface{}) error { @@ -1997,7 +1997,7 @@ func unmarshalInet(info TypeInfo, data []byte, value interface{}) error { *v = ip.String() return nil } - return unmarshalErrorf("cannot unmarshal %s into %T", info, value) + return unmarshalErrorf("cannot unmarshal %s into %T. Accepted types: Unmarshaler, *net.IP, *string.", info, value) } func marshalTuple(info TypeInfo, value interface{}) ([]byte, error) { @@ -2088,7 +2088,7 @@ func marshalTuple(info TypeInfo, value interface{}) ([]byte, error) { return buf, nil } - return nil, marshalErrorf("cannot marshal %T into %s", value, tuple) + return nil, marshalErrorf("cannot marshal %T into %s. Accepted types: struct, unsetColumn, []interface{}, array, slice.", value, tuple) } func readBytes(p []byte) ([]byte, []byte) { @@ -2208,7 +2208,7 @@ func unmarshalTuple(info TypeInfo, data []byte, value interface{}) error { return nil } - return unmarshalErrorf("cannot unmarshal %s into %T", info, value) + return unmarshalErrorf("cannot unmarshal %s into %T. Accepted types: struct, []interface{}, array, slice.", info, value) } // UDTMarshaler is an interface which should be implemented by users wishing to @@ -2280,7 +2280,7 @@ func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) { } if k.Kind() != reflect.Struct || !k.IsValid() { - return nil, marshalErrorf("cannot marshal %T into %s", value, info) + return nil, marshalErrorf("cannot marshal %T into %s. Accepted types: Marshaler, unsetColumn, UDTMarshaler, UDTMarshaler, map[string]interface{}, struct.", value, info) } fields := make(map[string]reflect.Value) @@ -2349,7 +2349,7 @@ func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error { rv = rv.Elem() t := rv.Type() if t.Kind() != reflect.Map { - return unmarshalErrorf("can not unmarshal %s into %T", info, value) + return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: Unmarshaler, UDTUnmarshaler, *map[string]interface{}, struct.", info, value) } else if data == nil { rv.Set(reflect.Zero(t)) return nil @@ -2392,7 +2392,7 @@ func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error { } k := rv.Elem() if k.Kind() != reflect.Struct || !k.IsValid() { - return unmarshalErrorf("cannot unmarshal %s into %T", info, value) + return unmarshalErrorf("cannot unmarshal %s into %T. Accepted types: Unmarshaler, UDTUnmarshaler, *map[string]interface{}, struct.", info, value) } if len(data) == 0 {