introduce FieldNotFound and NoOpenContainer errors

This commit is contained in:
Pavel Merzlyakov 2023-07-26 23:03:37 +03:00
parent fe1e485334
commit 04e68cc197
1 changed files with 19 additions and 15 deletions

View File

@ -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]