Skip to content

Commit

Permalink
Add support for v prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ffurrer2 committed Oct 20, 2024
1 parent e271f6b commit 77e7017
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pkg/semver/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package semver

type Builder struct {
prefix string
major uint
minor uint
patch uint
Expand All @@ -12,6 +13,7 @@ type Builder struct {

func NewBuilder() *Builder {
return &Builder{
prefix: "",
major: 0,
minor: 0,
patch: 0,
Expand All @@ -20,8 +22,20 @@ func NewBuilder() *Builder {
}
}

func (b *Builder) Prefix(prefix string) *Builder {
return &Builder{
prefix: prefix,
major: b.major,
minor: b.minor,
patch: b.patch,
preRelease: b.preRelease,
buildMetadata: b.buildMetadata,
}
}

func (b *Builder) Major(major uint) *Builder {
return &Builder{
prefix: b.prefix,
major: major,
minor: b.minor,
patch: b.patch,
Expand All @@ -32,6 +46,7 @@ func (b *Builder) Major(major uint) *Builder {

func (b *Builder) Minor(minor uint) *Builder {
return &Builder{
prefix: b.prefix,
major: b.major,
minor: minor,
patch: b.patch,
Expand All @@ -42,6 +57,7 @@ func (b *Builder) Minor(minor uint) *Builder {

func (b *Builder) Patch(patch uint) *Builder {
return &Builder{
prefix: b.prefix,
major: b.major,
minor: b.minor,
patch: patch,
Expand All @@ -52,6 +68,7 @@ func (b *Builder) Patch(patch uint) *Builder {

func (b *Builder) PreRelease(preRelease []string) *Builder {
return &Builder{
prefix: b.prefix,
major: b.major,
minor: b.minor,
patch: b.patch,
Expand All @@ -62,6 +79,7 @@ func (b *Builder) PreRelease(preRelease []string) *Builder {

func (b *Builder) PreReleaseField(preRelease string) *Builder {
return &Builder{
prefix: b.prefix,
major: b.major,
minor: b.minor,
patch: b.patch,
Expand All @@ -72,6 +90,7 @@ func (b *Builder) PreReleaseField(preRelease string) *Builder {

func (b *Builder) BuildMetadata(buildMetadata []string) *Builder {
return &Builder{
prefix: b.prefix,
major: b.major,
minor: b.minor,
patch: b.patch,
Expand All @@ -82,6 +101,7 @@ func (b *Builder) BuildMetadata(buildMetadata []string) *Builder {

func (b *Builder) BuildMetadataField(buildMetadata string) *Builder {
return &Builder{
prefix: b.prefix,
major: b.major,
minor: b.minor,
patch: b.patch,
Expand All @@ -92,6 +112,7 @@ func (b *Builder) BuildMetadataField(buildMetadata string) *Builder {

func (b *Builder) Build() (*SemVer, bool) {
semver := &SemVer{
Prefix: b.prefix,
Major: b.major,
Minor: b.minor,
Patch: b.patch,
Expand Down
5 changes: 4 additions & 1 deletion pkg/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
MaxMajor = ^uint(0)
MaxMinor = ^uint(0)
MaxPatch = ^uint(0)
NamedGroupsPattern = `^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)` +
NamedGroupsPattern = `^(?P<prefix>v?)(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)` +
`(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?` +
`(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`
)
Expand All @@ -39,6 +39,7 @@ func (s InvalidSemVerError) Error() string {
}

type SemVer struct {
Prefix string `json:"prefix"`
Major uint `json:"major"`
Minor uint `json:"minor"`
Patch uint `json:"patch"`
Expand Down Expand Up @@ -70,6 +71,8 @@ func Parse(s string) (*SemVer, error) {
for groupIdx, group := range matches[0] {
name := groupNames[groupIdx]
switch name {
case "prefix":
semver.Prefix = group
case "major":
semver.Major = number.MustParseUint(group)
case "minor":
Expand Down

0 comments on commit 77e7017

Please sign in to comment.