exver

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: MIT Imports: 7 Imported by: 0

README

exver

EXtended VERsion.

The extended Sementic Versionning 2.0 that

  • allows v prefix (like go's semver does), also permits versions without it.
  • one extra version field is allowed (a.b.c.d is allowed)
    • That's rare, but there's software that works under this 4-fields versioning schema.
  • shortened versions are also allowed (e.g. v1, 1.2, etc)
    • These forms not can be suffixed with pre-release or build-meta.

version upper limitation

Each segment of version fields are limited up to 9999.

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.

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 represents only numeric part of Version.

The number of version fields may vary based on input between `a` to `a.b.c.d`. Each field is limited up to 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. Similar 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.

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)` but more efficient.

func (Core) MarshalJSON

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

func (Core) MarshalText

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

func (Core) Nums

func (c Core) Nums() []uint

Nums returns version fields as slice of uint. The length of the return value may vary on input, 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) 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 is an implementaion of semantic versioning v2 but with slight extensions. The general form of a semantic version string accepted by Version is

[v]A[.B[.C[.D][-PRERELEASE][+BUILD]]

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

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) 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(vFlag bool) Version

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

Jump to

Keyboard shortcuts

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