@@ -30,6 +30,79 @@ BigEndianInt32(len(marhsal(node))+marshal(node)+
3030...
3131```
3232
33+ As an example of how to manage the serialized data programatically, checkout out the Go code below:
34+ ``` go
35+ import (
36+ " bytes"
37+ " encoding/binary"
38+ " errors"
39+ " io"
40+
41+ " github.com/bblfsh/sdk/uast"
42+ )
43+
44+ func marshalNodes (nodes []*uast .Node ) (out []byte , err error ) {
45+ defer func () {
46+ if r := recover (); r != nil {
47+ out, err = nil , r.(error )
48+ }
49+ }()
50+
51+ buf := &bytes.Buffer {}
52+ for _ , n := range nodes {
53+ if n != nil {
54+ data , err := n.Marshal ()
55+ if err != nil {
56+ return nil , err
57+ }
58+
59+ if err := binary.Write (
60+ buf, binary.BigEndian , int32 (len (data)),
61+ ); err != nil {
62+ return nil , err
63+ }
64+
65+ n , _ := buf.Write (data)
66+ if n != len (data) {
67+ return nil , errors.New (" couldn't write all the data" )
68+ }
69+ }
70+ }
71+
72+ return buf.Bytes (), nil
73+ }
74+
75+ func unmarshalNodes (data []byte ) ([]*uast .Node , error ) {
76+ if len (data) == 0 {
77+ return nil , nil
78+ }
79+
80+ nodes := []*uast.Node {}
81+ buf := bytes.NewBuffer (data)
82+ for {
83+ var nodeLen int32
84+ if err := binary.Read (
85+ buf, binary.BigEndian , &nodeLen,
86+ ); err != nil {
87+ if err == io.EOF {
88+ break
89+ }
90+
91+ return nil , err
92+ }
93+
94+ node := uast.NewNode ()
95+ if err := node.Unmarshal (buf.Next (int (nodeLen))); err != nil {
96+ return nil , err
97+ }
98+
99+ nodes = append (nodes, node)
100+ }
101+
102+ return nodes, nil
103+ }
104+ ```
105+
33106## How to formulate XPath queries when use uast and uast_xpath functions
34107
35108Have a look at the [ bblfsh docs] ( https://docs.sourced.tech/babelfish/using-babelfish/uast-querying ) to query UASTs with XPath.
0 commit comments