version – github.com/mooltiverse/nyx/src/go/version Index | Files

package version

import "github.com/mooltiverse/nyx/src/go/version"

This is the version package for Nyx, providing classes required to manage the supported versioning schemes (i.e. SemVer and Maven).

Index

Constants

const (
	// The range of allowed characters in string identifiers.
	ALLOWED_ALPHANUMERIC_CHARACTERS = "[0-9a-zA-Z-]"

	// The regexp pattern that can be used to validate characters in string identifiers.
	ALLOWED_ALPHANUMERIC_CHARACTERS_REGEXP_PATTERN = "^[0-9a-zA-Z-]+$"
)
const (
	DEFAULT_BUMP_VALUE = 1

	SEMANTIC_VERSION_DEFAULT_INITIAL_VERSION = "0.1.0"

	PRERELEASE_DELIMITER = "-"

	BUILD_DELIMITER = "+"

	SEMANTIC_VERSION_PATTERN_RELAXED = "" /* 182 byte string literal not displayed */

	SEMANTIC_VERSION_PATTERN = "" /* 193 byte string literal not displayed */
)
const (
	// The default separator used in the string representation if no other is specified.
	DEFAULT_SEPARATOR string = "."
)

Functions

func Compare

func Compare(scheme Scheme, v1 *string, v2 *string) int

Returns a negative integer, zero, or a positive integer as the version represented by v1 is less than, equal to, or greater than the version represented by v2, according to the given scheme. nil values are always considered less than non nil valid version identifiers. This method does no sanitization or prefix interpretation.

Arguments are as follows:

- scheme the scheme to check against. - v1 the first version to compare. It may be nil. If it's not a valid version it's considered as nil. - v2 the second version to compare. It may be nil. If it's not a valid version it's considered as nil.

Errors can be returned if:

- a given string doesn't represent a legal version, according to the selected scheme

func CompareWithPrefix

func CompareWithPrefix(scheme Scheme, v1 *string, v2 *string, prefix *string) int

Returns a negative integer, zero, or a positive integer as the version represented by v1 is less than, equal to, or greater than the version represented by v2, according to the given scheme. nil values are always considered less than non nil valid version identifiers. This method is different than CompareWithSanitization as it only tolerates a prefix, while CompareWithSanitization is more lenient as it also sanitizes extra characters in the body of the version identifier (when sanitize is true).

Arguments are as follows:

Errors can be returned if:

- a given string doesn't represent a legal version, according to the selected scheme

func CompareWithSanitization

func CompareWithSanitization(scheme Scheme, v1 *string, v2 *string, sanitize bool) int

Returns a negative integer, zero, or a positive integer as the version represented by v1 is less than, equal to, or greater than the version represented by v2, according to the given scheme. nil values are always considered less than non nil valid version identifiers. If sanitize is true this method will try to sanitize the given strings before parsing so that if there are illegal characters like a prefix or leading zeroes in numeric identifiers they are removed.

This method is different than CompareWithPrefix as it also sanitizes extra characters in the body of the version identifiers instead of just an optional prefix (when sanitize is true).

Arguments are as follows:

Errors can be returned if:

- a given string doesn't represent a legal version, according to the selected scheme

func GetSemanticVersionPrefix

func GetSemanticVersionPrefix(s string) (*string, error)

Takes the given string and tries to parse it as a semantic version with an optional prefix (which may be any string before the core major.minor.patch numbers). The returned string is the prefix before the core version number, if any, or nil otherwise.

Arguments are as follows:

- s a semantic version string which may have an additional prefix to be isolated

Errors can be returned if:

- the given string doesn't represent a legal semantic version, even tolerating the prefix

func HasCoreIdentifierWithName

func HasCoreIdentifierWithName(s string) bool

Returns true if there is a value with the given name. This method can be used to invoke ValueOfCoreIdentifiers safely.

func IsCore

func IsCore(scheme Scheme, s string) bool

Returns true if the given string is a legal version and it only contains core identifiers according to the given scheme.

This method uses a strict criteria, without trying to sanitize the given string.

Arguments are as follows:

- scheme the scheme to check against. - s the string version to check.

func IsCoreWithLenience

func IsCoreWithLenience(scheme Scheme, s string, lenient bool) bool

Returns true if the given string is a legal version and it only contains core identifiers according to the given scheme.

This method is different than IsCoreWithPrefix as it also sanitizes extra characters in the body of the version identifier instead of just an optional prefix (when sanitize is true).

Arguments are as follows:

func IsCoreWithPrefix

func IsCoreWithPrefix(scheme Scheme, s string, prefix *string) bool

