From 8849c7c30abc14d6bb4233f006203877123bdb88 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 11 Dec 2017 17:49:58 +0000 Subject: [PATCH] Enable specifying a regex using a function --- sql/funcs.go | 1 + util/fncs/fnc.go | 2 ++ util/fncs/regex.go | 25 +++++++++++++++++++++++++ util/fncs/regex_test.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 util/fncs/regex.go create mode 100644 util/fncs/regex_test.go diff --git a/sql/funcs.go b/sql/funcs.go index 78c7e0af..de638cd6 100644 --- a/sql/funcs.go +++ b/sql/funcs.go @@ -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}, diff --git a/util/fncs/fnc.go b/util/fncs/fnc.go index 0f5a77f3..fcd15ae2 100644 --- a/util/fncs/fnc.go +++ b/util/fncs/fnc.go @@ -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": diff --git a/util/fncs/regex.go b/util/fncs/regex.go new file mode 100644 index 00000000..830faff2 --- /dev/null +++ b/util/fncs/regex.go @@ -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) +} diff --git a/util/fncs/regex_test.go b/util/fncs/regex_test.go new file mode 100644 index 00000000..759db35b --- /dev/null +++ b/util/fncs/regex_test.go @@ -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]+\]$`)) + }) + +}