Skip to content

Commit 86f2173

Browse files
authored
add high level api support to []byte (#69)
1 parent 9b149ed commit 86f2173

File tree

11 files changed

+260
-58
lines changed

11 files changed

+260
-58
lines changed

basic.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,32 @@ func (e *basicEncoderImpl) encodeBasic(input interface{}, signals []*PrimitivePa
7979
var primitiveEncoder *PrimitivePacketEncoder
8080

8181
value := reflect.ValueOf(input)
82-
switch value.Kind() {
83-
case reflect.String:
84-
primitiveEncoder = e.encodeBasicString(input)
85-
case reflect.Int32:
86-
primitiveEncoder = e.encodeBasicInt32(input)
87-
case reflect.Uint32:
88-
primitiveEncoder = e.encodeBasicUint32(input)
89-
case reflect.Int64:
90-
primitiveEncoder = e.encodeBasicInt64(input)
91-
case reflect.Uint64:
92-
primitiveEncoder = e.encodeBasicUint64(input)
93-
case reflect.Float32:
94-
primitiveEncoder = e.encodeBasicFloat32(input)
95-
case reflect.Float64:
96-
primitiveEncoder = e.encodeBasicFloat64(input)
97-
case reflect.Bool:
98-
primitiveEncoder = e.encodeBasicBool(input)
99-
case reflect.Array, reflect.Slice:
100-
//e.marshalBasicSlice(value, e.root)
101-
return e.encodeBasicSlice(value, signals)
102-
default:
103-
panic(fmt.Errorf("marshal error, no matching type: %v", value.Kind()))
82+
if value.Type() == utils.TypeOfByteSlice {
83+
primitiveEncoder = e.encodeBytes(input)
84+
} else {
85+
switch value.Kind() {
86+
case reflect.String:
87+
primitiveEncoder = e.encodeBasicString(input)
88+
case reflect.Int32:
89+
primitiveEncoder = e.encodeBasicInt32(input)
90+
case reflect.Uint32:
91+
primitiveEncoder = e.encodeBasicUint32(input)
92+
case reflect.Int64:
93+
primitiveEncoder = e.encodeBasicInt64(input)
94+
case reflect.Uint64:
95+
primitiveEncoder = e.encodeBasicUint64(input)
96+
case reflect.Float32:
97+
primitiveEncoder = e.encodeBasicFloat32(input)
98+
case reflect.Float64:
99+
primitiveEncoder = e.encodeBasicFloat64(input)
100+
case reflect.Bool:
101+
primitiveEncoder = e.encodeBasicBool(input)
102+
case reflect.Array, reflect.Slice:
103+
//e.marshalBasicSlice(value, e.root)
104+
return e.encodeBasicSlice(value, signals)
105+
default:
106+
panic(fmt.Errorf("marshal error, no matching type: %v", value.Kind()))
107+
}
104108
}
105109

106110
if primitiveEncoder == nil {
@@ -230,6 +234,13 @@ func (e *basicEncoderImpl) encodeBasicBool(input interface{}) *PrimitivePacketEn
230234
return encoder
231235
}
232236

237+
// encodeBytes encode []byte to PrimitivePacketEncoder
238+
func (e *basicEncoderImpl) encodeBytes(input interface{}) *PrimitivePacketEncoder {
239+
var encoder = NewPrimitivePacketEncoder(int(e.observe))
240+
encoder.SetBytesValue(input.([]byte))
241+
return encoder
242+
}
243+
233244
// encodeBasicStringSlice encode reflect.Value of []string to NodePacketEncoder
234245
func (e *basicEncoderImpl) encodeBasicStringSlice(value reflect.Value) *NodePacketEncoder {
235246
var node = NewNodeSlicePacketEncoder(int(e.observe))

encoder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ func (enc *PrimitivePacketEncoder) SetStringValue(v string) {
135135
enc.valbuf = []byte(v)
136136
}
137137

138+
// SetBytesValue encode []byte
139+
func (enc *PrimitivePacketEncoder) SetBytesValue(v []byte) {
140+
enc.valbuf = v
141+
}
142+
138143
// SetBytes set bytes to internal buf variable
139144
func (enc *PrimitivePacketEncoder) SetBytes(buf []byte) {
140145
enc.valbuf = buf

examples/high_level/bytes/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
7+
"github.com/yomorun/y3-codec-golang"
8+
)
9+
10+
// Example of encoding and decoding string []byte type.
11+
func main() {
12+
// Simulate source to generate and send data
13+
data := []byte{0x20, 0x21, 0x22}
14+
sendingBuf, _ := y3.NewCodec(0x10).Marshal(data)
15+
//fmt.Printf("sendingBuf=%#v\n", sendingBuf)
16+
source := y3.FromStream(bytes.NewReader(sendingBuf))
17+
// Simulate flow listening and decoding data
18+
var decode = func(v []byte) (interface{}, error) {
19+
sl, err := y3.ToBytes(v)
20+
if err != nil {
21+
return nil, err
22+
}
23+
fmt.Printf("encoded data: %#v\n", sl)
24+
return sl, nil
25+
}
26+
consumer := source.Subscribe(0x10).OnObserve(decode)
27+
for range consumer {
28+
}
29+
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
7+
"github.com/yomorun/y3-codec-golang"
8+
)
9+
10+
// Example of encoding and decoding struct type with []byte
11+
func main() {
12+
// Simulate source to generate and send data
13+
data := ShakeData{Topic: "shake", Payload: []byte{0x20, 0x21, 0x22}}
14+
sendingBuf, _ := y3.NewCodec(0x10).Marshal(data)
15+
//fmt.Printf("sendingBuf=%#v\n", sendingBuf)
16+
source := y3.FromStream(bytes.NewReader(sendingBuf))
17+
// Simulate flow listening and decoding data
18+
var decode = func(v []byte) (interface{}, error) {
19+
var obj ShakeData
20+
err := y3.ToObject(v, &obj)
21+
if err != nil {
22+
return nil, err
23+
}
24+
fmt.Printf("encoded Payload: %#v\n", obj.Payload)
25+
return obj, nil
26+
}
27+
consumer := source.Subscribe(0x10).OnObserve(decode)
28+
for range consumer {
29+
}
30+
}
31+
32+
type ShakeData struct {
33+
Topic string `y3:"0x11"`
34+
Payload []byte `y3:"0x12"`
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
7+
"github.com/yomorun/y3-codec-golang"
8+
)
9+
10+
// Example of encoding and decoding struct slice type with []byte
11+
func main() {
12+
// Simulate source to generate and send data
13+
data := []ShakeData{
14+
{Topic: "shake", Payload: []byte{0x20, 0x21, 0x22}},
15+
{Topic: "shake2", Payload: []byte{0x30, 0x31, 0x32}},
16+
}
17+
sendingBuf, _ := y3.NewCodec(0x10).Marshal(data)
18+
source := y3.FromStream(bytes.NewReader(sendingBuf))
19+
20+
// Simulate flow listening and decoding data
21+
var decode = func(v []byte) (interface{}, error) {
22+
var sl []ShakeData
23+
err := y3.ToObject(v, &sl)
24+
if err != nil {
25+
return nil, err
26+
}
27+
for _, s := range sl {
28+
fmt.Printf("encoded Payload: %#v\n", s.Payload)
29+
}
30+
return sl, nil
31+
}
32+
consumer := source.Subscribe(0x10).OnObserve(decode)
33+
for range consumer {
34+
}
35+
}
36+
37+
type ShakeData struct {
38+
Topic string `y3:"0x11"`
39+
Payload []byte `y3:"0x12"`
40+
}

explainer.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
104104
| bool slice | y3.ToBoolSlice |
105105
| string | y3.ToUTF8String |
106106
| string slice | y3.ToUTF8StringSlice |
107+
| []byte | y3.ToBytes |
107108

108109
<details>
109110
<summary>struct</summary>
110-
111+
111112
```golang
112113
func main() {
113114
// Simulate source to generate and send data
@@ -137,7 +138,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
137138
</details>
138139
<details>
139140
<summary>struct slice</summary>
140-
141+
141142
```golang
142143
func main() {
143144
// Simulate source to generate and send data
@@ -170,7 +171,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
170171
</details>
171172
<details>
172173
<summary>int32</summary>
173-
174+
174175
```golang
175176
// Simulate source to generate and send data
176177
var data int32 = 123
@@ -192,7 +193,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
192193
</details>
193194
<details>
194195
<summary>int32 slice</summary>
195-
196+
196197
```golang
197198
// Simulate source to generate and send data
198199
data := []int32{123, 456}
@@ -214,7 +215,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
214215
</details>
215216
<details>
216217
<summary>uint32</summary>
217-
218+
218219
```golang
219220
// Simulate source to generate and send data
220221
var data uint32 = 123
@@ -236,7 +237,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
236237
</details>
237238
<details>
238239
<summary>uint32 slice</summary>
239-
240+
240241
```golang
241242
// Simulate source to generate and send data
242243
data := []uint32{123, 456}
@@ -258,7 +259,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
258259
</details>
259260
<details>
260261
<summary>int64</summary>
261-
262+
262263
```golang
263264
// Simulate source to generate and send data
264265
var data int64 = 123
@@ -280,7 +281,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
280281
</details>
281282
<details>
282283
<summary>int64 slice</summary>
283-
284+
284285
```golang
285286
// Simulate source to generate and send data
286287
data := []int64{123, 456}
@@ -302,7 +303,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
302303
</details>
303304
<details>
304305
<summary>uint64</summary>
305-
306+
306307
```golang
307308
// Simulate source to generate and send data
308309
var data uint64 = 123
@@ -324,7 +325,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
324325
</details>
325326
<details>
326327
<summary>uint64 slice</summary>
327-
328+
328329
```golang
329330
// Simulate source to generate and send data
330331
data := []uint64{123, 456}
@@ -346,7 +347,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
346347
</details>
347348
<details>
348349
<summary>float32</summary>
349-
350+
350351
```golang
351352
// Simulate source to generate and send data
352353
var data float32 = 1.23
@@ -368,7 +369,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
368369
</details>
369370
<details>
370371
<summary>float32 slice</summary>
371-
372+
372373
```golang
373374
// Simulate source to generate and send data
374375
data := []float32{1.23, 4.56}
@@ -390,7 +391,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
390391
</details>
391392
<details>
392393
<summary>float64</summary>
393-
394+
394395
```golang
395396
// Simulate source to generate and send data
396397
var data float64 = 1.23
@@ -412,7 +413,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
412413
</details>
413414
<details>
414415
<summary>float64 slice</summary>
415-
416+
416417
```golang
417418
// Simulate source to generate and send data
418419
data := []float64{1.23, 4.56}
@@ -434,7 +435,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
434435
</details>
435436
<details>
436437
<summary>bool</summary>
437-
438+
438439
```golang
439440
// Simulate source to generate and send data
440441
data := true
@@ -456,7 +457,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
456457
</details>
457458
<details>
458459
<summary>bool slice</summary>
459-
460+
460461
```golang
461462
// Simulate source to generate and send data
462463
data := []bool{true, false}
@@ -478,7 +479,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
478479
</details>
479480
<details>
480481
<summary>string</summary>
481-
482+
482483
```golang
483484
// Simulate source to generate and send data
484485
data := "abc"
@@ -500,7 +501,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
500501
</details>
501502
<details>
502503
<summary>string slice</summary>
503-
504+
504505
```golang
505506
// Simulate source to generate and send data
506507
data := []string{"a", "b"}
@@ -520,6 +521,28 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
520521
}
521522
```
522523
</details>
524+
<details>
525+
<summary>[]byte</summary>
526+
527+
```golang
528+
// Simulate source to generate and send data
529+
data := []byte{0x20, 0x21, 0x22}
530+
sendingBuf, _ := y3.NewCodec(0x10).Marshal(data)
531+
source := y3.FromStream(bytes.NewReader(sendingBuf))
532+
// Simulate flow listening and decoding data
533+
var decode = func(v []byte) (interface{}, error) {
534+
sl, err := y3.ToBytes(v)
535+
if err != nil {
536+
return nil, err
537+
}
538+
fmt.Printf("encoded data: %#v\n", sl)
539+
return sl, nil
540+
}
541+
consumer := source.Subscribe(0x10).OnObserve(decode)
542+
for range consumer {
543+
}
544+
```
545+
</details>
523546

524547
More examples in `/examples/`
525548

0 commit comments

Comments
 (0)