Skip to content

Commit

Permalink
Remove wrappErrorUnpack
Browse files Browse the repository at this point in the history
  • Loading branch information
meparle committed Nov 21, 2023
1 parent 3ec9706 commit 4b92ec4
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,27 +230,13 @@ func (m *Message) Unpack(src []byte) error {
m.mu.Lock()
defer m.mu.Unlock()

return m.wrappErrorUnpack(src)
}

// wrappErrorUnpack calls the core unpacking logic and wraps any
// errors in a *UnpackError. It assumes that the mutex is already
// locked by the caller.
func (m *Message) wrappErrorUnpack(src []byte) error {
if field, err := m.unpack(src); err != nil {
return &UnpackError{
Err: err,
Field: field,
RawMessage: src,
}
}
return nil
return m.unpack(src)
}

// unpack contains the core logic for unpacking the message. This method does
// not handle locking or error wrapping and should typically be used internally
// after ensuring concurrency safety.
func (m *Message) unpack(src []byte) (string, error) {
func (m *Message) unpack(src []byte) error {
var off int

// reset fields that were set
Expand All @@ -261,7 +247,11 @@ func (m *Message) unpack(src []byte) (string, error) {

read, err := m.fields[mtiIdx].Unpack(src)
if err != nil {
return "", fmt.Errorf("failed to unpack MTI: %w", err)
return &UnpackError{
Err: fmt.Errorf("failed to unpack MTI: %w", err),
Field: strconv.Itoa(mtiIdx),
RawMessage: src,
}
}

m.fieldsMap[mtiIdx] = struct{}{}
Expand All @@ -271,7 +261,11 @@ func (m *Message) unpack(src []byte) (string, error) {
// unpack Bitmap
read, err = m.fields[bitmapIdx].Unpack(src[off:])
if err != nil {
return "", fmt.Errorf("failed to unpack bitmap: %w", err)
return &UnpackError{
Err: fmt.Errorf("failed to unpack bitmap: %w", err),
Field: strconv.Itoa(bitmapIdx),
RawMessage: src,
}
}

off += read
Expand All @@ -285,12 +279,20 @@ func (m *Message) unpack(src []byte) (string, error) {
if m.bitmap().IsSet(i) {
fl, ok := m.fields[i]
if !ok {
return strconv.Itoa(i), fmt.Errorf("failed to unpack field %d: no specification found", i)
return &UnpackError{
Err: fmt.Errorf("failed to unpack field %d: no specification found", i),
Field: strconv.Itoa(i),
RawMessage: src,
}
}

read, err = fl.Unpack(src[off:])
if err != nil {
return strconv.Itoa(i), fmt.Errorf("failed to unpack field %d (%s): %w", i, fl.Spec().Description, err)
return &UnpackError{
Err: fmt.Errorf("failed to unpack field %d (%s): %w", i, fl.Spec().Description, err),
Field: strconv.Itoa(i),
RawMessage: src,
}
}

m.fieldsMap[i] = struct{}{}
Expand All @@ -299,7 +301,7 @@ func (m *Message) unpack(src []byte) (string, error) {
}
}

return "", nil
return nil
}

func (m *Message) MarshalJSON() ([]byte, error) {
Expand Down

0 comments on commit 4b92ec4

Please sign in to comment.