catch incomplete rows and fill them with empty string[s]

This commit is contained in:
2022-10-15 14:15:36 +02:00
parent 4ec6ccd0fd
commit 752406815c
3 changed files with 44 additions and 6 deletions

View File

@@ -1,17 +1,11 @@
## Fixes to be implemented ## Fixes to be implemented
- catch rows with less fields as headers and fill them up to avoid
a panic
## Features to be implemented ## Features to be implemented
- ability to change sort order (ascending vs descending) - ability to change sort order (ascending vs descending)
- sorting by: numerical, time, duration, string(default) - sorting by: numerical, time, duration, string(default)
- allow regexp in -c like: `-c N.*,ST,8` which means, match "NAME",
"NAMESPACE", "STATUS", 8th Header
- add output modes yaml and csv - add output modes yaml and csv
- add --no-headers option - add --no-headers option

View File

@@ -105,6 +105,12 @@ func parseFile(input io.Reader, pattern string) (Tabdata, error) {
idx++ idx++
} }
// fill up missing fields, if any
for i := len(values); i < len(data.headers); i++ {
values = append(values, "")
}
data.entries = append(data.entries, values) data.entries = append(data.entries, values)
} }
} }

View File

@@ -110,3 +110,41 @@ asd igig cxxxncnc
}) })
} }
} }
func TestParserIncompleteRows(t *testing.T) {
data := Tabdata{
maxwidthHeader: 5,
maxwidthPerCol: []int{
5, 5, 1,
},
columns: 3,
headers: []string{
"ONE", "TWO", "THREE",
},
entries: [][]string{
{
"asd", "igig", "",
},
{
"19191", "EDD 1", "X",
},
},
}
table := `ONE TWO THREE
asd igig
19191 EDD 1 X`
readFd := strings.NewReader(table)
gotdata, err := parseFile(readFd, "")
Separator = DefaultSeparator
if err != nil {
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
}
if !reflect.DeepEqual(data, gotdata) {
t.Errorf("Parser returned invalid data, Regex: %s\nExp: %+v\nGot: %+v\n",
Separator, data, gotdata)
}
}