Skip to content

Commit 9314865

Browse files
authored
Merge pull request #498 from mcarmonaa/improvement/uast-extract-flatten
internal/funciton: made uast_extract return a flattened array
2 parents d2abab3 + 724b027 commit 9314865

File tree

2 files changed

+50
-60
lines changed

2 files changed

+50
-60
lines changed

internal/function/uast.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ func (u *UASTExtract) String() string {
421421

422422
// Type implements the sql.Expression interface.
423423
func (u *UASTExtract) Type() sql.Type {
424-
return sql.Array(sql.Array(sql.Text))
424+
return sql.Array(sql.Text)
425425
}
426426

427427
// Eval implements the sql.Expression interface.
@@ -458,9 +458,12 @@ func (u *UASTExtract) Eval(ctx *sql.Context, row sql.Row) (out interface{}, err
458458
return nil, nil
459459
}
460460

461-
extracted := make([]interface{}, len(nodes))
462-
for i, n := range nodes {
463-
extracted[i] = extractInfo(n, key)
461+
extracted := []interface{}{}
462+
for _, n := range nodes {
463+
info := extractInfo(n, key)
464+
if len(info) > 0 {
465+
extracted = append(extracted, info...)
466+
}
464467
}
465468

466469
return extracted, nil
@@ -474,25 +477,34 @@ const (
474477
keyEndPos = "@endpos"
475478
)
476479

477-
func extractInfo(n *uast.Node, key string) []string {
478-
479-
info := []string{}
480+
func extractInfo(n *uast.Node, key string) []interface{} {
481+
var info []interface{}
480482
switch key {
481483
case keyType:
482-
info = append(info, n.InternalType)
484+
if n.InternalType != "" {
485+
info = append(info, n.InternalType)
486+
}
483487
case keyToken:
484-
info = append(info, n.Token)
485-
case keyRoles:
486-
roles := make([]string, len(n.Roles))
487-
for i, rol := range n.Roles {
488-
roles[i] = rol.String()
488+
if n.Token != "" {
489+
info = append(info, n.Token)
489490
}
491+
case keyRoles:
492+
if len(n.Roles) > 0 {
493+
roles := make([]interface{}, len(n.Roles))
494+
for i, rol := range n.Roles {
495+
roles[i] = rol.String()
496+
}
490497

491-
info = append(info, roles...)
498+
info = append(info, roles...)
499+
}
492500
case keyStartPos:
493-
info = append(info, n.StartPosition.String())
501+
if n.StartPosition != nil {
502+
info = append(info, n.StartPosition.String())
503+
}
494504
case keyEndPos:
495-
info = append(info, n.EndPosition.String())
505+
if n.StartPosition != nil {
506+
info = append(info, n.EndPosition.String())
507+
}
496508
default:
497509
v, ok := n.Properties[key]
498510
if ok {

internal/function/uast_test.go

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -162,89 +162,67 @@ func TestUASTExtract(t *testing.T) {
162162
name: "key_" + keyType,
163163
key: keyType,
164164
expected: []interface{}{
165-
[]string{"FunctionDef"},
166-
[]string{"Name"},
167-
[]string{"Name"},
168-
[]string{"Name"},
169-
[]string{"Name"},
165+
"FunctionDef", "Name", "Name", "Name", "Name",
170166
},
171167
},
172168
{
173169
name: "key_" + keyToken,
174170
key: keyToken,
175171
expected: []interface{}{
176-
[]string{"sum"},
177-
[]string{"a"},
178-
[]string{"b"},
179-
[]string{"print"},
180-
[]string{"sum"},
172+
"sum", "a", "b", "print", "sum",
181173
},
182174
},
183175
{
184176
name: "key_" + keyRoles,
185177
key: keyRoles,
186178
expected: []interface{}{
187-
[]string{"Unannotated", "Function", "Declaration", "Name", "Identifier"},
188-
[]string{"Unannotated", "Identifier", "Expression", "Binary", "Left"},
189-
[]string{"Unannotated", "Identifier", "Expression", "Binary", "Right"},
190-
[]string{"Unannotated", "Identifier", "Expression", "Call", "Callee"},
191-
[]string{"Unannotated", "Identifier", "Expression", "Call", "Callee"},
179+
"Unannotated", "Function", "Declaration", "Name", "Identifier",
180+
"Unannotated", "Identifier", "Expression", "Binary", "Left",
181+
"Unannotated", "Identifier", "Expression", "Binary", "Right",
182+
"Unannotated", "Identifier", "Expression", "Call", "Callee",
183+
"Unannotated", "Identifier", "Expression", "Call", "Callee",
192184
},
193185
},
194186
{
195187
name: "key_" + keyStartPos,
196188
key: keyStartPos,
197189
expected: []interface{}{
198-
[]string{"Offset:28 Line:4 Col:5 "},
199-
[]string{"Offset:47 Line:5 Col:9 "},
200-
[]string{"Offset:51 Line:5 Col:13 "},
201-
[]string{"Offset:54 Line:7 Col:1 "},
202-
[]string{"Offset:60 Line:7 Col:7 "},
190+
"Offset:28 Line:4 Col:5 ",
191+
"Offset:47 Line:5 Col:9 ",
192+
"Offset:51 Line:5 Col:13 ",
193+
"Offset:54 Line:7 Col:1 ",
194+
"Offset:60 Line:7 Col:7 ",
203195
},
204196
},
205197
{
206198
name: "key_" + keyEndPos,
207199
key: keyEndPos,
208200
expected: []interface{}{
209-
[]string{"Offset:31 Line:4 Col:8 "},
210-
[]string{"Offset:48 Line:5 Col:10 "},
211-
[]string{"Offset:52 Line:5 Col:14 "},
212-
[]string{"Offset:59 Line:7 Col:6 "},
213-
[]string{"Offset:63 Line:7 Col:10 "},
201+
"Offset:31 Line:4 Col:8 ",
202+
"Offset:48 Line:5 Col:10 ",
203+
"Offset:52 Line:5 Col:14 ",
204+
"Offset:59 Line:7 Col:6 ",
205+
"Offset:63 Line:7 Col:10 ",
214206
},
215207
},
216208
{
217209
name: "key_internalRole",
218210
key: "internalRole",
219211
expected: []interface{}{
220-
[]string{"body"},
221-
[]string{"left"},
222-
[]string{"right"},
223-
[]string{"func"},
224-
[]string{"func"},
212+
"body", "left", "right", "func", "func",
225213
},
226214
},
227215
{
228216
name: "key_ctx",
229217
key: "ctx",
230218
expected: []interface{}{
231-
[]string{},
232-
[]string{"Load"},
233-
[]string{"Load"},
234-
[]string{"Load"},
235-
[]string{"Load"},
219+
"Load", "Load", "Load", "Load",
236220
},
237221
},
238222
{
239-
name: "key_foo",
240-
key: "foo",
241-
expected: []interface{}{
242-
[]string{},
243-
[]string{},
244-
[]string{},
245-
[]string{},
246-
[]string{},
247-
},
223+
name: "key_foo",
224+
key: "foo",
225+
expected: nil,
248226
},
249227
}
250228

0 commit comments

Comments
 (0)