-
Notifications
You must be signed in to change notification settings - Fork 27
/
uast.go
91 lines (72 loc) · 1.79 KB
/
uast.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package service
import (
"encoding/json"
"gopkg.in/bblfsh/sdk.v1/protocol"
"gopkg.in/bblfsh/sdk.v1/uast"
)
// need to move to service to avoid circular imports
// UnmarshallUAST tries to cast data as [][]byte and unmarshall uast nodes
func UnmarshallUAST(data interface{}) ([]*Node, error) {
var protobufs [][]byte
if err := json.Unmarshal(*data.(*[]byte), &protobufs); err != nil {
return nil, err
}
nodes := make([]*Node, len(protobufs))
for i, v := range protobufs {
n := uast.NewNode()
if err := n.Unmarshal(v); err != nil {
return nil, err
}
nodes[i] = (*Node)(n)
}
return nodes, nil
}
// ParseResponse amends default MarshalJSON to be compatible with frontend
type ParseResponse protocol.ParseResponse
// MarshalJSON returns the JSON representation of the protocol.ParseResponse
func (r *ParseResponse) MarshalJSON() ([]byte, error) {
resp := struct {
*protocol.ParseResponse
UAST *Node `json:"uast"`
}{
(*protocol.ParseResponse)(r),
(*Node)(r.UAST),
}
return json.Marshal(resp)
}
type Node uast.Node
// MarshalJSON returns the JSON representation of the Node
func (n *Node) MarshalJSON() ([]byte, error) {
var nodes = make([]*Node, len(n.Children))
for i, n := range n.Children {
nodes[i] = (*Node)(n)
}
var roles = make([]string, len(n.Roles))
for i, r := range n.Roles {
roles[i] = r.String()
}
node := struct {
*uast.Node
Roles []string
Children []*Node
}{
(*uast.Node)(n),
roles,
nodes,
}
return json.Marshal(node)
}
type Language struct {
ID string `json:"id"`
Name string `json:"name"`
}
func DriverManifestsToLangs(drivers []protocol.DriverManifest) []Language {
result := make([]Language, len(drivers))
for i, driver := range drivers {
result[i] = Language{
ID: driver.Language,
Name: driver.Name,
}
}
return result
}