Skip to content

Commit

Permalink
Merge pull request #37 from supabase-community/development
Browse files Browse the repository at this point in the history
workflow: change versions in actions and wrap some errors
  • Loading branch information
muratmirgun authored Oct 4, 2023
2 parents 2253328 + d6efc74 commit 6b8aad2
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.16
- name: Set up Go 1.19
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.19
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.16'
go-version: '1.19'
- name: golangci-lint
uses: reviewdog/action-golangci-lint@v1
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.0
hooks:
- id: go-fmt
- id: go-imports
- id: no-go-testing
- id: golangci-lint
- id: go-unit-tests
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Full documentation can be found [here](https://pkg.go.dev/github.com/supabase/po
Install

```bash
go get github.com/supabase/postgrest-go
go get github.com/supabase-community/postgrest-go
```

Usage
Expand Down
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,20 @@ func (c *Client) Ping() bool {
req, err := http.NewRequest("GET", path.Join(c.clientTransport.baseURL.Path, ""), nil)
if err != nil {
c.ClientError = err

return false
}

resp, err := c.session.Do(req)
if err != nil {
c.ClientError = err

return false
}

if resp.Status != "200 OK" {
c.ClientError = errors.New("ping failed")

return false
}

Expand Down Expand Up @@ -157,6 +160,7 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add(headerName, val)
}
}

req.URL = t.baseURL.ResolveReference(req.URL)
return http.DefaultTransport.RoundTrip(req)
}
15 changes: 8 additions & 7 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgrest
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -33,7 +34,7 @@ func executeHelper(client *Client, method string, body []byte, urlFragments []st
baseUrl := path.Join(append([]string{client.clientTransport.baseURL.Path}, urlFragments...)...)
req, err := http.NewRequest(method, baseUrl, readerBody)
if err != nil {
return nil, 0, err
return nil, 0, fmt.Errorf("error creating request: %s", err.Error())
}

for key, val := range headers {
Expand All @@ -49,17 +50,17 @@ func executeHelper(client *Client, method string, body []byte, urlFragments []st
return nil, 0, err
}

respbody, err := io.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}

// https://postgrest.org/en/stable/api.html#errors-and-http-status-codes
if resp.StatusCode >= 400 {
var errmsg *ExecuteError
err := json.Unmarshal(respbody, &errmsg)
err := json.Unmarshal(respBody, &errmsg)
if err != nil {
return nil, 0, err
return nil, 0, fmt.Errorf("error parsing error response: %s", err.Error())
}
return nil, 0, fmt.Errorf("(%s) %s", errmsg.Code, errmsg.Message)
}
Expand All @@ -72,17 +73,17 @@ func executeHelper(client *Client, method string, body []byte, urlFragments []st
if len(split) > 1 && split[1] != "*" {
count, err = strconv.ParseInt(split[1], 0, 64)
if err != nil {
return nil, 0, err
return nil, 0, fmt.Errorf("error parsing count from Content-Range header: %s", err.Error())
}
}
}

err = resp.Body.Close()
if err != nil {
return nil, 0, err
return nil, 0, errors.New("error closing response body")
}

return respbody, count, nil
return respBody, count, nil
}