Returns true if the given string is a legal version and it only contains core identifiers according to the given scheme.

This method is different than IsCoreWithLenience as it only tolerates a prefix, while IsCoreWithLenience is more lenient as it also sanitizes extra characters in the body of the version identifier (when sanitize is true).

Arguments are as follows:

func IsLegal

func IsLegal(scheme Scheme, s string) bool

Returns true if the given string is a legal version which, for example, can be parsed using ValueOf(Scheme, String) without errors using the implementation selected by the given scheme.

This method uses a strict criteria, without trying to sanitize the given string.

Arguments are as follows:

- scheme the scheme to check against. - s the string version to check.

func IsLegalSemanticVersion

func IsLegalSemanticVersion(s string) bool

Returns true if the given string is a legal semantic version which, for example, can be parsed without errors.

This method uses a strict criteria, without trying to sanitize the given string.

Arguments are as follows:

- s the string version to check.

func IsLegalSemanticVersionWithLenience

func IsLegalSemanticVersionWithLenience(s string, lenient bool) bool

Returns true if the given string is a legal semantic version which, for example, can be parsed without errors.

Arguments are as follows:

func IsLegalWithLenience

func IsLegalWithLenience(scheme Scheme, s string, lenient bool) bool

Returns true if the given string is a legal version which, for example, can be parsed using ValueOf(Scheme, String, boolean) without errors using the implementation selected by the given scheme.

This method is different than IsLegalWithPrefix as it also sanitizes extra characters in the body of the version identifier instead of just an optional prefix (when sanitize is true).

Arguments are as follows:

func IsLegalWithPrefix

func IsLegalWithPrefix(scheme Scheme, s string, prefix *string) bool

Returns true if the given string is a legal version which, for example, can be parsed using ValueOf(Scheme, String, boolean) without errors using the implementation selected by the given scheme.

This method is different than IsLegalWithLenience as it only tolerates a prefix, while IsLegalWithLenience is more lenient as it also sanitizes extra characters in the body of the version identifier (when sanitize is true).

Arguments are as follows:

func MostRelevantIdentifierBetween

func MostRelevantIdentifierBetween(scheme Scheme, identifier1 *string, identifier2 *string) *string

Returns the most relevant identifier between the given arguments, according to the given scheme ordering, or nil if both arguments are nil.

Arguments are as follows:

- scheme the scheme to peek the most relevand item from - identifier1 the first identifier to inspect - identifier2 the secondt identifier to inspect

func MostRelevantIdentifierIn

func MostRelevantIdentifierIn(scheme Scheme, identifiers []string) *string

Returns the most relevant identifier in the given collection, according to the given scheme ordering, or nil if the given list is empty.

Arguments are as follows:

- scheme the scheme to peek the most relevand item from - identifiers the identifiers to inspect

func SanitizeSemanticVersion

func SanitizeSemanticVersion(s string) (string, error)

Performs all of the sanitizations in the given string by sanitizing, in order, the prefix and leading zeroes in numeric identifiers. This order is the one that yields to the highest success probability in obtaining a legal version. Invoking this method is like invoking the sanitization methods SanitizeNumbers and SanitizePrefix.

Arguments are as follows:

- s the semantic version string to sanitize

Errors can be returned if:

- the given string doesn't represent a legal semantic version, even tolerating the aspects to sanitize

func SanitizeSemanticVersionNumbers

func SanitizeSemanticVersionNumbers(s string) (string, error)

Takes the given string and tries to parse it as a semantic version number, even with illegal characters or prefix. All numeric identifiers in the core version (major.minor.patch) and in the prerelease metadata are sanitized by removing all leading zeroes to make them compliant. Numeric identifiers in the build metadata part are left intact, even when they have leading zeroes. If the given string contains a prefix (see SanitizePrefix) or illegal characters they are left untouched and they are returned as they were in the input string.

Arguments are as follows:

Errors can be returned if:

- the given string doesn't represent a legal semantic version, even tolerating the prefix

func SanitizeSemanticVersionPrefix

func SanitizeSemanticVersionPrefix(s string) (string, error)

Takes the given string and tries to parse it as a semantic version with an optional prefix (which may be any string before the core major.minor.patch numbers). The returned string is the semantic version passed as input with the prefix removed. If no prefix is present then the returned string is the same as the one passed as input. Prefixes are often used (i.e. the 'v' used it Git tags or release-, rel etc) so this method helps in stripping those prefixes to get a compliant semantic version.

Arguments are as follows:

- s a semantic version string which may have an additional prefix to be removed

Errors can be returned if:

