Skip to content

Commit d6efc74

Browse files
committed
workflow: change versions in actions and wrap some errors
Signed-off-by: Murat Mirgun Ercan <muratmirgunercan225@gmail.com>
1 parent 409646e commit d6efc74

11 files changed

+59
-39
lines changed

.github/workflows/golangci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88
build:
99
runs-on: ubuntu-latest
1010
steps:
11-
- name: Set up Go 1.16
11+
- name: Set up Go 1.19
1212
uses: actions/setup-go@v2
1313
with:
14-
go-version: 1.16
14+
go-version: 1.19
1515
id: go
1616
- name: Check out code into the Go module directory
1717
uses: actions/checkout@v2

.github/workflows/reviewdog.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ jobs:
1010
- name: Set up Go
1111
uses: actions/setup-go@v2
1212
with:
13-
go-version: '1.16'
13+
go-version: '1.19'
1414
- name: golangci-lint
1515
uses: reviewdog/action-golangci-lint@v1

.pre-commit-config.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.3.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-added-large-files
9+
- repo: https://github.com/dnephin/pre-commit-golang
10+
rev: v0.5.0
11+
hooks:
12+
- id: go-fmt
13+
- id: go-imports
14+
- id: no-go-testing
15+
- id: golangci-lint
16+
- id: go-unit-tests

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Full documentation can be found [here](https://pkg.go.dev/github.com/supabase/po
1313
Install
1414

1515
```bash
16-
go get github.com/supabase/postgrest-go
16+
go get github.com/supabase-community/postgrest-go
1717
```
1818

1919
Usage

client.go

+4
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,20 @@ func (c *Client) Ping() bool {
6161
req, err := http.NewRequest("GET", path.Join(c.clientTransport.baseURL.Path, ""), nil)
6262
if err != nil {
6363
c.ClientError = err
64+
6465
return false
6566
}
6667

6768
resp, err := c.session.Do(req)
6869
if err != nil {
6970
c.ClientError = err
71+
7072
return false
7173
}
7274

7375
if resp.Status != "200 OK" {
7476
c.ClientError = errors.New("ping failed")
77+
7578
return false
7679
}
7780

@@ -157,6 +160,7 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
157160
req.Header.Add(headerName, val)
158161
}
159162
}
163+
160164
req.URL = t.baseURL.ResolveReference(req.URL)
161165
return http.DefaultTransport.RoundTrip(req)
162166
}

execute.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgrest
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
89
"net/http"
@@ -33,7 +34,7 @@ func executeHelper(client *Client, method string, body []byte, urlFragments []st
3334
baseUrl := path.Join(append([]string{client.clientTransport.baseURL.Path}, urlFragments...)...)
3435
req, err := http.NewRequest(method, baseUrl, readerBody)
3536
if err != nil {
36-
return nil, 0, err
37+
return nil, 0, fmt.Errorf("error creating request: %s", err.Error())
3738
}
3839

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

52-
respbody, err := io.ReadAll(resp.Body)
53+
respBody, err := io.ReadAll(resp.Body)
5354
if err != nil {
5455
return nil, 0, err
5556
}
5657

5758
// https://postgrest.org/en/stable/api.html#errors-and-http-status-codes
5859
if resp.StatusCode >= 400 {
5960
var errmsg *ExecuteError
60-
err := json.Unmarshal(respbody, &errmsg)
61+
err := json.Unmarshal(respBody, &errmsg)
6162
if err != nil {
62-
return nil, 0, err
63+
return nil, 0, fmt.Errorf("error parsing error response: %s", err.Error())
6364
}
6465
return nil, 0, fmt.Errorf("(%s) %s", errmsg.Code, errmsg.Message)
6566
}
@@ -72,17 +73,17 @@ func executeHelper(client *Client, method string, body []byte, urlFragments []st
7273
if len(split) > 1 && split[1] != "*" {
7374
count, err = strconv.ParseInt(split[1], 0, 64)
7475
if err != nil {
75-
return nil, 0, err
76+
return nil, 0, fmt.Errorf("error parsing count from Content-Range header: %s", err.Error())
7677
}
7778
}
7879
}
7980

8081
err = resp.Body.Close()
8182
if err != nil {
82-
return nil, 0, err
83+
return nil, 0, errors.New("error closing response body")
8384
}
8485

85-
return respbody, count, nil
86+
return respBody, count, nil
8687
}
8788

