From b9865ee99a756f5b291e3856ae1a06a3dd4a7ddc Mon Sep 17 00:00:00 2001 From: reesporte Date: Fri, 29 Jul 2022 16:27:57 -0500 Subject: [PATCH] add `row-col-mode` to `featurebase import` most types are imported in the format `,`, but ints and decimals aren't. with this new flag, ints and decimals are imported using the `,` format, instead of `,`. --- cmd/import.go | 1 + ctl/import.go | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/import.go b/cmd/import.go index 2f589fcf8..fe69c1cb3 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -71,6 +71,7 @@ omitted. If it is present then its format should be YYYY-MM-DDTHH:MM. flags.StringVarP(&Importer.Index, "index", "i", "", "FeatureBase index to import into.") flags.StringVarP(&Importer.Field, "field", "f", "", "Field to import into.") flags.BoolVar(&Importer.IndexOptions.Keys, "index-keys", false, "Specify keys=true when creating an index") + flags.BoolVar(&Importer.RowColMode, "row-col-mode", false, "Specify row-col-mode=true to read csv files as ,") flags.BoolVar(&Importer.FieldOptions.Keys, "field-keys", false, "Specify keys=true when creating a field") flags.StringVar(&Importer.FieldOptions.Type, "field-type", "", "Specify the field type when creating a field. One of: set, int, decimal, time, bool, mutex") flags.Var(&fieldMin, "field-min", "Specify the minimum for an int field on creation") // TODO: noting that decimal field min/max are not supported here. diff --git a/ctl/import.go b/ctl/import.go index 963aaaf52..b2cdc1706 100644 --- a/ctl/import.go +++ b/ctl/import.go @@ -41,6 +41,9 @@ type ImportCommand struct { // nolint: maligned // Clear clears the import data as opposed to setting it. Clear bool + // RowColMode indicates whether to read csv values as row id, col id or use default behavior. + RowColMode bool + // Filenames to import from. Paths []string `json:"paths"` @@ -331,26 +334,33 @@ func (cmd *ImportCommand) bufferValues(ctx context.Context, useColumnKeys, parse return fmt.Errorf("bad column count on row %d: col=%d", rnum, len(record)) } + var rowIdx, colIdx uint32 + if cmd.RowColMode { + colIdx = 1 + } else { + rowIdx = 1 + } + // Parse column id. if useColumnKeys { - req.ColumnKeys = append(req.ColumnKeys, record[0]) - } else if columnID, err := strconv.ParseUint(record[0], 10, 64); err == nil { + req.ColumnKeys = append(req.ColumnKeys, record[colIdx]) + } else if columnID, err := strconv.ParseUint(record[colIdx], 10, 64); err == nil { req.ColumnIDs = append(req.ColumnIDs, columnID) } else { - return fmt.Errorf("invalid column id on row %d: %q", rnum, record[0]) + return fmt.Errorf("invalid column id on row %d: %q", rnum, record[colIdx]) } // Parse value. if parseAsFloat { - value, err := strconv.ParseFloat(record[1], 64) + value, err := strconv.ParseFloat(record[rowIdx], 64) if err != nil { - return errors.Wrapf(err, "parseing value '%s' as float", record[1]) + return errors.Wrapf(err, "parsing value '%s' as float", record[rowIdx]) } req.FloatValues = append(req.FloatValues, value) } else { - value, err := strconv.ParseInt(record[1], 10, 64) + value, err := strconv.ParseInt(record[rowIdx], 10, 64) if err != nil { - return errors.Wrapf(err, "invalid value on row %d: %q", rnum, record[1]) + return errors.Wrapf(err, "invalid value on row %d: %q", rnum, record[rowIdx]) } req.Values = append(req.Values, value) }