From 9d13c602a1b74e4f02a5bda0d3411269e041784d Mon Sep 17 00:00:00 2001 From: Mike Berezin Date: Sun, 9 Jul 2023 22:33:45 +0500 Subject: [PATCH] build: move to any from interface{} --- README.md | 8 ++++---- base.go | 10 +++++----- client.go | 12 ++++++------ field-converter.go | 4 ++-- field-converter_test.go | 12 ++++++------ go.mod | 2 +- record.go | 12 ++++++------ record_test.go | 6 +++--- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index d13c8ef..8588cbd 100644 --- a/README.md +++ b/README.md @@ -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, }, @@ -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 } @@ -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, }, diff --git a/base.go b/base.go index b6be4f8..2419ee3 100644 --- a/base.go +++ b/base.go @@ -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 { diff --git a/client.go b/client.go index 54b45ec..5741400 100644 --- a/client.go +++ b/client.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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") } diff --git a/field-converter.go b/field-converter.go index 8a0d737..20b80ab 100644 --- a/field-converter.go +++ b/field-converter.go @@ -16,7 +16,7 @@ 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 @@ -24,6 +24,6 @@ func ToDateTime(field interface{}) (time.Time, error) { return time.Parse(dateTimeFormat, fS) } -func FromDateTime(t time.Time) interface{} { +func FromDateTime(t time.Time) any { return t.Format(dateTimeFormat) } diff --git a/field-converter_test.go b/field-converter_test.go index 28058d1..4b54097 100644 --- a/field-converter_test.go +++ b/field-converter_test.go @@ -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) { @@ -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) { diff --git a/go.mod b/go.mod index 82a0ec0..2500c8b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/mehanizm/airtable -go 1.19 +go 1.20 diff --git a/record.go b/record.go index a344921..63cab33 100644 --- a/record.go +++ b/record.go @@ -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. @@ -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{ { diff --git a/record_test.go b/record_test.go index 9f1849e..61d01cb 100644 --- a/record_test.go +++ b/record_test.go @@ -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", @@ -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") } @@ -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)