8889
func executeString(client *Client, method string, body []byte, urlFragments []string, headers map[string]string, params map[string]string) (string, countType, error) {

export_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const apiKeyEnv = "API_KEY"
1212
// If false, mock responses with httpmock. If true, use POSTGREST_URL (and
1313
// optionally, API_KEY for Supabase), to run tests against an actual Postgres
1414
// instance.
15-
var mockResponses bool = false
15+
var mockResponses = false
1616

1717
var mockPath *regexp.Regexp
1818

filterbuilder.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ type FilterBuilder struct {
2020

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

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

@@ -237,7 +237,7 @@ var DefaultOrderOpts = OrderOpts{
237237
ForeignTable: "",
238238
}
239239

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

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

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

295-
// Retrieves only one row from the result. The total result set must be one row
295+
// Single Retrieves only one row from the result. The total result set must be one row
296296
// (e.g., by using Limit). Otherwise, this will result in an error.
297297
func (f *FilterBuilder) Single() *FilterBuilder {
298298
f.headers["Accept"] = "application/vnd.pgrst.object+json"

querybuilder.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ type QueryBuilder struct {
1818

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

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

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

@@ -46,7 +46,7 @@ func (q *QueryBuilder) Select(columns, count string, head bool) *FilterBuilder {
4646
q.params["select"] = "*"
4747
} else {
4848
quoted := false
49-
var resultArr = []string{}
49+
var resultArr []string
5050
for _, char := range strings.Split(columns, "") {
5151
if char == `"` {
5252
quoted = !quoted
@@ -79,7 +79,7 @@ func (q *QueryBuilder) Insert(value interface{}, upsert bool, onConflict, return
7979
q.params["on_conflict"] = onConflict
8080
}
8181

82-
headerList := []string{}
82+
var headerList []string
8383
if upsert {
8484
headerList = append(headerList, "resolution=merge-duplicates")
8585
}

querybuilder_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ func TestNewClient(t *testing.T) {
1414
}
1515

1616
func TestSelect(t *testing.T) {
17-
assert := assert.New(t)
17+
assertions := assert.New(t)
1818
c := createClient(t)
1919

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

2323
if mockResponses {
2424
httpmock.Activate()
@@ -29,16 +29,16 @@ func TestSelect(t *testing.T) {
2929
}
3030

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

3434
err = json.Unmarshal(bs, &got)
35-
assert.NoError(err)
36-
assert.EqualValues(users, got)
37-
assert.Equal(countType(0), count)
35+
assertions.NoError(err)
36+
assertions.EqualValues(users, got)
37+
assertions.Equal(countType(0), count)
3838
})
3939

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

4343
if mockResponses {
4444
httpmock.Activate()
@@ -53,17 +53,17 @@ func TestSelect(t *testing.T) {
5353
}
5454

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

5858
err = json.Unmarshal(bs, &got)
59-
assert.NoError(err)
60-
assert.EqualValues(users, got)
61-
assert.Equal(countType(2), count)
59+
assertions.NoError(err)
60+
assertions.EqualValues(users, got)
61+
assertions.Equal(countType(2), count)
6262
})
6363
}
6464

6565
func TestFilter(t *testing.T) {
66-
assert := assert.New(t)
66+
assertions := assert.New(t)
6767
c := createClient(t)
6868

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

7979
got, _, err := c.From("users").Select("email", "", false).Eq("email", "patti@test.com").ExecuteString()
80-
assert.NoError(err)
81-
assert.Equal(want, got)
80+
assertions.NoError(err)
81+
assertions.Equal(want, got)
8282
})
8383
}

test/basic/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ var (
1919
func main() {
2020
client := postgrest.NewClient(RestUrl, schema, headers)
2121

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

2727
fmt.Println(res)
28-
fmt.Printf("count: %v", count)
2928
}

0 commit comments

Comments
 (0)