From 7b6f4de8860b6517f861a4d96b070f4f7b37d949 Mon Sep 17 00:00:00 2001 From: Abhimanyu Sharma Date: Fri, 19 Apr 2024 22:37:10 +0530 Subject: [PATCH] Add: new processor escape quote and fix test cases text --- cmd/processor_escape-quotes.go | 62 +++++++++++++++ processors/ascii85_test.go | 4 +- processors/base32_test.go | 4 +- processors/base64_test.go | 4 +- processors/base64url_test.go | 4 +- processors/crypto_test.go | 14 ++-- processors/emails_test.go | 2 +- processors/hex_test.go | 4 +- processors/html_test.go | 4 +- processors/ip_test.go | 2 +- processors/json_test.go | 10 +-- processors/lines_test.go | 10 +-- processors/markdown_test.go | 2 +- processors/morse_test.go | 2 +- processors/processor.go | 1 + processors/processor_test.go | 2 +- processors/rgb_test.go | 2 +- processors/rot_test.go | 2 +- processors/spaces_test.go | 4 +- processors/strings.go | 72 +++++++++++++++++ processors/strings_test.go | 139 ++++++++++++++++++++++++++++++--- processors/url_test.go | 4 +- 22 files changed, 304 insertions(+), 50 deletions(-) create mode 100644 cmd/processor_escape-quotes.go diff --git a/cmd/processor_escape-quotes.go b/cmd/processor_escape-quotes.go new file mode 100644 index 0000000..cc54397 --- /dev/null +++ b/cmd/processor_escape-quotes.go @@ -0,0 +1,62 @@ +// Code generated by github.com/abhimanyu003/sttr/cmd/generate.go. DO NOT EDIT + +package cmd + +import ( + "fmt" + "os" + + "github.com/abhimanyu003/sttr/processors" + "github.com/abhimanyu003/sttr/utils" + "github.com/spf13/cobra" +) + +var ( + escapeQuotes_flag_d bool + escapeQuotes_flag_s bool +) + +func init() { + escapeQuotesCmd.Flags().BoolVarP(&escapeQuotes_flag_d, "double-quote", "d", true, "Escape double quote") + escapeQuotesCmd.Flags().BoolVarP(&escapeQuotes_flag_s, "single-quote", "s", true, "Escape single quote") + rootCmd.AddCommand(escapeQuotesCmd) +} + +var escapeQuotesCmd = &cobra.Command{ + Use: "escape-quotes [string]", + Short: "Escapes single and double quotes by default", + Aliases: []string{"esc-quotes", "escape-quotes"}, + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + var err error + var in []byte + var out string + + if len(args) == 0 { + in = []byte(utils.ReadMultilineInput()) + } else { + if fi, err := os.Stat(args[0]); err == nil && !fi.IsDir() { + d, err := os.ReadFile(args[0]) + if err != nil { + return err + } + in = d + } else { + in = []byte(args[0]) + } + } + + flags := make([]processors.Flag, 0) + p := processors.EscapeQuotes{} + flags = append(flags, processors.Flag{Short: "d", Value: escapeQuotes_flag_d}) + flags = append(flags, processors.Flag{Short: "s", Value: escapeQuotes_flag_s}) + + out, err = p.Transform(in, flags...) + if err != nil { + return err + } + + _, err = fmt.Fprint(os.Stdout, out) + return err + }, +} diff --git a/processors/ascii85_test.go b/processors/ascii85_test.go index c5b1d30..adad3fd 100644 --- a/processors/ascii85_test.go +++ b/processors/ascii85_test.go @@ -29,7 +29,7 @@ func TestAscii85Encoding_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -106,7 +106,7 @@ func TestAscii85Decoding_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/base32_test.go b/processors/base32_test.go index b635274..b8f978d 100644 --- a/processors/base32_test.go +++ b/processors/base32_test.go @@ -29,7 +29,7 @@ func TestBase32Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -106,7 +106,7 @@ func TestBase32Decode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/base64_test.go b/processors/base64_test.go index 33a776f..3f7d143 100644 --- a/processors/base64_test.go +++ b/processors/base64_test.go @@ -29,7 +29,7 @@ func TestBase64Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -127,7 +127,7 @@ func TestBase64Decode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/base64url_test.go b/processors/base64url_test.go index 7ecd9ad..ca1472c 100644 --- a/processors/base64url_test.go +++ b/processors/base64url_test.go @@ -29,7 +29,7 @@ func TestBase64URLEncode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -127,7 +127,7 @@ func TestBase64URLDecode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/crypto_test.go b/processors/crypto_test.go index e065d27..79a15e6 100644 --- a/processors/crypto_test.go +++ b/processors/crypto_test.go @@ -31,7 +31,7 @@ func TestMD5Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -136,7 +136,7 @@ func TestSHA1Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -217,7 +217,7 @@ func TestSHA256Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -298,7 +298,7 @@ func TestSHA224Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -379,7 +379,7 @@ func TestSHA384Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -460,7 +460,7 @@ func TestSHA512Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -549,7 +549,7 @@ func TestBcrypt_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/emails_test.go b/processors/emails_test.go index 79afe0e..f7bd706 100644 --- a/processors/emails_test.go +++ b/processors/emails_test.go @@ -37,7 +37,7 @@ func TestExtractEmails_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/hex_test.go b/processors/hex_test.go index 4548e76..b5a3b89 100644 --- a/processors/hex_test.go +++ b/processors/hex_test.go @@ -29,7 +29,7 @@ func TestHexEncode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -115,7 +115,7 @@ func TestHexDecode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/html_test.go b/processors/html_test.go index c06e569..cbb77cb 100644 --- a/processors/html_test.go +++ b/processors/html_test.go @@ -29,7 +29,7 @@ func TestHTMLEncode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -123,7 +123,7 @@ func TestHTMLDecode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/ip_test.go b/processors/ip_test.go index 438a67e..1589e55 100644 --- a/processors/ip_test.go +++ b/processors/ip_test.go @@ -29,7 +29,7 @@ func TestExtractIPs_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/json_test.go b/processors/json_test.go index 8ca7a6e..fe42288 100644 --- a/processors/json_test.go +++ b/processors/json_test.go @@ -40,7 +40,7 @@ func TestJSON_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -163,7 +163,7 @@ func TestJSONToMSGPACK_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -251,7 +251,7 @@ func TestMSGPACKToJSON_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -347,7 +347,7 @@ func TestJSONUnescape_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -479,7 +479,7 @@ func TestJSONEscape_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/lines_test.go b/processors/lines_test.go index 5467730..bbd2d79 100644 --- a/processors/lines_test.go +++ b/processors/lines_test.go @@ -30,7 +30,7 @@ func TestCountLines_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -117,7 +117,7 @@ func TestShuffleLines_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -189,7 +189,7 @@ func TestSortLines_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -258,7 +258,7 @@ func TestUniqueLines_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -345,7 +345,7 @@ func TestReverseLines_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/markdown_test.go b/processors/markdown_test.go index 5365e06..bc600cb 100644 --- a/processors/markdown_test.go +++ b/processors/markdown_test.go @@ -29,7 +29,7 @@ func TestMarkdown_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/morse_test.go b/processors/morse_test.go index 5d8b215..86aca33 100644 --- a/processors/morse_test.go +++ b/processors/morse_test.go @@ -29,7 +29,7 @@ func TestMorseCodeEncode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/processor.go b/processors/processor.go index 6330bc3..6b75b7f 100644 --- a/processors/processor.go +++ b/processors/processor.go @@ -22,6 +22,7 @@ var List = []list.Item{ CountCharacters{}, CountLines{}, CountWords{}, + EscapeQuotes{}, ExtractEmails{}, ExtractIPs{}, FormatJSON{}, diff --git a/processors/processor_test.go b/processors/processor_test.go index e7d36b8..6d3b608 100644 --- a/processors/processor_test.go +++ b/processors/processor_test.go @@ -44,7 +44,7 @@ func TestZeropad_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/rgb_test.go b/processors/rgb_test.go index 5011b57..4b27b54 100644 --- a/processors/rgb_test.go +++ b/processors/rgb_test.go @@ -29,7 +29,7 @@ func TestRGB_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/rot_test.go b/processors/rot_test.go index c909522..6810901 100644 --- a/processors/rot_test.go +++ b/processors/rot_test.go @@ -73,7 +73,7 @@ func TestROT13Encode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/spaces_test.go b/processors/spaces_test.go index 4417616..870d978 100644 --- a/processors/spaces_test.go +++ b/processors/spaces_test.go @@ -37,7 +37,7 @@ func TestRemoveNewLines_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -134,7 +134,7 @@ func TestRemoveSpaces_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) diff --git a/processors/strings.go b/processors/strings.go index d52bfb4..cc0394b 100644 --- a/processors/strings.go +++ b/processors/strings.go @@ -345,3 +345,75 @@ func (p Reverse) Description() string { func (p Reverse) FilterValue() string { return p.Title() } + +// EscapeQuotes escape quotes from a given string +// Example: "test" to \"tset\". +type EscapeQuotes struct{} + +func (p EscapeQuotes) Name() string { + return "escape-quotes" +} + +func (p EscapeQuotes) Alias() []string { + return []string{"esc-quotes", "escape-quotes"} +} + +func (p EscapeQuotes) Transform(data []byte, f ...Flag) (string, error) { + result := "" + for _, v := range data { + for _, flag := range f { + switch flag.Short { + case "d": + if v == '"' { + result += "\\" + } + case "s": + if v == '\'' { + result += "\\" + } + } + } + if len(f) == 0 { + if v == '"' { + result += "\\" + } + if v == '\'' { + result += "\\" + } + } + result += string(v) + } + return result, nil +} + +func (p EscapeQuotes) Flags() []Flag { + return []Flag{ + { + Name: "double-quote", + Short: "d", + Desc: "Escape double quote", + Value: true, + Type: FlagBool, + }, + { + Name: "single-quote", + Short: "s", + Desc: "Escape single quote", + Value: true, + Type: FlagBool, + }, + } +} + +func (p EscapeQuotes) Title() string { + title := "Escape Quotes" + return fmt.Sprintf("%s (%s)", title, p.Name()) +} + +func (p EscapeQuotes) Description() string { + return "Escapes single and double quotes by default" +} + +func (p EscapeQuotes) FilterValue() string { + return p.Title() +} diff --git a/processors/strings_test.go b/processors/strings_test.go index 060fca6..d3436a8 100644 --- a/processors/strings_test.go +++ b/processors/strings_test.go @@ -29,7 +29,7 @@ func TestLower_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -101,7 +101,7 @@ func TestUpper_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -173,7 +173,7 @@ func TestCountCharacters_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -250,7 +250,7 @@ func TestTitle_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -329,7 +329,7 @@ func TestSnake_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -408,7 +408,7 @@ func TestKebab_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -487,7 +487,7 @@ func TestSlug_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -566,7 +566,7 @@ func TestReverse_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -650,7 +650,7 @@ func TestCountWords_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -724,7 +724,7 @@ func TestCamel_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -787,3 +787,122 @@ func TestStringToCamel(t *testing.T) { }) } } + +func TestEscapeQuotes_Command(t *testing.T) { + test := struct { + alias []string + description string + filterValue string + flags []Flag + name string + title string + }{ + alias: []string{"esc-quotes", "escape-quotes"}, + description: "Escapes single and double quotes by default", + filterValue: "Escape Quotes (escape-quotes)", + flags: []Flag{ + { + Name: "double-quote", + Short: "d", + Desc: "Escape double quote", + Value: true, + Type: FlagBool, + }, + { + Name: "single-quote", + Short: "s", + Desc: "Escape single quote", + Value: true, + Type: FlagBool, + }, + }, + name: "escape-quotes", + title: "Escape Quotes (escape-quotes)", + } + p := EscapeQuotes{} + if got := p.Alias(); !reflect.DeepEqual(got, test.alias) { + t.Errorf("Alias() = %v, want %v", got, test.alias) + } + if got := p.Description(); got != test.description { + t.Errorf("Description() = %v, want %v", got, test.description) + } + if got := p.FilterValue(); got != test.filterValue { + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) + } + if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { + t.Errorf("Flags() = %v, want %v", got, test.flags) + } + if got := p.Name(); got != test.name { + t.Errorf("Name() = %v, want %v", got, test.name) + } + if got := p.Title(); got != test.title { + t.Errorf("Title() = %v, want %v", got, test.title) + } +} + +func TestEscapeQuotes(t *testing.T) { + type args struct { + data []byte + f []Flag + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "Normal String double quote", + args: args{data: []byte("this is great \"test\"")}, + want: "this is great \\\"test\\\"", + }, + { + name: "Normal String single quote", + args: args{data: []byte("this is great 'test'")}, + want: "this is great \\'test\\'", + }, + { + name: "Both single and double quote", + args: args{data: []byte("this is 'great' \"test\"")}, + want: "this is \\'great\\' \\\"test\\\"", + }, + { + name: "Normal String double quote", + args: args{data: []byte("this is great \"test\"")}, + want: "this is great \\\"test\\\"", + }, + { + name: "Normal String double quote", + args: args{data: []byte("this is great \"test\""), f: []Flag{{Short: "d", Value: true}}}, + want: "this is great \\\"test\\\"", + }, + { + name: "Normal String single quote", + args: args{data: []byte("this is great 'test'"), f: []Flag{{Short: "s", Value: true}}}, + want: "this is great \\'test\\'", + }, + { + name: "Both single and double quote", + args: args{data: []byte("this is 'great' \"test\""), f: []Flag{{Short: "d", Value: true}, {Short: "s", Value: true}}}, + want: "this is \\'great\\' \\\"test\\\"", + }, + { + name: "String with no quote", + args: args{data: []byte("this is great test")}, + want: "this is great test", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := EscapeQuotes{} + got, err := p.Transform(tt.args.data, tt.args.f...) + if (err != nil) != tt.wantErr { + t.Errorf("Transform() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Transform() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/processors/url_test.go b/processors/url_test.go index 973235c..1fc13d8 100644 --- a/processors/url_test.go +++ b/processors/url_test.go @@ -29,7 +29,7 @@ func TestURLEncode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags) @@ -103,7 +103,7 @@ func TestURLDecode_Command(t *testing.T) { t.Errorf("Description() = %v, want %v", got, test.description) } if got := p.FilterValue(); got != test.filterValue { - t.Errorf("Flags() = %v, want %v", got, test.filterValue) + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) } if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { t.Errorf("Flags() = %v, want %v", got, test.flags)