version

package module
Version: v0.0.0-...-035f676 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2019 License: MIT Imports: 5 Imported by: 519

README

go-version Build Status GoDoc

Version normalizer and comparison library for go, heavy based on PHP version_compare function and Version comparsion libs from Composer PHP project

Installation

The recommended way to install go-version

go get github.com/mcuadros/go-version

Examples

How import the package

import (
    "github.com/mcuadros/go-version"
)

version.Normalize(): Normalizes a version string to be able to perform comparisons on it

version.Normalize("10.4.13-b")
//Returns: 10.4.13.0-beta

version.CompareSimple(): Compares two normalizated version number strings

version.CompareSimple("1.2", "1.0.1")
//Returns: 1

version.CompareSimple("1.0rc1", "1.0")
//Returns: -1

version.Compare(): Compares two normalizated version number strings, for a particular relationship

version.Compare("1.0-dev", "1.0", "<")
//Returns: true

version.Compare("1.0rc1", "1.0", ">=")
//Returns: false

version.Compare("2.3.4", "v3.1.2", "<")
//Returns: true

version.ConstrainGroup.Match(): Match a given version againts a group of constrains, read about constraint string format at Composer documentation

c := version.NewConstrainGroupFromString(">2.0,<=3.0")
c.Match("2.5.0beta")
//Returns: true

c := version.NewConstrainGroupFromString("~1.2.3")
c.Match("1.2.3.5")
//Returns: true

version.Sort(): Sorts a string slice of version number strings using version.CompareSimple()

version.Sort([]string{"1.10-dev", "1.0rc1", "1.0", "1.0-dev"})
//Returns []string{"1.0-dev", "1.0rc1", "1.0", "1.10-dev"}

License

MIT, see LICENSE

Documentation

Overview

Version normalizer and comparison library for go, heavy based on PHP version_compare function and Version comparsion libs from Composer (https://github.com/composer/composer) PHP project

Index

Constants

View Source
const (
	Development = iota
	Alpha
	Beta
	RC
	Stable
)

Variables

This section is empty.

Functions

func Compare

func Compare(version1, version2, operator string) bool

Compare compares two version number strings, for a particular relationship

Usage

version.Compare("2.3.4", "v3.1.2", "<")
Returns: true

version.Compare("1.0rc1", "1.0", ">=")
Returns: false

func CompareNormalized

func CompareNormalized(version1, version2, operator string) bool

CompareNormalized compares two normalizated version number strings, for a particular relationship

The function first replaces _, - and + with a dot . in the version strings and also inserts dots . before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'.

Then it splits the results like if you were using Split(version, '.'). Then it compares the parts starting from left to right. If a part contains special version strings these are handled in the following order: any string not found in this list:

< dev < alpha = a < beta = b < RC = rc < # < pl = p.

Usage

version.CompareNormalized("1.0-dev", "1.0", "<")
Returns: true

version.CompareNormalized("1.0rc1", "1.0", ">=")
Returns: false

version.CompareNormalized("1.0", "1.0b1", "ge")
Returns: true

func CompareSimple

func CompareSimple(version1, version2 string) int

CompareSimple compares two normalizated version number strings

Just the same of CompareVersion but return a int result, 0 if both version are equal, 1 if the right side is bigger and -1 if the right side is lower

Usage

version.CompareSimple("1.2", "1.0.1")
Returns: 1

version.CompareSimple("1.0rc1", "1.0")
Returns: -1

func GetStability

func GetStability(version string) int

func Normalize

func Normalize(version string) string

Normalizes a version string to be able to perform comparisons on it

Example:

version.Normalize("10.4.13-b")
Returns: 10.4.13.0-beta

func RegFind

func RegFind(pattern, subject string) []string

func RegSplit

func RegSplit(pattern, subject string) []string

func Sort

func Sort(versionStrings []string)

Sorts a string slice of version number strings using version.CompareSimple()

Example:

version.Sort([]string{"1.10-dev", "1.0rc1", "1.0", "1.0-dev"})
Returns []string{"1.0-dev", "1.0rc1", "1.0", "1.10-dev"}

func ValidSimpleVersionFormat

func ValidSimpleVersionFormat(value string) bool

Types

type Constraint

type Constraint struct {
	// contains filtered or unexported fields
}

func NewConstrain

func NewConstrain(operator, version string) *Constraint

NewConstrain returns a new Constrain and sets operator and version to compare

func (*Constraint) GetOperator

func (self *Constraint) GetOperator() string

GetOperator gets operator to compare

func (*Constraint) GetVersion

func (self *Constraint) GetVersion() string

GetVersion gets version to compare

func (*Constraint) Match

func (self *Constraint) Match(version string) bool

Match a given version againts the constraint

func (*Constraint) SetOperator

func (self *Constraint) SetOperator(operator string)

Sets operator to compare

func (*Constraint) SetVersion

func (self *Constraint) SetVersion(version string)

Sets version to compare

func (*Constraint) String

func (self *Constraint) String() string

String returns a string representation

type ConstraintGroup

type ConstraintGroup struct {
	// contains filtered or unexported fields
}

func NewConstrainGroup

func NewConstrainGroup() *ConstraintGroup

NewConstrainGroup returns a new NewConstrainGroup

func NewConstrainGroupFromString

func NewConstrainGroupFromString(name string) *ConstraintGroup

NewConstrainGroupFromString returns a new NewConstrainGroup and create the constraints based on a string

Version constraints can be specified in a few different ways:

Exact version: You can specify the exact version of a package, for example 1.0.2.

Range: By using comparison operators you can specify ranges of valid versions. Valid operators are >, >=, <, <=, !=. An example range would be >=1.0. You can define multiple ranges, separated by a comma: >=1.0,<2.0.

Wildcard: You can specify a pattern with a * wildcard. 1.0.* is the equivalent of >=1.0,<1.1.

Next Significant Release (Tilde Operator): The ~ operator is best explained by example: ~1.2 is equivalent to >=1.2,<2.0, while ~1.2.3 is equivalent to >=1.2.3,<1.3. As you can see it is mostly useful for projects respecting semantic versioning. A common usage would be to mark the minimum minor version you depend on, like ~1.2 (which allows anything up to, but not including, 2.0). Since in theory there should be no backwards compatibility breaks until 2.0, that works well. Another way of looking at it is that using ~ specifies a minimum version, but allows the last digit specified to go up.

By default only stable releases are taken into consideration. If you would like to also get RC, beta, alpha or dev versions of your dependencies you can do so using stability flags. To change that for all packages instead of doing per dependency you can also use the minimum-stability setting.

From: http://getcomposer.org/doc/01-basic-usage.md#package-versions

func (*ConstraintGroup) AddConstraint

func (self *ConstraintGroup) AddConstraint(constraint ...*Constraint)

AddConstraint adds a Contraint to the group

func (*ConstraintGroup) GetConstraints

func (self *ConstraintGroup) GetConstraints() []*Constraint

GetConstraints returns all the constraints

func (*ConstraintGroup) Match

func (self *ConstraintGroup) Match(version string) bool

Match a given version againts the group

Usage

c := version.NewConstrainGroupFromString(">2.0,<=3.0")
c.Match("2.5.0beta")
Returns: true

c := version.NewConstrainGroupFromString("~1.2.3")
c.Match("1.2.3.5")
Returns: true

type PCRegMap

type PCRegMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

PCRegMap : PreCompiled Regex Map

func (*PCRegMap) MustCompile

func (p *PCRegMap) MustCompile(pattern string) *regexp.Regexp

MustCompile : to replace regexp.MustCompile in RegFind.

Jump to

Keyboard shortcuts

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