Skip to content

Commit

Permalink
fix the bug when selecting with field numbers. #242
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Aug 15, 2023
1 parent fb9e487 commit 38f8da1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`:
Expand Down
11 changes: 8 additions & 3 deletions csvtk/cmd/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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,
}
}

Expand Down
8 changes: 4 additions & 4 deletions csvtk/cmd/filter2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -237,15 +237,15 @@ 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

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))
Expand 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)
Expand Down
10 changes: 5 additions & 5 deletions csvtk/cmd/mutate2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -305,15 +305,15 @@ 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

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))
Expand 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)
Expand Down

0 comments on commit 38f8da1

Please sign in to comment.