- the given string doesn't represent a legal semantic version, even tolerating the prefix

Types

type CoreIdentifiers

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

The identifiers used for core version numbers.

var (
	/*
	   The major number.
	*/
	MAJOR CoreIdentifiers = CoreIdentifiers{/* contains filtered or unexported fields */}

	/*
		The minor number.
	*/
	MINOR CoreIdentifiers = CoreIdentifiers{/* contains filtered or unexported fields */}

	/*
		The patch number.
	*/
	PATCH CoreIdentifiers = CoreIdentifiers{/* contains filtered or unexported fields */}
)

we can't have these instances as constants so we back up to a variables block

func ValueOfCoreIdentifiers

func ValueOfCoreIdentifiers(s string) (CoreIdentifiers, error)

Returns the value corresponding to the given string.

Errors can be returned:

- in case an unknown value is passed

func (CoreIdentifiers) GetName

func (ci CoreIdentifiers) GetName() string

Returns the name of the identifier.

func (CoreIdentifiers) GetPosition

func (ci CoreIdentifiers) GetPosition() int

Returns the relative position of this identifier (starting from 0.

func (CoreIdentifiers) String

func (ci CoreIdentifiers) String() string

Returns the string representation of the value

type MavenVersion

type MavenVersion struct {
}

The implementation of a Maven compliant version.

TODO: implement this class as per https://github.com/mooltiverse/nyx/issues/4. As of now this class is just a placeholder.

type Scheme

type Scheme string

The values of this enum are used to select the versioning scheme to use.

const (

	// The Semantic Versioning (https://semver.org/) scheme.
	SEMVER Scheme = "SEMVER"
)

func PointerToScheme

func PointerToScheme(s Scheme) *Scheme

Returns a pointer to the scheme passed as parameter.

This is useful for inline assignment of a constant scheme value.

func ValueOfScheme

func ValueOfScheme(s string) (Scheme, error)

Returns the scheme corresponding to the given string.

Errors can be returned:

- in case an unknown scheme is passed

func (Scheme) String

func (s Scheme) String() string

Returns the string representation of the scheme

type SemanticVersion

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

The implementation of a Semantic Versioning 2.0.0 compliant version.

Instances of this class are immutable so whenever you alter some values you actually receive a new instance holding the new value, while the old one remains unchanged.

func NewSemanticVersionWith

func NewSemanticVersionWith(major int, minor int, patch int) (SemanticVersion, error)

Builds a new version object with the given values.

Arguments are as follows:

- major the major number - minor the minor number - patch the patch number

Errors can be returned if:

- one of major, minor, patch is negative

func NewSemanticVersionWithAllIdentifiers

func NewSemanticVersionWithAllIdentifiers(major int, minor int, patch int, prereleaseIdentifiers []interface{}, buildIdentifiers []string) (SemanticVersion, error)

Builds a new version object with the given values.

Arguments are as follows:

Errors can be returned if:

func ValueOfSemanticVersion

func ValueOfSemanticVersion(s string) (SemanticVersion, error)

Returns a SemanticVersion instance representing the specified String value. No sanitization attempt is done.

Arguments are as follows:

- s the string to parse

Errors can be returned if:

- the given string doesn't represent a legal semantic version

func ValueOfSemanticVersionWithSanitization

func ValueOfSemanticVersionWithSanitization(s string, sanitize bool) (SemanticVersion, error)

This method is a shorthand for ValueOfSemanticVersion and SanitizeSemanticVersion.

Returns a SemanticVersion instance representing the specified String value. If sanitize is true this method will try to sanitize the given string before parsing so that if there are illegal characters like a prefix or leading zeroes in numeric identifiers they are removed.

When sanitization is enabled on a string that actually needs sanitization the string representation of the returned object will not exactly match the input value.

Arguments are as follows:

- s the string to parse - sanitize optionally enables sanitization before parsing

Errors can be returned if:

- the given string doesn't represent a legal semantic version

func (SemanticVersion) Bump

Returns a new instance with the number identified by the given value bumped. If the given value represents a core identifier (CoreIdentifiers}, namely major, minor, patch) then that identifier is bumped, otherwise the given id is used to bump a prerelease identifier by invoking BumpPrerelease.

In other words this method is a shorthand for BumpIdentifier and BumpPrerelease, the latter being used only when the given id is not a core identifier.

If the version already has multiple identifiers in the prerelease block that equal to the given value then all of them will be bumped. In case they have different numeric values (or missing) each occurrence is bumped independently according to the above rules.

Arguments are as follows:

- id the name of the identifier to bump

Errors can be returned if:

