build: move to any from interface{}

This commit is contained in:
Mike Berezin
2023-07-09 22:33:45 +05:00
parent 1cd5fe48d6
commit 9d13c602a1
8 changed files with 33 additions and 33 deletions

View File

@@ -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,
},

View File

@@ -23,7 +23,7 @@ type Field struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Options map[string]interface{} `json:"options"`
Options map[string]any `json:"options"`
}
type View struct {

View File

@@ -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")
}

View File

@@ -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)
}

View File

@@ -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) {

2
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/mehanizm/airtable
go 1.19
go 1.20

View File

@@ -15,7 +15,7 @@ type Record struct {
client *Client
table *Table
ID string `json:"id,omitempty"`
Fields map[string]interface{} `json:"fields"`
Fields map[string]any `json:"fields"`
CreatedTime string `json:"createdTime,omitempty"`
Deleted bool `json:"deleted,omitempty"`
@@ -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{
{

View File

@@ -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)