From 393e3ef289f2fd4cb13a36f81b59f84249f0ce34 Mon Sep 17 00:00:00 2001 From: shubhamrasal Date: Tue, 22 Nov 2022 15:38:46 +0530 Subject: [PATCH 1/5] Check comma inside quotes before spliting [WIP] --- slice_common.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/slice_common.go b/slice_common.go index c466adf..60e9894 100644 --- a/slice_common.go +++ b/slice_common.go @@ -91,7 +91,11 @@ func ToStringSlice(value string, options Options) ([]string, error) { } else { commaFound, part := searchPart(value[index:], ',') - if commaFound { + // ingore comma if it inside quotes eg. [uncover -q 'org:"Something, Inc."'] + if commaFound && commaBetweenQuotes(value[index:]) { + part = value[index:] + index += len(part) + } else if commaFound { index += len(part) + 1 } else { index += len(part) @@ -120,3 +124,21 @@ func normalize(s string) string { func normalizeLowercase(s string) string { return strings.TrimSpace(strings.Trim(strings.TrimSpace(strings.ToLower(s)), string(quotes))) } + +func commaBetweenQuotes(value string) bool { + commaPos := strings.IndexRune(value, ',') // TODO debug the test + // check comma is inside quotes or not + if commaPos != -1 { + for _, quote := range quotes { + + firstQuote := strings.IndexRune(value, quote) + if firstQuote != -1 { + lastQuote := strings.IndexRune(value[firstQuote+1:], quote) + if firstQuote < commaPos && commaPos < (lastQuote+firstQuote) { + return true + } + } + } + } + return false +} From bf6e5ccee2b1dfc15bba337ab70cdf5150516a42 Mon Sep 17 00:00:00 2001 From: shubhamrasal Date: Wed, 23 Nov 2022 13:21:21 +0530 Subject: [PATCH 2/5] Add as a raw string if not file for FileStringSliceOptions - If input is FileStringSliceOptions then it will only accept filename or string input. (not a list of input seperated by commas) - Adding this RawString as boolean flag in options - Set above flag as true for FileStringSliceOptions --- slice_common.go | 31 +++++++------------------------ string_slice_options.go | 1 + string_slice_test.go | 2 -- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/slice_common.go b/slice_common.go index edcf028..456556e 100644 --- a/slice_common.go +++ b/slice_common.go @@ -49,6 +49,7 @@ type Options struct { IsFromFile func(string) bool IsEmpty func(string) bool Normalize func(string) string + RawString bool } // ToStringSlice converts a value to string slice based on options @@ -59,6 +60,7 @@ func ToStringSlice(value string, options Options) ([]string, error) { } addPartToResult := func(part string) { + if !options.IsEmpty(part) { if options.Normalize != nil { part = options.Normalize(part) @@ -74,6 +76,8 @@ func ToStringSlice(value string, options Options) ([]string, error) { for line := range linesChan { addPartToResult(line) } + } else if options.RawString { + addPartToResult(value) } else { index := 0 for index < len(value) { @@ -91,12 +95,9 @@ func ToStringSlice(value string, options Options) ([]string, error) { } else { commaFound, part := searchPart(value[index:], ',') - // ingore comma if it inside quotes eg. [uncover -q 'org:"Something, Inc."'] - if commaFound && commaBetweenQuotes(value[index:]) { - part = value[index:] - index += len(part) - } else if commaFound { + if commaFound { index += len(part) + 1 + } else { index += len(part) } @@ -104,8 +105,8 @@ func ToStringSlice(value string, options Options) ([]string, error) { addPartToResult(part) } } - } + } return result, nil } @@ -128,21 +129,3 @@ func normalize(s string) string { func normalizeLowercase(s string) string { return strings.TrimSpace(strings.Trim(strings.TrimSpace(strings.ToLower(s)), string(quotes))) } - -func commaBetweenQuotes(value string) bool { - commaPos := strings.IndexRune(value, ',') // TODO debug the test - // check comma is inside quotes or not - if commaPos != -1 { - for _, quote := range quotes { - - firstQuote := strings.IndexRune(value, quote) - if firstQuote != -1 { - lastQuote := strings.IndexRune(value[firstQuote+1:], quote) - if firstQuote < commaPos && commaPos < (lastQuote+firstQuote) { - return true - } - } - } - } - return false -} diff --git a/string_slice_options.go b/string_slice_options.go index 179b12f..2075a5d 100644 --- a/string_slice_options.go +++ b/string_slice_options.go @@ -58,6 +58,7 @@ var FileStringSliceOptions = Options{ IsEmpty: isEmpty, Normalize: normalizeTrailingParts, IsFromFile: isFromFile, + RawString: true, } // NormalizedStringSliceOptions represents a list of items diff --git a/string_slice_test.go b/string_slice_test.go index 5727f18..b79350a 100644 --- a/string_slice_test.go +++ b/string_slice_test.go @@ -1,7 +1,6 @@ package goflags import ( - "fmt" "os" "testing" @@ -46,7 +45,6 @@ func TestNormalizedStringSlicePositive(t *testing.T) { for value, expected := range slices { result, err := ToStringSlice(value, NormalizedStringSliceOptions) - fmt.Println(result) assert.Nil(t, err) assert.Equal(t, result, expected) } From f7b5369eeb6561892f08dd6fd914c56c2955759a Mon Sep 17 00:00:00 2001 From: shubhamrasal Date: Wed, 23 Nov 2022 16:26:11 +0530 Subject: [PATCH 3/5] Remove extra lines --- slice_common.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/slice_common.go b/slice_common.go index 456556e..4911cb0 100644 --- a/slice_common.go +++ b/slice_common.go @@ -60,7 +60,6 @@ func ToStringSlice(value string, options Options) ([]string, error) { } addPartToResult := func(part string) { - if !options.IsEmpty(part) { if options.Normalize != nil { part = options.Normalize(part) @@ -97,7 +96,6 @@ func ToStringSlice(value string, options Options) ([]string, error) { if commaFound { index += len(part) + 1 - } else { index += len(part) } @@ -105,7 +103,6 @@ func ToStringSlice(value string, options Options) ([]string, error) { addPartToResult(part) } } - } return result, nil } From fb6067ee213433ca06f4ade935314033880db5e3 Mon Sep 17 00:00:00 2001 From: shubhamrasal Date: Wed, 23 Nov 2022 17:09:20 +0530 Subject: [PATCH 4/5] Add test case for FileStringSliceOptions command line value --- string_slice_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/string_slice_test.go b/string_slice_test.go index b79350a..9a1274a 100644 --- a/string_slice_test.go +++ b/string_slice_test.go @@ -94,6 +94,11 @@ func TestFileStringSliceOptions(t *testing.T) { result, err := ToStringSlice(filename, FileStringSliceOptions) assert.Nil(t, err) assert.Equal(t, []string{"value1,value2", "value3"}, result, "could not get correct path") + + // command line input value + result, err = ToStringSlice("string:\"contains, comma and quotes.\"", FileStringSliceOptions) + assert.Nil(t, err) + assert.Equal(t, []string{"string:\"contains, comma and quotes.\""}, result, "could not get correct path") } func TestFileNormalizedOriginalStringSliceOptions(t *testing.T) { From 76b93703f57b7dc5b75657f2f38e4beedda0a723 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Thu, 24 Nov 2022 08:53:04 +0100 Subject: [PATCH 5/5] adding comments to options --- slice_common.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/slice_common.go b/slice_common.go index 4911cb0..031c233 100644 --- a/slice_common.go +++ b/slice_common.go @@ -46,10 +46,14 @@ func ToString(slice []string) string { } type Options struct { + // IsFromFile determines if the values are from file IsFromFile func(string) bool - IsEmpty func(string) bool - Normalize func(string) string - RawString bool + // IsEmpty determines if the values are empty + IsEmpty func(string) bool + // Normalize the value (eg. removing trailing spaces) + Normalize func(string) string + // RawString determines if the value should be considered as is + RawString bool } // ToStringSlice converts a value to string slice based on options