semver

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2022 License: MIT Imports: 7 Imported by: 0

README

semver

Version 2.0.0 compliant semantic versioning library, with parsing and comparison and no external dependencies. JSON and SQL ready.

Parse

v, err := semver.Parse("1.6.3-alpha")
if err != nil {
	// handle err
}
// v == &semver.Version{Major:1, Minor:6, Patch:3, Prerelease:[]string{"alpha"}, Build:[]string(nil), Stable:true}

vs, _ := semver.ParseMultiple([]string{"1.0.0", "1.1.0"})

Validate

var valid bool
if _, err := semver.Parse("1.0.0"); err == nil {
    valid = true
}

Compare

a, _ := semver.Parse("1.0.0")
b, _ := semver.Parse("1.0.1")

n := a.CompareTo(b) // n == 1 means a greater than b, n == 0 means a equal to b, n == -1 means a less than b

Filtering

vers, _ := semver.ParseMultiple([]string{"1.0.0", "2.0.0", "2.1.0", "3.0.0"})

v, err := semver.Filter("^2.0.0", vers)   
if err != nil {
// handle err
}
// v == [2.0.0, 2.1.0]
Specifying version ranges
  • ^ - include everything greater than or equal to the stated version that doesn't increment the first non-zero item of the version core
    • eg ^2.1.0 includes version 2.1.0 and any newer 2.x.x versions
    • eg ^0.3.0 will match only versions 0.3.0 and any newer 0.3.x versions
    • For example, ^2.2.1 can be expanded out as >=2.2.1 <3.0.0
  • ~ - include everything greater than or equal to the stated version in the current minor range
    • eg ~2.2.0 will match version 2.2.0 and any newer 2.2.x but not 2.3.x
  • > < = >= <= for version comparisons - specify a range of versions
    • eg >2.1.0 matches anything greater than 2.1.0
  • || - include multiple sets of version specifiers
    • eg ^2.0.0 <2.2.0 || > 2.3.0 matches versions that satisfy both ^2.0.0 <2.2.0 and >2.3.0

Version numbers must be in their complete form, for example 2 will not work - it must be 2.0.0.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorNoFilter             = errors.New("semver: Filter: no filter provided")
	ErrorEmptyFilter          = errors.New("semver: Filter: empty filter")
	ErrorPrereleaseDisallowed = errors.New("semver: Filter: ^ filter must not have prerelease identifiers")
)
View Source
var (
	ErrorIncompleteVersionCore     = errors.New("semver: Parse: incomplete version core")
	ErrorLeadingZero               = errors.New("semver: Parse: leading zeros are disallowed")
	ErrorEmptyPrereleaseIdentifier = errors.New("semver: Parse: empty prerelease identifier")
	ErrorEmptyBuildIdentifier      = errors.New("semver: Parse: empty build identifier")
)

Functions

func Format

func Format(x string) (string, error)

Types

type Slice

type Slice []*Version

func Filter

func Filter(filter string, options Slice) (Slice, error)

func ParseMultiple

func ParseMultiple(rawVersions []string) (Slice, error)

func (Slice) Len

func (s Slice) Len() int

func (Slice) Less

func (s Slice) Less(i, j int) bool

func (Slice) Swap

func (s Slice) Swap(i, j int)

type Version

type Version struct {
	Major, Minor, Patch int
	Prerelease, Build   []string
	Stable              bool
}

func MustParse

func MustParse(in string) *Version

func Parse

func Parse(in string) (*Version, error)

func (*Version) CompareTo

func (v *Version) CompareTo(vx *Version) int

CompareTo compares two instances of Version. If the return value is 0, the versions are equal. 1 is v > vx, -1 is v < vx, 0 is v == vx

func (*Version) MarshalJSON

func (v *Version) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Version) Scan

func (v *Version) Scan(data interface{}) error

Scan implements sql.Scanner

func (*Version) String

func (v *Version) String() string

func (*Version) UnmarshalJSON

func (v *Version) UnmarshalJSON(x []byte) error

UnmarshalJSON implements json.Unmarshaler

func (*Version) Value

func (v *Version) Value() (driver.Value, error)

Value implements driver.Valuer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL