Skip to content

Commit 1191b49

Browse files
committed
refactor: use new input parser
1 parent 0c21072 commit 1191b49

File tree

4 files changed

+35
-177
lines changed

4 files changed

+35
-177
lines changed

collection.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"fmt"
1313
"net/http"
1414
"net/url"
15+
16+
"github.com/durudex/go-polybase/input"
1517
)
1618

1719
type Collection[T any] interface {
@@ -38,62 +40,61 @@ func (c *collection[T]) Get(ctx context.Context) *Response[T] {
3840
defer recoverFunc(ctx, c.client.Config().RecoverHandler)
3941

4042
req := &Request{
41-
Endpoint: fmt.Sprintf("/collections/%s/records", c.name),
43+
Endpoint: fmt.Sprintf(recordsEndpointFormat, c.name),
4244
Method: http.MethodGet,
4345
}
4446

4547
var resp Response[T]
4648

4749
if err := c.client.MakeRequest(ctx, req, &resp); err != nil {
48-
panic("error getting collection records: " + err.Error())
50+
panic("error: getting collection records: " + err.Error())
4951
}
5052

5153
return &resp
5254
}
5355

5456
func (c *collection[T]) Before(cursor string) Query[T] {
5557
return newQuery[T](c.client,
56-
fmt.Sprintf("/collections/%s/records", c.name)).Before(cursor)
58+
fmt.Sprintf(recordsEndpointFormat, c.name)).Before(cursor)
5759
}
5860

5961
func (c *collection[T]) After(cursor string) Query[T] {
6062
return newQuery[T](c.client,
61-
fmt.Sprintf("/collections/%s/records", c.name)).After(cursor)
63+
fmt.Sprintf(recordsEndpointFormat, c.name)).After(cursor)
6264
}
6365

6466
func (c *collection[T]) Limit(num int) Query[T] {
6567
return newQuery[T](c.client,
66-
fmt.Sprintf("/collections/%s/records", c.name)).Limit(num)
68+
fmt.Sprintf(recordsEndpointFormat, c.name)).Limit(num)
6769
}
6870

6971
func (c *collection[T]) Sort(field string, direction ...string) Query[T] {
7072
return newQuery[T](c.client,
71-
fmt.Sprintf("/collections/%s/records", c.name)).Sort(field, direction...)
73+
fmt.Sprintf(recordsEndpointFormat, c.name)).Sort(field, direction...)
7274
}
7375

7476
func (c *collection[T]) Where(field string, op WhereOperator, value any) Query[T] {
7577
return newQuery[T](c.client,
76-
fmt.Sprintf("/collections/%s/records", c.name)).Where(field, op, value)
78+
fmt.Sprintf(recordsEndpointFormat, c.name)).Where(field, op, value)
7779
}
7880

7981
func (c *collection[T]) Record(id string) RecordDoer[T] {
80-
return newRecordDoer[T](c.client,
81-
fmt.Sprintf("/collections/%s/records/%s", c.name, url.QueryEscape(id)))
82+
return newRecordDoer[T](c.client, c.name, id)
8283
}
8384

8485
func (c *collection[T]) Create(ctx context.Context, args ...any) *SingleResponse[T] {
8586
defer recoverFunc(ctx, c.client.Config().RecoverHandler)
8687

8788
req := &Request{
88-
Endpoint: fmt.Sprintf("/collections/%s/records", c.name),
89+
Endpoint: fmt.Sprintf(recordsEndpointFormat, c.name),
8990
Method: http.MethodPost,
90-
Body: Body{Args: ParseInput(args)},
91+
Body: Body{Args: input.Parse(args)},
9192
}
9293

9394
var resp SingleResponse[T]
9495

9596
if err := c.client.MakeRequest(ctx, req, &resp); err != nil {
96-
panic("error creating a new record: " + err.Error())
97+
panic("error: creating a new record instance: " + err.Error())
9798
}
9899

99100
return &resp

input.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

input_test.go

Lines changed: 0 additions & 73 deletions
This file was deleted.

record.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import (
1212
"fmt"
1313
"net/http"
1414
"net/url"
15+
16+
"github.com/durudex/go-polybase/input"
17+
)
18+
19+
const (
20+
recordsEndpointFormat = "/collections/%s/records"
21+
recordEndpointFormat = recordsEndpointFormat + "/%s"
1522
)
1623

1724
// Record structure stores the Polybase record.
@@ -38,11 +45,19 @@ type RecordDoer[T any] interface {
3845
type recordDoer[T any] struct {
3946
client Client
4047
endpoint string
48+
config *input.Foreign
4149
}
4250

4351
// newRecordDoer function returns a new record doer.
44-
func newRecordDoer[T any](client Client, endpoint string) RecordDoer[T] {
45-
return &recordDoer[T]{client: client, endpoint: endpoint}
52+
func newRecordDoer[T any](client Client, name, id string) RecordDoer[T] {
53+
endpoint := fmt.Sprintf(recordEndpointFormat, name, url.QueryEscape(id))
54+
config := &input.Foreign{CollectionID: name, ID: id}
55+
56+
return &recordDoer[T]{
57+
client: client,
58+
endpoint: endpoint,
59+
config: config,
60+
}
4661
}
4762

4863
// Get method sends a request to getting collection record by the specified ID and decodes
@@ -58,7 +73,7 @@ func (r *recordDoer[T]) Get(ctx context.Context) *SingleResponse[T] {
5873
var resp SingleResponse[T]
5974

6075
if err := r.client.MakeRequest(ctx, req, &resp); err != nil {
61-
panic("error getting record: " + err.Error())
76+
panic("error: getting record: " + err.Error())
6277
}
6378

6479
return &resp
@@ -71,14 +86,16 @@ func (r *recordDoer[T]) Call(ctx context.Context, fc string, args ...any) *Singl
7186
req := &Request{
7287
Endpoint: r.endpoint + fmt.Sprintf("/call/%s", url.QueryEscape(fc)),
7388
Method: http.MethodPost,
74-
Body: Body{Args: ParseInput(args)},
89+
Body: Body{Args: input.Parse(args)},
7590
}
7691

7792
var resp SingleResponse[T]
7893

7994
if err := r.client.MakeRequest(ctx, req, &resp); err != nil {
80-
panic("error call collection function: " + err.Error())
95+
panic("error: call collection function: " + err.Error())
8196
}
8297

8398
return &resp
8499
}
100+
101+
func (r *recordDoer[T]) Reference() *input.Foreign { return r.config }

0 commit comments

Comments
 (0)