• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unpackdev / solgo / 6403076442

04 Oct 2023 07:48AM UTC coverage: 83.146% (-1.1%) from 84.2%
6403076442

push

github

web-flow
AST Import from JSON (#122)

* Just basic comment to spin off the PR
* Contract nil check fixes
* Proper function processing
* Fixing unary prefix/suffix
* Proper parsing of contract definitions
* AST import completed including unit tests
* IR import from JSON

290 of 623 new or added lines in 17 files covered. (46.55%)

8 existing lines in 3 files now uncovered.

13789 of 16584 relevant lines covered (83.15%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.48
/ast/root_node.go
1
package ast
2

3
import (
4
        "encoding/json"
5

6
        v3 "github.com/cncf/xds/go/xds/type/v3"
7
        ast_pb "github.com/unpackdev/protos/dist/go/ast"
8
)
9

10
// RootNode is the root node of the AST.
11
type RootNode struct {
12
        // Id is the unique identifier of the root node.
13
        Id int64 `json:"id"`
14

15
        // NodeType is the type of the AST node.
16
        NodeType ast_pb.NodeType `json:"node_type"`
17

18
        // EntrySourceUnit is the entry source unit of the root node.
19
        EntrySourceUnit int64 `json:"entry_source_unit"`
20

21
        // Globals is the list of global nodes.
22
        Globals []Node[NodeType] `json:"globals"`
23

24
        // SourceUnits is the list of source units.
25
        SourceUnits []*SourceUnit[Node[ast_pb.SourceUnit]] `json:"root"`
26

27
        // Comments is the list of comments.
28
        Comments []*Comment `json:"comments"`
29
}
30

31
// NewRootNode creates a new RootNode with the provided ASTBuilder, entry source unit, source units, and comments.
32
func NewRootNode(builder *ASTBuilder, entrySourceUnit int64, sourceUnits []*SourceUnit[Node[ast_pb.SourceUnit]], comments []*Comment) *RootNode {
1✔
33
        return &RootNode{
1✔
34
                Id:              builder.GetNextID(),
1✔
35
                EntrySourceUnit: entrySourceUnit,
1✔
36
                NodeType:        ast_pb.NodeType_ROOT_SOURCE_UNIT,
1✔
37
                Comments:        comments,
1✔
38
                SourceUnits:     sourceUnits,
1✔
39
                Globals:         make([]Node[NodeType], 0),
1✔
40
        }
1✔
41
}
1✔
42

43
// GetId returns the id of the RootNode node.
44
func (r *RootNode) GetId() int64 {
1✔
45
        return r.Id
1✔
46
}
1✔
47

48
// GetType returns the type of the RootNode node.
49
func (r *RootNode) GetType() ast_pb.NodeType {
1✔
50
        return r.NodeType
1✔
51
}
1✔
52

53
// GetSrc returns the source code location of the RootNode node.
54
func (r *RootNode) GetSrc() SrcNode {
1✔
55
        return SrcNode{}
1✔
56
}
1✔
57

58
// GetTypeDescription returns the type description of the RootNode node.
59
// RootNode nodes do not have type descriptions.
60
func (r *RootNode) GetTypeDescription() *TypeDescription {
1✔
61
        return &TypeDescription{
1✔
62
                TypeString:     "root",
1✔
63
                TypeIdentifier: "t_root",
1✔
64
        }
1✔
65
}
1✔
66

67
// SetReferenceDescriptor sets the reference descriptions of the RootNode node.
68
func (r *RootNode) SetReferenceDescriptor(refId int64, refDesc *TypeDescription) bool {
1✔
69
        return false
1✔
70
}
1✔
71

72
// GetSourceUnits returns the source units of the root node.
73
func (r *RootNode) GetSourceUnits() []*SourceUnit[Node[ast_pb.SourceUnit]] {
1✔
74
        return r.SourceUnits
1✔
75
}
1✔
76

77
// GetSourceUnitByName returns the source unit with the provided name.
78
func (r *RootNode) GetSourceUnitByName(name string) *SourceUnit[Node[ast_pb.SourceUnit]] {
1✔
79
        for _, sourceUnit := range r.SourceUnits {
2✔
80
                if sourceUnit.Name == name {
2✔
81
                        return sourceUnit
1✔
82
                }
1✔
83
        }
84

85
        return nil
×
86
}
87

88
// GetSourceUnitById returns the source unit with the provided id.
89
func (r *RootNode) GetSourceUnitById(id int64) *SourceUnit[Node[ast_pb.SourceUnit]] {
1✔
90
        for _, sourceUnit := range r.SourceUnits {
2✔
91
                if sourceUnit.Id == id {
2✔
92
                        return sourceUnit
1✔
93
                }
1✔
94
        }
95

96
        return nil
×
97
}
98

99
// HasSourceUnits returns true if the root node has source units.
100
func (r *RootNode) HasSourceUnits() bool {
1✔
101
        return len(r.SourceUnits) > 0
1✔
102
}
1✔
103

104
// GetSourceUnitCount returns the number of source units of the root node.
105
func (r *RootNode) GetSourceUnitCount() int32 {
1✔
106
        return int32(len(r.SourceUnits))
1✔
107
}
1✔
108

109
// GetEntrySourceUnit returns the entry source unit of the root node.
110
func (r *RootNode) GetEntrySourceUnit() int64 {
1✔
111
        return r.EntrySourceUnit
1✔
112
}
1✔
113

114
// SetEntrySourceUnit sets the entry source unit of the root node.
115
func (r *RootNode) SetEntrySourceUnit(entrySourceUnit int64) {
1✔
116
        r.EntrySourceUnit = entrySourceUnit
1✔
117
}
1✔
118

119
// GetComments returns the comments of the root node.
120
func (r *RootNode) GetComments() []*Comment {
1✔
121
        return r.Comments
1✔
122
}
1✔
123

124
// GetNodes returns the nodes of the root node.
125
func (r *RootNode) GetNodes() []Node[NodeType] {
1✔
126
        toReturn := make([]Node[NodeType], 0)
1✔
127
        for _, sourceUnit := range r.SourceUnits {
2✔
128
                toReturn = append(toReturn, sourceUnit)
1✔
129
        }
1✔
130
        return toReturn
1✔
131
}
132

133
// GetGlobalNodes returns the global nodes of the root node.
134
func (r *RootNode) GetGlobalNodes() []Node[NodeType] {
×
135
        return r.Globals
×
136
}
×
137

138
func (r *RootNode) UnmarshalJSON(data []byte) error {
1✔
139
        var tempMap map[string]json.RawMessage
1✔
140
        if err := json.Unmarshal(data, &tempMap); err != nil {
1✔
141
                return err
×
142
        }
×
143

144
        if id, ok := tempMap["id"]; ok {
2✔
145
                if err := json.Unmarshal(id, &r.Id); err != nil {
1✔
NEW
146
                        return err
×
NEW
147
                }
×
148
        }
149

150
        if nodeType, ok := tempMap["node_type"]; ok {
2✔
151
                if err := json.Unmarshal(nodeType, &r.NodeType); err != nil {
1✔
152
                        return err
×
153
                }
×
154
        }
155

156
        if esu, ok := tempMap["entry_source_unit"]; ok {
2✔
157
                if err := json.Unmarshal(esu, &r.EntrySourceUnit); err != nil {
1✔
158
                        return err
×
159
                }
×
160
        }
161

162
        if glob, ok := tempMap["globals"]; ok {
2✔
163
                r.Globals = make([]Node[NodeType], 0)
1✔
164

1✔
165
                var globals []json.RawMessage
1✔
166
                if err := json.Unmarshal(glob, &globals); err != nil {
1✔
167
                        return err
×
168
                }
×
169

170
                for _, tempGlobal := range globals {
2✔
171
                        var global map[string]json.RawMessage
1✔
172
                        if err := json.Unmarshal(tempGlobal, &global); err != nil {
1✔
NEW
173
                                return err
×
NEW
174
                        }
×
175

176
                        var tempGlobalType ast_pb.NodeType
1✔
177
                        if err := json.Unmarshal(global["node_type"], &tempGlobalType); err != nil {
1✔
NEW
178
                                return err
×
NEW
179
                        }
×
180

181
                        node, err := unmarshalNode(tempGlobal, tempGlobalType)
1✔
182
                        if err != nil {
1✔
NEW
183
                                return err
×
NEW
184
                        }
×
185
                        r.Globals = append(r.Globals, node)
1✔
186

187
                }
188
        }
189

190
        if root, ok := tempMap["root"]; ok {
2✔
191
                if err := json.Unmarshal(root, &r.SourceUnits); err != nil {
1✔
NEW
192
                        return err
×
NEW
193
                }
×
194
        }
195

196
        if comments, ok := tempMap["comments"]; ok {
2✔
197
                if err := json.Unmarshal(comments, &r.Comments); err != nil {
1✔
NEW
198
                        return err
×
NEW
199
                }
×
200
        }
201

202
        return nil
1✔
203
}
204

205
// ToProto returns the protobuf representation of the root node.
206
func (r *RootNode) ToProto() *ast_pb.RootSourceUnit {
1✔
207
        sourceUnits := []*ast_pb.SourceUnit{}
1✔
208
        for _, sourceUnit := range r.SourceUnits {
2✔
209
                sourceUnits = append(sourceUnits, sourceUnit.ToProto().(*ast_pb.SourceUnit))
1✔
210
        }
1✔
211

212
        comments := []*ast_pb.Comment{}
1✔
213
        for _, comment := range r.Comments {
2✔
214
                comments = append(comments, comment.ToProto())
1✔
215
        }
1✔
216

217
        globals := []*v3.TypedStruct{}
1✔
218
        for _, global := range r.Globals {
2✔
219
                globals = append(globals, global.ToProto().(*v3.TypedStruct))
1✔
220
        }
1✔
221

222
        return &ast_pb.RootSourceUnit{
1✔
223
                Id:              r.GetId(),
1✔
224
                NodeType:        r.GetType(),
1✔
225
                EntrySourceUnit: r.GetEntrySourceUnit(),
1✔
226
                GlobalNodes:     globals,
1✔
227
                SourceUnits:     sourceUnits,
1✔
228
                Comments:        comments,
1✔
229
        }
1✔
230
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc