build: move to any from interface{}
This commit is contained in:
@@ -94,7 +94,7 @@ if err != nil {
|
|||||||
recordsToSend := &airtable.Records{
|
recordsToSend := &airtable.Records{
|
||||||
Records: []*airtable.Record{
|
Records: []*airtable.Record{
|
||||||
{
|
{
|
||||||
Fields: map[string]interface{}{
|
Fields: map[string]any{
|
||||||
"Field1": "value1",
|
"Field1": "value1",
|
||||||
"Field2": true,
|
"Field2": true,
|
||||||
},
|
},
|
||||||
@@ -121,7 +121,7 @@ if err != nil {
|
|||||||
To partial update one record
|
To partial update one record
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
res, err := record.UpdateRecordPartial(map[string]interface{}{"Field_2": false})
|
res, err := record.UpdateRecordPartial(map[string]any{"Field_2": false})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Handle error
|
// Handle error
|
||||||
}
|
}
|
||||||
@@ -133,13 +133,13 @@ To full update records
|
|||||||
toUpdateRecords := &airtable.Records{
|
toUpdateRecords := &airtable.Records{
|
||||||
Records: []*airtable.Record{
|
Records: []*airtable.Record{
|
||||||
{
|
{
|
||||||
Fields: map[string]interface{}{
|
Fields: map[string]any{
|
||||||
"Field1": "value1",
|
"Field1": "value1",
|
||||||
"Field2": true,
|
"Field2": true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Fields: map[string]interface{}{
|
Fields: map[string]any{
|
||||||
"Field1": "value1",
|
"Field1": "value1",
|
||||||
"Field2": true,
|
"Field2": true,
|
||||||
},
|
},
|
||||||
|
|||||||
10
base.go
10
base.go
@@ -19,11 +19,11 @@ type Bases struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Field struct {
|
type Field struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Options map[string]interface{} `json:"options"`
|
Options map[string]any `json:"options"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type View struct {
|
type View struct {
|
||||||
|
|||||||
12
client.go
12
client.go
@@ -77,7 +77,7 @@ func (at *Client) rateLimit() {
|
|||||||
<-at.rateLimiter
|
<-at.rateLimiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (at *Client) get(ctx context.Context, db, table, recordID string, params url.Values, target interface{}) error {
|
func (at *Client) get(ctx context.Context, db, table, recordID string, params url.Values, target any) error {
|
||||||
at.rateLimit()
|
at.rateLimit()
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
||||||
@@ -103,7 +103,7 @@ func (at *Client) get(ctx context.Context, db, table, recordID string, params ur
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (at *Client) post(ctx context.Context, db, table string, data, response interface{}) error {
|
func (at *Client) post(ctx context.Context, db, table string, data, response any) error {
|
||||||
at.rateLimit()
|
at.rateLimit()
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
||||||
@@ -124,7 +124,7 @@ func (at *Client) post(ctx context.Context, db, table string, data, response int
|
|||||||
return at.do(req, response)
|
return at.do(req, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (at *Client) delete(ctx context.Context, db, table string, recordIDs []string, target interface{}) error {
|
func (at *Client) delete(ctx context.Context, db, table string, recordIDs []string, target any) error {
|
||||||
at.rateLimit()
|
at.rateLimit()
|
||||||
|
|
||||||
rawURL := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
rawURL := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
||||||
@@ -152,7 +152,7 @@ func (at *Client) delete(ctx context.Context, db, table string, recordIDs []stri
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (at *Client) patch(ctx context.Context, db, table, data, response interface{}) error {
|
func (at *Client) patch(ctx context.Context, db, table, data, response any) error {
|
||||||
at.rateLimit()
|
at.rateLimit()
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
||||||
@@ -173,7 +173,7 @@ func (at *Client) patch(ctx context.Context, db, table, data, response interface
|
|||||||
return at.do(req, response)
|
return at.do(req, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (at *Client) put(ctx context.Context, db, table, data, response interface{}) error {
|
func (at *Client) put(ctx context.Context, db, table, data, response any) error {
|
||||||
at.rateLimit()
|
at.rateLimit()
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
|
||||||
@@ -194,7 +194,7 @@ func (at *Client) put(ctx context.Context, db, table, data, response interface{}
|
|||||||
return at.do(req, response)
|
return at.do(req, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (at *Client) do(req *http.Request, response interface{}) error {
|
func (at *Client) do(req *http.Request, response any) error {
|
||||||
if req == nil {
|
if req == nil {
|
||||||
return errors.New("nil request")
|
return errors.New("nil request")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const (
|
|||||||
|
|
||||||
var ErrNotDateTime = errors.New("field is not date time")
|
var ErrNotDateTime = errors.New("field is not date time")
|
||||||
|
|
||||||
func ToDateTime(field interface{}) (time.Time, error) {
|
func ToDateTime(field any) (time.Time, error) {
|
||||||
fS, err := field.(string)
|
fS, err := field.(string)
|
||||||
if !err {
|
if !err {
|
||||||
return time.Time{}, ErrNotDateTime
|
return time.Time{}, ErrNotDateTime
|
||||||
@@ -24,6 +24,6 @@ func ToDateTime(field interface{}) (time.Time, error) {
|
|||||||
return time.Parse(dateTimeFormat, fS)
|
return time.Parse(dateTimeFormat, fS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromDateTime(t time.Time) interface{} {
|
func FromDateTime(t time.Time) any {
|
||||||
return t.Format(dateTimeFormat)
|
return t.Format(dateTimeFormat)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,13 +14,13 @@ import (
|
|||||||
func TestToDateTime(t *testing.T) {
|
func TestToDateTime(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
field interface{}
|
field any
|
||||||
want time.Time
|
want time.Time
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{"not string", interface{}(1), time.Time{}, true},
|
{"not string", any(1), time.Time{}, true},
|
||||||
{"string not time", interface{}("hello"), time.Time{}, true},
|
{"string not time", any("hello"), time.Time{}, true},
|
||||||
{"string time", interface{}("2022-03-24T11:12:13.000Z"), time.Date(2022, 0o3, 24, 11, 12, 13, 0, time.UTC), false},
|
{"string time", any("2022-03-24T11:12:13.000Z"), time.Date(2022, 0o3, 24, 11, 12, 13, 0, time.UTC), false},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@@ -40,9 +40,9 @@ func TestFromDateTime(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
t time.Time
|
t time.Time
|
||||||
want interface{}
|
want any
|
||||||
}{
|
}{
|
||||||
{"positive", time.Date(2022, 0o3, 24, 11, 12, 13, 1, time.UTC), interface{}("2022-03-24T11:12:13.000Z")},
|
{"positive", time.Date(2022, 0o3, 24, 11, 12, 13, 1, time.UTC), any("2022-03-24T11:12:13.000Z")},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module github.com/mehanizm/airtable
|
module github.com/mehanizm/airtable
|
||||||
|
|
||||||
go 1.19
|
go 1.20
|
||||||
|
|||||||
12
record.go
12
record.go
@@ -14,10 +14,10 @@ import (
|
|||||||
type Record struct {
|
type Record struct {
|
||||||
client *Client
|
client *Client
|
||||||
table *Table
|
table *Table
|
||||||
ID string `json:"id,omitempty"`
|
ID string `json:"id,omitempty"`
|
||||||
Fields map[string]interface{} `json:"fields"`
|
Fields map[string]any `json:"fields"`
|
||||||
CreatedTime string `json:"createdTime,omitempty"`
|
CreatedTime string `json:"createdTime,omitempty"`
|
||||||
Deleted bool `json:"deleted,omitempty"`
|
Deleted bool `json:"deleted,omitempty"`
|
||||||
|
|
||||||
// The Airtable API will perform best-effort automatic data conversion
|
// The Airtable API will perform best-effort automatic data conversion
|
||||||
// from string values if the typecast parameter is passed in.
|
// from string values if the typecast parameter is passed in.
|
||||||
@@ -49,13 +49,13 @@ func (t *Table) GetRecordContext(ctx context.Context, recordID string) (*Record,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRecordPartial updates partial info on record.
|
// UpdateRecordPartial updates partial info on record.
|
||||||
func (r *Record) UpdateRecordPartial(changedFields map[string]interface{}) (*Record, error) {
|
func (r *Record) UpdateRecordPartial(changedFields map[string]any) (*Record, error) {
|
||||||
return r.UpdateRecordPartialContext(context.Background(), changedFields)
|
return r.UpdateRecordPartialContext(context.Background(), changedFields)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRecordPartialContext updates partial info on record
|
// UpdateRecordPartialContext updates partial info on record
|
||||||
// with custom context
|
// with custom context
|
||||||
func (r *Record) UpdateRecordPartialContext(ctx context.Context, changedFields map[string]interface{}) (*Record, error) {
|
func (r *Record) UpdateRecordPartialContext(ctx context.Context, changedFields map[string]any) (*Record, error) {
|
||||||
data := &Records{
|
data := &Records{
|
||||||
Records: []*Record{
|
Records: []*Record{
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func TestRecord_GetRecord(t *testing.T) {
|
|||||||
table: table,
|
table: table,
|
||||||
ID: "recnTq6CsvFM6vX2m",
|
ID: "recnTq6CsvFM6vX2m",
|
||||||
CreatedTime: "2020-04-10T11:30:57.000Z",
|
CreatedTime: "2020-04-10T11:30:57.000Z",
|
||||||
Fields: map[string]interface{}{
|
Fields: map[string]any{
|
||||||
"Field1": "Field1",
|
"Field1": "Field1",
|
||||||
"Field2": true,
|
"Field2": true,
|
||||||
"Field3": "2020-04-06T06:00:00.000Z",
|
"Field3": "2020-04-06T06:00:00.000Z",
|
||||||
@@ -61,7 +61,7 @@ func TestRecord_DeleteRecord(t *testing.T) {
|
|||||||
func TestRecord_UpdateRecordPartial(t *testing.T) {
|
func TestRecord_UpdateRecordPartial(t *testing.T) {
|
||||||
record := testRecord(t)
|
record := testRecord(t)
|
||||||
record.client.baseURL = mockResponse("get_records_with_filter.json").URL
|
record.client.baseURL = mockResponse("get_records_with_filter.json").URL
|
||||||
res, err := record.UpdateRecordPartial(map[string]interface{}{"Field_2": true})
|
res, err := record.UpdateRecordPartial(map[string]any{"Field_2": true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("must be no error")
|
t.Error("must be no error")
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ func TestRecord_UpdateRecordPartial(t *testing.T) {
|
|||||||
t.Errorf("expected that Field_2 will be true, but was: %#v", res.Fields["Field2"].(bool))
|
t.Errorf("expected that Field_2 will be true, but was: %#v", res.Fields["Field2"].(bool))
|
||||||
}
|
}
|
||||||
record.client.baseURL = mockErrorResponse(404).URL
|
record.client.baseURL = mockErrorResponse(404).URL
|
||||||
_, err = record.UpdateRecordPartial(map[string]interface{}{})
|
_, err = record.UpdateRecordPartial(map[string]any{})
|
||||||
var e *HTTPClientError
|
var e *HTTPClientError
|
||||||
if errors.Is(err, e) {
|
if errors.Is(err, e) {
|
||||||
t.Errorf("should be an http error, but was not: %v", err)
|
t.Errorf("should be an http error, but was not: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user