Skip to content

Commit

Permalink
add high level api support to []byte (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjunbiao authored Jun 24, 2021
1 parent 9b149ed commit 86f2173
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 58 deletions.
55 changes: 33 additions & 22 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,32 @@ func (e *basicEncoderImpl) encodeBasic(input interface{}, signals []*PrimitivePa
var primitiveEncoder *PrimitivePacketEncoder

value := reflect.ValueOf(input)
switch value.Kind() {
case reflect.String:
primitiveEncoder = e.encodeBasicString(input)
case reflect.Int32:
primitiveEncoder = e.encodeBasicInt32(input)
case reflect.Uint32:
primitiveEncoder = e.encodeBasicUint32(input)
case reflect.Int64:
primitiveEncoder = e.encodeBasicInt64(input)
case reflect.Uint64:
primitiveEncoder = e.encodeBasicUint64(input)
case reflect.Float32:
primitiveEncoder = e.encodeBasicFloat32(input)
case reflect.Float64:
primitiveEncoder = e.encodeBasicFloat64(input)
case reflect.Bool:
primitiveEncoder = e.encodeBasicBool(input)
case reflect.Array, reflect.Slice:
//e.marshalBasicSlice(value, e.root)
return e.encodeBasicSlice(value, signals)
default:
panic(fmt.Errorf("marshal error, no matching type: %v", value.Kind()))
if value.Type() == utils.TypeOfByteSlice {
primitiveEncoder = e.encodeBytes(input)
} else {
switch value.Kind() {
case reflect.String:
primitiveEncoder = e.encodeBasicString(input)
case reflect.Int32:
primitiveEncoder = e.encodeBasicInt32(input)
case reflect.Uint32:
primitiveEncoder = e.encodeBasicUint32(input)
case reflect.Int64:
primitiveEncoder = e.encodeBasicInt64(input)
case reflect.Uint64:
primitiveEncoder = e.encodeBasicUint64(input)
case reflect.Float32:
primitiveEncoder = e.encodeBasicFloat32(input)
case reflect.Float64:
primitiveEncoder = e.encodeBasicFloat64(input)
case reflect.Bool:
primitiveEncoder = e.encodeBasicBool(input)
case reflect.Array, reflect.Slice:
//e.marshalBasicSlice(value, e.root)
return e.encodeBasicSlice(value, signals)
default:
panic(fmt.Errorf("marshal error, no matching type: %v", value.Kind()))
}
}

if primitiveEncoder == nil {
Expand Down Expand Up @@ -230,6 +234,13 @@ func (e *basicEncoderImpl) encodeBasicBool(input interface{}) *PrimitivePacketEn
return encoder
}

// encodeBytes encode []byte to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBytes(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetBytesValue(input.([]byte))
return encoder
}

// encodeBasicStringSlice encode reflect.Value of []string to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicStringSlice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
Expand Down
5 changes: 5 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ func (enc *PrimitivePacketEncoder) SetStringValue(v string) {
enc.valbuf = []byte(v)
}

// SetBytesValue encode []byte
func (enc *PrimitivePacketEncoder) SetBytesValue(v []byte) {
enc.valbuf = v
}

// SetBytes set bytes to internal buf variable
func (enc *PrimitivePacketEncoder) SetBytes(buf []byte) {
enc.valbuf = buf
Expand Down
30 changes: 30 additions & 0 deletions examples/high_level/bytes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"bytes"
"fmt"

"github.com/yomorun/y3-codec-golang"
)

// Example of encoding and decoding string []byte type.
func main() {
// Simulate source to generate and send data
data := []byte{0x20, 0x21, 0x22}
sendingBuf, _ := y3.NewCodec(0x10).Marshal(data)
//fmt.Printf("sendingBuf=%#v\n", sendingBuf)
source := y3.FromStream(bytes.NewReader(sendingBuf))
// Simulate flow listening and decoding data
var decode = func(v []byte) (interface{}, error) {
sl, err := y3.ToBytes(v)
if err != nil {
return nil, err
}
fmt.Printf("encoded data: %#v\n", sl)
return sl, nil
}
consumer := source.Subscribe(0x10).OnObserve(decode)
for range consumer {
}

}
35 changes: 35 additions & 0 deletions examples/high_level/struct_bytes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"bytes"
"fmt"

"github.com/yomorun/y3-codec-golang"
)

// Example of encoding and decoding struct type with []byte
func main() {
// Simulate source to generate and send data
data := ShakeData{Topic: "shake", Payload: []byte{0x20, 0x21, 0x22}}
sendingBuf, _ := y3.NewCodec(0x10).Marshal(data)
//fmt.Printf("sendingBuf=%#v\n", sendingBuf)
source := y3.FromStream(bytes.NewReader(sendingBuf))
// Simulate flow listening and decoding data
var decode = func(v []byte) (interface{}, error) {
var obj ShakeData
err := y3.ToObject(v, &obj)
if err != nil {
return nil, err
}
fmt.Printf("encoded Payload: %#v\n", obj.Payload)
return obj, nil
}
consumer := source.Subscribe(0x10).OnObserve(decode)
for range consumer {
}
}

type ShakeData struct {
Topic string `y3:"0x11"`
Payload []byte `y3:"0x12"`
}
40 changes: 40 additions & 0 deletions examples/high_level/struct_bytes_slice/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"bytes"
"fmt"

"github.com/yomorun/y3-codec-golang"
)

