Documentation
¶
Overview ¶
Package calver parses, renders, and compares Calendar Versioning values.
Calendar Versioning defines common calendar components but does not define a universal version grammar or modifier precedence. This package therefore requires callers to compile an explicit Format before parsing values. A compiled Format is immutable and safe for concurrent use.
Parse with Format.Parse is allocation-free for valid input. Parsed Version values retain the input string, including a zero-copy Modifier slice when it is present. Keep this in mind when parsing small values from large buffers.
Compare provides a deterministic structural order for sorting arbitrary values. CompareCalendar is restricted to semantically compatible formats; it does not compare week-based values with month/day-based values.
Index ¶
- Variables
- type CompileError
- type ComponentKind
- type ErrorKind
- type Format
- type Level
- type List
- type ParseError
- type Parts
- type Version
- func (v Version) AppendTo(dst []byte) []byte
- func (v Version) BumpMajor() (Version, error)
- func (v Version) BumpMicro() (Version, error)
- func (v Version) BumpMinor() (Version, error)
- func (v Version) Compare(other Version) int
- func (v Version) CompareCalendar(other Version) (order int, ok bool)
- func (v Version) CompatibleWith(other Version) bool
- func (v Version) Equal(other Version) bool
- func (v Version) Format() Format
- func (v Version) Greater(other Version) bool
- func (v Version) GreaterOrEqual(other Version) bool
- func (v Version) IsValid() bool
- func (v Version) Less(other Version) bool
- func (v Version) LessOrEqual(other Version) bool
- func (v Version) Major() (uint64, bool)
- func (v Version) MarshalJSON() ([]byte, error)
- func (v Version) MarshalText() ([]byte, error)
- func (v Version) Max(other Version) Version
- func (v Version) Micro() (uint64, bool)
- func (v Version) Min(other Version) Version
- func (v Version) Minor() (uint64, bool)
- func (v Version) Modifier() string
- func (v Version) Series(level Level) string
- func (v Version) String() string
- func (v *Version) UnmarshalJSON(data []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidParts indicates Parts that do not match a Format's schema. ErrInvalidParts = errors.New("calver: invalid parts for format") // ErrUnsupportedBump indicates a bump requested for a calendar token. ErrUnsupportedBump = errors.New("calver: bump requires a generic numeric level") // ErrBumpOverflow indicates an attempt to increment math.MaxUint64. ErrBumpOverflow = errors.New("calver: bump overflows uint64") )
Functions ¶
This section is empty.
Types ¶
type CompileError ¶
CompileError describes an invalid CalVer layout.
type ComponentKind ¶
type ComponentKind uint8
ComponentKind identifies the calendar meaning of a format level.
const ( // ComponentKindNone indicates an absent or invalid level. ComponentKindNone ComponentKind = iota // ComponentKindYear identifies YYYY, YY, or 0Y. ComponentKindYear // ComponentKindMajor identifies MAJOR. ComponentKindMajor // ComponentKindMonth identifies MM or 0M. ComponentKindMonth // ComponentKindMinor identifies MINOR. ComponentKindMinor // ComponentKindWeek identifies WW or 0W. ComponentKindWeek // ComponentKindDay identifies DD or 0D. ComponentKindDay // ComponentKindMicro identifies MICRO. ComponentKindMicro // ComponentKindModifier identifies MODIFIER. ComponentKindModifier )
type ErrorKind ¶
type ErrorKind string
ErrorKind identifies a format compilation or version parsing failure.
const ( // ErrEmptyFormat indicates an empty layout. ErrEmptyFormat ErrorKind = "empty format" // ErrInvalidToken indicates an unknown or malformed token. ErrInvalidToken ErrorKind = "invalid token" // ErrInvalidEscape indicates an unescaped angle bracket in a layout. ErrInvalidEscape ErrorKind = "invalid escape" // ErrDuplicateLevel indicates more than one token for a logical level. ErrDuplicateLevel ErrorKind = "duplicate level" // ErrIncompatibleTokens indicates an invalid month/day/week combination. ErrIncompatibleTokens ErrorKind = "incompatible tokens" // ErrAmbiguousLayout indicates a variable-width token without a boundary. ErrAmbiguousLayout ErrorKind = "ambiguous layout" // ErrModifierPosition indicates a non-terminal modifier token. ErrModifierPosition ErrorKind = "modifier must be final" // ErrLiteralMismatch indicates that an input literal does not match. ErrLiteralMismatch ErrorKind = "literal mismatch" // ErrInvalidNumber indicates an invalid numeric component. ErrInvalidNumber ErrorKind = "invalid number" // ErrOverflow indicates a component that does not fit into uint64. ErrOverflow ErrorKind = "numeric overflow" // ErrOutOfRange indicates a calendar component outside its range. ErrOutOfRange ErrorKind = "value out of range" // ErrInvalidDate indicates an invalid Gregorian calendar date. ErrInvalidDate ErrorKind = "invalid date" // ErrEmptyModifier indicates an empty modifier component. ErrEmptyModifier ErrorKind = "empty modifier" )
type Format ¶
type Format struct {
// contains filtered or unexported fields
}
Format is an immutable compiled CalVer layout.
The zero value is invalid. Obtain a Format with Compile or MustCompile.
func Compile ¶
Compile compiles layout into an immutable Format.
Layout tokens are YYYY, YY, 0Y, MAJOR, MM, 0M, MINOR, WW, 0W, DD, 0D, MICRO, and MODIFIER, each enclosed in angle brackets. Literal '<' and '>' are escaped as '<<' and '>>'. MODIFIER is allowed only as the final token.
func MustCompile ¶
MustCompile compiles layout and panics if it is invalid.
func (Format) Kind ¶
func (f Format) Kind(level Level) ComponentKind
Kind returns the calendar meaning of level in f.
func (Format) Layout ¶
Layout returns the original layout. It returns an empty string for the zero Format.
func (Format) Parse ¶
Parse parses input according to f. It returns false for an invalid Format or an input that does not match the layout.
Example ¶
package main
import (
"fmt"
"github.com/woozymasta/calver"
)
func main() {
format := calver.MustCompile("release-<YYYY>.<0M>.<0D>-<MODIFIER>")
version, ok := format.Parse("release-2026.07.24-rc.1")
if !ok {
return
}
major, _ := version.Major()
fmt.Println(major)
fmt.Println(version.Series(calver.LevelMicro))
}
Output: 2026 release-2026.07.24
type ParseError ¶
ParseError describes an input rejected by a Format.
type Parts ¶
type Parts struct {
// Modifier is the opaque trailing component.
Modifier string `json:"modifier,omitempty" yaml:"modifier,omitempty"`
// Major is the first numeric component or full calendar year.
Major uint64 `json:"major" yaml:"major"`
// Minor is the second numeric component or calendar month.
Minor uint64 `json:"minor" yaml:"minor"`
// Micro is the third numeric component, day, or week.
Micro uint64 `json:"micro" yaml:"micro"`
}
Parts contains normalized values used to construct a Version.
Major is a full calendar year for YYYY, YY, and 0Y formats. A Format defines which parts it consumes; non-zero values for absent numeric levels and a modifier that disagrees with the format return ErrInvalidParts.
type Version ¶
type Version struct {
// contains filtered or unexported fields
}
Version is an immutable parsed CalVer value.
func Parse ¶
Parse parses input with layout. It is a convenience wrapper for Compile(layout) followed by Format.Parse.
func ParseStrict ¶
ParseStrict parses input with layout and returns a typed error on failure.
func (Version) AppendTo ¶
AppendTo appends v's rendered value to dst. Invalid versions leave dst unchanged.
func (Version) BumpMajor ¶
BumpMajor returns v with its generic MAJOR component incremented. It does not reset lower components.
func (Version) BumpMinor ¶
BumpMinor returns v with its generic MINOR component incremented. It does not reset lower components.
func (Version) Compare ¶
Compare returns a deterministic structural order between v and other.
Invalid values sort before valid values. Numeric components compare by value, absent components sort before present components, and modifiers compare bitwise. Equal components use String as a final tie-breaker.
func (Version) CompareCalendar ¶
CompareCalendar compares v and other when their semantic profiles match. It returns ok=false for incompatible formats.
Example ¶
package main
import (
"fmt"
"github.com/woozymasta/calver"
)
func main() {
dash := calver.MustCompile("<YYYY>-<0M>-<0D>")
dot := calver.MustCompile("<YYYY>.<0M>.<0D>")
first, _ := dash.Parse("2026-07-24")
second, _ := dot.Parse("2026.07.25")
order, compatible := first.CompareCalendar(second)
fmt.Println(compatible, order)
}
Output: true -1
func (Version) CompatibleWith ¶
CompatibleWith reports whether v and other have matching calendar semantics. Layout literals and zero-padding do not affect compatibility.
func (Version) Format ¶
Format returns the Format that parsed v. It returns the zero Format when v is invalid.
func (Version) GreaterOrEqual ¶
GreaterOrEqual reports whether v does not sort before other according to Compare.
func (Version) LessOrEqual ¶
LessOrEqual reports whether v does not sort after other according to Compare.
func (Version) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Version) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (Version) Modifier ¶
Modifier returns the opaque trailing modifier. It returns an empty string when the format has no modifier token or v is invalid.
func (Version) Series ¶
Series returns v up to and including level. It returns the full version when v is invalid or the requested level is absent.
func (Version) String ¶
String implements fmt.Stringer. It returns the original input for valid versions and an empty string for invalid versions.
func (*Version) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It leaves v unchanged on error.