- the given string is empty, contains illegal characters or represents a number

func (SemanticVersion) BumpIdentifier

func (sv SemanticVersion) BumpIdentifier(id CoreIdentifiers) (SemanticVersion, error)

Returns a new instance with the number identified by the given value bumped.

Arguments are as follows:

- id the selector of the identifier to bump

func (SemanticVersion) BumpMajor

func (sv SemanticVersion) BumpMajor() (SemanticVersion, error)

Returns a new instance with the major number of this current instance incremented by one and the minor and patch numbers reset to zero. Prerelease and build parts are left intact.

func (SemanticVersion) BumpMinor

func (sv SemanticVersion) BumpMinor() (SemanticVersion, error)

Returns a new instance with the major number of this current instance, the minor number incremented by one and the patch number reset to zero. Prerelease and build parts are left intact.

func (SemanticVersion) BumpPatch

func (sv SemanticVersion) BumpPatch() (SemanticVersion, error)

Returns a new instance with the major and minor numbers of this current instance and the patch number incremented by one. Prerelease and build parts are left intact.

func (SemanticVersion) BumpPrerelease

func (sv SemanticVersion) BumpPrerelease(id string) (SemanticVersion, error)

Returns a new instance with the number identified by the given value bumped in the prerelease part. The core and the build blocks (if present) are left unchanged.

If this version doesn't have a prerelease block the returned version will have one, containing two identifiers: the given string and the following number .0.

If this version already has a prerelease block without any identifier that equals the given id, then the returned version has all the previous prerelease identifiers preceded by the two new identifiers the given string and the following number .1. If this version already has a prerelease block that contains a string identifier equal to the given id there are two options: if the selected identifier already has a numeric value that follows, the returned version will have that numeric identifier incremented by one; if the selected identifier doesn't have a numeric identifier that follows, a new numeric identifiers is added after the string with the initial value .1.

If the version already has multiple identifiers in the prerelease block that equal to the given value then all of them will be bumped. In case they have different numeric values (or missing) each occurrence is bumped independently according to the above rules.

Examples of invoking BumpPrerelease("alpha") on different versions: - 1.2.3 = 1.2.3-alpha.0 - 1.2.3-alpha = 1.2.3-alpha.0 - 1.2.3-alpha.beta = 1.2.3-alpha.0.beta - 1.2.3-gamma = 1.2.3-alpha.0.gamma - 1.2.3-gamma.delta = 1.2.3-alpha.0.gamma.delta - 1.2.3+999 = 1.2.3-alpha.0+999 - 1.2.3-alpha+999 = 1.2.3-alpha.0+999 - 1.2.3-alpha.beta+999 = 1.2.3-alpha.0.beta+999 - 1.2.3-gamma+999 = 1.2.3-alpha.0.gamma+999 - 1.2.3-gamma.delta+999 = 1.2.3-alpha.0.gamma.delta+999 - 1.2.3-alpha.alpha.1.alpha.2 = 1.2.3-alpha.0.alpha.2.alpha.3

Arguments are as follows:

- id the selector of the identifier to bump

Errors can be returned if:

- the given string is empty, contains illegal characters or represents a number

func (SemanticVersion) BumpVersion

func (sv SemanticVersion) BumpVersion(id string) (Version, error)

Returns a new instance with the number identified by the given value bumped. This is the same as Bump method but returns a generic object.

Arguments are as follows:

- id the name of the identifier to bump

Errors can be returned if:

- the given string is empty, contains illegal characters or does not represent a valid identifier to be bumped

func (SemanticVersion) CompareTo

func (sv SemanticVersion) CompareTo(v SemanticVersion) int

Compares this version with the specified version for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified version.

Semantic Versioning 2.0.0 states that:

Note that when the specification says "lower precedence" it translates to <, which means the "less" part is supposed to appear first in order. this means that the object with "lower precedence" returns a negative number.

However, to cope with total ordering, we need to amend the above rules and make them a little stricter just because two versions with different values can't be considered equal. With more detail:

Arguments are as follows:

- v the version to be compared.

func (SemanticVersion) Equals

func (sv SemanticVersion) Equals(obj interface{}) bool

Indicates whether some other object is "equal to" this one. This object equals to the given one if they are the same object or they are of the same type and hold exactly the same version.

Arguments are as follows:

- obj the reference object with which to compare.

func (SemanticVersion) GetBuild

func (sv SemanticVersion) GetBuild() *string

Returns the build part of the version, if any, or nil otherwise.

func (SemanticVersion) GetBuildAttributeValue

func (sv SemanticVersion) GetBuildAttributeValue(name string) *string