func executeString(client *Client, method string, body []byte, urlFragments []string, headers map[string]string, params map[string]string) (string, countType, error) {
Expand Down
2 changes: 1 addition & 1 deletion export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const apiKeyEnv = "API_KEY"
// If false, mock responses with httpmock. If true, use POSTGREST_URL (and
// optionally, API_KEY for Supabase), to run tests against an actual Postgres
// instance.
var mockResponses bool = false
var mockResponses = false

var mockPath *regexp.Regexp

Expand Down
12 changes: 6 additions & 6 deletions filterbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ type FilterBuilder struct {

// ExecuteString runs the PostgREST query, returning the result as a JSON
// string.
func (f *FilterBuilder) ExecuteString() (string, countType, error) {
func (f *FilterBuilder) ExecuteString() (string, int64, error) {
return executeString(f.client, f.method, f.body, []string{f.tableName}, f.headers, f.params)
}

// Execute runs the PostgREST query, returning the result as a byte slice.
func (f *FilterBuilder) Execute() ([]byte, countType, error) {
func (f *FilterBuilder) Execute() ([]byte, int64, error) {
return execute(f.client, f.method, f.body, []string{f.tableName}, f.headers, f.params)
}

Expand Down Expand Up @@ -237,7 +237,7 @@ var DefaultOrderOpts = OrderOpts{
ForeignTable: "",
}

// Limits the result to the specified count.
// Limit the result to the specified count.
func (f *FilterBuilder) Limit(count int, foreignTable string) *FilterBuilder {
if foreignTable != "" {
f.params[foreignTable+".limit"] = strconv.Itoa(count)
Expand All @@ -248,7 +248,7 @@ func (f *FilterBuilder) Limit(count int, foreignTable string) *FilterBuilder {
return f
}

// Orders the result with the specified column. A pointer to an OrderOpts
// Order the result with the specified column. A pointer to an OrderOpts
// object can be supplied to specify ordering options.
func (f *FilterBuilder) Order(column string, opts *OrderOpts) *FilterBuilder {
if opts == nil {
Expand Down Expand Up @@ -280,7 +280,7 @@ func (f *FilterBuilder) Order(column string, opts *OrderOpts) *FilterBuilder {
return f
}

// Limits the result to rows within the specified range, inclusive.
// Range Limits the result to rows within the specified range, inclusive.
func (f *FilterBuilder) Range(from, to int, foreignTable string) *FilterBuilder {
if foreignTable != "" {
f.params[foreignTable+".offset"] = strconv.Itoa(from)
Expand All @@ -292,7 +292,7 @@ func (f *FilterBuilder) Range(from, to int, foreignTable string) *FilterBuilder
return f
}

// Retrieves only one row from the result. The total result set must be one row
// Single Retrieves only one row from the result. The total result set must be one row
// (e.g., by using Limit). Otherwise, this will result in an error.
func (f *FilterBuilder) Single() *FilterBuilder {
f.headers["Accept"] = "application/vnd.pgrst.object+json"
Expand Down
10 changes: 5 additions & 5 deletions querybuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ type QueryBuilder struct {

// ExecuteString runs the Postgrest query, returning the result as a JSON
// string.
func (q *QueryBuilder) ExecuteString() (string, countType, error) {
func (q *QueryBuilder) ExecuteString() (string, int64, error) {
return executeString(q.client, q.method, q.body, []string{q.tableName}, q.headers, q.params)
}

// Execute runs the Postgrest query, returning the result as a byte slice.
func (q *QueryBuilder) Execute() ([]byte, countType, error) {
func (q *QueryBuilder) Execute() ([]byte, int64, error) {
return execute(q.client, q.method, q.body, []string{q.tableName}, q.headers, q.params)
}

// ExecuteTo runs the Postgrest query, encoding the result to the supplied
// interface. Note that the argument for the to parameter should always be a
// reference to a slice.
func (q *QueryBuilder) ExecuteTo(to interface{}) (countType, error) {
func (q *QueryBuilder) ExecuteTo(to interface{}) (int64, error) {
return executeTo(q.client, q.method, q.body, to, []string{q.tableName}, q.headers, q.params)
}

Expand All @@ -46,7 +46,7 @@ func (q *QueryBuilder) Select(columns, count string, head bool) *FilterBuilder {
q.params["select"] = "*"
} else {
quoted := false
var resultArr = []string{}
var resultArr []string
for _, char := range strings.Split(columns, "") {
if char == `"` {
quoted = !quoted
Expand Down Expand Up @@ -79,7 +79,7 @@ func (q *QueryBuilder) Insert(value interface{}, upsert bool, onConflict, return
q.params["on_conflict"] = onConflict
}

headerList := []string{}
var headerList []string
if upsert {
headerList = append(headerList, "resolution=merge-duplicates")
}
Expand Down
28 changes: 14 additions & 14 deletions querybuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func TestNewClient(t *testing.T) {
}

func TestSelect(t *testing.T) {
assert := assert.New(t)
assertions := assert.New(t)
c := createClient(t)

t.Run("ValidResult", func(t *testing.T) {
got := []map[string]interface{}{}
var got []map[string]interface{}

if mockResponses {
httpmock.Activate()
Expand All @@ -29,16 +29,16 @@ func TestSelect(t *testing.T) {
}

bs, count, err := c.From("users").Select("id, name, email", "", false).Execute()
assert.NoError(err)
assertions.NoError(err)

err = json.Unmarshal(bs, &got)
assert.NoError(err)
assert.EqualValues(users, got)
assert.Equal(countType(0), count)
assertions.NoError(err)
assertions.EqualValues(users, got)
assertions.Equal(countType(0), count)
})

t.Run("WithCount", func(t *testing.T) {
got := []map[string]interface{}{}
var got []map[string]interface{}

if mockResponses {
httpmock.Activate()
Expand All @@ -53,17 +53,17 @@ func TestSelect(t *testing.T) {
}

bs, count, err := c.From("users").Select("id, name, email", "exact", false).Execute()
assert.NoError(err)
assertions.NoError(err)

err = json.Unmarshal(bs, &got)
assert.NoError(err)
assert.EqualValues(users, got)
assert.Equal(countType(2), count)
assertions.NoError(err)
assertions.EqualValues(users, got)
assertions.Equal(countType(2), count)
})
}

func TestFilter(t *testing.T) {
assert := assert.New(t)
assertions := assert.New(t)
c := createClient(t)

t.Run("Eq", func(t *testing.T) {
Expand All @@ -77,7 +77,7 @@ func TestFilter(t *testing.T) {
}

got, _, err := c.From("users").Select("email", "", false).Eq("email", "[email protected]").ExecuteString()
assert.NoError(err)
assert.Equal(want, got)
assertions.NoError(err)
assertions.Equal(want, got)
})
}
3 changes: 1 addition & 2 deletions test/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ var (
func main() {
client := postgrest.NewClient(RestUrl, schema, headers)

res, count, err := client.From("todos").Select("id,task,done", "", false).Eq("task", "that created from postgrest-go").Execute()
res, _, err := client.From("actor").Select("actor_id,first_name", "", false).ExecuteString()
if err != nil {
panic(err)
}

fmt.Println(res)
fmt.Printf("count: %v", count)
}

0 comments on commit 6b8aad2

Please sign in to comment.