diff --git a/msgpack_reader.go b/msgpack_reader.go index 0461c87..74239d0 100644 --- a/msgpack_reader.go +++ b/msgpack_reader.go @@ -9,6 +9,10 @@ import ( "github.com/vmihailenco/msgpack/v5/msgpcode" ) +var FieldNotFound = errors.New("field not found") + +var NoOpenContainer = errors.New("there is no open container") + type msgpackReader struct { dec *msgpack.Decoder stack []readContainer @@ -69,7 +73,7 @@ func (rd *msgpackReader) ReadInt8(v *int8, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadInt16(v *int16, targetField string) error { @@ -99,7 +103,7 @@ func (rd *msgpackReader) ReadInt16(v *int16, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadInt32(v *int32, targetField string) error { @@ -129,7 +133,7 @@ func (rd *msgpackReader) ReadInt32(v *int32, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadInt64(v *int64, targetField string) error { @@ -159,7 +163,7 @@ func (rd *msgpackReader) ReadInt64(v *int64, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadUint8(v *uint8, targetField string) error { @@ -189,7 +193,7 @@ func (rd *msgpackReader) ReadUint8(v *uint8, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadUint16(v *uint16, targetField string) error { @@ -219,7 +223,7 @@ func (rd *msgpackReader) ReadUint16(v *uint16, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadUint32(v *uint32, targetField string) error { @@ -249,7 +253,7 @@ func (rd *msgpackReader) ReadUint32(v *uint32, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadUint64(v *uint64, targetField string) error { @@ -279,7 +283,7 @@ func (rd *msgpackReader) ReadUint64(v *uint64, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadBool(v *bool, targetField string) error { @@ -309,7 +313,7 @@ func (rd *msgpackReader) ReadBool(v *bool, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadFloat32(v *float32, targetField string) error { @@ -339,7 +343,7 @@ func (rd *msgpackReader) ReadFloat32(v *float32, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadFloat64(v *float64, targetField string) error { @@ -369,7 +373,7 @@ func (rd *msgpackReader) ReadFloat64(v *float64, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadString(v *string, targetField string) error { @@ -399,7 +403,7 @@ func (rd *msgpackReader) ReadString(v *string, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) ReadBytes(v *[]byte, targetField string) error { @@ -429,7 +433,7 @@ func (rd *msgpackReader) ReadBytes(v *[]byte, targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) BeginContainer(targetField string) error { @@ -459,7 +463,7 @@ func (rd *msgpackReader) BeginContainer(targetField string) error { rd.curr.values[field] = raw } - return errors.Errorf("field `%s` not found", targetField) + return FieldNotFound } func (rd *msgpackReader) beginContainer(field string) error { @@ -504,7 +508,7 @@ func (rd *msgpackReader) beginContainer(field string) error { func (rd *msgpackReader) EndContainer() error { if len(rd.stack) == 0 { - return errors.New("there is no open containers") + return NoOpenContainer } rd.curr = rd.stack[len(rd.stack)-1] rd.stack = rd.stack[:len(rd.stack)-1]