If an attribute with the given name is present in the build part, return the identifier after that, otherwise return nil.

Arguments are as follows:

- name the name of the attribute to look up. If nil or empty nil is returned

func (SemanticVersion) GetBuildIdentifiers

func (sv SemanticVersion) GetBuildIdentifiers() *[]string

Returns an array of the single identifiers of the build part of the version, if any, or nil otherwise.

func (SemanticVersion) GetCore

func (sv SemanticVersion) GetCore() string

Returns the core part (major.minor.patch) of the version as a string.

func (SemanticVersion) GetCoreIdentifiers

func (sv SemanticVersion) GetCoreIdentifiers() []int

Returns an array of the single identifiers of the core part of the version

func (SemanticVersion) GetMajor

func (sv SemanticVersion) GetMajor() int

Returns the major version number

func (SemanticVersion) GetMinor

func (sv SemanticVersion) GetMinor() int

Returns the minor version number

func (SemanticVersion) GetPatch

func (sv SemanticVersion) GetPatch() int

Returns the patch version number

func (SemanticVersion) GetPrerelease

func (sv SemanticVersion) GetPrerelease() *string

Returns the prerelease part of the version, if any, or nil otherwise.

func (SemanticVersion) GetPrereleaseAttributeValue

func (sv SemanticVersion) GetPrereleaseAttributeValue(name string) *int

If an attribute with the given name is present in the prerelease part, return the identifier after that, otherwise return nil.

Arguments are as follows:

- name the name of the attribute to look up. If nil or empty nil is returned

func (SemanticVersion) GetPrereleaseIdentifiers

func (sv SemanticVersion) GetPrereleaseIdentifiers() *[]interface{}

Returns an array of the single identifiers of the prerelease part of the version, if any, or nil otherwise.

func (SemanticVersion) GetScheme

func (sv SemanticVersion) GetScheme() Scheme

Returns SEMVER.

func (SemanticVersion) HasBuildAttribute

func (sv SemanticVersion) HasBuildAttribute(name string) bool

Returns true if an attribute with the given name is present in the build part, false otherwise.

Arguments are as follows:

- name the name of the attribute to look up. If nil or empty false is returned

func (SemanticVersion) HasPrereleaseAttribute

func (sv SemanticVersion) HasPrereleaseAttribute(name string) bool

Returns true if an attribute with the given name is present in the prerelease part, false otherwise.

Arguments are as follows:

- name the name of the attribute to look up. If nil or empty false is returned

func (SemanticVersion) RemoveBuildAttribute

func (sv SemanticVersion) RemoveBuildAttribute(name *string, removeValue bool) (SemanticVersion, error)

Returns a new instance with the new attribute removed from the build part, if any was present, otherwise the same version is returned. If the attribute is found and removeValue is true then also the attribute value (the attribute after the one identified by name) is removed, unless there are no more attributes after name.

Arguments are as follows:

- name the name of the attribute to remove from the build part, if present. If nil or empty no action is taken - removeValue if true also the attribute after name is removed (if any)

func (SemanticVersion) RemovePrereleaseAttribute

func (sv SemanticVersion) RemovePrereleaseAttribute(name *string, removeValue bool) (SemanticVersion, error)

Returns a new instance with the new attribute removed from the prerelease part, if any was present, otherwise the same version is returned. If the attribute is found and removeValue is true then also the attribute value (the attribute after the one identified by name) is removed, unless there are no more attributes after name or the value attribute is not numeric.

Arguments are as follows:

- name the name of the attribute to remove from the prerelease part, if present. If nil or empty no action is taken - removeValue if true also the attribute after name is removed (if any)

func (SemanticVersion) SetBuild

func (sv SemanticVersion) SetBuild(identifiers ...string) (SemanticVersion, error)

Returns a new version object with the build part set to the given values. If a nil value or an array of all nil values is passed then the returned version will have no build part, otherwise it will have all of the given non nil identifiers, with the core and prerelease elements of this version instance. If the current version had a build part it is completely replaced by the given identifiers.

Arguments are as follows:

Errors can be returned if:

- some non nil item passed contains illegal characters

func (SemanticVersion) SetBuildAttribute

func (sv SemanticVersion) SetBuildAttribute(name string) (SemanticVersion, error)

Returns a new version object with the new attribute added or replaced in the build part. This method is a shorthand for SetBuildAttribute to only set a simple identifier instead of a pair.

Arguments are as follows:

- name the name to set for the attribute

Errors can be returned if:

- some non nil item passed contains illegal characters

func (SemanticVersion) SetBuildAttributeWith

func (sv SemanticVersion) SetBuildAttributeWith(name string, value *string) (SemanticVersion, error)

Returns a new version object with the new attribute added or replaced in the build part. This method tries to be less intrusive as it only works on the given attribute (and its optional value) while leaving the other attributes unchanged.

If this version doesn't have a build part, the returned version will have one with the new attribute appended (and its value as well, if not nil).

If this version already has a build part with no identifier matching the given attribute name then the returned version will have the same build part as the current one with the new attribute appended (and its value as well, if not nil).

If this version already has a build part that contains an identifier matching the given attribute name then the identifier matching the attribute name is left unchanged and if the given value is not nil, the next identifier is added or replaced with the given value. ATTENTION: if the value is not nil the current identifier after the name (if any) is replaced without further consideration.

Examples of invoking SetBuildAttribute("build") with nil value: - 1.2.3 = 1.2.3+build - 1.2.3-alpha = 1.2.3-alpha+build - 1.2.3-alpha.beta = 1.2.3-alpha.beta+build - 1.2.3+timestamp = 1.2.3+timestamp.build - 1.2.3-alpha+timestamp.20200101 = 1.2.3-alpha+timestamp.20200101.build - 1.2.3+build = 1.2.3+build (unchanged) - 1.2.3+build.12345 = 1.2.3+build.12345 (unchanged) - 1.2.3+build.12345.timestamp.20200101 = 1.2.3+build.12345.timestamp.20200101 (unchanged)

Examples of invoking SetBuildAttribute("build") with 12345 value: - 1.2.3 = 1.2.3+build.12345 - 1.2.3-alpha = 1.2.3-alpha+build.12345 - 1.2.3-alpha.beta = 1.2.3-alpha.beta+build.12345 - 1.2.3+timestamp = 1.2.3+timestamp.build.12345 - 1.2.3-alpha+timestamp.20200101 = 1.2.3-alpha+timestamp.20200101.build.12345 - 1.2.3+build = 1.2.3+build.12345 - 1.2.3+build.12345 = 1.2.3+build.12345 (unchanged) - 1.2.3+build.12345.timestamp.20200101 = 1.2.3+build.12345.timestamp.20200101 (unchanged)

Arguments are as follows:

- name the name to set for the attribute - value the value to set for the attribute, or nil just set the attribute name, ignoring the value

Errors can be returned if:

- some non nil item passed contains illegal characters

func (SemanticVersion) SetCore

func (sv SemanticVersion) SetCore(major int, minor int, patch int) (SemanticVersion, error)

Returns a new version object with the major, minor and patch numbers set to the given values. This method doesn't reset any number and the prerelease and build blocks are left unchanged.

Arguments are as follows:

- major the major number - minor the minor number - patch the patch number

Errors can be returned if:

- any of the given values is negative

func (SemanticVersion) SetMajor

func (sv SemanticVersion) SetMajor(major int) (SemanticVersion, error)

Returns a new version object with the major number set to the given value. This method doesn't reset any number and the prerelease and build blocks are left unchanged.

Arguments are as follows:

- major the major number to set

Errors can be returned if:

- the given value is negative

func (SemanticVersion) SetMinor

func (sv SemanticVersion) SetMinor(minor int) (SemanticVersion, error)

Returns a new version object with the minor number set to the given value. This method doesn't reset any number and the prerelease and build blocks are left unchanged.

Arguments are as follows:

- minor the minor number to set

Errors can be returned if:

- the given value is negative

func (SemanticVersion) SetPatch

func (sv SemanticVersion) SetPatch(patch int) (SemanticVersion, error)

Returns a new version object with the patch number set to the given value. This method doesn't reset any number and the prerelease and build blocks are left unchanged.

Arguments are as follows:

- patch the patch number to set

Errors can be returned if:

- the given value is negative

func (SemanticVersion) SetPrerelease

func (sv SemanticVersion) SetPrerelease(identifiers ...interface{}) (SemanticVersion, error)

Returns a new version object with the prerelease part set to the given values. If a nil value or an array of all nil values is passed then the returned version will have no prerelease part, otherwise it will have all of the given non nil identifiers, with the core and build elements of this version instance.

Arguments are as follows:

Errors can be returned if:

func (SemanticVersion) SetPrereleaseAttribute

func (sv SemanticVersion) SetPrereleaseAttribute(name string) (SemanticVersion, error)

Returns a new version object with the new attribute added or replaced in the prerelease part. This method is a shorthand for SetPrereleaseAttribute to only set a simple identifier instead of a pair.

Arguments are as follows:

- name the name to set for the attribute

