Skip to content

Commit

Permalink
Fixed #31: Added LineErrors to qbclient.InsertRecordsOutputMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
cpliakas committed Feb 5, 2021
1 parent 2370e10 commit 4f54cce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
21 changes: 21 additions & 0 deletions qbcli/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"sort"
"strconv"
"time"

"github.com/QuickBase/quickbase-cli/qbclient"
Expand Down Expand Up @@ -128,6 +129,7 @@ type ImportOptions struct {
func Import(qb *qbclient.Client, opts *ImportOptions) (*qbclient.InsertRecordsOutputMetadata, error) {
metadata := &qbclient.InsertRecordsOutputMetadata{
CreatedRecordIDs: []int{},
LineErrors: map[string][]string{},
TotalNumberOfRecordsProcessed: 0,
UnchangedRecordIDs: []int{},
UpdatedRecordIDs: []int{},
Expand Down Expand Up @@ -247,13 +249,32 @@ func Import(qb *qbclient.Client, opts *ImportOptions) (*qbclient.InsertRecordsOu
}

// Empty the records for the next batch.
rlen := len(records)
records = []map[int]*qbclient.InsertRecordsInputData{}

metadata.CreatedRecordIDs = append(metadata.CreatedRecordIDs, iro.Metadata.CreatedRecordIDs...)
metadata.TotalNumberOfRecordsProcessed += iro.Metadata.TotalNumberOfRecordsProcessed
metadata.UnchangedRecordIDs = append(metadata.UnchangedRecordIDs, iro.Metadata.UnchangedRecordIDs...)
metadata.UpdatedRecordIDs = append(metadata.UpdatedRecordIDs, iro.Metadata.UpdatedRecordIDs...)

// Merging the line errors isn't so simple.
for k, v := range iro.Metadata.LineErrors {
n, err := strconv.Atoi(k)
if err != nil {
return metadata, fmt.Errorf("%s: expecting lineErrors key to be an integer", k)
}

// This works no matter the batch size, but I have no idea why
// we have to subtract 1 from n when eof is true. If this is
// buggy, blame cpliakas@quickbase.com.
n = line - rlen + n
if eof {
n--
}

metadata.LineErrors[strconv.Itoa(n)] = v
}

// Delay before the next API call.
if opts.Delay > 0 && !eof {
time.Sleep(time.Duration(opts.Delay) * time.Millisecond)
Expand Down
9 changes: 5 additions & 4 deletions qbclient/resource_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func (o *InsertRecordsOutput) decode(body io.ReadCloser) error { return unmarsha

// InsertRecordsOutputMetadata models the metadata property.
type InsertRecordsOutputMetadata struct {
CreatedRecordIDs []int `json:"createdRecordIds"`
TotalNumberOfRecordsProcessed int `json:"totalNumberOfRecordsProcessed"`
UnchangedRecordIDs []int `json:"unchangedRecordIds"`
UpdatedRecordIDs []int `json:"updatedRecordIds"`
CreatedRecordIDs []int `json:"createdRecordIds"`
LineErrors map[string][]string `json:"lineErrors,omptempty"`
TotalNumberOfRecordsProcessed int `json:"totalNumberOfRecordsProcessed"`
UnchangedRecordIDs []int `json:"unchangedRecordIds"`
UpdatedRecordIDs []int `json:"updatedRecordIds"`
}

// InsertRecords sends a request to POST /v1/records.
Expand Down

0 comments on commit 4f54cce

Please sign in to comment.