Documentation
¶
Index ¶
- func WithFormat(formats ...string) parseOption
- type Collection
- type Version
- func (c *Version) Compare(v *Version) int
- func (c *Version) Equal(v *Version) bool
- func (c *Version) GetFormat() string
- func (c *Version) GetMajor() string
- func (c *Version) GetMicro() string
- func (c *Version) GetMinor() string
- func (c *Version) GetModifier() string
- func (c *Version) GreaterThan(v *Version) bool
- func (c *Version) GreaterThanOrEqual(v *Version) bool
- func (c *Version) IncMajor() error
- func (c *Version) IncMicro() error
- func (c *Version) IncMinor() error
- func (c *Version) IncModifier() error
- func (c *Version) LessThan(v *Version) bool
- func (c *Version) LessThanOrEqual(v *Version) bool
- func (c *Version) Series(level string) string
- func (c *Version) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithFormat ¶
func WithFormat(formats ...string) parseOption
WithFormat is a parse option that specifies the format string that should be used to parse the version string.
Example:
ver, err := ParseWithOptions("2025.07.14", WithFormat("<YYYY>.<0M>.<0D>")) if err != nil { return err } fmt.Println(ver.String()) // 2025.07.14
If there are more than one possible format that may match the version string, this function can be used with the WithFormat option.
Example:
formats := []string{ "<YYYY>.<0M>.<0D>", "<YYYY>.<0M>.<0D>-<MODIFIER>", } ver, err := ParseWithOptions("2025.07.14", WithFormat(formats...)) if err != nil { return err } fmt.Println(ver.String()) // 2025.07.14
If multiple formats are provided, the format that matches the version string will be used. For example, in the following code, the format `"Rel-<YYYY>-<0M>-<0D>"` will be used at it matches the version string while the other formats do not.
ver, err := ParseWithOptions( "Rel-2025-07-14", WithFormat( "Rel-<YYYY>", "Rel-<YYYY>-<0M>", "Rel-<YYYY>-<0M>-<0D>", ) ) if err != nil { return err } fmt.Println(ver.String()) // Rel-2025-07-14
Types ¶
type Collection ¶
type Collection []*Version
Collection is a collection of Version objects. It implements the sort.Interface interface.
func NewCollection ¶
func NewCollection(format string, versions ...string) (Collection, error)
NewCollection creates a new Collection from a format string and a list of versions. It will return an error if any of the versions do not match the format.
Example:
collection, err := calver.NewCollection( "<YYYY>.<0M>.<0D>", "2025.01.18", "2023.07.14", "2025.03.16", ) if err != nil { return err }
This is the same as calling `NewCollectionWithOptions(versions, WithFormat(format))`. Note that `WithFormat` option can take a list of formats.
func NewCollectionWithOptions ¶
func NewCollectionWithOptions(versions []string, opts ...parseOption) (Collection, error)
NewCollectionWithOptions creates a new `Collection` from a list of versions and a list of parse options. It will return an error if any of the versions do not match (any of) the format or if no options are provided.
Example:
collection, err := calver.NewCollectionWithOptions( []string{"2025.01.18", "2023.07.14", "2025.03.16"}, calver.WithFormat("<YYYY>.<0M>.<0D>"), ) if err != nil { return err }
If there are more than one possible format that may match the version string, this function can be used with the `WithFormat` option.
Example:
formats := []string{ "<YYYY>.<0M>.<0D>", "<YYYY>.<0M>.<0D>-<MODIFIER>", } collection, err := calver.NewCollectionWithOptions( []string{"2025.01.18", "2023.07.14", "2025.03.16"}, calver.WithFormat(formats...), ) if err != nil { return err }
func (Collection) Len ¶
func (c Collection) Len() int
func (Collection) Less ¶
func (c Collection) Less(i, j int) bool
func (Collection) Swap ¶
func (c Collection) Swap(i, j int)
type Version ¶
type Version struct { // Format is the original format string. If multiple formats were provided, // this will be the format that matched the version string. Format string // Major is the major version. This is guaranteed to be a number. Major string // Minor is the minor version. This is guaranteed to be a number. Minor string // Micro is the micro version. This is guaranteed to be a number. Micro string // Modifier is the modifier version. This can be a number or a string. Modifier string }
Version is the object representing a CalVer version. To get the string representation of the Version, use the String method.
func Parse ¶
Parse creates a new Version object from a format string and a version. The format string is expected to follow the conventions defined in ConventionsRegex.
Example:
ver, err := Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-14") if err != nil { return err } fmt.Println(ver.String()) // Rel-2025-07-14
This is the same as calling `ParseWithOptions(version, WithFormat(format))`
func ParseWithOptions ¶
ParseWithOptions creates a new Version object from a version string and a list of parse options.
Example:
ver, err := ParseWithOptions( "Rel-2025-07-14", WithFormat("Rel-<YYYY>-<0M>-<0D>"), ) if err != nil { return err } fmt.Println(ver.String()) // Rel-2025-07-14
If there are more than one possible format that may match the version string, this function can be used with the WithFormat option.
Example:
formats := []string{ "Rel-<YYYY>-<0M>-<0D>", "<YYYY>.<0M>.<0D>", } ver, err := ParseWithOptions("Rel-2025-07-14", WithFormat(formats...)) if err != nil { return err } fmt.Println(ver.String()) // Rel-2025-07-14
func (*Version) Compare ¶
Compare returns 0 if the versions are equal, -1 if the current version is less than the other version, and 1 if the current version is greater than the other version.
Example:
ver1, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-14") if err != nil { return err } ver2, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-15") if err != nil { return err } fmt.Printf("%d\n", ver1.Compare(ver2)) // -1
The comparison is done in the following order: major, minor, micro, modifier. Major, minor and micro are compared as integers whereas the modifier is compared as integer if it is a number otherwise as a string.
func (*Version) Equal ¶
Equal reports whether the version is equal to the other version. Example:
ver1, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-14") if err != nil { return err } ver2, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-15") if err != nil { return err } fmt.Printf("%t\n", ver1.Equal(ver2)) // false
func (*Version) GetModifier ¶
GetModifier returns the modifier version.
func (*Version) GreaterThan ¶
GreaterThan reports whether the version is greater than the other version. Example:
ver1, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-14") if err != nil { return err } ver2, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-15") if err != nil { return err } fmt.Printf("%t\n", ver1.GreaterThan(ver2)) // false
func (*Version) GreaterThanOrEqual ¶
GreaterThanOrEqual reports whether the version is greater than or equal to the other version. Example:
ver1, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-14") if err != nil { return err } ver2, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-15") if err != nil { return err } fmt.Printf("%t\n", ver1.GreaterThanOrEqual(ver2)) // false
func (*Version) IncMajor ¶
IncMajor increments the major version. If the major version is 0 padded it will retain the 0 padding unless the major version is of the form 09 or 099 or 0999 and so on.
func (*Version) IncMicro ¶
IncMicro increments the micro version. If the micro version is 0 padded it will retain the 0 padding unless the micro version is of the form 09 or 099 or 0999 and so on.
func (*Version) IncMinor ¶
IncMinor increments the minor version. If the minor version is 0 padded it will retain the 0 padding unless the minor version is of the form 09 or 099 or 0999 and so on.
func (*Version) IncModifier ¶
IncModifier increments the modifier version. If the modifier version is 0 padded it will retain the 0 padding unless the modifier version is of the form 09 or 099 or 0999 and so on.
It will return an error if the modifier is not a number.
func (*Version) LessThan ¶
LessThan reports whether the version is less than the other version. Example:
ver1, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-14") if err != nil { return err } ver2, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-15") if err != nil { return err } fmt.Printf("%t\n", ver1.LessThan(ver2)) // true
func (*Version) LessThanOrEqual ¶
LessThanOrEqual reports whether the version is less than or equal to the other version. Example:
ver1, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-14") if err != nil { return err } ver2, err := calver.Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-15") if err != nil { return err } fmt.Printf("%t\n", ver1.LessThanOrEqual(ver2)) // true
func (*Version) Series ¶
Series returns the series of the Version object. The series determined using the provided level. For example, if the level is major, the series will be the major version. If the level is minor, the series will be the major and minor version and so on.
If no level or an unrecognized level is provided, the series will be the entire version string.
Example:
ver, err := Parse("Rel-<YYYY>-<0M>-<0D>", "Rel-2025-07-14") if err != nil { return err } fmt.Println(ver.Series("major")) // Rel-2025 fmt.Println(ver.Series("minor")) // Rel-2025-07 fmt.Println(ver.Series("micro")) // Rel-2025-07-14 fmt.Println(ver.Series("modifier")) // Rel-2025-07-14-0 fmt.Println(ver.Series("")) // Rel-2025-07-14
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
collection
command
|
|
compare
command
|
|
newversion
command
|
|
series
command
|
|