summaryrefslogtreecommitdiff
path: root/funcs
diff options
context:
space:
mode:
authorwuhuizuo <wuhuizuo@126.com>2023-11-25 00:21:01 +0800
committerGitHub <noreply@github.com>2023-11-24 16:21:01 +0000
commit635e78c38aa1cb0f31efff7f5cb0eecfe93f505c (patch)
tree6bbdfebc1bb28c7e90e7b1a82d04f59adfb330af /funcs
parentdba98c6d51f327959bf43691032ca16cee599264 (diff)
feat(funcs): add semver functions (#1829)
* feat(funcs): add semver functions - semver.Version: new a SemVer struct. - semver.MatchConstraint: match in arg with semver constraint string. refer to: https://github.com/Masterminds/sprig/blob/master/semver.go * docs(functions): add documents for semver functions * Update docs-src/content/functions/semver.yaml Co-authored-by: Dave Henderson <dhenderson@gmail.com> * fix(funcs,docs,docs-src): fix the reviewing issues --------- Co-authored-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'funcs')
-rw-r--r--funcs/semver.go40
-rw-r--r--funcs/semver_test.go61
2 files changed, 101 insertions, 0 deletions
diff --git a/funcs/semver.go b/funcs/semver.go
new file mode 100644
index 00000000..0212c998
--- /dev/null
+++ b/funcs/semver.go
@@ -0,0 +1,40 @@
+package funcs
+
+import (
+ "context"
+
+ "github.com/Masterminds/semver/v3"
+)
+
+// CreateSemverFuncs -
+func CreateSemverFuncs(ctx context.Context) map[string]interface{} {
+ ns := &SemverFuncs{ctx}
+ return map[string]interface{}{
+ "semver": func() interface{} { return ns },
+ }
+}
+
+// SemverFuncs -
+type SemverFuncs struct {
+ ctx context.Context
+}
+
+// Semver -
+func (SemverFuncs) Semver(version string) (*semver.Version, error) {
+ return semver.NewVersion(version)
+}
+
+// CheckConstraint -
+func (SemverFuncs) CheckConstraint(constraint, in string) (bool, error) {
+ c, err := semver.NewConstraint(constraint)
+ if err != nil {
+ return false, err
+ }
+
+ v, err := semver.NewVersion(in)
+ if err != nil {
+ return false, err
+ }
+
+ return c.Check(v), nil
+}
diff --git a/funcs/semver_test.go b/funcs/semver_test.go
new file mode 100644
index 00000000..f10aad5c
--- /dev/null
+++ b/funcs/semver_test.go
@@ -0,0 +1,61 @@
+package funcs
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestSemverFuncs_MatchConstraint(t *testing.T) {
+ tests := []struct {
+ name string
+ constraint string
+ in string
+ want bool
+ wantErr bool
+ }{
+ {
+ name: "mached constraint",
+ constraint: ">=1.0.0",
+ in: "v1.1.1",
+ want: true,
+ wantErr: false,
+ },
+ {
+ name: "not matched constraint",
+ constraint: "<1.0.0",
+ in: "v1.1.1",
+ want: false,
+ wantErr: false,
+ },
+ {
+ name: "wrong constraint",
+ constraint: "abc",
+ in: "v1.1.1",
+ want: false,
+ wantErr: true,
+ },
+ {
+ name: "wrong in",
+ constraint: ">1.0.0",
+ in: "va.b.c",
+ want: false,
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ s := SemverFuncs{
+ ctx: context.Background(),
+ }
+ got, err := s.CheckConstraint(tt.constraint, tt.in)
+ if tt.wantErr {
+ assert.Errorf(t, err, "SemverFuncs.CheckConstraint() error = %v, wantErr %v", err, tt.wantErr)
+ } else {
+ assert.NoErrorf(t, err, "SemverFuncs.CheckConstraint() error = %v, wantErr %v", err, tt.wantErr)
+ assert.Equal(t, tt.want, got)
+ }
+ })
+ }
+}