Skip to content

Commit 52014be

Browse files
committed
gitbase, function: add uast_mode function
uast function now returns semantic uast Signed-off-by: Javi Fontan <[email protected]>
1 parent 1a10b31 commit 52014be

File tree

5 files changed

+303
-189
lines changed

5 files changed

+303
-189
lines changed

docs/using-gitbase/functions.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
To make some common tasks easier for the user, there are some functions to interact with the aforementioned tables:
66

7-
| Name | Description |
8-
|:-------------|:----------------------------------------------------------------------------------------------------|
9-
|is_remote(reference_name)bool| check if the given reference name is from a remote one |
10-
|is_tag(reference_name)bool| check if the given reference name is a tag |
11-
|language(path, [blob])text| gets the language of a file given its path and the optional content of the file |
12-
|uast(blob, [lang, [xpath, [mode]]])json_blob| returns an array of UAST nodes as blobs, you can specify a mode (annotated, semantic or native) |
13-
|uast_xpath(json_blob, xpath)| performs an XPath query over the given UAST nodes |
7+
| Name | Description |
8+
|:-------------|:-------------------------------------------------------------------------------------------------------------------------------|
9+
|is_remote(reference_name)bool| check if the given reference name is from a remote one |
10+
|is_tag(reference_name)bool| check if the given reference name is a tag |
11+
|language(path, [blob])text| gets the language of a file given its path and the optional content of the file |
12+
|uast(blob, [lang, [xpath]])json_blob| returns an array of UAST nodes as blobs in semantic mode |
13+
|uast_mode(blob, lang, mode)json_blob| returns an array of UAST nodes as blobs specifying its language and mode (semantic, annotated or native) |
14+
|uast_xpath(json_blob, xpath)| performs an XPath query over the given UAST nodes |
1415

1516
## Standard functions
1617

integration_test.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,26 +261,56 @@ func TestIntegration(t *testing.T) {
261261
}
262262

263263
func TestUastQueries(t *testing.T) {
264-
require := require.New(t)
265-
266264
engine, pool, cleanup := setup(t)
267265
defer cleanup()
268266

269-
session := gitbase.NewSession(pool)
270-
ctx := sql.NewContext(context.TODO(), sql.WithSession(session))
271-
_, iter, err := engine.Query(ctx, `
272-
SELECT uast_xpath(uast(blob_content, language(tree_entry_name, blob_content)), '//*[@roleIdentifier]') as uast,
267+
testCases := []struct {
268+
query string
269+
rows int
270+
}{
271+
{`SELECT uast_xpath(uast(blob_content, language(tree_entry_name, blob_content)), '//Identifier') as uast,
273272
tree_entry_name
274273
FROM tree_entries te
275274
INNER JOIN blobs b
276275
ON b.blob_hash = te.blob_hash
277-
WHERE te.tree_entry_name = 'crappy.php'`,
278-
)
279-
require.NoError(err)
276+
WHERE te.tree_entry_name = 'example.go'`, 1},
277+
{`SELECT uast_xpath(uast_mode(blob_content, language(tree_entry_name, blob_content), 'semantic'), '//Identifier') as uast,
278+
tree_entry_name
279+
FROM tree_entries te
280+
INNER JOIN blobs b
281+
ON b.blob_hash = te.blob_hash
282+
WHERE te.tree_entry_name = 'example.go'`, 1},
283+
{`SELECT uast_xpath(uast_mode(blob_content, language(tree_entry_name, blob_content), 'annotated'), '//*[@roleIdentifier]') as uast,
284+
tree_entry_name
285+
FROM tree_entries te
286+
INNER JOIN blobs b
287+
ON b.blob_hash = te.blob_hash
288+
WHERE te.tree_entry_name = 'example.go'`, 1},
289+
{`SELECT uast_xpath(uast_mode(blob_content, language(tree_entry_name, blob_content), 'native'), '//*[@ast_type=\'FunctionDef\']') as uast,
290+
tree_entry_name
291+
FROM tree_entries te
292+
INNER JOIN blobs b
293+
ON b.blob_hash = te.blob_hash
294+
WHERE te.tree_entry_name = 'example.go'`, 1},
295+
}
280296

281-
rows, err := sql.RowIterToRows(iter)
282-
require.NoError(err)
283-
require.Len(rows, 1)
297+
_ = testCases
298+
299+
for _, c := range testCases {
300+
t.Run(c.query, func(t *testing.T) {
301+
require := require.New(t)
302+
303+
session := gitbase.NewSession(pool)
304+
ctx := sql.NewContext(context.TODO(), sql.WithSession(session))
305+
306+
_, iter, err := engine.Query(ctx, c.query)
307+
require.NoError(err)
308+
309+
rows, err := sql.RowIterToRows(iter)
310+
require.NoError(err)
311+
require.Len(rows, c.rows)
312+
})
313+
}
284314
}
285315

286316
func TestSquashCorrectness(t *testing.T) {

internal/function/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ var Functions = sql.Functions{
88
"is_remote": sql.Function1(NewIsRemote),
99
"language": sql.FunctionN(NewLanguage),
1010
"uast": sql.FunctionN(NewUAST),
11+
"uast_mode": sql.Function3(NewUASTMode),
1112
"uast_xpath": sql.Function2(NewUASTXPath),
1213
}

0 commit comments

Comments
 (0)