exver

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: MIT Imports: 7 Imported by: 1

README

exver

Package exver implements parser and type for extended Sementic Versionning 2.0.

exver is superset of go's semver.

The general form of a semantic version string accepted by this package is

[v]MAJOR[.MINOR[.PATCH[.EXTRA][-PRERELEASE][+BUILD]]]

Basically same as go's semver package. But small extensios are added:

  • v prefix is optional.
  • One EXTRA version component is added.
  • All version components are limited at maximum of 9999.
    • This limitation is placed to convert version to numeric form.

numeric conversion

Core.Int64 converts core parts (version without pre-release and build-meta) into numeric form so that it can be saved in any storage and compared naturally within them.

The conversion logic is roghly equivalent of strconv.ParseInt(fmt.Sprintf("%04d%04d%04d%04d", a, b, c, d), 10, 64) but more efficient.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Core

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

Core is Version without v-prefix, pre-release and build-meta.

Core represents numeric version. In text representation, it contains 1 to 4 numeric component separated by '.' letter: <major> "." <minor> "." <patch> "." <extra>. Each comopnent is limited at maximum of 9999.

The zero value of Core is invalid and must be initialized with NewCore or ParseCore. However calling Core.String with zero value returns "0.0.0" and works fine almost all situation.

func MustNewCore

func MustNewCore(nums []uint16) Core

MustNewCore is like NewCore but panics if an error occurs.

func MustParseCore

func MustParseCore(s string) Core

MustParseCore is like ParseCore but panics if any error occurs.

func NewCore

func NewCore(nums []uint16) (Core, error)

NewCore initializes Core. len(nums) must be between 1 and 4. If any component of nums are greater than 9999, it returns an error.

func ParseCore

func ParseCore(s string) (Core, error)

ParseCore parses string expression and returns Core. s must be dot-separated numeric values without leading zeros. Same rules to NewCore apply here: the number of version fields must be in between 1 to 4 and each version component must not be greater than 9999.

func (Core) Compare

func (c Core) Compare(u Core) int

Compare returns

-1 if c is less than u,
 0 if c equals u,
+1 if c is greater than u.

Missing parts are always treated as 0. e.g. comparing 1 and 1.0 returns 0, 1.0.0.2 and 1.0 returns +1.

func (Core) Component

func (c Core) Component() [4]uint16

Component returns internal version field expression, ordered as <major>, <minor>, <patch>, <extra>.

func (Core) Extra added in v1.1.0

func (c Core) Extra() uint16

func (Core) Int64

func (c Core) Int64() int64

Int64 returns version fields as int64 value. The conversion logic is roghly same of `strconv.ParseInt(fmt.Sprintf("%04d%04d%04d%04d", a, b, c, d), 10, 64)`.

func (Core) Len added in v1.1.0

func (c Core) Len() int

func (Core) Major added in v1.1.0

func (c Core) Major() uint16

func (Core) MarshalJSON

func (c Core) MarshalJSON() ([]byte, error)

func (Core) MarshalText

func (c Core) MarshalText() ([]byte, error)

func (Core) Minor added in v1.1.0

func (c Core) Minor() uint16

func (Core) Normalize added in v1.1.0

func (c Core) Normalize() Core

Normalize normalizes c into sem ver compatible form, that is, <major> "." <minor> "." <patch>. If version compoments were missed, Normalize fills them as 0.

func (Core) NormalizeExtended added in v1.1.0

func (c Core) NormalizeExtended() Core

NormalizeExtended normalizes c into extended form, that is, <major> "." <minor> "." <patch> "." <extra>. If version compoments were missed, NormalizeExtended fills them as 0.

func (Core) Nums

func (c Core) Nums() []uint

Nums returns version fields as slice of uint. The length of the return value may vary depending on input for NewCore or ParseCore, e.g. if input was `1` then Nums returns []uint{1}, if was `1.2.3.4`, it returns []uint{1, 2, 3, 4}.

