### Install semver with go get Source: https://github.com/sixafter/semver/blob/main/README.md Run this command to install the semver package. ```sh go get -u github.com/sixafter/semver ``` -------------------------------- ### Install yaml package with go get Source: https://github.com/sixafter/semver/blob/main/vendor/go.yaml.in/yaml/v3/README.md Install the go.yaml.in/yaml/v3 package using the go get command. ```bash go get go.yaml.in/yaml/v3 ``` -------------------------------- ### String usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/prerelease-version.md Shows how to get the string representation of numeric and alphanumeric identifiers, with expected outputs. ```go num, _ := semver.NewPrereleaseVersion("123") alpha, _ := semver.NewPrereleaseVersion("alpha") fmt.Println(num.String()) // Output: 123 fmt.Println(alpha.String()) // Output: alpha ``` -------------------------------- ### New constructor usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using semver.New to create a Version with pre-release and build metadata, printing the result. ```go pr1, _ := semver.NewPrereleaseVersion("alpha") pr2, _ := semver.NewPrereleaseVersion("1") v := semver.New(1, 2, 3, []semver.PrereleaseVersion{pr1, pr2}, []string{"build", "2024"}) fmt.Println(v) // Output: 1.2.3-alpha.1+build.2024 ``` -------------------------------- ### Parse usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of parsing a version string and handling errors. ```go v, err := semver.Parse("1.2.3-alpha.1+build.123") if err != nil { log.Fatalf("Error parsing version: %v", err) } fmt.Println(v) // Output: 1.2.3-alpha.1+build.123 ``` -------------------------------- ### Output of YAML example Source: https://github.com/sixafter/semver/blob/main/vendor/go.yaml.in/yaml/v3/README.md Expected output from running the Go example, showing the unmarshalled struct and map, and their marshalled YAML representations. ```text --- t: {Easy! {2 [3 4]}} --- t dump: a: Easy! b: c: 2 d: [3, 4] --- m: map[a:Easy! b:map[c:2 d:[3 4]]] --- m dump: a: Easy! b: c: 2 d: - 3 - 4 ``` -------------------------------- ### Equal method usage Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using the Equal method. ```go a := semver.MustParse("1.2.3") b := semver.MustParse("1.2.3+build.5") // build metadata does not affect equality fmt.Println(a.Equal(b)) // Output: true ``` -------------------------------- ### MustParse usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using semver.MustParse with a known-good version string. ```go v := semver.MustParse("1.2.3-alpha.1+build.123") fmt.Println(v) // Output: 1.2.3-alpha.1+build.123 ``` -------------------------------- ### Compare method usage Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using the Compare method. ```go v1 := semver.MustParse("1.2.3") v2 := semver.MustParse("1.2.4") fmt.Println(v1.Compare(v2)) // Output: -1 fmt.Println(v1.Compare(v1)) // Output: 0 ``` -------------------------------- ### Default Configuration Example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/parser.md Shows how to create a parser with default strict adherence enabled, parse a version string, and handle errors. Outputs the parsed version. ```go // Default configuration (strict adherence enabled). parser, err := semver.NewParser() if err != nil { log.Fatalf("Failed to create parser: %v", err) } v, err := parser.Parse("1.2.3-beta+build.123") if err != nil { log.Fatalf("Failed to parse version: %v", err) } fmt.Printf("Parsed version: %v\n", v) // Output: Parsed version: 1.2.3-beta+build.123 ``` -------------------------------- ### YAML unmarshal and marshal example in Go Source: https://github.com/sixafter/semver/blob/main/vendor/go.yaml.in/yaml/v3/README.md Complete Go example showing how to unmarshal YAML into a struct and a map, then marshal them back to YAML. Demonstrates struct tags for field renaming and flow style, and notes that struct fields must be public for unmarshal to work. ```go package main import ( "fmt" "log" "go.yaml.in/yaml/v3" ) var data = ` a: Easy! b: c: 2 d: [3, 4] ` // Note: struct fields must be public in order for unmarshal to // correctly populate the data. type T struct { A string B struct { RenamedC int `yaml:"c"` D []int `yaml:",flow"` } } func main() { t := T{} err := yaml.Unmarshal([]byte(data), &t) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- t:\n%v\n\n", t) d, err := yaml.Marshal(&t) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- t dump:\n%s\n\n", string(d)) m := make(map[interface{}]interface{}) err = yaml.Unmarshal([]byte(data), &m) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- m:\n%v\n\n", m) d, err = yaml.Marshal(&m) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- m dump:\n%s\n\n", string(d)) } ``` -------------------------------- ### String method usage Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using the String method and fmt.Stringer. ```go v := semver.New(1, 2, 3, nil, nil) fmt.Println(v.String()) // Output: 1.2.3 fmt.Println(v) // fmt.Stringer: 1.2.3 ``` -------------------------------- ### Parse error-handling example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example iterating over invalid version strings to demonstrate error messages for missing elements, leading zeros, and empty pre-release identifiers. ```go for _, s := range []string{"1.2.3", "1.2", "01.2.3", "1.2.3-"} { v, err := semver.Parse(s) if err != nil { fmt.Printf("%q -> error: %v\n", s, err) continue } fmt.Printf("%q -> %v\n", s, v) } // Output: // "1.2.3" -> 1.2.3 // "1.2" -> error: missing major, minor, or patch elements // "01.2.3" -> error: leading zeros are not allowed in numeric identifiers // "1.2.3-" -> error: empty pre-release identifier ``` -------------------------------- ### Sample Benchmark Output Source: https://github.com/sixafter/semver/blob/main/README.md Example output from running the benchmarks, showing performance metrics for parsing versions. The results include ns/op, B/op, and allocs/op for serial, concurrent, and allocation-focused benchmarks. ```shell go test -bench=. -benchmem -memprofile=mem.out -cpuprofile=cpu.out goos: darwin goarch: arm64 pkg: github.com/sixafter/semver cpu: Apple M2 Ultra BenchmarkParseVersionSerial-24 9170428 123.1 ns/op 128 B/op 2 allocs/op BenchmarkParseVersionConcurrent-24 17886765 68.35 ns/op 128 B/op 2 allocs/op BenchmarkParseVersionAllocations-24 7576131 157.2 ns/op 144 B/op 4 allocs/op PASS ok github.com/sixafter/semver 4.109s ``` -------------------------------- ### Compare usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/prerelease-version.md Demonstrates comparison of numeric and alphanumeric identifiers. Numeric is always lower than alphanumeric; non-numeric compare lexicographically. ```go num, _ := semver.NewPrereleaseVersion("123") alpha, _ := semver.NewPrereleaseVersion("alpha") beta, _ := semver.NewPrereleaseVersion("beta") fmt.Println(num.Compare(alpha)) // Output: -1 (numeric < alphanumeric) fmt.Println(alpha.Compare(beta)) // Output: -1 (lexicographic) fmt.Println(beta.Compare(alpha)) // Output: 1 ``` -------------------------------- ### Binary marshal and unmarshal example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/marshalers.md Shows how to marshal a Version to binary and unmarshal it back. The output is the canonical string representation. ```go data, _ := semver.MustParse("1.2.3-alpha").MarshalBinary() var v semver.Version err := v.UnmarshalBinary(data) if err != nil { log.Fatal(err) } fmt.Println(v) // Output: 1.2.3-alpha ``` -------------------------------- ### LessThanOrEqual method usage Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using the LessThanOrEqual method. ```go v1 := semver.MustParse("1.2.3") fmt.Println(v1.LessThanOrEqual(semver.MustParse("1.2.3"))) // Output: true ``` -------------------------------- ### LessThan method usage Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using the LessThan method. ```go v1 := semver.MustParse("1.2.3") v2 := semver.MustParse("1.2.4") fmt.Println(v1.LessThan(v2)) // Output: true ``` -------------------------------- ### IsNumeric usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/prerelease-version.md Shows how to use IsNumeric on numeric and alphanumeric identifiers, with expected outputs true and false. ```go num, _ := semver.NewPrereleaseVersion("123") alpha, _ := semver.NewPrereleaseVersion("alpha") fmt.Println(num.IsNumeric()) // Output: true fmt.Println(alpha.IsNumeric()) // Output: false ``` -------------------------------- ### GreaterThanOrEqual method usage Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using the GreaterThanOrEqual method. ```go v1 := semver.MustParse("1.2.3") fmt.Println(v1.GreaterThanOrEqual(semver.MustParse("1.2.3"))) // Output: true ``` -------------------------------- ### GreaterThan method usage Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Example of using the GreaterThan method. ```go v1 := semver.MustParse("1.2.4") fmt.Println(v1.GreaterThan(semver.MustParse("1.2.3"))) // Output: true ``` -------------------------------- ### Scan usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/marshalers.md Shows how to call Scan with a string version. On error, logs fatal. Prints the parsed version. ```go var v semver.Version err := v.Scan("1.2.3-alpha+build") if err != nil { log.Fatal(err) } fmt.Println(v) // Output: 1.2.3-alpha+build ``` -------------------------------- ### Usage example with strict and non-strict parsers Source: https://github.com/sixafter/semver/blob/main/_autodocs/configuration.md Shows default strict parser, explicit non-strict parser via WithStrictAdherence(false), and inspecting the effective configuration. Note that non-strict mode still rejects leading zeros in numeric identifiers; only the error type differs. ```go // Default: strict adherence is ON. strict, err := semver.NewParser() if err != nil { log.Fatalf("Failed to create parser: %v", err) } v, err := strict.Parse("1.0.0-alpha.1") if err != nil { log.Fatalf("Failed to parse version: %v", err) } fmt.Println(v) // Output: 1.0.0-alpha.1 // Explicitly non-strict parser instance. loose, err := semver.NewParser(semver.WithStrictAdherence(false)) if err != nil { log.Fatalf("Failed to create parser: %v", err) } v2, err := loose.Parse("1.0.0-rc.1") if err != nil { log.Fatalf("Failed to parse version: %v", err) } fmt.Println(v2) // Output: 1.0.0-rc.1 // Inspect the effective configuration. cfg := loose.(semver.Configuration).Config() fmt.Println(cfg.StrictAdherence()) // Output: false ``` -------------------------------- ### Custom Option Example (WithStrictAdherence(false)) Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/parser.md Shows how to create a parser with strict adherence disabled, parse a version string, and handle errors. Outputs the parsed version. ```go parser, err := semver.NewParser(semver.WithStrictAdherence(false)) if err != nil { log.Fatalf("Failed to create parser: %v", err) } v, err := parser.Parse("1.2.3-rc.1") if err != nil { log.Fatalf("Failed to parse version: %v", err) } fmt.Println(v) // Output: 1.2.3-rc.1 ``` -------------------------------- ### VersionRange.AND usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/range.md Combines two ranges with AND and tests versions. Outputs true for 2.1.0 and false for 2.0.3-beta.2. ```go r1, _ := semver.ParseRange(">1.0.0 <3.0.0") r2, _ := semver.ParseRange("!=2.0.3-beta.2") combined := r1.AND(r2) fmt.Println(combined.Contains(semver.MustParse("2.1.0"))) // Output: true fmt.Println(combined.Contains(semver.MustParse("2.0.3-beta.2"))) // Output: false ``` -------------------------------- ### VersionRange.OR usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/range.md Combines two ranges with OR and tests a version. Outputs true for 3.1.0. ```go r1, _ := semver.ParseRange(">1.0.0 <2.0.0") r2, _ := semver.ParseRange(">=3.0.0 !=4.2.1") combined := r1.OR(r2) fmt.Println(combined.Contains(semver.MustParse("3.1.0"))) // Output: true ``` -------------------------------- ### JSON marshal and unmarshal example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/marshalers.md Shows how to marshal a struct containing a Version to JSON and unmarshal it back. The Version is encoded as a JSON string. ```go type Release struct { Name string `json:"name"` Version semver.Version `json:"version"` } r := Release{Name: "app", Version: semver.MustParse("1.2.3-alpha.1+build.123")} data, err := json.Marshal(r) if err != nil { log.Fatal(err) } fmt.Println(string(data)) // Output: {"name":"app","version":"1.2.3-alpha.1+build.123"} var r2 Release if err := json.Unmarshal(data, &r2); err != nil { log.Fatal(err) } fmt.Println(r2.Version) // Output: 1.2.3-alpha.1+build.123 ``` -------------------------------- ### Requirement.Contains usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/range.md Tests a requirement with OpGt against versions. Outputs true for 1.1.0 and false for 1.0.0. ```go req := semver.Requirement{Op: semver.OpGt, Ver: semver.MustParse("1.0.0")} fmt.Println(req.Contains(semver.MustParse("1.1.0"))) // Output: true fmt.Println(req.Contains(semver.MustParse("1.0.0"))) // Output: false ``` -------------------------------- ### VersionRange.Contains usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/range.md Parses a range with OR groups and tests multiple versions. Outputs true for 1.5.0 and 3.1.0, false for 2.5.0. ```go r, _ := semver.ParseRange(">=1.2.3 <2.0.0 || >=3.0.0") fmt.Println(r.Contains(semver.MustParse("1.5.0"))) // Output: true fmt.Println(r.Contains(semver.MustParse("2.5.0"))) // Output: false fmt.Println(r.Contains(semver.MustParse("3.1.0"))) // Output: true ``` -------------------------------- ### Type-assert parser to Configuration and read StrictAdherence Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/parser.md Shows how to type-assert a Parser to Configuration to access the immutable runtime config. The example prints StrictAdherence() which outputs false because strict adherence was disabled via WithStrictAdherence(false). ```go parser, _ := semver.NewParser(semver.WithStrictAdherence(false)) cfg, ok := parser.(semver.Configuration) if !ok { log.Fatal("parser does not expose configuration") } fmt.Println(cfg.Config().StrictAdherence()) // Output: false ``` -------------------------------- ### ParseRange error-handling example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/range.md Iterates over several range strings, printing the number of OR groups or the error. Shows that "&&" and "==1.0.0" produce invalid range token errors, while valid ranges parse successfully. ```go for _, s := range []string{">=1.2.3", ">=1.2.3 <2.0.0 || >=3.0.0", ">=1.0.0 && 2.0.0", "==1.0.0"} { r, err := semver.ParseRange(s) if err != nil { fmt.Printf("%q -> error: %v\n", s, err) continue } fmt.Printf("%q -> %d OR groups\n", s, len(r.Requirements)) } // Output: // ">=1.2.3" -> 1 OR groups // ">=1.2.3 <2.0.0 || >=3.0.0" -> 2 OR groups // ">=1.0.0 && 2.0.0" -> error: invalid range token: && // "==1.0.0" -> error: invalid range token: ==1.0.0 ``` -------------------------------- ### ParseRange usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/range.md Parses a range string and checks if a version is contained. Outputs true for 1.5.0 in the range ">1.0.0 <2.0.0". ```go r, err := semver.ParseRange(">1.0.0 <2.0.0") if err != nil { fmt.Println("Error parsing range:", err) return } v := semver.MustParse("1.5.0") fmt.Println(r.Contains(v)) // Output: true ``` -------------------------------- ### Clone the Repository and Navigate to Project Source: https://github.com/sixafter/semver/blob/main/CONTRIBUTING.md Clones the fork of the semver repository and navigates into the project directory as the first step of contributing. ```bash git clone https://github.com/sixafter/semver.git cd semver ``` -------------------------------- ### Run Benchmarks with make bench Source: https://github.com/sixafter/semver/blob/main/README.md Use this command to run the benchmark suite for the semver package. It executes the Go benchmark tests with memory and CPU profiling enabled. ```shell make bench ``` -------------------------------- ### Serialize and deserialize versions with JSON Source: https://github.com/sixafter/semver/blob/main/README.md Shows how to marshal and unmarshal a version using JSON. The output is shown in the comments. ```go package main import ( "encoding/json" "fmt" "github.com/sixafter/semver" ) func main() { v := semver.MustParse("1.2.3-alpha.1+build.123") data, _ := json.Marshal(v) fmt.Println(string(data)) // Output: "1.2.3-alpha.1+build.123" var v2 semver.Version _ = json.Unmarshal(data, &v2) fmt.Println(v2) // Output: 1.2.3-alpha.1+build.123 } ``` -------------------------------- ### Compare two versions Source: https://github.com/sixafter/semver/blob/main/README.md Compares two versions to determine their order. The output is shown in the comments. ```go package main import ( "fmt" "github.com/sixafter/semver" ) func main() { v1, _ := semver.Parse("1.2.3") v2, _ := semver.Parse("2.0.0") fmt.Println(v1.LessThan(v2)) // Output: true fmt.Println(v2.GreaterThan(v1)) // Output: true } ``` -------------------------------- ### Compare (PrereleaseVersion) Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Compares two prerelease versions according to SemVer precedence rules. ```APIDOC ## Compare (PrereleaseVersion) ### Description Compares two prerelease versions according to SemVer precedence rules. ### Method Method ### Signature `func (p PrereleaseVersion) Compare(other PrereleaseVersion) int` ### Parameters - **other** (PrereleaseVersion) - Required - The prerelease version to compare against. ### Returns - **int** - -1, 0, or 1. ### Example ```go pr1 := semver.MustParsePrerelease("alpha") pr2 := semver.MustParsePrerelease("beta") result := pr1.Compare(pr2) // -1 ``` ``` -------------------------------- ### AND Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Combines two ranges with a logical AND. ```APIDOC ## AND ### Description Combines two ranges with a logical AND. ### Method Method ### Signature `func (r *VersionRange) AND(other *VersionRange) *VersionRange` ### Parameters - **other** (*VersionRange) - Required - The other range to combine. ### Returns - **VersionRange** (*VersionRange) - The combined range. ### Example ```go r1 := semver.MustParseRange(">=1.2.3") r2 := semver.MustParseRange("<2.0.0") r := r1.AND(r2) ``` ``` -------------------------------- ### String (PrereleaseVersion) Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Returns the string representation of the prerelease version. ```APIDOC ## String (PrereleaseVersion) ### Description Returns the string representation of the prerelease version. ### Method Method ### Signature `func (p PrereleaseVersion) String() string` ### Parameters None ### Returns - **string** - The prerelease string. ### Example ```go pr := semver.MustParsePrerelease("alpha.1") fmt.Println(pr.String()) // "alpha.1" ``` ``` -------------------------------- ### String method signature Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Returns the canonical SemVer string representation of the version. ```go func (v Version) String() string ``` -------------------------------- ### NewParser Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Creates a new Parser with the given configuration options. ```APIDOC ## NewParser ### Description Creates a new Parser with the given configuration options. ### Method Function ### Signature `func NewParser(opts ...Option) (*Parser, error)` ### Parameters - **opts** (Option) - Optional - Configuration options for the parser. ### Returns - **Parser** (*Parser) - The configured parser. - **error** (error) - An error if configuration is invalid. ### Example ```go parser, err := semver.NewParser(semver.WithStrictAdherence(true)) ``` ``` -------------------------------- ### String Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Returns the string representation of the Version object. ```APIDOC ## String ### Description Returns the string representation of the Version object. ### Method Method ### Signature `func (v Version) String() string` ### Parameters None ### Returns - **string** - The version string. ### Example ```go v := semver.MustParse("1.2.3") fmt.Println(v.String()) // "1.2.3" ``` ``` -------------------------------- ### MustParseRange usage example Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/range.md Parses a range string without error handling and checks if a version is contained. Outputs true for 1.5.0 in the range ">1.0.0 <2.0.0". ```go r := semver.MustParseRange(">1.0.0 <2.0.0") v := semver.MustParse("1.5.0") fmt.Println(r.Contains(v)) // Output: true ``` -------------------------------- ### Scan Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Implements database/sql.Scanner for Version. ```APIDOC ## Scan ### Description Implements database/sql.Scanner for Version. ### Method Method ### Signature `func (v *Version) Scan(src interface{}) error` ### Parameters - **src** (interface{}) - Required - The source value from the database. ### Returns - **error** - An error if scanning fails. ### Example ```go var v semver.Version err := v.Scan("1.2.3") ``` ``` -------------------------------- ### Run Linter and Tests Source: https://github.com/sixafter/semver/blob/main/CONTRIBUTING.md Runs the project's linter and test suite to verify changes before committing. ```bash make lint make test ``` -------------------------------- ### NewPrereleaseVersion Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Creates a new PrereleaseVersion from a string. ```APIDOC ## NewPrereleaseVersion ### Description Creates a new PrereleaseVersion from a string. ### Method Function ### Signature `func NewPrereleaseVersion(v string) (*PrereleaseVersion, error)` ### Parameters - **v** (string) - Required - The prerelease identifier string. ### Returns - **PrereleaseVersion** (*PrereleaseVersion) - The parsed prerelease version. - **error** (error) - An error if parsing fails. ### Example ```go pr, err := semver.NewPrereleaseVersion("alpha.1") ``` ``` -------------------------------- ### Compare Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Compares two versions according to SemVer precedence rules. Returns -1, 0, or 1. ```APIDOC ## Compare ### Description Compares two versions according to SemVer precedence rules. Returns -1, 0, or 1. ### Method Method ### Signature `func (v Version) Compare(other Version) int` ### Parameters - **other** (Version) - Required - The version to compare against. ### Returns - **int** - -1 if v < other, 0 if equal, 1 if v > other. ### Example ```go v1 := semver.MustParse("1.2.3") v2 := semver.MustParse("1.2.4") result := v1.Compare(v2) // -1 ``` ``` -------------------------------- ### Equal Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Checks if two versions are equal. ```APIDOC ## Equal ### Description Checks if two versions are equal. ### Method Method ### Signature `func (v Version) Equal(other Version) bool` ### Parameters - **other** (Version) - Required - The version to compare against. ### Returns - **bool** - true if equal, false otherwise. ### Example ```go v1 := semver.MustParse("1.2.3") v2 := semver.MustParse("1.2.3") result := v1.Equal(v2) // true ``` ``` -------------------------------- ### New Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Creates a new Version from a semantic version string. Returns an error if the string is not a valid semantic version. ```APIDOC ## New ### Description Creates a new Version from a semantic version string. Returns an error if the string is not a valid semantic version. ### Method Function ### Signature `func New(v string) (*Version, error)` ### Parameters - **v** (string) - Required - The semantic version string to parse. ### Returns - **Version** (*Version) - The parsed version object. - **error** (error) - An error if parsing fails. ### Example ```go v, err := semver.New("1.2.3") ``` ``` -------------------------------- ### Value Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Implements database/sql/driver.Valuer for Version. ```APIDOC ## Value ### Description Implements database/sql/driver.Valuer for Version. ### Method Method ### Signature `func (v Version) Value() (driver.Value, error)` ### Parameters None ### Returns - **driver.Value** - The value to store in a database. - **error** - An error if conversion fails. ### Example ```go v := semver.MustParse("1.2.3") val, err := v.Value() ``` ``` -------------------------------- ### OR Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Combines two ranges with a logical OR. ```APIDOC ## OR ### Description Combines two ranges with a logical OR. ### Method Method ### Signature `func (r *VersionRange) OR(other *VersionRange) *VersionRange` ### Parameters - **other** (*VersionRange) - Required - The other range to combine. ### Returns - **VersionRange** (*VersionRange) - The combined range. ### Example ```go r1 := semver.MustParseRange(">=1.2.3 <2.0.0") r2 := semver.MustParseRange(">=3.0.0") r := r1.OR(r2) ``` ``` -------------------------------- ### Parse and validate a version Source: https://github.com/sixafter/semver/blob/main/README.md Parses a version string into a Version object and prints it. The output is shown in the comment. ```go package main import ( "fmt" "log" "github.com/sixafter/semver" ) func main() { v, err := semver.Parse("1.2.3-alpha.1+build.123") if err != nil { log.Fatalf("Error parsing version: %v", err) } fmt.Println(v) // Output: 1.2.3-alpha.1+build.123 } ``` -------------------------------- ### Sort Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Sorts a slice of versions in ascending order. ```APIDOC ## Sort ### Description Sorts a slice of versions in ascending order. ### Method Function ### Signature `func Sort(versions Versions)` ### Parameters - **versions** (Versions) - Required - The slice of versions to sort. ### Returns Nothing ### Example ```go versions := semver.Versions{v1, v2, v3} semver.Sort(versions) ``` ``` -------------------------------- ### Correct NewPrereleaseVersion usage with error handling Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/prerelease-version.md Demonstrates correct usage with one identifier per call. Shows successful creation of "alpha" and error handling for "01" which produces the error 'leading zeros are not allowed in numeric identifiers'. ```go v1, err := semver.NewPrereleaseVersion("alpha") if err != nil { log.Fatalf("Error: %v", err) } fmt.Println(v1.String()) // Output: alpha v2, err := semver.NewPrereleaseVersion("01") if err != nil { fmt.Println("Error:", err) // Output: Error: leading zeros are not allowed in numeric identifiers } ``` -------------------------------- ### UnmarshalText Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Implements encoding.TextUnmarshaler for Version. ```APIDOC ## UnmarshalText ### Description Implements encoding.TextUnmarshaler for Version. ### Method Method ### Signature `func (v *Version) UnmarshalText(text []byte) error` ### Parameters - **text** ([]byte) - Required - The text to unmarshal. ### Returns - **error** - An error if unmarshaling fails. ### Example ```go var v semver.Version err := v.UnmarshalText([]byte("1.2.3")) ``` ``` -------------------------------- ### MarshalText Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Implements encoding.TextMarshaler for Version. ```APIDOC ## MarshalText ### Description Implements encoding.TextMarshaler for Version. ### Method Method ### Signature `func (v Version) MarshalText() ([]byte, error)` ### Parameters None ### Returns - **[]byte** - The text representation. - **error** - An error if marshaling fails. ### Example ```go v := semver.MustParse("1.2.3") text, err := v.MarshalText() ``` ``` -------------------------------- ### Evaluate version ranges Source: https://github.com/sixafter/semver/blob/main/README.md Checks if a version satisfies a given range. The output is shown in the comment. ```go package main import ( "fmt" "github.com/sixafter/semver" ) func main() { r, _ := semver.ParseRange(">=1.0.0 <2.0.0") v, _ := semver.Parse("1.5.0") fmt.Println(r.Contains(v)) // Output: true } ``` -------------------------------- ### New constructor signature Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Signature for the New constructor that builds a Version directly from components without validation. ```go func New(major, minor, patch uint64, preRelease []PrereleaseVersion, buildMetadata []string) Version ``` -------------------------------- ### String() Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Renders the version in canonical SemVer form: MAJOR.MINOR.PATCH, plus pre-release and build metadata if present. Implements fmt.Stringer and backs all marshalers. ```APIDOC ## String() ### Description Renders the version in canonical SemVer form: `MAJOR.MINOR.PATCH`, plus `-` + dot-joined pre-release identifiers if present, plus `+` + dot-joined build metadata if present. Numeric pre-release components are rendered without leading zeros. Also implements `fmt.Stringer` and backs all marshalers. ### Method String ### Signature `func (v Version) String() string` ### Parameters - `v` (receiver) (Version) - Required - Value receiver; safe on the zero value ### Returns - `string` - The canonical SemVer string representation ### Example ```go v := semver.New(1, 2, 3, nil, nil) fmt.Println(v.String()) // Output: 1.2.3 fmt.Println(v) // fmt.Stringer: 1.2.3 ``` ``` -------------------------------- ### Compare method signature Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/version.md Compares two versions per SemVer precedence rules. ```go func (v Version) Compare(other Version) int ``` -------------------------------- ### MarshalJSON Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Implements json.Marshaler for Version. ```APIDOC ## MarshalJSON ### Description Implements json.Marshaler for Version. ### Method Method ### Signature `func (v Version) MarshalJSON() ([]byte, error)` ### Parameters None ### Returns - **[]byte** - The JSON representation. - **error** - An error if marshaling fails. ### Example ```go v := semver.MustParse("1.2.3") json, err := v.MarshalJSON() ``` ``` -------------------------------- ### MustParseRange Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Parses a version range string and panics if invalid. ```APIDOC ## MustParseRange ### Description Parses a version range string and panics if invalid. ### Method Function ### Signature `func MustParseRange(s string) *VersionRange` ### Parameters - **s** (string) - Required - The range string. ### Returns - **VersionRange** (*VersionRange) - The parsed range. ### Example ```go r := semver.MustParseRange(">=1.2.3 <2.0.0") ``` ``` -------------------------------- ### Sorting with Versions and sort.Sort Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/sort.md Creates a Versions slice and sorts it in place using sort.Sort, then prints the sorted versions. Output shows SemVer precedence order with pre-release versions before their release. ```go versions := semver.Versions{ semver.MustParse("2.0.0"), semver.MustParse("1.0.1"), semver.MustParse("1.0.0-alpha"), semver.MustParse("1.0.0-beta"), semver.MustParse("1.0.0"), } sort.Sort(versions) for _, v := range versions { fmt.Println(v) } // Output: // 1.0.0-alpha // 1.0.0-beta // 1.0.0 // 1.0.1 // 2.0.0 ``` -------------------------------- ### ParseRange Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Parses a version range string into a VersionRange object. ```APIDOC ## ParseRange ### Description Parses a version range string into a VersionRange object. ### Method Function ### Signature `func ParseRange(s string) (*VersionRange, error)` ### Parameters - **s** (string) - Required - The range string (e.g., ">=1.2.3 <2.0.0"). ### Returns - **VersionRange** (*VersionRange) - The parsed range. - **error** (error) - An error if parsing fails. ### Example ```go r, err := semver.ParseRange(">=1.2.3 <2.0.0") ``` ``` -------------------------------- ### MarshalBinary Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Implements encoding.BinaryMarshaler for Version. ```APIDOC ## MarshalBinary ### Description Implements encoding.BinaryMarshaler for Version. ### Method Method ### Signature `func (v Version) MarshalBinary() ([]byte, error)` ### Parameters None ### Returns - **[]byte** - The binary representation. - **error** - An error if marshaling fails. ### Example ```go v := semver.MustParse("1.2.3") bin, err := v.MarshalBinary() ``` ``` -------------------------------- ### Parse loop with error handling and output Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/parser.md Demonstrates a full parse loop using NewParser and Parse. It handles errors, prints each input and its parsed version or error, and shows the output for valid, invalid pre-release, and empty inputs. ```go parser, err := semver.NewParser() if err != nil { log.Fatalf("Failed to create parser: %v", err) } inputs := []string{"1.0.0", "1.0.0-alpha.01", ""} for _, s := range inputs { v, err := parser.Parse(s) if err != nil { fmt.Printf("%q -> %v\n", s, err) continue } fmt.Printf("%q -> %v\n", s, v) } // Output: // "1.0.0" -> 1.0.0 // "1.0.0-alpha.01" -> invalid pre-release identifier // "" -> version string is empty ``` -------------------------------- ### LessThan Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Checks if a version is less than another. ```APIDOC ## LessThan ### Description Checks if a version is less than another. ### Method Method ### Signature `func (v Version) LessThan(other Version) bool` ### Parameters - **other** (Version) - Required - The version to compare against. ### Returns - **bool** - true if v < other, false otherwise. ### Example ```go v1 := semver.MustParse("1.2.3") v2 := semver.MustParse("1.2.4") result := v1.LessThan(v2) // true ``` ``` -------------------------------- ### Create a New Branch with git checkout Source: https://github.com/sixafter/semver/blob/main/CONTRIBUTING.md Creates and switches to a new branch for your contribution. ```bash git checkout -b feature/your-feature-name ``` -------------------------------- ### Reverse Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Reverses the order of a slice of versions. ```APIDOC ## Reverse ### Description Reverses the order of a slice of versions. ### Method Function ### Signature `func Reverse(versions Versions)` ### Parameters - **versions** (Versions) - Required - The slice of versions to reverse. ### Returns Nothing ### Example ```go versions := semver.Versions{v1, v2, v3} semver.Reverse(versions) ``` ``` -------------------------------- ### Verify release with Cosign Source: https://github.com/sixafter/semver/blob/main/README.md Use Cosign to verify the integrity of a release by checking the signature and checksums. The script fetches the latest release tag, downloads the source tarball and its Sigstore bundle, verifies the tarball, downloads checksums.txt and its bundle, verifies the checksums signature, and validates file integrity with shasum. Requires curl, jq, cosign, and shasum. ```sh # Fetch the latest release tag from GitHub API (e.g., "v1.13.0") TAG=$(curl -s https://api.github.com/repos/sixafter/semver/releases/latest | jq -r .tag_name) # Remove leading "v" for filenames (e.g., "v1.13.0" -> "1.13.0") VERSION=${TAG#v} # --------------------------------------------------------------------- # Verify the source tarball using Sigstore bundle # --------------------------------------------------------------------- # Download the release tarball and its Sigstore bundle curl -LO "https://github.com/sixafter/semver/releases/download/${TAG}/semver-${VERSION}.tar.gz" curl -LO "https://github.com/sixafter/semver/releases/download/${TAG}/semver-${VERSION}.tar.gz.sigstore.json" # Verify the tarball with Cosign cosign verify-blob \ --key "https://raw.githubusercontent.com/sixafter/semver/main/cosign.pub" \ --bundle "semver-${VERSION}.tar.gz.sigstore.json" \ "semver-${VERSION}.tar.gz" # --------------------------------------------------------------------- # Verify checksums.txt using Sigstore bundle # --------------------------------------------------------------------- # Download checksums.txt and its bundle curl -LO "https://github.com/sixafter/semver/releases/download/${TAG}/checksums.txt" curl -LO "https://github.com/sixafter/semver/releases/download/${TAG}/checksums.txt.sigstore.json" # Verify the checksums.txt signature cosign verify-blob \ --key "https://raw.githubusercontent.com/sixafter/semver/main/cosign.pub" \ --bundle "checksums.txt.sigstore.json" \ checksums.txt # --------------------------------------------------------------------- # Validate file integrity # --------------------------------------------------------------------- shasum -a 256 -c checksums.txt ``` ```shell Verified OK ``` -------------------------------- ### UnmarshalBinary Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Implements encoding.BinaryUnmarshaler for Version. ```APIDOC ## UnmarshalBinary ### Description Implements encoding.BinaryUnmarshaler for Version. ### Method Method ### Signature `func (v *Version) UnmarshalBinary(data []byte) error` ### Parameters - **data** ([]byte) - Required - The binary data to unmarshal. ### Returns - **error** - An error if unmarshaling fails. ### Example ```go var v semver.Version err := v.UnmarshalBinary(bin) ``` ``` -------------------------------- ### Parse Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Parses a semantic version string into a Version object. Returns an error if the string is invalid. ```APIDOC ## Parse ### Description Parses a semantic version string into a Version object. Returns an error if the string is invalid. ### Method Function ### Signature `func Parse(v string) (*Version, error)` ### Parameters - **v** (string) - Required - The semantic version string to parse. ### Returns - **Version** (*Version) - The parsed version object. - **error** (error) - An error if parsing fails. ### Example ```go v, err := semver.Parse("1.2.3") ``` ``` -------------------------------- ### UnmarshalJSON Source: https://github.com/sixafter/semver/blob/main/_autodocs/README.md Implements json.Unmarshaler for Version. ```APIDOC ## UnmarshalJSON ### Description Implements json.Unmarshaler for Version. ### Method Method ### Signature `func (v *Version) UnmarshalJSON(data []byte) error` ### Parameters - **data** ([]byte) - Required - The JSON data to unmarshal. ### Returns - **error** - An error if unmarshaling fails. ### Example ```go var v semver.Version err := v.UnmarshalJSON([]byte(`"1.2.3"`)) ``` ``` -------------------------------- ### Stage and Commit Changes Source: https://github.com/sixafter/semver/blob/main/CONTRIBUTING.md Stages all changes and commits them with a descriptive message. ```bash git add . git commit -m "Add descriptive commit message" ``` -------------------------------- ### NewPrereleaseVersion Source: https://github.com/sixafter/semver/blob/main/_autodocs/errors.md Creates a new prerelease version. Can return errors for invalid prerelease identifiers. ```APIDOC ## NewPrereleaseVersion ### Description Creates a new prerelease version. Can return errors for invalid prerelease identifiers. ### Errors - `ErrEmptyPrereleaseIdentifier` - Empty prerelease identifier - `ErrLeadingZeroInNumericIdentifier` - Leading zero in numeric identifier - `ErrInvalidNumericIdentifier` - Invalid numeric identifier ``` -------------------------------- ### Using semver.Sort Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/sort.md Sorts a slice of version pointers in place using semver.Sort and prints the result. The output shows the versions in increasing order. ```go versions := []*semver.Version{ semver.MustParse("1.2.3"), semver.MustParse("1.0.0"), semver.MustParse("1.1.1"), } semver.Sort(versions) for _, v := range versions { fmt.Println(v) } // Output: // 1.0.0 // 1.1.1 // 1.2.3 ``` -------------------------------- ### Import semver in Go Source: https://github.com/sixafter/semver/blob/main/README.md Add this import statement to use the semver package in your Go project. ```go import "github.com/sixafter/semver" ``` -------------------------------- ### Using semver.Reverse Source: https://github.com/sixafter/semver/blob/main/_autodocs/api-reference/sort.md Sorts a slice of version pointers in place using semver.Reverse and prints the result. The output shows the versions in decreasing order, with pre-release versions after their release. ```go versions := []*semver.Version{ semver.MustParse("1.0.0"), semver.MustParse("1.0.1"), semver.MustParse("2.0.0"), semver.MustParse("1.0.0-beta"), semver.MustParse("1.0.0-alpha"), } semver.Reverse(versions) for _, v := range versions { fmt.Println(v) } // Output: // 2.0.0 // 1.0.1 // 1.0.0 // 1.0.0-beta // 1.0.0-alpha ```