From 38f8da1cdc103370cf70b2fcf220fe7d8bf34b9c Mon Sep 17 00:00:00 2001 From: Wei Shen Date: Tue, 15 Aug 2023 22:08:00 +0800 Subject: [PATCH] fix the bug when selecting with field numbers. #242 --- CHANGELOG.md | 4 ++++ csvtk/cmd/csv.go | 11 ++++++++--- csvtk/cmd/filter2.go | 8 ++++---- csvtk/cmd/mutate2.go | 10 +++++----- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79cc999..73e49d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +- [csvtk v0.27.1](https://github.com/shenwei356/csvtk/releases/tag/v0.27.1) +[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.27.1/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.27.1) + - `csvtk filter2/mutate2`: + - fix the bug when selecting with field numbers. [#242](https://github.com/shenwei356/csvtk/issues/242) - [csvtk v0.27.0](https://github.com/shenwei356/csvtk/releases/tag/v0.27.0) [![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.27.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.27.0) - `csvtk`: diff --git a/csvtk/cmd/csv.go b/csvtk/cmd/csv.go index 87022be..2ab3954 100644 --- a/csvtk/cmd/csv.go +++ b/csvtk/cmd/csv.go @@ -37,7 +37,8 @@ type Record struct { Row int // the row number, header row skipped Err error - IsHeaderRow bool + IsHeaderRow bool // is current record the header row + SelectWithColnames bool // wether user use colnames to select fields All []string Fields []int // selected fields @@ -127,6 +128,8 @@ func (csvReader *CSVReader) Read(opt ReadOption) { fields, colnames, negativeFields, needParseHeaderRow, x2ends := parseFields(fieldStr, fieldStrSep, csvReader.NoHeaderRow) var fieldsMap map[int]struct{} + selectWithColnames := len(fields) == 0 + if len(fields) > 0 && negativeFields { fieldsMap = make(map[int]struct{}, len(fields)) for _, f := range fields { @@ -474,7 +477,8 @@ func (csvReader *CSVReader) Read(opt ReadOption) { Fields: fields, // the first variable Selected: items, // copied values - IsHeaderRow: isHeaderRow, + IsHeaderRow: isHeaderRow, + SelectWithColnames: selectWithColnames, } continue @@ -499,7 +503,8 @@ func (csvReader *CSVReader) Read(opt ReadOption) { Fields: fields, // the first variable Selected: items, // copied values - IsHeaderRow: isHeaderRow, + IsHeaderRow: isHeaderRow, + SelectWithColnames: selectWithColnames, } } diff --git a/csvtk/cmd/filter2.go b/csvtk/cmd/filter2.go index 49e3d28..d13bccb 100644 --- a/csvtk/cmd/filter2.go +++ b/csvtk/cmd/filter2.go @@ -224,7 +224,7 @@ Custom functions: var colnamesMap map[string]*regexp.Regexp var fieldsUniq []int - var hasHeaderRow bool + var selectWithColnames bool keys := make([]string, 0, 8) @@ -237,6 +237,8 @@ Custom functions: if checkFirstLine { checkFirstLine = false + selectWithColnames = record.SelectWithColnames + parameters = make(map[string]string, len(record.All)) parameters2 = make(map[string]interface{}, len(record.All)) parameters2["shenweiNULL"] = nil @@ -244,8 +246,6 @@ Custom functions: fieldsUniq = UniqInts(record.Fields) if !config.NoHeaderRow || record.IsHeaderRow { // do not replace head line - hasHeaderRow = true - colnames2fileds = make(map[string][]int, len(record.All)) colnamesMap = make(map[string]*regexp.Regexp, len(record.All)) @@ -270,7 +270,7 @@ Custom functions: N++ // prepaire parameters - if !hasHeaderRow { + if !selectWithColnames { for _, fieldTmp = range fieldsUniq { value = record.All[fieldTmp-1] col = strconv.Itoa(fieldTmp) diff --git a/csvtk/cmd/mutate2.go b/csvtk/cmd/mutate2.go index 5cfaaf1..c196acb 100644 --- a/csvtk/cmd/mutate2.go +++ b/csvtk/cmd/mutate2.go @@ -292,7 +292,7 @@ Custom functions: var colnames2fileds map[string][]int // column name -> []field var colnamesMap map[string]*regexp.Regexp var fieldsUniq []int - var hasHeaderRow bool + var selectWithColnames bool var record2 []string // for output keys := make([]string, 0, 8) @@ -305,6 +305,8 @@ Custom functions: if checkFirstLine { checkFirstLine = false + selectWithColnames = record.SelectWithColnames + parameters = make(map[string]string, len(record.All)) parameters2 = make(map[string]interface{}, len(record.All)) parameters2["shenweiNULL"] = nil @@ -312,8 +314,6 @@ Custom functions: fieldsUniq = UniqInts(record.Fields) if !config.NoHeaderRow || record.IsHeaderRow { // do not replace head line - hasHeaderRow = true - colnames2fileds = make(map[string][]int, len(record.All)) colnamesMap = make(map[string]*regexp.Regexp, len(record.All)) @@ -332,8 +332,8 @@ Custom functions: } } - // prepaire parameters - if !hasHeaderRow { + // prepare parameters + if !selectWithColnames { for _, fieldTmp = range fieldsUniq { value = record.All[fieldTmp-1] col = strconv.Itoa(fieldTmp)