-
Notifications
You must be signed in to change notification settings - Fork 45
/
composite_metadata.go
140 lines (123 loc) · 3.86 KB
/
composite_metadata.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package extension
import (
"bytes"
"fmt"
"github.com/rsocket/rsocket-go/internal/bytesconv"
"math"
"github.com/rsocket/rsocket-go/internal/u24"
)
// CompositeMetadata provides multi Metadata payloads with different MIME types.
type CompositeMetadata []byte
// CompositeMetadataScanner can be used to scan entry in CompositeMetadata.
type CompositeMetadataScanner struct {
offset int
raw []byte
}
// CompositeMetadataBuilder can be used to build a CompositeMetadata.
type CompositeMetadataBuilder struct {
k []interface{}
v [][]byte
}
// Scanner returns a entry scanner.
func (c CompositeMetadata) Scanner() *CompositeMetadataScanner {
return &CompositeMetadataScanner{
offset: 0,
raw: c,
}
}
// Scan returns true when scanner has more content.
func (c *CompositeMetadataScanner) Scan() bool {
return c.offset < len(c.raw)
}
// MetadataUTF8 returns current metadata in utf8 string.
func (c *CompositeMetadataScanner) MetadataUTF8() (mimeType string, metadata string, err error) {
mimeType, metadataRaw, err := c.Metadata()
if err != nil {
return
}
metadata = string(metadataRaw)
return
}
// Metadata returns current metadata bytes.
func (c *CompositeMetadataScanner) Metadata() (mimeType string, metadata []byte, err error) {
l, mimeType, metadata, err := c.decodeCompositeMetadataOnce(c.raw[c.offset:])
if err != nil {
return
}
c.offset += l
return
}
func (c *CompositeMetadataScanner) decodeCompositeMetadataOnce(raw []byte) (length int, mimeType string, metadata []byte, err error) {
m := raw[0]
size := 1
idOrLen := (m << 1) >> 1
if m&0x80 == 0x80 {
mimeType = MIME(idOrLen).String()
} else {
mimeTypeLen := int(idOrLen) + 1
size += mimeTypeLen
mimeType = string(raw[1 : 1+mimeTypeLen])
}
metadataLen := u24.NewUint24Bytes(raw[size : size+3]).AsInt()
length = size + 3 + metadataLen
metadata = raw[size+3 : length]
return
}
// PushWellKnown push a WellKnownMimeType and metadata bytes.
func (c *CompositeMetadataBuilder) PushWellKnown(mimeType MIME, metadata []byte) *CompositeMetadataBuilder {
c.k = append(c.k, mimeType)
c.v = append(c.v, metadata)
return c
}
// PushWellKnownString push a WellKnownMimeType and metadata string.
func (c *CompositeMetadataBuilder) PushWellKnownString(mimeType MIME, metadata string) *CompositeMetadataBuilder {
return c.PushWellKnown(mimeType, bytesconv.StringToBytes(metadata))
}
// Push push a custom MimeType and metadata bytes.
func (c *CompositeMetadataBuilder) Push(mimeType string, metadata []byte) *CompositeMetadataBuilder {
if well, ok := ParseMIME(mimeType); ok {
c.k = append(c.k, well)
} else {
c.k = append(c.k, mimeType)
}
c.v = append(c.v, metadata)
return c
}
// PushString push a custom MimeType and metadata string.
func (c *CompositeMetadataBuilder) PushString(mimeType string, metadata string) *CompositeMetadataBuilder {
return c.Push(mimeType, bytesconv.StringToBytes(metadata))
}
// Build build a new CompositeMetadata.
func (c *CompositeMetadataBuilder) Build() (CompositeMetadata, error) {
bf := bytes.Buffer{}
for i := 0; i < len(c.k); i++ {
switch mimeType := c.k[i].(type) {
case MIME:
bf.WriteByte(0x80 | byte(mimeType))
case string:
mimeTypeLen := len(mimeType)
if mimeTypeLen > math.MaxInt8 {
return nil, fmt.Errorf("length of MIME type is over %d", math.MaxInt8)
}
bf.WriteByte(byte(mimeTypeLen - 1))
bf.Write(bytesconv.StringToBytes(mimeType))
default:
panic("unreachable")
}
metadata := c.v[i]
metadataLen := len(metadata)
bf.Write(u24.MustNewUint24(metadataLen).Bytes())
if metadataLen > 0 {
bf.Write(metadata)
}
}
return bf.Bytes(), nil
}
// NewCompositeMetadataBuilder returns a CompositeMetadata builder.
func NewCompositeMetadataBuilder() *CompositeMetadataBuilder {
return &CompositeMetadataBuilder{}
}
// NewCompositeMetadataBytes returns a CompositeMetadata.
func NewCompositeMetadataBytes(raw []byte) CompositeMetadata {
return raw
}