-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
117 lines (102 loc) · 2.72 KB
/
rows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package airtablewatcher
import (
"encoding/json"
"fmt"
"time"
)
// Defaults
const (
AirtableDateFormat = "2006-01-02T15:04:05.000Z"
)
// More defaults
var (
DefaultBlankTime = time.Date(1, 0, 0, 0, 0, 0, 0, time.UTC)
)
// Row Generic row from airtable
type Row struct {
ID string
Fields interface{}
}
// GetField Get a generic field value from a row, returns nil if not found
func (r *Row) GetField(fieldName string) interface{} {
// Attempt to cast and get state
if res, ok := r.Fields.(map[string]interface{}); ok {
if state, ok := res[fieldName]; ok {
return state
}
}
return nil
}
// GetFieldString Get string value from a row
func (r *Row) GetFieldString(fieldName string) string {
// Attempt to cast and get state
value := r.GetField(fieldName)
if valueString, ok := value.(string); ok {
return valueString
}
if valueBool, ok := value.(bool); ok {
if valueBool {
return "true"
}
return "false"
}
if valueFloat, ok := value.(float64); ok {
return fmt.Sprintf("%f", valueFloat)
}
if value == nil {
return ""
}
// Return string representation of field
return fmt.Sprintf("%s", value)
}
// GetFieldAttachments Gets the attachments from a field
func (r *Row) GetFieldAttachments(fieldName string) ([]AirtableAttachment, error) {
value := r.GetField(fieldName)
// Try to unmarshall and remarshal to airtable attachment
JSON, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("error converting: %w", err)
}
attachments := []AirtableAttachment{}
err = json.Unmarshal(JSON, &attachments)
if err != nil {
return nil, fmt.Errorf("error converting: %w", err)
}
return attachments, nil
}
// GetFieldTime Get a field value in time format from a row, returns DefaultBlankTime if parsing fails
func (r *Row) GetFieldTime(fieldName string) time.Time {
// Attempt to cast and get state
// TODO: Support date field without time
timeStr := r.GetFieldString(fieldName)
if timeStr == "" {
return DefaultBlankTime
}
time, err := time.Parse(AirtableDateFormat, timeStr)
if err == nil {
return time
}
return DefaultBlankTime
}
// GetRows Get list of tasks in airtable
func (t *Watcher) GetRows(tableName string) ([]Row, error) {
tasks := []Row{}
err := t.AirtableClient.ListRecords(tableName, &tasks)
if err != nil {
return nil, err
}
return tasks, nil
}
// GetRow Get airtable row
func (t *Watcher) GetRow(tableName, recordID string) (*Row, error) {
row := &Row{}
err := t.AirtableClient.RetrieveRecord(tableName, recordID, row)
if err != nil {
return nil, err
}
return row, nil
}
// SetRow Set provided fields for a row
func (t *Watcher) SetRow(tableName, recordID string, fields map[string]interface{}) error {
return t.AirtableClient.UpdateRecord(tableName, recordID, fields, nil)
}