Skip to content

Commit

Permalink
Add enry functions (#140)
Browse files Browse the repository at this point in the history
* added enry_detect_language

First of many. Pushing for review. How's it look?

* updated changes to function file

forgot to save the file, so these didn't get pushed

* update to error handling

* added return

* added all enry funcitons

Problem: IsTest won't register for some reason(EnryIsTest doesn't show under registered objects, "make" returns error: "tables/tables.go:63:31: undefined: enry.EnryIsTest")

* commented EnryIsTest out for now

* modified directory, minor if statement changes

* updated tests for each function

enry_is_binary needs work

* spelled enry correctly

* updated enry_is_binary_test.go

input filecontents not filepath now

* some formatting fixes

* add some documentation to the README

Co-authored-by: Patrick DeVivo <[email protected]>
  • Loading branch information
vaibhavnshah and patrickdevivo authored Jul 27, 2021
1 parent 32ba86d commit 96f3c4e
Show file tree
Hide file tree
Showing 25 changed files with 505 additions and 11 deletions.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,82 @@ SELECT str_split('hello,world', ',', 1)
-- +----------------------------------+
```

#### Enry Functions

Functions from the [`enry` project](https://github.com/go-enry/go-enry) are also available as SQL scalar functions

##### `enry_detect_language`

Supply a file path and some source code to detect the language.

```sql
SELECT enry_detect_language('some/path/to/file.go', '<contents of file>')
```

##### `enry_is_binary`

Given a blob, determine if it's a binary file or not (returns 1 or 0).

```sql
SELECT enry_is_binary('<contents of file>')
```

##### `enry_is_configuration`

Detect whether a file path is to a configuration file (returns 1 or 0).

```sql
SELECT enry_is_configuration('some/path/to/file/config.json')
```

##### `enry_is_documentation`

Detect whether a file path is to a documentation file (returns 1 or 0).

```sql
SELECT enry_is_documentation('some/path/to/file/README.md')
```

##### `enry_is_dot_file`

Detect whether a file path is to a dot file (returns 1 or 0).

```sql
SELECT enry_is_dot_file('some/path/to/file/.gitignore')
```

##### `enry_is_generated`

Detect whether a file path is generated (returns 1 or 0).

```sql
SELECT enry_is_generated('some/path/to/file/generated.go', '<contents of file>')
```

##### `enry_is_image`

Detect whether a file path is to an image (returns 1 or 0).

```sql
SELECT enry_is_image('some/path/to/file/image.png')
```

##### `enry_is_test`

Detect whether a file path is to a test file (returns 1 or 0).

```sql
SELECT enry_is_test('some/path/to/file/image.png')
```

##### `enry_is_vendor`

Detect whether a file path is to a vendored file (returns 1 or 0).

```sql
SELECT enry_is_vendor('vendor/file.go')
```


### Example Queries

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/augmentable-dev/vtab v0.0.0-20210717200339-0c8dcfe2033b
github.com/clbanning/mxj/v2 v2.5.5
github.com/ghodss/yaml v1.0.0
github.com/go-enry/go-enry/v2 v2.7.1
github.com/go-git/go-billy/v5 v5.3.1
github.com/go-git/go-git/v5 v5.4.2
github.com/go-openapi/errors v0.20.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-enry/go-enry/v2 v2.7.1 h1:WCqtfyteIz61GYk9lRVy8HblvIv4cP9GIiwm/6txCbU=
github.com/go-enry/go-enry/v2 v2.7.1/go.mod h1:GVzIiAytiS5uT/QiuakK7TF1u4xDab87Y8V5EJRpsIQ=
github.com/go-enry/go-oniguruma v1.2.1 h1:k8aAMuJfMrqm/56SG2lV9Cfti6tC4x8673aHCcBk+eo=
github.com/go-enry/go-oniguruma v1.2.1/go.mod h1:bWDhYP+S6xZQgiRL7wlTScFYBe023B6ilRZbCAD5Hf4=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
Expand Down
19 changes: 19 additions & 0 deletions tables/internal/funcs/enry_detect_language.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package funcs

import (
"github.com/go-enry/go-enry/v2"
"go.riyazali.net/sqlite"
)

type EnryDetectLanguage struct{}

func (f *EnryDetectLanguage) Args() int { return 2 }
func (f *EnryDetectLanguage) Deterministic() bool { return true }
func (f *EnryDetectLanguage) Apply(context *sqlite.Context, value ...sqlite.Value) {
if lang := enry.GetLanguage(value[0].Text(), value[1].Blob()); lang == "" {
context.ResultNull()
return
} else {
context.ResultText(lang)
}
}
29 changes: 29 additions & 0 deletions tables/internal/funcs/enry_detect_language_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package funcs

import (
"io/ioutil"
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
)

func TestEnryDetectLanguage(t *testing.T) {
path := "./testdata/main.go"
fileContents, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
rows, err := FixtureDatabase.Query("SELECT enry_detect_language(?,?)", path, fileContents)
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row %d", err, rowNum)
}

if contents[0][0] != "Go" {
t.Fatalf("expected string: %s, got %s", "Go", contents[0][0])
}
}
18 changes: 18 additions & 0 deletions tables/internal/funcs/enry_is_binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package funcs

import (
"github.com/go-enry/go-enry/v2"
"go.riyazali.net/sqlite"
)

type EnryIsBinary struct{}

func (f *EnryIsBinary) Args() int { return 1 }
func (f *EnryIsBinary) Deterministic() bool { return true }
func (f *EnryIsBinary) Apply(context *sqlite.Context, value ...sqlite.Value) {
if enry.IsBinary(value[0].Blob()) {
context.ResultInt(1)
} else {
context.ResultInt(0)
}
}
29 changes: 29 additions & 0 deletions tables/internal/funcs/enry_is_binary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package funcs

import (
"io/ioutil"
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
)

func TestEnryIsBinary(t *testing.T) {
path := "./testdata/binary"
fileContents, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
rows, err := FixtureDatabase.Query("SELECT enry_is_binary(?)", fileContents)
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row %d", err, rowNum)
}

if contents[0][0] != "1" {
t.Fatalf("expected string: %s, got %s", "1", contents[0][0])
}
}
18 changes: 18 additions & 0 deletions tables/internal/funcs/enry_is_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package funcs

import (
"github.com/go-enry/go-enry/v2"
"go.riyazali.net/sqlite"
)

type EnryIsConfiguration struct{}

func (f *EnryIsConfiguration) Args() int { return 1 }
func (f *EnryIsConfiguration) Deterministic() bool { return true }
func (f *EnryIsConfiguration) Apply(context *sqlite.Context, value ...sqlite.Value) {
if enry.IsConfiguration(value[0].Text()) {
context.ResultInt(1)
} else {
context.ResultInt(0)
}
}
24 changes: 24 additions & 0 deletions tables/internal/funcs/enry_is_configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package funcs

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
)

func TestEnryIsConfiguration(t *testing.T) {
path := "./testdata/configuration.json" // from here -> https://github.com/go-enry/go-enry/tree/master/_testdata
rows, err := FixtureDatabase.Query("SELECT enry_is_configuration(?)", path)
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row %d", err, rowNum)
}

if contents[0][0] != "1" {
t.Fatalf("expected string: %s, got %s", "1", contents[0][0])
}
}
18 changes: 18 additions & 0 deletions tables/internal/funcs/enry_is_documentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package funcs

import (
"github.com/go-enry/go-enry/v2"
"go.riyazali.net/sqlite"
)

type EnryIsDocumentation struct{}

func (f *EnryIsDocumentation) Args() int { return 1 }
func (f *EnryIsDocumentation) Deterministic() bool { return true }
func (f *EnryIsDocumentation) Apply(context *sqlite.Context, value ...sqlite.Value) {
if enry.IsDocumentation(value[0].Text()) {
context.ResultInt(1)
} else {
context.ResultInt(0)
}
}
24 changes: 24 additions & 0 deletions tables/internal/funcs/enry_is_documentation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package funcs

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
)

func TestEnryIsDocumentation(t *testing.T) {
path := "./README.md"
rows, err := FixtureDatabase.Query("SELECT enry_is_documentation(?)", path)
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row %d", err, rowNum)
}

if contents[0][0] != "1" {
t.Fatalf("expected string: %s, got %s", "1", contents[0][0])
}
}
18 changes: 18 additions & 0 deletions tables/internal/funcs/enry_is_dot_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package funcs

import (
"github.com/go-enry/go-enry/v2"
"go.riyazali.net/sqlite"
)

type EnryIsDotFile struct{}

func (f *EnryIsDotFile) Args() int { return 1 }
func (f *EnryIsDotFile) Deterministic() bool { return true }
func (f *EnryIsDotFile) Apply(context *sqlite.Context, value ...sqlite.Value) {
if enry.IsDotFile(value[0].Text()) {
context.ResultInt(1)
} else {
context.ResultInt(0)
}
}
24 changes: 24 additions & 0 deletions tables/internal/funcs/enry_is_dot_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package funcs

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
)

func TestEnryIsDotFile(t *testing.T) {
path := "./testdata/.hidden"
rows, err := FixtureDatabase.Query("SELECT enry_is_dot_file(?)", path)
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row %d", err, rowNum)
}

if contents[0][0] != "1" {
t.Fatalf("expected string: %s, got %s", "1", contents[0][0])
}
}
18 changes: 18 additions & 0 deletions tables/internal/funcs/enry_is_generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package funcs

import (
"github.com/go-enry/go-enry/v2"
"go.riyazali.net/sqlite"
)

type EnryIsGenerated struct{}

func (f *EnryIsGenerated) Args() int { return 2 }
func (f *EnryIsGenerated) Deterministic() bool { return true }
func (f *EnryIsGenerated) Apply(context *sqlite.Context, value ...sqlite.Value) {
if enry.IsGenerated(value[0].Text(), value[1].Blob()) {
context.ResultInt(1)
} else {
context.ResultInt(0)
}
}
24 changes: 24 additions & 0 deletions tables/internal/funcs/enry_is_generated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package funcs

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
)

func TestEnryIsGenerated(t *testing.T) {
path := ".xcworkspacedata"
rows, err := FixtureDatabase.Query("SELECT enry_is_generated(?, ?)", path, "")
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row %d", err, rowNum)
}

if contents[0][0] != "1" {
t.Fatalf("expected string: %s, got %s", "1", contents[0][0])
}
}
18 changes: 18 additions & 0 deletions tables/internal/funcs/enry_is_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package funcs

import (
"github.com/go-enry/go-enry/v2"
"go.riyazali.net/sqlite"
)

type EnryIsImage struct{}

func (f *EnryIsImage) Args() int { return 1 }
func (f *EnryIsImage) Deterministic() bool { return true }
func (f *EnryIsImage) Apply(context *sqlite.Context, value ...sqlite.Value) {
if enry.IsImage(value[0].Text()) {
context.ResultInt(1)
} else {
context.ResultInt(0)
}
}
Loading

0 comments on commit 96f3c4e

Please sign in to comment.