Skip to content

Commit 9213545

Browse files
committed
refactor: revice cmd/cmd_airtable.go changes
1 parent dbb07d2 commit 9213545

File tree

3 files changed

+328
-256
lines changed

3 files changed

+328
-256
lines changed

Diff for: cmd/cmd_airtable.go

+31-33
Original file line numberDiff line numberDiff line change
@@ -109,47 +109,42 @@ func airtableSync(opts *airtableOptions) error {
109109

110110
// unique entries
111111
features := make([]map[string]repo.Feature, airtabledb.NumTables)
112+
for i, _ := range features {
113+
features[i] = make(map[string]repo.Feature)
114+
}
112115

113116
for _, issue := range filtered {
114117
// providers
115-
//providerMap[issue.Repository.Provider.ID] = issue.Repository.Provider
116118
features[airtabledb.ProviderIndex][issue.Repository.Provider.ID] = issue.Repository.Provider
117119

118120
// labels
119121
for _, label := range issue.Labels {
120-
//labelMap[label.ID] = label
121122
features[airtabledb.LabelIndex][label.ID] = label
122123
}
123124

124125
// accounts
125126
if issue.Repository.Owner != nil {
126-
//accountMap[issue.Repository.Owner.ID] = issue.Repository.Owner
127127
features[airtabledb.AccountIndex][issue.Repository.Owner.ID] = issue.Repository.Owner
128128
}
129-
//accountMap[issue.Author.ID] = issue.Author
129+
130130
features[airtabledb.AccountIndex][issue.Author.ID] = issue.Author
131131
for _, assignee := range issue.Assignees {
132-
//accountMap[assignee.ID] = assignee
133132
features[airtabledb.AccountIndex][assignee.ID] = assignee
134133
}
135134
if issue.Milestone != nil && issue.Milestone.Creator != nil {
136-
//accountMap[issue.Milestone.Creator.ID] = issue.Milestone.Creator
137135
features[airtabledb.AccountIndex][issue.Milestone.Creator.ID] = issue.Milestone.Creator
138136
}
139137

140138
// repositories
141-
//repositoryMap[issue.Repository.ID] = issue.Repository
142139
features[airtabledb.RepositoryIndex][issue.Repository.ID] = issue.Repository
143140
// FIXME: find external repositories based on depends-on links
144141

145142
// milestones
146143
if issue.Milestone != nil {
147-
//milestoneMap[issue.Milestone.ID] = issue.Milestone
148144
features[airtabledb.MilestoneIndex][issue.Milestone.ID] = issue.Milestone
149145
}
150146

151147
// issue
152-
//issueMap[issue.ID] = issue
153148
features[airtabledb.IssueIndex][issue.ID] = issue
154149
// FIXME: find external issues based on depends-on links
155150
}
@@ -165,8 +160,7 @@ func airtableSync(opts *airtableOptions) error {
165160
cache := airtabledb.NewDB()
166161
for tableKind, tableName := range opts.TableNames {
167162
table := at.Table(tableName)
168-
records := cache.Tables[tableKind]
169-
if err := table.List(&records, &airtable.Options{}); err != nil {
163+
if err := cache.Tables[tableKind].Fetch(table); err != nil {
170164
return err
171165
}
172166
}
@@ -181,20 +175,21 @@ func airtableSync(opts *airtableOptions) error {
181175
for _, dbEntry := range featureMap {
182176
matched := false
183177
dbRecord := dbEntry.ToRecord(cache)
184-
for idx, atEntry := range cache.Tables[tableKind] {
185-
if atEntry.Fields.ID == dbEntry.GetID() {
186-
if atEntry.Equals(dbRecord) {
187-
cache.Tables[tableKind][idx].State = airtabledb.StateUnchanged
178+
for idx := 0; idx < cache.Tables[tableKind].Len(); idx ++ {
179+
t := cache.Tables[tableKind]
180+
if t.GetFieldID(idx) == dbEntry.GetID() {
181+
if t.RecordsEqual(idx, dbRecord) {
182+
t.SetState(idx, airtabledb.StateUnchanged)
188183
} else {
189-
cache.Tables[tableKind][idx].Fields = dbRecord.Fields
190-
cache.Tables[tableKind][idx].State = airtabledb.StateChanged
184+
t.CopyFields(idx, dbRecord)
185+
t.SetState(idx, airtabledb.StateChanged)
191186
}
192187
matched = true
193188
break
194189
}
195190
}
196191
if !matched {
197-
unmatched.Tables[tableKind] = append(unmatched.Tables[tableKind], dbRecord)
192+
unmatched.Tables[tableKind].Append(dbRecord)
198193
}
199194
}
200195
}
@@ -204,28 +199,30 @@ func airtableSync(opts *airtableOptions) error {
204199
//
205200
for tableKind, tableName := range opts.TableNames {
206201
table := at.Table(tableName)
207-
for _, entry := range unmatched.Tables[tableKind] {
208-
zap.L().Debug("create airtable entry", zap.String("type", tableName), zap.Stringer("entry", entry))
209-
if err := table.Create(&entry); err != nil {
202+
ut := unmatched.Tables[tableKind]
203+
ct := cache.Tables[tableKind]
204+
for i := 0; i < ut.Len(); i++ {
205+
zap.L().Debug("create airtable entry", zap.String("type", tableName), zap.String("entry", ut.StringAt(i)))
206+
if err := table.Create(ut.GetPtr(i)); err != nil {
210207
return err
211208
}
212-
entry.State = airtabledb.StateNew
213-
cache.Tables[tableKind] = append(cache.Tables[tableKind], entry)
209+
ut.SetState(i, airtabledb.StateNew)
210+
ct.Append(ut.Get(i))
214211
}
215-
for _, entry := range cache.Tables[tableKind] {
212+
for i := 0; i < ct.Len(); i++ {
216213
var err error
217-
switch entry.State {
214+
switch ct.GetState(i) {
218215
case airtabledb.StateUnknown:
219-
err = table.Delete(&entry)
220-
zap.L().Debug("delete airtable entry", zap.String("type", tableName), zap.Stringer("entry", entry), zap.Error(err))
216+
err = table.Delete(ct.GetPtr(i))
217+
zap.L().Debug("delete airtable entry", zap.String("type", tableName), zap.String("entry", ct.StringAt(i)), zap.Error(err))
221218
case airtabledb.StateChanged:
222-
err = table.Update(&entry)
223-
zap.L().Debug("update airtable entry", zap.String("type", tableName), zap.Stringer("entry", entry), zap.Error(err))
219+
err = table.Update(ct.GetPtr(i))
220+
zap.L().Debug("update airtable entry", zap.String("type", tableName), zap.String("entry", ct.StringAt(i)), zap.Error(err))
224221
case airtabledb.StateUnchanged:
225-
zap.L().Debug("unchanged airtable entry", zap.String("type", tableName), zap.Stringer("entry", entry), zap.Error(err))
222+
zap.L().Debug("unchanged airtable entry", zap.String("type", tableName), zap.String("entry", ct.StringAt(i)), zap.Error(err))
226223
// do nothing
227224
case airtabledb.StateNew:
228-
zap.L().Debug("new airtable entry", zap.String("type", tableName), zap.Stringer("entry", entry), zap.Error(err))
225+
zap.L().Debug("new airtable entry", zap.String("type", tableName), zap.String("entry", ct.StringAt(i)), zap.Error(err))
229226
// do nothing
230227
}
231228
}
@@ -236,8 +233,9 @@ func airtableSync(opts *airtableOptions) error {
236233
//
237234
for tableKind, tableName := range opts.TableNames {
238235
fmt.Println("-------", tableName)
239-
for _, entry := range cache.Tables[tableKind] {
240-
fmt.Println(entry.ID, airtabledb.StateString[entry.State], entry.Fields.ID)
236+
ct := cache.Tables[tableKind]
237+
for i := 0; i < ct.Len(); i++ {
238+
fmt.Println(ct.GetID(i), airtabledb.StateString[ct.GetState(i)], ct.GetFieldID(i))
241239
}
242240
}
243241
fmt.Println("-------")

0 commit comments

Comments
 (0)