Skip to content

Commit 43c2c25

Browse files
authored
internal/function: use custom fixed struct as LOC result (#931)
internal/function: use custom fixed struct as LOC result
2 parents 0b9806b + d1cf933 commit 43c2c25

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

docs/using-gitbase/functions.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)