/
data.go
97 lines (83 loc) · 3.18 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package payload
import (
"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/cerrors"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/stringify"
)
// GenericDataPayloadType is the Type of a generic GenericDataPayload.
var GenericDataPayloadType = NewType(0, "GenericDataPayloadType", GenericDataPayloadUnmarshaler)
// GenericDataPayloadUnmarshaler is the UnmarshalerFunc of the GenericDataPayload which is also used as a unmarshaler for unknown Types.
func GenericDataPayloadUnmarshaler(data []byte) (Payload, error) {
payload, consumedBytes, err := GenericDataPayloadFromBytes(data)
if err != nil {
return nil, err
}
if consumedBytes != len(data) {
return nil, errors.New("not all payload bytes were consumed")
}
return payload, nil
}
// GenericDataPayload represents a payload which just contains a blob of data.
type GenericDataPayload struct {
payloadType Type
data []byte
}
// NewGenericDataPayload creates new GenericDataPayload.
func NewGenericDataPayload(data []byte) *GenericDataPayload {
return &GenericDataPayload{
payloadType: GenericDataPayloadType,
data: data,
}
}
// GenericDataPayloadFromBytes unmarshals a GenericDataPayload from a sequence of bytes.
func GenericDataPayloadFromBytes(bytes []byte) (genericDataPayload *GenericDataPayload, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if genericDataPayload, err = GenericDataPayloadFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse GenericDataPayload from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// GenericDataPayloadFromMarshalUtil unmarshals a GenericDataPayload using a MarshalUtil (for easier unmarshaling).
func GenericDataPayloadFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (genericDataPayload *GenericDataPayload, err error) {
payloadSize, err := marshalUtil.ReadUint32()
if err != nil {
err = errors.Errorf("failed to parse payload size (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
genericDataPayload = &GenericDataPayload{}
if genericDataPayload.payloadType, err = TypeFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse Type from MarshalUtil: %w", err)
return
}
if genericDataPayload.data, err = marshalUtil.ReadBytes(int(payloadSize) - TypeLength); err != nil {
err = errors.Errorf("failed to parse data (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
return
}
// Type returns the Type of the Payload.
func (g *GenericDataPayload) Type() Type {
return g.payloadType
}
// Blob returns the contained data of the GenericDataPayload (without its type and size headers).
func (g *GenericDataPayload) Blob() []byte {
return g.data
}
// Bytes returns a marshaled version of the Payload.
func (g *GenericDataPayload) Bytes() []byte {
return marshalutil.New().
WriteUint32(TypeLength + uint32(len(g.data))).
WriteBytes(g.Type().Bytes()).
WriteBytes(g.Blob()).
Bytes()
}
// String returns a human readable version of the Payload.
func (g *GenericDataPayload) String() string {
return stringify.Struct("GenericDataPayload",
stringify.StructField("type", g.Type()),
stringify.StructField("blob", g.Blob()),
)
}