semver

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: MIT Imports: 9 Imported by: 1

README

SemVer

Go Reference Test Status codecov Go Report Card

https://github.com/Masterminds/semver, but support any numbers version part.

Install and Use

go get github.com/ImSingee/semver

See documents

Version Compare & Constraint Rules

Help me translate into English, wrap it in a pre block in markdown format for me; this is a Github Readme

Supported constraint syntax

Basic comparison

  • = or no symbol
  • !=
  • >
  • >=
  • <
  • <=

Logical operations

  • AND: Multiple matching conditions can be separated by ,

Range

  • V1 - V2 is equivalent to >= V1, <= V2

Wildcard

  • A single * matches any version number
  • 1.2.x is equivalent to >=1.2, <1.3

Minor version

  • ~1.2.3.4 is equivalent to >= 1.2.3.4, < 1.2.4
  • ~1.2.3 is equivalent to >= 1.2.3, < 1.3
  • Special ~1.2 is equivalent to >= 1.2, < 1.3
  • Special ~1 is equivalent to >= 1, < 2

Major version

  • ^1.2.3 is equivalent to >= 1.2.3, < 2
  • ^1.2 is equivalent to >= 1.2, < 2
  • ^1 is equivalent to >= 1, < 2

Matching different number of digits

Compare by padding 0 to the maximum significant digit

According to this rule, there are 1.1 == 1.1.0 == 1.1.0.0

Matching with pre-release version numbers

When excluding the same pre-release version numbers, the pre-release version number match will be performed

  • One has a pre-release version number, the other does not: the one without is larger
    • 1.0-hello < 1.0
  • Both have pre-release version numbers: perform pre-release version number ASCII comparison, which will be grouped by . and numeric groups will be matched numerically
    • 1.0-alpha < 1.0-beta
    • 1.0-alpha10 < 1.0-alpha2
    • 1.0-alpha.2 < 1.0-alpha.10
    • 1.0-alpha.100 < 1.0-beta

Metadata information

Only when = is matched and the constraint contains metadata will the metadata be checked, otherwise the metadata will be ignored

  • =1.0.0+hello can only match 1.0.0+hello but not 1.0.0 (however, it can match 1.0+hello 1+hello)
  • =1.0.0 can match 1.0.0 1.0.0+anything
  • >1.0.0 can match 1.0.1 1.0.1+anything
  • >1.0.0+hello is invalid

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidSemVer is returned a version is found to be invalid when
	// being parsed.
	ErrInvalidSemVer = errors.New("invalid Semantic Version")

	// ErrEmptyString is returned when an empty string is passed in for parsing.
	ErrEmptyString = errors.New("version string empty")

	// ErrInvalidCharacters is returned when invalid characters are found as
	// part of a version
	ErrInvalidCharacters = errors.New("invalid characters in version")

	// ErrSegmentStartsZero is returned when a version segment starts with 0.
	// This is invalid in SemVer.
	ErrSegmentStartsZero = errors.New("version segment starts with 0")

	// ErrInvalidMetadata is returned when the metadata is an invalid format
	ErrInvalidMetadata = errors.New("invalid Metadata string")

	// ErrInvalidPrerelease is returned when the pre-release is an invalid format
	ErrInvalidPrerelease = errors.New("invalid Prerelease string")
)

Functions

func FuzzNewConstraint added in v0.1.0

func FuzzNewConstraint(f *testing.F)

func FuzzNewVersion added in v0.1.0

func FuzzNewVersion(f *testing.F)

func FuzzStrictNewVersion added in v0.1.0

func FuzzStrictNewVersion(f *testing.F)

Types

type Collection

type Collection []*Version

Collection is a collection of Version instances and implements the sort interface. See the sort package for more details. https://golang.org/pkg/sort/

func (Collection) Len

func (c Collection) Len() int

Len returns the length of a collection. The number of Version instances on the slice.

func (Collection) Less

func (c Collection) Less(i, j int) bool

Less is needed for the sort interface to compare two Version objects on the slice. If checks if one is less than the other.

func (Collection) Swap

func (c Collection) Swap(i, j int)

Swap is needed for the sort interface to replace the Version objects at two different positions in the slice.

type Constraints

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

Constraints is one or more constraint that a semantic version can be checked against.

func NewConstraint

func NewConstraint(c string) (*Constraints, error)

NewConstraint returns a Constraints instance that a Version instance can be checked against. If there is a parse error it will be returned.

func (Constraints) Check

func (cs Constraints) Check(v *Version) bool

Check tests if a version satisfies the constraints.

func (Constraints) MarshalText added in v0.1.0

func (cs Constraints) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Constraints) String

func (cs Constraints) String() string

