From ce4c936bd8f91f4ca8f43c4d39e054f5e988bac2 Mon Sep 17 00:00:00 2001 From: shenwei356 Date: Wed, 5 Oct 2016 21:28:50 +0800 Subject: [PATCH] fix bugs --- csvtk/cmd/box.go | 2 +- csvtk/cmd/csv2md.go | 2 +- csvtk/cmd/helper.go | 16 ++++- csvtk/cmd/hist.go | 2 +- csvtk/cmd/join.go | 3 +- csvtk/cmd/line.go | 2 +- csvtk/cmd/pretty.go | 15 ++--- csvtk/cmd/sort.go | 4 +- testdata/1.svg | 152 -------------------------------------------- 9 files changed, 28 insertions(+), 170 deletions(-) delete mode 100644 testdata/1.svg diff --git a/csvtk/cmd/box.go b/csvtk/cmd/box.go index d4cb1fb..0786d40 100644 --- a/csvtk/cmd/box.go +++ b/csvtk/cmd/box.go @@ -51,7 +51,7 @@ var boxCmd = &cobra.Command{ runtime.GOMAXPROCS(config.NumCPUs) file := files[0] - headerRow, data, fields := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false) + headerRow, fields, data, _, _ := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false) // ======================================= diff --git a/csvtk/cmd/csv2md.go b/csvtk/cmd/csv2md.go index ee3ed55..bc880e0 100644 --- a/csvtk/cmd/csv2md.go +++ b/csvtk/cmd/csv2md.go @@ -70,7 +70,7 @@ var csv2mdCmd = &cobra.Command{ file := files[0] fieldStr := "*" fuzzyFields := true - headerRow, data, _ := parseCSVfile(cmd, config, + headerRow, _, data, _, _ := parseCSVfile(cmd, config, file, fieldStr, fuzzyFields) var header []string diff --git a/csvtk/cmd/helper.go b/csvtk/cmd/helper.go index 6bd3a59..8d08ce8 100644 --- a/csvtk/cmd/helper.go +++ b/csvtk/cmd/helper.go @@ -372,7 +372,7 @@ func fuzzyField2Regexp(field string) *regexp.Regexp { } func parseCSVfile(cmd *cobra.Command, config Config, file string, - fieldStr string, fuzzyFields bool) ([]string, [][]string, []int) { + fieldStr string, fuzzyFields bool) ([]string, []int, [][]string, []string, [][]string) { fields, colnames, negativeFields, needParseHeaderRow := parseFields(cmd, fieldStr, config.NoHeaderRow) var fieldsMap map[int]struct{} var fieldsOrder map[int]int // for set the order of fields @@ -413,7 +413,9 @@ func parseCSVfile(cmd *cobra.Command, config Config, file string, var colnamesMap map[string]*regexp.Regexp var HeaderRow []string + var HeaderRowAll []string var Data [][]string + var DataAll [][]string checkFields := true @@ -477,9 +479,13 @@ func parseCSVfile(cmd *cobra.Command, config Config, file string, items := make([]string, len(fields)) for i, f := range fields { + if f > len(record) { + continue + } items[i] = record[f-1] } HeaderRow = items + HeaderRowAll = record continue } if checkFields { @@ -520,9 +526,15 @@ func parseCSVfile(cmd *cobra.Command, config Config, file string, items[i] = record[f-1] } Data = append(Data, items) + if fieldStr != "*" { + DataAll = append(DataAll, record) + } } } - return HeaderRow, Data, fields + if fieldStr != "*" { + return HeaderRow, fields, Data, HeaderRowAll, DataAll + } + return HeaderRow, fields, Data, HeaderRowAll, Data } func removeComma(s string) string { diff --git a/csvtk/cmd/hist.go b/csvtk/cmd/hist.go index 2d2bff9..226c70a 100644 --- a/csvtk/cmd/hist.go +++ b/csvtk/cmd/hist.go @@ -50,7 +50,7 @@ var histCmd = &cobra.Command{ runtime.GOMAXPROCS(config.NumCPUs) file := files[0] - headerRow, data, fields := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false) + headerRow, fields, data, _, _ := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false) // ======================================= diff --git a/csvtk/cmd/join.go b/csvtk/cmd/join.go index dfc31eb..60ba5f4 100644 --- a/csvtk/cmd/join.go +++ b/csvtk/cmd/join.go @@ -84,7 +84,7 @@ Multiple keys supported, but the orders are ignored. var key string var items []string for i, file := range files { - headerRow, data, fields := parseCSVfile(cmd, config, + _, fields, _, headerRow, data := parseCSVfile(cmd, config, file, allFields[i], fuzzyFields) if firstFile { HeaderRow, Data, Fields = headerRow, data, fields @@ -100,7 +100,6 @@ Multiple keys supported, but the orders are ignored. for _, f := range fields { fieldsMap[f] = struct{}{} } - // csv to map keysMaps := make(map[string][]string) items = make([]string, len(fields)) diff --git a/csvtk/cmd/line.go b/csvtk/cmd/line.go index c6e1db8..2c39200 100644 --- a/csvtk/cmd/line.go +++ b/csvtk/cmd/line.go @@ -89,7 +89,7 @@ var lineCmd = &cobra.Command{ } file := files[0] - headerRow, data, fields := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false) + headerRow, fields, data, _, _ := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false) // ======================================= diff --git a/csvtk/cmd/pretty.go b/csvtk/cmd/pretty.go index 828f331..1edb668 100644 --- a/csvtk/cmd/pretty.go +++ b/csvtk/cmd/pretty.go @@ -46,18 +46,17 @@ var prettyCmd = &cobra.Command{ alignRight := getFlagBool(cmd, "align-right") separator := getFlagString(cmd, "separator") - minWidth :=getFlagNonNegativeInt(cmd, "min-width") - maxWidth :=getFlagNonNegativeInt(cmd, "max-width") + minWidth := getFlagNonNegativeInt(cmd, "min-width") + maxWidth := getFlagNonNegativeInt(cmd, "max-width") outfh, err := xopen.Wopen(config.OutFile) checkError(err) defer outfh.Close() - file := files[0] fieldStr := "*" fuzzyFields := true - headerRow, data, _ := parseCSVfile(cmd, config, + headerRow, _, data, _, _ := parseCSVfile(cmd, config, file, fieldStr, fuzzyFields) var header []string @@ -65,7 +64,7 @@ var prettyCmd = &cobra.Command{ if len(headerRow) > 0 { header = headerRow datas = data - } else{ + } else { if len(data) == 0 { checkError(fmt.Errorf("no data found in file: %s", file)) } else if len(data) > 0 { @@ -76,16 +75,16 @@ var prettyCmd = &cobra.Command{ columns := make([]prettytable.Column, len(header)) for i, c := range header { columns[i] = prettytable.Column{Header: c, AlignRight: alignRight, - MinWidth: minWidth, MaxWidth:maxWidth} + MinWidth: minWidth, MaxWidth: maxWidth} } tbl, err := prettytable.NewTable(columns...) checkError(err) tbl.Separator = separator for _, record := range datas { // have to do this stupid convertion - record2:=make([]interface{}, len(record)) + record2 := make([]interface{}, len(record)) for i, r := range record { - record2[i]=r + record2[i] = r } tbl.AddRow(record2...) } diff --git a/csvtk/cmd/sort.go b/csvtk/cmd/sort.go index de6d357..f4d48a2 100644 --- a/csvtk/cmd/sort.go +++ b/csvtk/cmd/sort.go @@ -28,8 +28,8 @@ import ( "strconv" "strings" - "github.com/shenwei356/xopen" "github.com/shenwei356/util/stringutil" + "github.com/shenwei356/xopen" "github.com/spf13/cobra" ) @@ -88,7 +88,7 @@ var sortCmd = &cobra.Command{ } file := files[0] - headerRow, data, _ := parseCSVfile(cmd, config, + headerRow, _, _, _, data := parseCSVfile(cmd, config, file, fieldsStr, fuzzyFields) if len(data) == 0 { checkError(fmt.Errorf("no data to sort")) diff --git a/testdata/1.svg b/testdata/1.svg deleted file mode 100644 index f3f5f97..0000000 --- a/testdata/1.svg +++ /dev/null @@ -1,152 +0,0 @@ - - - - - -Length -60 -90 -120 - - - - - - - - - - - - -Count - -0 -100 -200 -300 -400 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -