-
Notifications
You must be signed in to change notification settings - Fork 10
/
test-marshalling.go
47 lines (34 loc) · 1018 Bytes
/
test-marshalling.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
// +build ignore
package main
import (
"encoding/json"
"log"
"github.com/gogo/protobuf/proto"
"github.com/ninjasphere/go-zigbee/nwkmgr"
)
func main() {
joinTime := uint32(30)
permitRequest := &nwkmgr.NwkSetPermitJoinReq{
PermitJoinTime: &joinTime,
PermitJoin: nwkmgr.NwkPermitJoinTypeT_PERMIT_ALL.Enum(),
}
proto.SetDefaults(permitRequest)
data, err := proto.Marshal(permitRequest)
if err != nil {
log.Fatal("marshaling error: ", err)
}
log.Printf("marshalled permit join to protobuf: %x", data)
jsonData, err := json.Marshal(permitRequest)
if err != nil {
log.Fatal("marshaling error: ", err)
}
log.Printf("marshalled permit join to json: %s", jsonData)
newTest := new(nwkmgr.NwkSetPermitJoinReq)
err = proto.Unmarshal(data, newTest)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
if permitRequest.GetPermitJoinTime() != newTest.GetPermitJoinTime() {
log.Fatalf("data mismatch %d != %d", permitRequest.GetPermitJoinTime(), newTest.GetPermitJoinTime())
}
}