Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ type Regexp struct {
CaseSensitive bool
}

// RegexpString returns the marshaled raw regexp pattern for q.
func (q *Regexp) RegexpString() string {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes sense no matter what. We want a way to get at this. I do think in Sourcegraph though we want to be able to directly use syntaxutil.RegexpString. We had a need to marshal the string for observability, which is outside of the need of marshalling a query.Regexp

return syntaxutil.RegexpString(q.Regexp)
}

func (q *Regexp) String() string {
pref := ""
if q.FileName {
Expand All @@ -87,7 +92,7 @@ func (q *Regexp) String() string {
if q.CaseSensitive {
pref = "case_" + pref
}
return fmt.Sprintf("%sregex:%q", pref, syntaxutil.RegexpString(q.Regexp))
return fmt.Sprintf("%sregex:%q", pref, q.RegexpString())
}

// gobRegexp wraps Regexp to make it gob-encodable/decodable. Regexp contains syntax.Regexp, which
Expand All @@ -100,7 +105,7 @@ type gobRegexp struct {

// GobEncode implements gob.Encoder.
func (q Regexp) GobEncode() ([]byte, error) {
gobq := gobRegexp{Regexp: q, RegexpString: syntaxutil.RegexpString(q.Regexp)}
gobq := gobRegexp{Regexp: q, RegexpString: q.RegexpString()}
gobq.Regexp.Regexp = nil // can't be gob-encoded/decoded
return json.Marshal(gobq)
}
Expand Down
22 changes: 22 additions & 0 deletions query/regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,25 @@ func TestOptimize(t *testing.T) {
})
}
}

func TestRegexpRegexpString(t *testing.T) {
tests := []struct {
in string
want string
}{
{in: `abc`, want: `abc`},
{in: `a.*b`, want: `a(?-s:.)*b`},
}

for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
q := &Regexp{Regexp: mustParseRE(tt.in)}
if got := q.RegexpString(); got != tt.want {
t.Fatalf("RegexpString(%q) = %q, want %q", tt.in, got, tt.want)
}
if got := q.String(); got == tt.want {
t.Fatalf("String(%q) = raw pattern %q, want query formatting", tt.in, got)
}
})
}
}
Loading