From 97747ea0f3178f7f5890512d483fdccaa82846b0 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 9 Oct 2021 21:10:29 +0300 Subject: vetting and linting and some code restyling --- internal/regex/regex.go | 9 ++------- internal/regex/regex_test.go | 17 ++++++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) (limited to 'internal/regex') diff --git a/internal/regex/regex.go b/internal/regex/regex.go index 352ffd6..eb6e1b3 100644 --- a/internal/regex/regex.go +++ b/internal/regex/regex.go @@ -48,9 +48,7 @@ func new(regexStr string, flags []Flag) (Regex, error) { regexStr: regexStr, flags: flags, } - re, err := regexp.Compile(regexStr) - if err != nil { return r, err } @@ -94,11 +92,9 @@ func (r Regex) Serialize() (string, error) { for _, flag := range r.flags { flags = append(flags, flag.String()) } - if !r.initialized { return "", fmt.Errorf("Unable to serialize regex as not initialized properly: %v", r) } - return fmt.Sprintf("regex:%s %s", strings.Join(flags, ","), r.regexStr), nil } @@ -109,12 +105,12 @@ func Deserialize(str string) (Regex, error) { if len(s) < 2 { return NewNoop(), nil } - flagsStr := s[0] regexStr := s[1] if !strings.HasPrefix(flagsStr, "regex") { - return Regex{}, fmt.Errorf("unable to deserialize regex '%s': should start with string 'regex'", str) + return Regex{}, fmt.Errorf("unable to deserialize regex '%s': should start "+ + "with string 'regex'", str) } // Parse regex flags, e.g. "regex:flag1,flag2,flag3..." @@ -129,6 +125,5 @@ func Deserialize(str string) (Regex, error) { flags = append(flags, flag) } } - return new(regexStr, flags) } diff --git a/internal/regex/regex_test.go b/internal/regex/regex_test.go index 2ce49ac..033a286 100644 --- a/internal/regex/regex_test.go +++ b/internal/regex/regex_test.go @@ -9,7 +9,8 @@ func TestRegex(t *testing.T) { r := NewNoop() if !r.MatchString(input) { - t.Errorf("expected to match string '%s' with noop regex '%v' but didn't\n", input, r) + t.Errorf("expected to match string '%s' with noop regex '%v' but didn't\n", + input, r) } r, err := New(".hello", Default) @@ -17,7 +18,8 @@ func TestRegex(t *testing.T) { t.Errorf("unable to create regex: %v\n", err) } if r.MatchString(input) { - t.Errorf("expected to match string '%s' with regex '%v' but didn't\n", input, r) + t.Errorf("expected to match string '%s' with regex '%v' but didn't\n", + input, r) } serialized, err := r.Serialize() @@ -29,8 +31,8 @@ func TestRegex(t *testing.T) { t.Errorf("unable to serialize deserialized regex: %v: %v\n", serialized, err) } if r.String() != r2.String() { - t.Errorf("regex should be the same after deserialize(serialize(..)), got '%s' but expected '%s'.\n", - r2.String(), r.String()) + t.Errorf("regex should be the same after deserialize(serialize(..)), got "+ + "'%s' but expected '%s'.\n", r2.String(), r.String()) } r, err = New(".hello", Invert) @@ -38,7 +40,8 @@ func TestRegex(t *testing.T) { t.Errorf("unable to create regex: %v\n", err) } if !r.MatchString(input) { - t.Errorf("expected to not match string '%s' with regex '%v' but matched\n", input, r) + t.Errorf("expected to not match string '%s' with regex '%v' but matched\n", + input, r) } serialized, err = r.Serialize() @@ -50,7 +53,7 @@ func TestRegex(t *testing.T) { t.Errorf("unable to serialize deserialized regex: %v: %v\n", serialized, err) } if r.String() != r2.String() { - t.Errorf("regex should be the same after deserialize(serialize(..)), got '%s' but expected '%s'.\n", - r2.String(), r.String()) + t.Errorf("regex should be the same after deserialize(serialize(..)), got "+ + "'%s' but expected '%s'.\n", r2.String(), r.String()) } } -- cgit v1.2.3