Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
ksonnet
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ijaz Ahmad
ksonnet
Commits
770e2725
Commit
770e2725
authored
7 years ago
by
Angus Lees
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #41 from anguslees/regex
Add several regex native functions
parents
ab0483e7
8f119f1b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
lib/kubecfg.libsonnet
+17
-1
17 additions, 1 deletion
lib/kubecfg.libsonnet
lib/kubecfg_test.jsonnet
+8
-0
8 additions, 0 deletions
lib/kubecfg_test.jsonnet
utils/nativefuncs.go
+15
-0
15 additions, 0 deletions
utils/nativefuncs.go
utils/nativefuncs_test.go
+34
-0
34 additions, 0 deletions
utils/nativefuncs_test.go
with
74 additions
and
1 deletion
lib/kubecfg.libsonnet
+
17
−
1
View file @
770e2725
...
...
@@ -9,8 +9,24 @@
// element.
parseYaml
::
std
.
native
(
"parseYaml"
),
// escapeStringRegex(s): Quote the regex metacharacters found in s.
// The result is a regex that will match the original literal
// characters.
escapeStringRegex
::
std
.
native
(
"escapeStringRegex"
),
// resolveImage(image): convert the docker image string from
// image:tag into a more specific image@digest, depending on kubecfg
// command line flags.
resolveImage
::
std
.
native
(
"resolveImage"
)
resolveImage
::
std
.
native
(
"resolveImage"
),
// regexMatch(regex, string): Returns true if regex is found in
// string. Regex is as implemented in golang regexp package
// (python-ish).
regexMatch
::
std
.
native
(
"regexMatch"
),
// regexSubst(regex, src, repl): Return the result of replacing
// regex in src with repl. Replacement string may include $1, etc
// to refer to submatches. Regex is as implemented in golang regexp
// package (python-ish).
regexSubst
::
std
.
native
(
"regexSubst"
),
}
This diff is collapsed.
Click to expand it.
lib/kubecfg_test.jsonnet
+
8
−
0
View file @
770e2725
...
...
@@ -15,6 +15,14 @@ assert x == [[3, 4], {foo: "bar", baz: "xyzzy"}] : "got " + x;
local
i
=
kubecfg
.
resolveImage
(
"busybox"
);
assert
i
==
"busybox:latest"
:
"got "
+
i
;
assert
kubecfg
.
regexMatch
(
"o$"
,
"foo"
);
local
r1
=
kubecfg
.
escapeStringRegex
(
"f[o"
);
assert
r1
==
"f
\\
[o"
:
"got "
+
r1
;
local
r2
=
kubecfg
.
regexSubst
(
"e"
,
"tree"
,
"oll"
);
assert
r2
==
"trolloll"
:
"got "
+
r2
;
// Kubecfg wants to see something that looks like a k8s object
{
apiVersion
:
"test"
,
...
...
This diff is collapsed.
Click to expand it.
utils/nativefuncs.go
+
15
−
0
View file @
770e2725
...
...
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"io"
"regexp"
jsonnet
"github.com/strickyak/jsonnet_cgo"
"k8s.io/client-go/pkg/util/yaml"
...
...
@@ -48,4 +49,18 @@ func RegisterNativeFuncs(vm *jsonnet.VM, resolver Resolver) {
vm
.
NativeCallback
(
"resolveImage"
,
[]
string
{
"image"
},
func
(
image
string
)
(
string
,
error
)
{
return
resolveImage
(
resolver
,
image
)
})
vm
.
NativeCallback
(
"escapeStringRegex"
,
[]
string
{
"str"
},
func
(
s
string
)
(
string
,
error
)
{
return
regexp
.
QuoteMeta
(
s
),
nil
})
vm
.
NativeCallback
(
"regexMatch"
,
[]
string
{
"regex"
,
"string"
},
regexp
.
MatchString
)
vm
.
NativeCallback
(
"regexSubst"
,
[]
string
{
"regex"
,
"src"
,
"repl"
},
func
(
regex
,
src
,
repl
string
)
(
string
,
error
)
{
r
,
err
:=
regexp
.
Compile
(
regex
)
if
err
!=
nil
{
return
""
,
err
}
return
r
.
ReplaceAllString
(
src
,
repl
),
nil
})
}
This diff is collapsed.
Click to expand it.
utils/nativefuncs_test.go
+
34
−
0
View file @
770e2725
...
...
@@ -57,3 +57,37 @@ func TestParseYaml(t *testing.T) {
a[0] + a[1]`
)
check
(
t
,
err
,
x
,
"
\"
helloworld
\"\n
"
)
}
func
TestRegexMatch
(
t
*
testing
.
T
)
{
vm
:=
jsonnet
.
Make
()
defer
vm
.
Destroy
()
RegisterNativeFuncs
(
vm
,
NewIdentityResolver
())
_
,
err
:=
vm
.
EvaluateSnippet
(
"failtest"
,
`std.native("regexMatch")("[f", "foo")`
)
if
err
==
nil
{
t
.
Errorf
(
"regexMatch succeeded with invalid regex"
)
}
x
,
err
:=
vm
.
EvaluateSnippet
(
"test"
,
`std.native("regexMatch")("foo.*", "seafood")`
)
check
(
t
,
err
,
x
,
"true
\n
"
)
x
,
err
=
vm
.
EvaluateSnippet
(
"test"
,
`std.native("regexMatch")("bar.*", "seafood")`
)
check
(
t
,
err
,
x
,
"false
\n
"
)
}
func
TestRegexSubst
(
t
*
testing
.
T
)
{
vm
:=
jsonnet
.
Make
()
defer
vm
.
Destroy
()
RegisterNativeFuncs
(
vm
,
NewIdentityResolver
())
_
,
err
:=
vm
.
EvaluateSnippet
(
"failtest"
,
`std.native("regexSubst")("[f",s "foo", "bar")`
)
if
err
==
nil
{
t
.
Errorf
(
"regexSubst succeeded with invalid regex"
)
}
x
,
err
:=
vm
.
EvaluateSnippet
(
"test"
,
`std.native("regexSubst")("a(x*)b", "-ab-axxb-", "T")`
)
check
(
t
,
err
,
x
,
"
\"
-T-T-
\"\n
"
)
x
,
err
=
vm
.
EvaluateSnippet
(
"test"
,
`std.native("regexSubst")("a(x*)b", "-ab-axxb-", "${1}W")`
)
check
(
t
,
err
,
x
,
"
\"
-W-xxW-
\"\n
"
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment