Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(funcs): add semver functions #1829

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs-src/content/functions/semver.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
ns: semver
preamble: |
These functions allow user you to parse a [semantic version](http://semver.org/) string or test it with constraint.

It's implemented with the https://github.com/Masterminds/semver library.
funcs:
- name: semver.Semver
description: |
Returns a semantic version struct holding the `input` version string.

The returned struct are defined at: [`semver.Version`](https://pkg.go.dev/github.com/Masterminds/semver/v3#Version).
pipeline: true
arguments:
- name: input
required: true
description: The input to parse
examples:
- |
$ gomplate -i '{{ semver.Semver "v1.1.1"}}'
1.1.1
- |
$ gomplate -i '{{ (semver.Semver "v1.1.1").Major }}'
1
- |
$ gomplate -i 'the pre release version is {{ ("v1.1.1" | semver.Semver).SetPrerelease "beta.1" }}'
the pre release version is 1.1.1-beta.1
- name: semver.CheckConstraint
description: |
Test whether the input version matchs the constraint.

Ref: https://github.com/Masterminds/semver#checking-version-constraints
pipeline: true
arguments:
- name: constraint
required: true
description: The constraints expression to test.
- name: input
required: true
description: The input semantic version string to test.
examples:
- |
$ gomplate -i '{{ semver.CheckConstraint "> 1.0" "v1.1.1" }}'
true
- |
$ gomplate -i '{{ semver.CheckConstraint "> 1.0, <1.1" "v1.1.1" }}'
false
- |
$ gomplate -i '{{ "v1.1.1" | semver.CheckConstraint "> 1.0" }}'
true
85 changes: 85 additions & 0 deletions docs/content/functions/semver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
title: semver functions
menu:
main:
parent: functions
---

These functions allow user you to parse a [semantic version](http://semver.org/) string or test it with constraint.

It's implemented with the https://github.com/Masterminds/semver library.

## `semver.Semver`_(unreleased)_
**Unreleased:** _This function is in development, and not yet available in released builds of gomplate._

Returns a semantic version struct holding the `input` version string.

The returned struct are defined at: [`semver.Version`](https://pkg.go.dev/github.com/Masterminds/semver/v3#Version).

### Usage

```
semver.Semver input
```
```
input | semver.Semver
```

### Arguments

| name | description |
|------|-------------|
| `input` | _(required)_ The input to parse |

### Examples

```console
$ gomplate -i '{{ semver.Semver "v1.1.1"}}'
1.1.1
```
```console
$ gomplate -i '{{ (semver.Semver "v1.1.1").Major }}'
1
```
```console
$ gomplate -i 'the pre release version is {{ ("v1.1.1" | semver.Semver).SetPrerelease "beta.1" }}'
the pre release version is 1.1.1-beta.1
```

## `semver.CheckConstraint`_(unreleased)_
**Unreleased:** _This function is in development, and not yet available in released builds of gomplate._

Test whether the input version matchs the constraint.

Ref: https://github.com/Masterminds/semver#checking-version-constraints

### Usage

```
semver.CheckConstraint constraint input
```
```
input | semver.CheckConstraint constraint
```

### Arguments

| name | description |
|------|-------------|
| `constraint` | _(required)_ The constraints expression to test. |
| `input` | _(required)_ The input semantic version string to test. |

### Examples

```console
$ gomplate -i '{{ semver.CheckConstraint "> 1.0" "v1.1.1" }}'
true
```
```console
$ gomplate -i '{{ semver.CheckConstraint "> 1.0, <1.1" "v1.1.1" }}'
false
```
```console
$ gomplate -i '{{ "v1.1.1" | semver.CheckConstraint "> 1.0" }}'
true
```
1 change: 1 addition & 0 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func CreateFuncs(ctx context.Context, d *data.Data) template.FuncMap {
addToMap(f, funcs.CreateCollFuncs(ctx))
addToMap(f, funcs.CreateUUIDFuncs(ctx))
addToMap(f, funcs.CreateRandomFuncs(ctx))
addToMap(f, funcs.CreateSemverFuncs(ctx))
return f
}

Expand Down
40 changes: 40 additions & 0 deletions funcs/semver.go
Original file line number Diff line number Diff line change
@@ -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
}
61 changes: 61 additions & 0 deletions funcs/semver_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading