From 752406815c5e19e503fc95a9b95c399fecbc27be Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sat, 15 Oct 2022 14:15:36 +0200 Subject: [PATCH] catch incomplete rows and fill them with empty string[s] --- TODO.md | 6 ------ lib/parser.go | 6 ++++++ lib/parser_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/TODO.md b/TODO.md index ec35a4e..ed07d10 100644 --- a/TODO.md +++ b/TODO.md @@ -1,17 +1,11 @@ ## Fixes to be implemented -- catch rows with less fields as headers and fill them up to avoid - a panic - ## Features to be implemented - ability to change sort order (ascending vs descending) - 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 --no-headers option diff --git a/lib/parser.go b/lib/parser.go index 04894f9..2b9c0e0 100644 --- a/lib/parser.go +++ b/lib/parser.go @@ -105,6 +105,12 @@ func parseFile(input io.Reader, pattern string) (Tabdata, error) { 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) } } diff --git a/lib/parser_test.go b/lib/parser_test.go index fcbd338..e6d9885 100644 --- a/lib/parser_test.go +++ b/lib/parser_test.go @@ -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) + } +}