2020-04-12 13:05:28 +03:00
|
|
|
package airtable
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-12 12:54:18 +05:00
|
|
|
"context"
|
2024-06-22 21:46:58 -07:00
|
|
|
"fmt"
|
2020-04-12 13:05:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Table struct {
|
2024-06-23 16:32:28 -07:00
|
|
|
ID *string `json:"id,omitempty"`
|
|
|
|
|
PrimaryFieldID *string `json:"primaryFieldId,omitempty"`
|
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
|
Description *string `json:"description,omitempty"`
|
2024-06-22 21:46:58 -07:00
|
|
|
Fields []*Field `json:"fields"`
|
|
|
|
|
Views []*View `json:"views"`
|
2021-10-02 14:27:00 +03:00
|
|
|
|
2024-06-22 21:46:58 -07:00
|
|
|
c *Client
|
|
|
|
|
b *Base
|
2020-04-12 13:05:28 +03:00
|
|
|
}
|
|
|
|
|
|
2024-06-22 21:46:58 -07:00
|
|
|
type Field struct {
|
2024-06-23 16:32:28 -07:00
|
|
|
ID *string `json:"id"`
|
|
|
|
|
Type *string `json:"type"`
|
|
|
|
|
Name *string `json:"name"`
|
|
|
|
|
Description *string `json:"description"`
|
2024-06-22 21:46:58 -07:00
|
|
|
Options map[string]any `json:"options"`
|
2023-01-12 12:54:18 +05:00
|
|
|
}
|
|
|
|
|
|
2024-06-22 21:46:58 -07:00
|
|
|
type View struct {
|
2024-06-23 16:32:28 -07:00
|
|
|
ID *string `json:"id"`
|
|
|
|
|
Type *string `json:"type"`
|
|
|
|
|
Name *string `json:"name"`
|
2020-04-12 13:05:28 +03:00
|
|
|
}
|
|
|
|
|
|
2024-06-22 21:46:58 -07:00
|
|
|
func (b *Base) ListTables(ctx context.Context) ([]*Table, error) {
|
2024-06-23 16:32:28 -07:00
|
|
|
return listAll[Table](ctx, b.c, fmt.Sprintf("meta/bases/%s/tables", *b.ID), nil, "tables", func(t *Table) error {
|
2024-06-22 21:46:58 -07:00
|
|
|
t.c = b.c
|
|
|
|
|
t.b = b
|
|
|
|
|
return nil
|
|
|
|
|
})
|
2023-01-12 12:54:18 +05:00
|
|
|
}
|
|
|
|
|
|
2024-06-22 21:46:58 -07:00
|
|
|
func (b *Base) GetTableByName(ctx context.Context, name string) (*Table, error) {
|
|
|
|
|
tables, err := b.ListTables(ctx)
|
2021-01-29 13:21:39 +03:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-10-02 14:27:00 +03:00
|
|
|
|
2024-06-22 21:46:58 -07:00
|
|
|
for _, table := range tables {
|
2024-06-23 16:32:28 -07:00
|
|
|
if *table.Name == name {
|
2024-06-22 21:46:58 -07:00
|
|
|
return table, nil
|
|
|
|
|
}
|
2021-01-29 13:21:39 +03:00
|
|
|
}
|
2021-10-02 14:27:00 +03:00
|
|
|
|
2024-06-22 21:46:58 -07:00
|
|
|
return nil, fmt.Errorf("table '%s' not found", name)
|
2023-01-12 12:54:18 +05:00
|
|
|
}
|
|
|
|
|
|
2024-06-22 21:46:58 -07:00
|
|
|
func (t Table) String() string {
|
|
|
|
|
return fmt.Sprintf("%s.%s", t.b, t.Name)
|
2020-04-12 13:05:28 +03:00
|
|
|
}
|