Skip to content

Commit 1a89332

Browse files
committed
internal/function: use custom fixed struct as LOC result
Signed-off-by: Miguel Molina <[email protected]>
1 parent 43982f9 commit 1a89332

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

docs/using-gitbase/examples.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ GROUP BY committer_email,
8181
```sql
8282
SELECT
8383
LANGUAGE(file_path, blob_content) as lang,
84-
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.Code')) as code,
85-
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.Comments')) as comments,
86-
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.Blanks')) as blanks,
84+
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.code')) as code,
85+
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.comments')) as comments,
86+
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.blanks')) as blanks,
8787
COUNT(1) as files
8888
FROM commit_files
8989
NATURAL JOIN refs

docs/using-gitbase/functions.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ To make some common tasks easier for the user, there are some functions to inter
1717
|`uast_xpath(blob, xpath) blob`| performs an XPath query over the given UAST nodes |
1818
|`uast_extract(blob, key) text array`| extracts information identified by the given key from the uast nodes |
1919
|`uast_children(blob) blob`| returns a flattened array of the children UAST nodes from each one of the UAST nodes in the given array |
20-
|`loc(path, blob) json`| returns a JSON map, containing the lines of code of a file, separated in three categories: Code, Blank and Comment lines |
20+
|`loc(path, blob) json`| returns a JSON map, containing the lines of code of a file, separated in three categories: code, blank and comment lines |
2121
|`version() text`| returns the gitbase version in the following format `8.0.11-{GITBASE_VERSION}` for compatibility with MySQL versioning |
2222
## Standard functions
2323

@@ -169,6 +169,26 @@ Also, if you want to retrieve values from a non common property, you can pass it
169169

170170
> uast_extract(nodes_column, 'some-property')
171171
172+
## How to use `loc`
173+
174+
`loc` will return statistics about the lines of code in a file, such as the code lines, comment lines, etc.
175+
176+
It requires a file path and a file content.
177+
178+
> loc(file_path, blob_content)
179+
180+
The result of this function is a JSON document with the following shape:
181+
182+
```
183+
{
184+
"Code": code lines,
185+
"Comment": comment lines,
186+
"Blank": blank lines,
187+
"Name": file name,
188+
"Lang": language
189+
}
190+
```
191+
172192
## How to use `commit_file_stats`
173193

174194
`commit_file_stats` will return statistics about the line changes in all files in the given range of commits classifying them in 4 categories: code, comments, blank lines and other.

internal/function/loc.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ func (f *LOC) WithChildren(children ...sql.Expression) (sql.Expression, error) {
5252
return NewLOC(children...)
5353
}
5454

55+
// LocFile is the result of the LOC function for each file.
56+
type LocFile struct {
57+
Code int32 `json:"Code"`
58+
Comments int32 `json:"Comment"`
59+
Blanks int32 `json:"Blank"`
60+
Name string `json:"Name"`
61+
Lang string `json:"Language"`
62+
}
63+
5564
// Eval implements the Expression interface.
5665
func (f *LOC) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
5766
span, ctx := ctx.Span("gitbase.LOC")
@@ -70,11 +79,19 @@ func (f *LOC) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
7079
return nil, nil
7180
}
7281

73-
return gocloc.AnalyzeReader(
82+
file := gocloc.AnalyzeReader(
7483
path,
7584
languages.Langs[lang],
7685
bytes.NewReader(blob), &gocloc.ClocOptions{},
77-
), nil
86+
)
87+
88+
return LocFile{
89+
Code: file.Code,
90+
Comments: file.Comments,
91+
Blanks: file.Blanks,
92+
Name: file.Name,
93+
Lang: file.Lang,
94+
}, nil
7895
}
7996

8097
func (f *LOC) getInputValues(ctx *sql.Context, row sql.Row) (string, []byte, error) {

internal/function/loc_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package function
33
import (
44
"testing"
55

6-
"github.com/hhatto/gocloc"
7-
"github.com/stretchr/testify/require"
8-
"gopkg.in/src-d/go-errors.v1"
96
"github.com/src-d/go-mysql-server/sql"
107
"github.com/src-d/go-mysql-server/sql/expression"
8+
"github.com/stretchr/testify/require"
9+
"gopkg.in/src-d/go-errors.v1"
1110
)
1211

1312
func TestLoc(t *testing.T) {
@@ -22,7 +21,7 @@ func TestLoc(t *testing.T) {
2221
{"too few args given", sql.NewRow("foo.foobar"), nil, nil},
2322
{"too many args given", sql.NewRow("foo.rb", "bar", "baz"), nil, sql.ErrInvalidArgumentNumber},
2423
{"invalid blob type given", sql.NewRow("foo", 5), nil, sql.ErrInvalidType},
25-
{"path and blob are given", sql.NewRow("foo", "#!/usr/bin/env python\n\nprint 'foo'"), &gocloc.ClocFile{
24+
{"path and blob are given", sql.NewRow("foo", "#!/usr/bin/env python\n\nprint 'foo'"), LocFile{
2625
Code: 2, Comments: 0, Blanks: 1, Name: "foo", Lang: "Python",
2726
}, nil},
2827
}

0 commit comments

Comments
 (0)