Enable specifying a regex using a function
This commit is contained in:
parent
0c607302ed
commit
8849c7c30a
4 changed files with 64 additions and 0 deletions
|
@ -106,6 +106,7 @@ var funcs = map[string]map[int]interface{}{
|
|||
"if": {3: nil},
|
||||
"intersect": {-1: nil},
|
||||
"model": {2: nil, 3: nil, 4: nil},
|
||||
"regex": {1: nil},
|
||||
"table": {1: nil},
|
||||
"thing": {2: nil},
|
||||
"union": {-1: nil},
|
||||
|
|
|
@ -34,6 +34,8 @@ func Run(ctx context.Context, name string, args ...interface{}) (interface{}, er
|
|||
return intersect(ctx, args...)
|
||||
case "model":
|
||||
return model(ctx, args...)
|
||||
case "regex":
|
||||
return regex(ctx, args...)
|
||||
case "table":
|
||||
return table(ctx, args...)
|
||||
case "thing":
|
||||
|
|
25
util/fncs/regex.go
Normal file
25
util/fncs/regex.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package fncs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func regex(ctx context.Context, args ...interface{}) (*regexp.Regexp, error) {
|
||||
reg, _ := ensureString(args[0])
|
||||
return regexp.Compile(reg)
|
||||
}
|
36
util/fncs/regex_test.go
Normal file
36
util/fncs/regex_test.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package fncs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestRegex(t *testing.T) {
|
||||
|
||||
var res interface{}
|
||||
|
||||
Convey("regex() works properly", t, func() {
|
||||
res, _ = Run(context.Background(), "regex", "something")
|
||||
So(res, ShouldResemble, regexp.MustCompile("something"))
|
||||
res, _ = Run(context.Background(), "regex", `^[a-z]+\[[0-9]+\]$`)
|
||||
So(res, ShouldResemble, regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`))
|
||||
})
|
||||
|
||||
}
|
Loading…
Reference in a new issue