func (*Constraints) UnmarshalText added in v0.1.0

func (cs *Constraints) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Constraints) Validate

func (cs Constraints) Validate(v *Version) (bool, []error)

Validate checks if a version satisfies a constraint. If not a slice of reasons for the failure are returned in addition to a bool.

type Version

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

Version represents a single semantic version.

func MustParse

func MustParse(v string) *Version

MustParse parses a given version and panics on error.

func NewVersion

func NewVersion(v string) (sv *Version, err error)

NewVersion parses a given version and returns an instance of Version or an error if unable to parse the version. This version allow a `v` prefix

func NewVersionByParts added in v0.1.0

func NewVersionByParts(nums ...uint64) *Version

func StrictNewVersion

func StrictNewVersion(v string) (*Version, error)

StrictNewVersion parses a given version and returns an instance of Version or an error if unable to parse the version. Only parses valid semantic versions. Performs checking that can find errors within the version. If you want to allow optional v prefix, use the NewVersion() function.

func (*Version) Compare

func (v *Version) Compare(o *Version) int

Compare compares this version to another one. It returns -1, 0, or 1 if the version smaller, equal, or larger than the other version.

Versions are compared by X.Y.Z. Build metadata is ignored. Prerelease is lower than the version without a prerelease. Compare always takes into account prereleases. If you want to work with ranges using typical range syntaxes that skip prereleases if the range is not looking for them use constraints.

func (*Version) Copy

func (v *Version) Copy() Version

func (*Version) Equal

func (v *Version) Equal(o *Version) bool

Equal tests if two versions are equal to each other. Note, versions can be equal with different metadata since metadata is not considered part of the comparable version.

func (*Version) GreaterThan

func (v *Version) GreaterThan(o *Version) bool

GreaterThan tests if one version is greater than another one.

func (*Version) IncLast added in v0.1.0

func (v *Version) IncLast() Version

IncLast produces the next version on last part Same as IncPart(len(v.PartsNumber()))

func (*Version) IncMajor

func (v *Version) IncMajor() Version

IncMajor produces the next major (1st part) version. Same as IncPart(1)

func (*Version) IncMinor

func (v *Version) IncMinor() Version

IncMinor produces the next minor (2nd part) version. Same as IncPart(2)

func (*Version) IncPart

func (v *Version) IncPart(part int) Version

IncPart produces the next version on specific part If the part is not exist - increase the part number to specific - do same as last part If the part is last part - if the pre exist, will remove pre and metadata and won't increase the part - if the pre not exist, will remove metadata and increase the part If the part is not last part - will remove pre and metadata and increase the part - following parts will be set to zero

func (*Version) IncPatch

func (v *Version) IncPatch() Version

IncPatch produces the next patch (3rd part) version. Same as IncPart(3)

func (*Version) LessThan

func (v *Version) LessThan(o *Version) bool

LessThan tests if one version is less than another one.

func (*Version) Major

func (v *Version) Major() uint64

Major returns the major version.

func (Version) MarshalJSON

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

MarshalJSON implements json.Marshaler interface.

func (Version) MarshalText added in v0.1.0

func (v Version) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Version) Metadata

func (v *Version) Metadata() string

Metadata returns the metadata on the version.

func (*Version) Minor

func (v *Version) Minor() uint64

Minor returns the minor version.

func (*Version) Original

func (v *Version) Original() string

Original returns the original value passed in to be parsed.

func (*Version) Part

func (v *Version) Part(part int) uint64

func (*Version) PartsNumber

func (v *Version) PartsNumber() int

func (*Version) Patch

func (v *Version) Patch() uint64

Patch returns the patch version.

func (*Version) Prerelease

func (v *Version) Prerelease() string

Prerelease returns the pre-release version.

func (*Version) Scan

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

Scan implements the SQL.Scanner interface.

func (*Version) SetMetadata

func (v *Version) SetMetadata(metadata string) (Version, error)

SetMetadata defines metadata value. Value must not include the required 'plus' prefix.

func (*Version) SetPrerelease

func (v *Version) SetPrerelease(prerelease string) (Version, error)

SetPrerelease defines the prerelease value. Value must not include the required 'hyphen' prefix.

func (*Version) String

func (v *Version) String() string

String converts a Version object to a string. Note, if the original version contained a leading v this version will not. See the Original() method to retrieve the original value. Semantic Versions don't contain a leading v per the spec. Instead it's optional on implementation.

func (*Version) UnmarshalJSON

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

UnmarshalJSON implements JSON.Unmarshaler interface.

func (*Version) UnmarshalText added in v0.1.0

func (v *Version) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Version) Value

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

Value implements the Driver.Valuer interface.

Jump to

Keyboard shortcuts

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