-
Notifications
You must be signed in to change notification settings - Fork 136
/
regexp.go
46 lines (37 loc) · 1023 Bytes
/
regexp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package yttlibrary
import (
"fmt"
"regexp"
"github.com/k14s/ytt/pkg/template/core"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
var (
RegexpAPI = starlark.StringDict{
"regexp": &starlarkstruct.Module{
Name: "regexp",
Members: starlark.StringDict{
"match": starlark.NewBuiltin("regexp.match", core.ErrWrapper(regexpModule{}.Match)),
},
},
}
)
type regexpModule struct{}
func (b regexpModule) Match(thread *starlark.Thread, f *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 2 {
return starlark.None, fmt.Errorf("expected exactly two arguments")
}
pattern, err := core.NewStarlarkValue(args.Index(0)).AsString()
if err != nil {
return starlark.None, err
}
target, err := core.NewStarlarkValue(args.Index(1)).AsString()
if err != nil {
return starlark.None, err
}
matched, err := regexp.MatchString(pattern, target)
if err != nil {
return starlark.None, err
}
return starlark.Bool(matched), nil
}