Skip to content

Commit 482f4a3

Browse files
committed
internal/function: registry of functions
Signed-off-by: Miguel Molina <[email protected]>
1 parent 130bd9b commit 482f4a3

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

cmd/gitquery/query_base.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/src-d/gitquery"
88
"github.com/src-d/gitquery/internal/format"
9+
"github.com/src-d/gitquery/internal/function"
910

1011
"gopkg.in/src-d/go-git.v4/utils/ioutil"
1112
sqle "gopkg.in/src-d/go-mysql-server.v0"
@@ -38,7 +39,7 @@ func (c *cmdQueryBase) buildDatabase() error {
3839
}
3940

4041
c.engine.AddDatabase(gitquery.NewDatabase(c.name, &pool))
41-
return err
42+
return function.Register(c.engine.Catalog)
4243
}
4344

4445
func (c *cmdQueryBase) executeQuery(sql string) (sql.Schema, sql.RowIter, error) {

internal/function/registry.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package function
2+
3+
import "gopkg.in/src-d/go-mysql-server.v0/sql"
4+
5+
var functions = map[string]interface{}{
6+
"is_tag": NewIsTag,
7+
"is_remote": NewIsRemote,
8+
}
9+
10+
// Register all the gitquery functions in the SQL catalog.
11+
func Register(c *sql.Catalog) error {
12+
for k, v := range functions {
13+
if err := c.RegisterFunction(k, v); err != nil {
14+
return err
15+
}
16+
}
17+
18+
return nil
19+
}

internal/function/registry_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package function
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"gopkg.in/src-d/go-mysql-server.v0/sql"
8+
)
9+
10+
func TestRegister(t *testing.T) {
11+
require := require.New(t)
12+
catalog := sql.NewCatalog()
13+
require.NoError(Register(catalog))
14+
15+
for fn := range functions {
16+
_, err := catalog.Function(fn)
17+
require.NoError(err, "expected to find function: %s", fn)
18+
}
19+
}

0 commit comments

Comments
 (0)