version

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package version provides parsing of version strings, such as "1.2.3". The package returns a struct that contains several pieces of information.

The primary motivation for this package was to create a representation of versions that can be stored and sorted in a Postgres database. To that end, we turn all versions into a slice of `*decimal.Big` (github.com/ericlagergren/decimal) values.

It is not possible to produce reasonably sortable versions across multiple language ecosystems, or even between different packages in the same ecosystem. Instead, we simply aim to make sure that all versions of a single package are sortable, even if the versioning scheme for that package changes over time. We assume that even if the versioning scheme changes, that the "major" version portion of the new scheme will sort higher than the "major" version of the old scheme. We have "major" in quotes there because some versioning schemes don't really distinguish between major, minor, and patch portions of a version.

The various parsing schemes handle non-numeric components as well as numbers appropriately. For some schemes, non-numeric components like strings have special meaning. For example, in semver there is a strict ordering for "alpha", "beta", etc. For other schemes we simply encode non-numeric values into decimal values using the Unicode codepoint for each letter.

We currently make no guarantee that the decimal representation of a version will not change between releases of this module. That means that if you store the versions you may have to re-parse existing versions if you upgrade this module. Given that fact, you should always make sure to store the original version that you parsed alongside the decimal slice representation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(v1, v2 *Version) int

Compare returns:

<0 if the version in v1 is less than the version in v2
 0 if the version in v1 is equal to the version in v2
>0 if the version in v1 is greater than the version in v2

Versions that differ only by trailing zeros (e.g. "1.2" and "1.2.0") are equal.

Types

type ParsedAs

type ParsedAs int

ParsedAs is an enum for the various types of versions we can parse.

const (
	// Unknown should never be used.
	Unknown ParsedAs = iota
	// Generic is a generic version.
	Generic
	// SemVer is the well known semver scheme (https://semver.org/).
	SemVer
	// PerlDecimal is for Perl versions which are simply numbers (42, 1.2, etc.).
	PerlDecimal
	// PerlVString is for Perl v-strings like "v1.1.2" (but these don't
	// require the leading "v", so "1.2.3" is also valid).
	PerlVString
	// PHP is for PHP versions as used by composer.
	PHP
	// PythonLegacy is for Python versions before PEP440 was adopted.
	PythonLegacy
	// PythonPEP440 is for versions as described in PEP440.
	PythonPEP440
	// Ruby is for Ruby versions.
	Ruby
)

func ParsedAsString added in v0.0.3

func ParsedAsString(s string) (ParsedAs, error)

ParsedAsString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func ParsedAsValues added in v0.0.3

func ParsedAsValues() []ParsedAs

ParsedAsValues returns all values of the enum

func (ParsedAs) IsAParsedAs added in v0.0.3

func (i ParsedAs) IsAParsedAs() bool

IsAParsedAs returns "true" if the value is listed in the enum definition. "false" otherwise

func (ParsedAs) String added in v0.0.3

func (i ParsedAs) String() string

type Version

type Version struct {
	// Original is the string that was passed to the parsing func.
	Original string `json:"version"`
	// Decimal contains a slice of `*decimal.Big` values. This will always
	// contain at least one element.
	Decimal []*decimal.Big `json:"sortable_version"`
	// ParsedAs indicates which type the version was parsed as.
	ParsedAs ParsedAs `json:"-"`
}

Version is the struct returned from all parsing funcs.

func ParseGeneric

func ParseGeneric(version string) (*Version, error)

ParseGeneric parses the version string into an array of decimal numbers such that two parsed version strings can be compared. This function treats numbers as individually comparable segments and not as decimal numbers, i.e. 1.2 is parsed to be compared as two numbers: 1 and 2.

func ParsePHP added in v0.0.7

func ParsePHP(version string) (*Version, error)

ParsePHP attempts to parse a version according to the same rules used by composer (https://github.com/composer/semver)

func ParsePerl

func ParsePerl(version string) (*Version, error)

ParsePerl parses version using the version parsing algorithm used by version.pm (https://metacpan.org/pod/distribution/version/lib/version.pm). version.pm considers there to be two perl version types: decimal (1.20) and dotted-decimal (v1.2.3). This function parses both types and normalizes them to dotted-decimal for comparison purposes.

func ParsePython

func ParsePython(version string) (*Version, error)

ParsePython attempts to parse a version according to PEP440 (https://www.python.org/dev/peps/pep-0440/) and falls back to legacy Python parsing if that fails.

func ParseRuby added in v0.0.4

func ParseRuby(version string) (*Version, error)

ParseRuby attempts to parse a version according to the same rules used by rubygems (https://github.com/rubygems/rubygems)

func ParseSemVer

func ParseSemVer(version string) (*Version, error)

ParseSemVer parses the semantic version (https://semver.org/) version string into an array of decimal numbers such that two parsed version strings can be compared as required by the semantic versioning specification.

func (*Version) Clone added in v0.0.3

func (v *Version) Clone() *Version

Clone returns a new *Version that is a clone of the one passed as the method receiver.

func (*Version) String added in v0.0.3

func (v *Version) String() string

String returns a string representation of the version. Note that this is not the same as v.Original.

Jump to

Keyboard shortcuts

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