Skip to content

Commit

Permalink
build: move to any from interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
mehanizm committed Jul 9, 2023
1 parent 1cd5fe4 commit 9d13c60
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 33 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ if err != nil {
recordsToSend := &airtable.Records{
Records: []*airtable.Record{
{
Fields: map[string]interface{}{
Fields: map[string]any{
"Field1": "value1",
"Field2": true,
},
Expand All @@ -121,7 +121,7 @@ if err != nil {
To partial update one record

```Go
res, err := record.UpdateRecordPartial(map[string]interface{}{"Field_2": false})
res, err := record.UpdateRecordPartial(map[string]any{"Field_2": false})
if err != nil {
// Handle error
}
Expand All @@ -133,13 +133,13 @@ To full update records
toUpdateRecords := &airtable.Records{
Records: []*airtable.Record{
{
Fields: map[string]interface{}{
Fields: map[string]any{
"Field1": "value1",
"Field2": true,
},
},
{
Fields: map[string]interface{}{
Fields: map[string]any{
"Field1": "value1",
"Field2": true,
},
Expand Down
10 changes: 5 additions & 5 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type Bases struct {
}

type Field struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Options map[string]interface{} `json:"options"`
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Options map[string]any `json:"options"`
}

type View struct {
Expand Down
12 changes: 6 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (at *Client) rateLimit() {
<-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()

url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
Expand All @@ -103,7 +103,7 @@ func (at *Client) get(ctx context.Context, db, table, recordID string, params ur
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()

url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
Expand All @@ -124,7 +124,7 @@ func (at *Client) post(ctx context.Context, db, table string, data, response int
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()

rawURL := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
Expand Down Expand Up @@ -152,7 +152,7 @@ func (at *Client) delete(ctx context.Context, db, table string, recordIDs []stri
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()

url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
Expand All @@ -173,7 +173,7 @@ func (at *Client) patch(ctx context.Context, db, table, data, response interface
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()

url := fmt.Sprintf("%s/%s/%s", at.baseURL, db, table)
Expand All @@ -194,7 +194,7 @@ func (at *Client) put(ctx context.Context, db, table, data, response interface{}
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 {
return errors.New("nil request")
}
Expand Down
4 changes: 2 additions & 2 deletions field-converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const (

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)
if !err {
return time.Time{}, ErrNotDateTime
}
return time.Parse(dateTimeFormat, fS)
}

func FromDateTime(t time.Time) interface{} {
func FromDateTime(t time.Time) any {
return t.Format(dateTimeFormat)
}
12 changes: 6 additions & 6 deletions field-converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
func TestToDateTime(t *testing.T) {
tests := []struct {
name string
field interface{}
field any
want time.Time
wantErr bool
}{
{"not string", interface{}(1), time.Time{}, true},
{"string not time", interface{}("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},
{"not string", any(1), time.Time{}, true},
{"string not time", any("hello"), time.Time{}, true},
{"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 {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -40,9 +40,9 @@ func TestFromDateTime(t *testing.T) {
tests := []struct {
name string
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 {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/mehanizm/airtable

go 1.19
go 1.20
12 changes: 6 additions & 6 deletions record.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
type Record struct {
client *Client
table *Table
ID string `json:"id,omitempty"`
Fields map[string]interface{} `json:"fields"`
CreatedTime string `json:"createdTime,omitempty"`
Deleted bool `json:"deleted,omitempty"`
ID string `json:"id,omitempty"`
Fields map[string]any `json:"fields"`
CreatedTime string `json:"createdTime,omitempty"`
Deleted bool `json:"deleted,omitempty"`

// The Airtable API will perform best-effort automatic data conversion
// from string values if the typecast parameter is passed in.
Expand Down Expand Up @@ -49,13 +49,13 @@ func (t *Table) GetRecordContext(ctx context.Context, recordID string) (*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)
}

// UpdateRecordPartialContext updates partial info on record
// 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{
Records: []*Record{
{
Expand Down
6 changes: 3 additions & 3 deletions record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestRecord_GetRecord(t *testing.T) {
table: table,
ID: "recnTq6CsvFM6vX2m",
CreatedTime: "2020-04-10T11:30:57.000Z",
Fields: map[string]interface{}{
Fields: map[string]any{
"Field1": "Field1",
"Field2": true,
"Field3": "2020-04-06T06:00:00.000Z",
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestRecord_DeleteRecord(t *testing.T) {
func TestRecord_UpdateRecordPartial(t *testing.T) {
record := testRecord(t)
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 {
t.Error("must be no error")
}
Expand All @@ -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))
}
record.client.baseURL = mockErrorResponse(404).URL
_, err = record.UpdateRecordPartial(map[string]interface{}{})
_, err = record.UpdateRecordPartial(map[string]any{})
var e *HTTPClientError
if errors.Is(err, e) {
t.Errorf("should be an http error, but was not: %v", err)
Expand Down

0 comments on commit 9d13c60

Please sign in to comment.