Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSON Marshal/Unmarshal #77

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 23 additions & 2 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import (
)

// Default timeout used when running regexp matches -- "forever"
var DefaultMatchTimeout = time.Duration(math.MaxInt64)
var (
DefaultMatchTimeout = time.Duration(math.MaxInt64)
DefaultUnmarshalOptions = None
dlclark marked this conversation as resolved.
Show resolved Hide resolved
)

// Regexp is the representation of a compiled regular expression.
// A Regexp is safe for concurrent use by multiple goroutines.
Expand All @@ -43,7 +46,7 @@ type Regexp struct {
code *syntax.Code // compiled program

// cache of machines for running regexp
muRun sync.Mutex
muRun *sync.Mutex
runner []*runner
}

Expand Down Expand Up @@ -72,6 +75,7 @@ func Compile(expr string, opt RegexOptions) (*Regexp, error) {
capsize: code.Capsize,
code: code,
MatchTimeout: DefaultMatchTimeout,
muRun: &sync.Mutex{},
}, nil
}

Expand Down Expand Up @@ -371,3 +375,20 @@ func (re *Regexp) GroupNumberFromName(name string) int {

return -1
}

// MarshalText implements [encoding.TextMarshaler]. The output
// matches that of calling the [Regexp.String] method.
func (re *Regexp) MarshalText() ([]byte, error) {
return []byte(re.String()), nil
}

// UnmarshalText implements [encoding.TextUnmarshaler] by calling
// [Compile] on the encoded value.
func (re *Regexp) UnmarshalText(text []byte) error {
newRE, err := Compile(string(text), DefaultUnmarshalOptions)
if err != nil {
return err
}
*re = *newRE
return nil
}
35 changes: 35 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package regexp2

import (
"encoding/json"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -1329,3 +1330,37 @@ func TestParseShortSlashPEnd(t *testing.T) {
t.Fatalf("Expected match")
}
}

func TestMarshal(t *testing.T) {
re := MustCompile(`.*`, 0)
m, err := json.Marshal(re)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if string(m) != `".*"` {
t.Fatalf(`Expected ".*"`)
}
}

func TestUnMarshal(t *testing.T) {
DefaultUnmarshalOptions = IgnoreCase
bytes := []byte(`"^[abc]"`)
var re *Regexp
err := json.Unmarshal(bytes, &re)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if re.options != IgnoreCase {
t.Fatalf("Expected options ignore case")
}
if re.String() != `^[abc]` {
t.Fatalf(`Expected "^[abc]"`)
}
ok, err := re.MatchString("A")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !ok {
t.Fatalf(`Expected match`)
}
dlclark marked this conversation as resolved.
Show resolved Hide resolved
}