Errors can be returned if:

- some non nil item passed contains illegal characters

func (SemanticVersion) SetPrereleaseAttributeWith

func (sv SemanticVersion) SetPrereleaseAttributeWith(name string, value *int) (SemanticVersion, error)

Returns a new version object with the new attribute added or replaced in the prerelease part. This method tries to be less intrusive as it only works on the given attribute (and its optional value) while leaving the other attributes unchanged.

If this version doesn't have a prerelease part, the returned version will have one with the new attribute appended (and its value as well, if not nil).

If this version already has a prerelease part with no identifier matching the given attribute name then the returned version will have the same prerelease part as the current one with the new attribute appended (and its value as well, if not nil).

If this version already has a prerelease part that contains an identifier matching the given attribute name then the identifier matching the attribute name is left unchanged and if the given value is not nil, the next identifier is added or replaced with the given value. ATTENTION: if the value is not nil the current identifier after the name (if any) is replaced if it's a numeric identifier.

Examples of invoking SetPrereleaseAttribute("build") with nil value: - 1.2.3 = 1.2.3-build - 1.2.3-alpha = 1.2.3-alpha.build - 1.2.3-alpha.beta = 1.2.3-alpha.beta.build - 1.2.3+timestamp = 1.2.3-build+timestamp - 1.2.3-alpha+timestamp.20200101 = 1.2.3-alpha.build+timestamp.20200101 - 1.2.3-build = 1.2.3-build (unchanged) - 1.2.3-build.12345 = 1.2.3-build.12345 (unchanged) - 1.2.3-build.12345.timestamp.20200101 = 1.2.3-build.12345.timestamp.20200101 (unchanged)

Examples of invoking SetPrereleaseAttribute("build") with 12345 value: - 1.2.3 = 1.2.3-build.12345 - 1.2.3-alpha = 1.2.3-alpha.build.12345 - 1.2.3-alpha.beta = 1.2.3-alpha.beta.build.12345 - 1.2.3+timestamp = 1.2.3-build.12345+timestamp - 1.2.3-alpha+timestamp.20200101 = 1.2.3-alpha.build.12345+timestamp.20200101 - 1.2.3-build = 1.2.3-build.12345 - 1.2.3-build.12345 = 1.2.3-build.12345 (unchanged) - 1.2.3-build.12345.timestamp.20200101 = 1.2.3-build.12345.timestamp.20200101 (unchanged)

Arguments are as follows:

- name the name to set for the attribute - value the value to set for the attribute, or nil just set the attribute name, ignoring the value

Errors can be returned if:

- some non nil item passed contains illegal characters

func (SemanticVersion) String

func (sv SemanticVersion) String() string

Returns a string representation of the object.

type SemanticVersionStrings

type SemanticVersionStrings []string

The slice of semantic version strings that allows sorting for semantic versions in their string representations.

Sorting of semantic version strings can be done using the https://pkg.go.dev/sort package like:

versions := []string{
		"3.5.1",
		"1.2.3",
		"2.0.1-alpha4",
	}

sort.Sort(SemanticVersionStrings(versions))

func (SemanticVersionStrings) Len

func (svs SemanticVersionStrings) Len() int

Len is the number of elements in the collection.

This method implements the sort.Interface

func (SemanticVersionStrings) Less

func (svs SemanticVersionStrings) Less(i, j int) bool

Less reports whether the element with index i must sort before the element with index j.

If both Less(i, j) and Less(j, i) are false, then the elements at index i and j are considered equal. Sort may place equal elements in any order in the final result, while Stable preserves the original input order of equal elements.

Less must describe a transitive ordering: - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well. - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.

This method implements the sort.Interface

func (SemanticVersionStrings) Swap

func (svs SemanticVersionStrings) Swap(i, j int)

Swap swaps the elements with indexes i and j.

This method implements the sort.Interface

type SemanticVersions

type SemanticVersions []SemanticVersion

The slice of semantic versions that allows sorting for semantic versions.

Sorting of semantic versions can be done using the https://pkg.go.dev/sort package like:

sv1, _ := ValueOfSemanticVersion("3.5.1")
sv2, _ := ValueOfSemanticVersion("1.2.3")
sv3, _ := ValueOfSemanticVersion("2.0.1-alpha4")
versions := []SemanticVersion{
		sv1,
		sv2,
		sv3,
	}

sort.Sort(SemanticVersions(versions))

func (SemanticVersions) Len

func (svs SemanticVersions) Len() int

Len is the number of elements in the collection.

This method implements the sort.Interface

func (SemanticVersions) Less

func (svs SemanticVersions) Less(i, j int) bool