// Example of encoding and decoding struct slice type with []byte
func main() {
// Simulate source to generate and send data
data := []ShakeData{
{Topic: "shake", Payload: []byte{0x20, 0x21, 0x22}},
{Topic: "shake2", Payload: []byte{0x30, 0x31, 0x32}},
}
sendingBuf, _ := y3.NewCodec(0x10).Marshal(data)
source := y3.FromStream(bytes.NewReader(sendingBuf))

// Simulate flow listening and decoding data
var decode = func(v []byte) (interface{}, error) {
var sl []ShakeData
err := y3.ToObject(v, &sl)
if err != nil {
return nil, err
}
for _, s := range sl {
fmt.Printf("encoded Payload: %#v\n", s.Payload)
}
return sl, nil
}
consumer := source.Subscribe(0x10).OnObserve(decode)
for range consumer {
}
}

type ShakeData struct {
Topic string `y3:"0x11"`
Payload []byte `y3:"0x12"`
}
59 changes: 41 additions & 18 deletions explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
| bool slice | y3.ToBoolSlice |
| string | y3.ToUTF8String |
| string slice | y3.ToUTF8StringSlice |
| []byte | y3.ToBytes |

<details>
<summary>struct</summary>

```golang
func main() {
// Simulate source to generate and send data
Expand Down Expand Up @@ -137,7 +138,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>struct slice</summary>

```golang
func main() {
// Simulate source to generate and send data
Expand Down Expand Up @@ -170,7 +171,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>int32</summary>

```golang
// Simulate source to generate and send data
var data int32 = 123
Expand All @@ -192,7 +193,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>int32 slice</summary>

```golang
// Simulate source to generate and send data
data := []int32{123, 456}
Expand All @@ -214,7 +215,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>uint32</summary>

```golang
// Simulate source to generate and send data
var data uint32 = 123
Expand All @@ -236,7 +237,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>uint32 slice</summary>

```golang
// Simulate source to generate and send data
data := []uint32{123, 456}
Expand All @@ -258,7 +259,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>int64</summary>

```golang
// Simulate source to generate and send data
var data int64 = 123
Expand All @@ -280,7 +281,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>int64 slice</summary>

```golang
// Simulate source to generate and send data
data := []int64{123, 456}
Expand All @@ -302,7 +303,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>uint64</summary>

```golang
// Simulate source to generate and send data
var data uint64 = 123
Expand All @@ -324,7 +325,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>uint64 slice</summary>

```golang
// Simulate source to generate and send data
data := []uint64{123, 456}
Expand All @@ -346,7 +347,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>float32</summary>

```golang
// Simulate source to generate and send data
var data float32 = 1.23
Expand All @@ -368,7 +369,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>float32 slice</summary>

```golang
// Simulate source to generate and send data
data := []float32{1.23, 4.56}
Expand All @@ -390,7 +391,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>float64</summary>

```golang
// Simulate source to generate and send data
var data float64 = 1.23
Expand All @@ -412,7 +413,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>float64 slice</summary>

```golang
// Simulate source to generate and send data
data := []float64{1.23, 4.56}
Expand All @@ -434,7 +435,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>bool</summary>

```golang
// Simulate source to generate and send data
data := true
Expand All @@ -456,7 +457,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>bool slice</summary>

```golang
// Simulate source to generate and send data
data := []bool{true, false}
Expand All @@ -478,7 +479,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>string</summary>

```golang
// Simulate source to generate and send data
data := "abc"
Expand All @@ -500,7 +501,7 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
</details>
<details>
<summary>string slice</summary>

```golang
// Simulate source to generate and send data
data := []string{"a", "b"}
Expand All @@ -520,6 +521,28 @@ Unified encoding method: `y3.NewCodec(observe byte).Marshal(input interface{})`
}
```
</details>
<details>
<summary>[]byte</summary>

```golang
// Simulate source to generate and send data
data := []byte{0x20, 0x21, 0x22}
sendingBuf, _ := y3.NewCodec(0x10).Marshal(data)
source := y3.FromStream(bytes.NewReader(sendingBuf))
// Simulate flow listening and decoding data
var decode = func(v []byte) (interface{}, error) {
sl, err := y3.ToBytes(v)
if err != nil {
return nil, err
}
fmt.Printf("encoded data: %#v\n", sl)
return sl, nil
}
consumer := source.Subscribe(0x10).OnObserve(decode)
for range consumer {
}
```
</details>

More examples in `/examples/`

Expand Down
Loading

0 comments on commit 86f2173

Please sign in to comment.