Files
airtable/table.go

60 lines
1.3 KiB
Go
Raw Permalink Normal View History

package airtable
import (
"context"
2024-06-22 21:46:58 -07:00
"fmt"
)
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
}
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"`
}
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"`
}
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
})
}
2024-06-22 21:46:58 -07:00
func (b *Base) GetTableByName(ctx context.Context, name string) (*Table, error) {
tables, err := b.ListTables(ctx)
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-10-02 14:27:00 +03:00
2024-06-22 21:46:58 -07:00
return nil, fmt.Errorf("table '%s' not found", name)
}
2024-06-22 21:46:58 -07:00
func (t Table) String() string {
return fmt.Sprintf("%s.%s", t.b, t.Name)
}