func (Core) Patch added in v1.1.0

func (c Core) Patch() uint16

func (Core) String

func (c Core) String() string

String returns string representation of the version. If c is zero, it returns "0.0.0".

func (*Core) UnmarshalJSON

func (c *Core) UnmarshalJSON(data []byte) error

func (*Core) UnmarshalText

func (c *Core) UnmarshalText(text []byte) error

type Version

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

Version represents an extended semantic versioning v2. The general form of a semantic version string accepted by Version is

[v]MAJOR[.MINOR[.PATCH[.EXTRA][-PRERELEASE][+BUILD]]

  • `v` prefix is optionally allowed
  • One extra version field (EXTRA) is also allowed
  • Each version components are limited to 9999.
  • pre-release and build-meta is only allowed when the verions is full (has PATCH) or extended (has EXTRA).

func MustParse

func MustParse(s string) Version

MustParse is like Parse but panics when s is not accepted as the extended version string.

func Parse

func Parse(s string) (Version, error)

Parse parses input string as Version.

func (Version) Build

func (v Version) Build() string

Build returns build-meta string. The retunred value is empty if v is not suffixed with build-meta.

func (Version) Compare

func (v Version) Compare(u Version) int

Compare returns

-1 if v is less than u,
 0 if v equals u,
+1 if v is greater than u.

Unlike Core.Compare, the number of version fields affects ordering: if cores and pre-release of both v and u are semantically same, the shorter one is considered less. e.g. 1.0 is less than 1.0.0.

The pre-release values are compared as per spec. see https://semver.org/#spec-item-11

func (Version) Core

func (v Version) Core() Core

Core rturns v wtihout v-prefix, pre-release and build-meta.

func (Version) MarshalJSON

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

func (Version) MarshalText

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

func (Version) PreRelease

func (v Version) PreRelease() string

PreRelease returns pre-release string. The returned value is empty if v is not suffixed with pre-prelease.

func (Version) PreReleaseSortable added in v1.1.0

func (v Version) PreReleaseSortable() string

PreReleaseSortable returns pre-release string normalized so that it can be sorted simply by ascii order.

The size of returned string is always fixed at 256 by laying following limitations;

  • Dot-separated parts are limited at maximum of 8.
  • Each sperated part is limited at maximum of 31 letters.

You can safely cut off the right-most character (v.PreReleaseSortable()[:255]) to fit it to 255 chars (for VARCHAR(255) fields.)

As per semantic version 2 spec, pre-release can be dot-separated ascii text. Each sperated part is compared as like version components in version core. Parts with only numeric value is compared as number, others as ascii text.

To simulate the comparison rule, PreReleaseSortable pads '0' at left if a part consits of only numeric value, otherwise right.

PreReleaseSortable returns strings.Repeat("~", 256) if v is not with pre-release, since as per the spec, version without pre-prelease is more than versions with that.

func (Version) String

func (v Version) String() string

func (*Version) UnmarshalJSON

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

func (*Version) UnmarshalText

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

func (Version) V

func (v Version) V() bool

V returns true if v is prefixed with `v`.

func (Version) WithBuild

func (v Version) WithBuild(buildMeta string) (Version, error)

WithBuild returns v with build-meta value is changed to the input. If the input is not compliant to the spec, it returns unmodified v and an non-nil error.

func (Version) WithCore

func (v Version) WithCore(core Core) Version

WithCore returns v with core value is changed to the input.

func (Version) WithPreRelease

func (v Version) WithPreRelease(prerelease string) (Version, error)

WithPreRelease returns v with pre-prelease value is changed to the input. If the input is not compliant to the spec, it returns unmodified v and an non-nil error.

func (Version) WithV

func (v Version) WithV(vPrefix bool) Version

WithV returns v with vPrefix value is changed to the input.

Jump to

Keyboard shortcuts

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