Skip to content

Commit

Permalink
add message parser
Browse files Browse the repository at this point in the history
  • Loading branch information
janishar committed Jul 1, 2024
1 parent 973e598 commit 754b336
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions arch/micro/message.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package micro

import (
"encoding/json"
"errors"
)

type Message[T any] struct {
Data T `json:"data,omitempty"`
Error *string `json:"error,omitempty"`
Expand Down Expand Up @@ -32,3 +37,17 @@ func NewMessage[T any](data T, err error) *Message[T] {
Error: e,
}
}

func ParseMsg[T any](data []byte) (*T, error) {
var msg Message[*T]
err := json.Unmarshal(data, &msg)
if err != nil {
return nil, err
}

if msg.Error != nil {
err = errors.New(*msg.Error)
}

return msg.Data, err
}

0 comments on commit 754b336

Please sign in to comment.