-
Notifications
You must be signed in to change notification settings - Fork 8
/
object_analyze_funcctx.go
60 lines (53 loc) · 1.59 KB
/
object_analyze_funcctx.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
package sibyl2
import (
"encoding/json"
"github.com/dominikbraun/graph"
"github.com/opensibyl/sibyl2/pkg/extractor"
)
type FunctionContext struct {
*extractor.FunctionWithPath
Calls []*extractor.FunctionWithPath `json:"calls" bson:"calls"`
ReverseCalls []*extractor.FunctionWithPath `json:"reverseCalls" bson:"reverseCalls"`
}
func (f *FunctionContext) ToGraph() *FuncGraphType {
markSelf := graph.VertexAttribute("fillcolor", "red")
markDirect := graph.VertexAttribute("fillcolor", "yellow")
markFill := graph.VertexAttribute("style", "filled")
ctxGraph := graph.New((*extractor.FunctionWithPath).GetIndexName, graph.Directed())
_ = ctxGraph.AddVertex(f.FunctionWithPath, markFill, markSelf)
for _, each := range f.Calls {
// bind itself
_ = ctxGraph.AddVertex(each, markFill, markDirect)
_ = ctxGraph.AddEdge(f.GetIndexName(), each.GetIndexName())
}
for _, each := range f.ReverseCalls {
// bind itself
_ = ctxGraph.AddVertex(each, markFill, markDirect)
_ = ctxGraph.AddEdge(each.GetIndexName(), f.GetIndexName())
}
return WrapFuncGraph(ctxGraph)
}
// ToMap export a very simple map without any custom structs. It will lose ptr to origin unit.
func (f *FunctionContext) ToMap() (map[string]any, error) {
b, err := json.Marshal(f)
if err != nil {
return nil, err
}
var m map[string]interface{}
err = json.Unmarshal(b, &m)
if err != nil {
return nil, err
}
return m, nil
}
func (f *FunctionContext) ToJson() ([]byte, error) {
m, err := f.ToMap()
if err != nil {
return nil, err
}
raw, err := json.Marshal(m)
if err != nil {
return nil, err
}
return raw, nil
}