Less reports whether the element with index i must sort before the element with index j.

If both Less(i, j) and Less(j, i) are false, then the elements at index i and j are considered equal. Sort may place equal elements in any order in the final result, while Stable preserves the original input order of equal elements.

Less must describe a transitive ordering: - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well. - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.

This method implements the sort.Interface

func (SemanticVersions) Swap

func (svs SemanticVersions) Swap(i, j int)

Swap swaps the elements with indexes i and j.

This method implements the sort.Interface

type UseIntegerIdentifiers

type UseIntegerIdentifiers int8

This enum tells when to use integers instead of strings as Identifiers

const (
	/*
	   Always use Integer identifiers, Strings are not allowed
	*/
	ALWAYS UseIntegerIdentifiers = 0

	/*
		Never use Integer identifiers, only Strings are allowed
	*/
	NEVER UseIntegerIdentifiers = 1

	/*
		Use Integers when possible (when Strings can be converted to Integers), otherwise fallback to Strings.
	*/
	WHEN_POSSIBLE UseIntegerIdentifiers = 2
)

func ValueOfUseIntegerIdentifiers

func ValueOfUseIntegerIdentifiers(s string) (UseIntegerIdentifiers, error)

Returns the value corresponding to the given string.

Errors can be returned:

- in case an unknown value is passed

func (UseIntegerIdentifiers) String

func (uii UseIntegerIdentifiers) String() string

Returns the string representation of the value

type Version

type Version interface {
	/*
		Returns a new instance with the number identified by the given value bumped. The supported identifiers depend
		on the concrete subclass.

		Arguments are as follows:

		- id the name of the identifier to bump

		Errors can be returned if:

		- the given string is empty, contains illegal characters or does not represent
		  a valid identifier to be bumped
	*/
	BumpVersion(id string) (Version, error)

	/*
		Returns true if this version is equal to the given object, false otherwise
	*/
	Equals(obj interface{}) bool

	/*
		Returns the scheme that identifies the implementation
	*/
	GetScheme() Scheme

	/*
		Returns the string representation of this version
	*/
	String() string
}

The interface of Version classes.

func DefaultInitial

func DefaultInitial(scheme Scheme) Version

Returns a Version instance representing the default initial value to use for the given scheme.

Arguments are as follows:

- scheme the scheme to get the initial version for

func ValueOf

func ValueOf(scheme Scheme, s string) (Version, error)

Returns a Version instance representing the specified String value. No sanitization attempt is done.

Arguments are as follows:

- scheme the scheme the version belongs to - s the string to parse

Errors can be returned if:

- the given string doesn't represent a legal version, according to the selected scheme

func ValueOfWithPrefix

func ValueOfWithPrefix(scheme Scheme, s string, prefix *string) (Version, error)

Returns a Version instance representing the specified String value.

This method is different than ValueOfWithSanitization as it only tolerates a prefix, while ValueOfWithSanitization is more lenient as it also sanitizes extra characters in the body of the version identifier (when sanitize is true).

Arguments are as follows:

Errors can be returned if:

-the given string doesn't represent a legal version, according to the selected scheme

func ValueOfWithSanitization

func ValueOfWithSanitization(scheme Scheme, s string, sanitize bool) (Version, error)

Returns a Version instance representing the specified String value. No sanitization attempt is done.

If sanitize is true this method will try to sanitize the given string before parsing so that if there are illegal characters like a prefix or leading zeroes in numeric identifiers they are removed.

When sanitization is enabled on a string that actually needs sanitization the string representation of the returned object will not exactly match the input value.

This method is different than ValueOfWithPrefix as it also sanitizes extra characters in the body of the version identifier instead of just an optional prefix (when sanitize is true).

Arguments are as follows:

- scheme the scheme the version belongs to - s the string to parse - sanitize optionally enables sanitization before parsing

Errors can be returned if:

- the given string doesn't represent a legal version, according to the selected scheme

Source Files

composite_identifier.go composite_integer_identifier.go composite_object_identifier.go composite_string_identifier.go core_identifiers.go identifier.go integer_identifier.go maven_version.go parser.go scheme.go semantic_version.go semantic_version_build_identifier.go semantic_version_core_identifier.go semantic_version_identifiers.go semantic_version_pre_release_identifier.go simple_identifier.go string_identifier.go use_integer_identifiers.go utils.go version.go versions.go

Version
v0.0.0-20250213092846-5c6c0e6e1834 (latest)
Published
Feb 13, 2025
Platform
linux/amd64
Imports
6 packages
Last checked
5 days ago

Tools for package owners.