Skip to content
Merged
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

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.

Are you sure you don't want to somehow export syntaxutil.RegexpString instead? I can't remember if we need to also use it outside of query.Regexp.String(), eg observability around regexes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I considered that, but I’d rather keep this PR narrower and avoid exporting the internal syntaxutil package unless really necessary. For what we're trying to fix here this should be enough.

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
2 changes: 1 addition & 1 deletion query/query_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func RegexpFromProto(p *webserverv1.Regexp) (*Regexp, error) {

func (r *Regexp) ToProto() *webserverv1.Regexp {
return &webserverv1.Regexp{
Regexp: r.Regexp.String(),
Regexp: r.RegexpString(),
FileName: r.FileName,
Content: r.Content,
CaseSensitive: r.CaseSensitive,
Expand Down
9 changes: 9 additions & 0 deletions query/query_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ func TestQueryRoundtrip(t *testing.T) {
}
}

func TestRegexpProtoUsesRegexpString(t *testing.T) {
q := &Regexp{Regexp: regexpMustParse(`a.*b`)}
protoQ := q.ToProto()

if got, want := protoQ.GetRegexp(), q.RegexpString(); got != want {
t.Fatalf("ToProto().Regexp = %q, want %q", got, want)
}
}

func regexpMustParse(s string) *syntax.Regexp {
re, err := syntax.Parse(s, syntax.Perl)
if err != nil {
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