-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add high level api support to []byte (#69)
- Loading branch information
1 parent
9b149ed
commit 86f2173
Showing
11 changed files
with
260 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.