Skip to content

Commit

Permalink
refactor: make everything private by default
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jan 5, 2025
1 parent 2a8275a commit 3cf966c
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 111 deletions.
46 changes: 23 additions & 23 deletions semantic/version-alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func weightAlpineSuffixString(suffixStr string) int {
return len(supported)
}

// AlpineVersion represents a version of an Alpine package.
// alpineVersion represents a version of an Alpine package.
//
// Currently, the APK version specification is as follows:
// *number{.number}...{letter}{\_suffix{number}}...{~hash}{-r#}*
Expand All @@ -81,7 +81,7 @@ func weightAlpineSuffixString(suffixStr string) int {
// Finally, an optional package build component *-r{number}* can follow.
//
// Also see https://github.com/alpinelinux/apk-tools/blob/master/doc/apk-package.5.scd#package-info-metadata
type AlpineVersion struct {
type alpineVersion struct {
// the original string that was parsed
original string
// whether the version was found to be invalid while parsing
Expand All @@ -103,7 +103,7 @@ type AlpineVersion struct {
buildComponent *big.Int
}

func (v AlpineVersion) compareComponents(w AlpineVersion) int {
func (v alpineVersion) compareComponents(w alpineVersion) int {
numberOfComponents := max(len(v.components), len(w.components))

for i := range numberOfComponents {
Expand All @@ -117,7 +117,7 @@ func (v AlpineVersion) compareComponents(w AlpineVersion) int {
return 0
}

func (v AlpineVersion) compareLetters(w AlpineVersion) int {
func (v alpineVersion) compareLetters(w alpineVersion) int {
if v.letter == "" && w.letter != "" {
return -1
}
Expand All @@ -128,7 +128,7 @@ func (v AlpineVersion) compareLetters(w AlpineVersion) int {
return strings.Compare(v.letter, w.letter)
}

func (v AlpineVersion) fetchSuffix(n int) alpineSuffix {
func (v alpineVersion) fetchSuffix(n int) alpineSuffix {
if len(v.suffixes) <= n {
return alpineSuffix{number: big.NewInt(0), weight: 5}
}
Expand All @@ -147,7 +147,7 @@ func (as alpineSuffix) Cmp(bs alpineSuffix) int {
return as.number.Cmp(bs.number)
}

func (v AlpineVersion) compareSuffixes(w AlpineVersion) int {
func (v alpineVersion) compareSuffixes(w alpineVersion) int {
numberOfSuffixes := max(len(v.suffixes), len(w.suffixes))

for i := range numberOfSuffixes {
Expand All @@ -161,7 +161,7 @@ func (v AlpineVersion) compareSuffixes(w AlpineVersion) int {
return 0
}

func (v AlpineVersion) compareBuildComponents(w AlpineVersion) int {
func (v alpineVersion) compareBuildComponents(w alpineVersion) int {
if v.buildComponent != nil && w.buildComponent != nil {
if diff := v.buildComponent.Cmp(w.buildComponent); diff != 0 {
return diff
Expand All @@ -171,7 +171,7 @@ func (v AlpineVersion) compareBuildComponents(w AlpineVersion) int {
return 0
}

func (v AlpineVersion) compareRemainder(w AlpineVersion) int {
func (v alpineVersion) compareRemainder(w alpineVersion) int {
if v.remainder == "" && w.remainder != "" {
return +1
}
Expand All @@ -183,7 +183,7 @@ func (v AlpineVersion) compareRemainder(w AlpineVersion) int {
return 0
}

func (v AlpineVersion) Compare(w AlpineVersion) int {
func (v alpineVersion) compare(w alpineVersion) int {
// if both versions are invalid, then just use a string compare
if v.invalid && w.invalid {
return strings.Compare(v.original, w.original)
Expand All @@ -209,18 +209,18 @@ func (v AlpineVersion) Compare(w AlpineVersion) int {
return 0
}

func (v AlpineVersion) CompareStr(str string) int {
return v.Compare(parseAlpineVersion(str))
func (v alpineVersion) CompareStr(str string) int {
return v.compare(parseAlpineVersion(str))
}

// parseAlpineNumberComponents parses the given string into AlpineVersion.components
// parseAlpineNumberComponents parses the given string into alpineVersion.components
// and then returns the remainder of the string for continued parsing.
//
// Each number component is a sequence of digits (0-9), separated with a ".",
// and with no limit on the value or amount of number components.
//
// This parser must be applied *before* any other parser.
func parseAlpineNumberComponents(v *AlpineVersion, str string) string {
func parseAlpineNumberComponents(v *alpineVersion, str string) string {
sub := cachedregexp.MustCompile(`^((\d+)\.?)*`).FindString(str)

if sub == "" {
Expand All @@ -238,28 +238,28 @@ func parseAlpineNumberComponents(v *AlpineVersion, str string) string {
return strings.TrimPrefix(str, sub)
}

// parseAlpineLetter parses the given string into an AlpineVersion.letter
// parseAlpineLetter parses the given string into an alpineVersion.letter
// and then returns the remainder of the string for continued parsing.
//
// The letter is optional, following after the numeric version components, and
// must be a single lower case letter (a-z).
//
// This parser must be applied *after* parseAlpineNumberComponents.
func parseAlpineLetter(v *AlpineVersion, str string) string {
func parseAlpineLetter(v *alpineVersion, str string) string {
if cachedregexp.MustCompile(`^[a-z]`).MatchString(str) {
v.letter = str[:1]
}

return strings.TrimPrefix(str, v.letter)
}

// parseAlpineSuffixes parses the given string into AlpineVersion.suffixes and
// parseAlpineSuffixes parses the given string into alpineVersion.suffixes and
// then returns the remainder of the string for continued parsing.
//
// Suffixes begin with an "_" and may optionally end with a number.
//
// This parser must be applied *after* parseAlpineLetter.
func parseAlpineSuffixes(v *AlpineVersion, str string) string {
func parseAlpineSuffixes(v *alpineVersion, str string) string {
re := cachedregexp.MustCompile(`_(alpha|beta|pre|rc|cvs|svn|git|hg|p)(\d*)`)

for _, match := range re.FindAllStringSubmatch(str, -1) {
Expand All @@ -277,30 +277,30 @@ func parseAlpineSuffixes(v *AlpineVersion, str string) string {
return str
}

// parseAlpineHash parses the given string into AlpineVersion.hash and then returns
// parseAlpineHash parses the given string into alpineVersion.hash and then returns
// the remainder of the string for continued parsing.
//
// The hash is an optional value representing a commit hash, which is a string of
// that starts with a "~" and is followed by any number of lower case hexadecimal
// digits (0-9a-f).
//
// This parser must be applied *after* parseAlpineSuffixes.
func parseAlpineHash(v *AlpineVersion, str string) string {
func parseAlpineHash(v *alpineVersion, str string) string {
re := cachedregexp.MustCompile(`^~([0-9a-f]+)`)

v.hash = re.FindString(str)

return strings.TrimPrefix(str, v.hash)
}

// parseAlpineBuildComponent parses the given string into AlpineVersion.buildComponent
// parseAlpineBuildComponent parses the given string into alpineVersion.buildComponent
// and then returns the remainder of the string for continued parsing.
//
// The build component is an optional value at the end of the version string which
// begins with "-r" followed by a number.
//
// This parser must be applied *after* parseAlpineBuildComponent
func parseAlpineBuildComponent(v *AlpineVersion, str string) string {
func parseAlpineBuildComponent(v *alpineVersion, str string) string {
if str == "" {
return str
}
Expand All @@ -326,8 +326,8 @@ func parseAlpineBuildComponent(v *AlpineVersion, str string) string {
return strings.TrimPrefix(str, matches[0])
}

func parseAlpineVersion(str string) AlpineVersion {
v := AlpineVersion{original: str, buildComponent: new(big.Int)}
func parseAlpineVersion(str string) alpineVersion {
v := alpineVersion{original: str, buildComponent: new(big.Int)}

str = parseAlpineNumberComponents(&v, str)
str = parseAlpineLetter(&v, str)
Expand Down
19 changes: 10 additions & 9 deletions semantic/version-cran.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"strings"
)

// CRANVersion is the representation of a version of a package that is held
// cranVersion is the representation of a version of a package that is held
// in the CRAN ecosystem (https://cran.r-project.org/).
//
// A version is a sequence of at least two non-negative integers separated by
// either a period or a dash.
//
// See https://astrostatistics.psu.edu/su07/R/html/base/html/package_version.html
type CRANVersion struct {
components Components
type cranVersion struct {
components components
}

func (v CRANVersion) Compare(w CRANVersion) int {
func (v cranVersion) compare(w cranVersion) int {
if diff := v.components.Cmp(w.components); diff != 0 {
return diff
}
Expand All @@ -34,21 +34,22 @@ func (v CRANVersion) Compare(w CRANVersion) int {
return -1
}

func (v CRANVersion) CompareStr(str string) int {
return v.Compare(parseCRANVersion(str))
func (v cranVersion) CompareStr(str string) int {
return v.compare(parseCRANVersion(str))
}

func parseCRANVersion(str string) CRANVersion {
func parseCRANVersion(str string) cranVersion {
// dashes and periods have the same weight, so we can just normalize to periods
parts := strings.Split(strings.ReplaceAll(str, "-", "."), ".")

components := make(Components, 0, len(parts))
// TODO: refactor to avoid shadowing the components type
components := make(components, 0, len(parts))

for _, s := range parts {
v, _ := new(big.Int).SetString(s, 10)

components = append(components, v)
}

return CRANVersion{components}
return cranVersion{components}
}
12 changes: 6 additions & 6 deletions semantic/version-debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ func compareDebianVersions(a, b string) int {
return 0
}

type DebianVersion struct {
type debianVersion struct {
epoch *big.Int
upstream string
revision string
}

func (v DebianVersion) Compare(w DebianVersion) int {
func (v debianVersion) compare(w debianVersion) int {
if diff := v.epoch.Cmp(w.epoch); diff != 0 {
return diff
}
Expand All @@ -137,11 +137,11 @@ func (v DebianVersion) Compare(w DebianVersion) int {
return 0
}

func (v DebianVersion) CompareStr(str string) int {
return v.Compare(parseDebianVersion(str))
func (v debianVersion) CompareStr(str string) int {
return v.compare(parseDebianVersion(str))
}

func parseDebianVersion(str string) DebianVersion {
func parseDebianVersion(str string) debianVersion {
var upstream, revision string

str = strings.TrimSpace(str)
Expand All @@ -160,5 +160,5 @@ func parseDebianVersion(str string) DebianVersion {
revision = "0"
}

return DebianVersion{epoch, upstream, revision}
return debianVersion{epoch, upstream, revision}
}
18 changes: 9 additions & 9 deletions semantic/version-maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ func (vt *mavenVersionToken) lessThan(wt mavenVersionToken) bool {
return vt.qualifierOrder() < wt.qualifierOrder()
}

type MavenVersion struct {
type mavenVersion struct {
tokens []mavenVersionToken
}

func (mv MavenVersion) equal(mw MavenVersion) bool {
func (mv mavenVersion) equal(mw mavenVersion) bool {
if len(mv.tokens) != len(mw.tokens) {
return false
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func newMavenNullVersionToken(token mavenVersionToken) mavenVersionToken {
panic(fmt.Sprintf("unknown prefix '%s' (value: '%s')", token.prefix, token.value))
}

func (mv MavenVersion) lessThan(mw MavenVersion) bool {
func (mv mavenVersion) lessThan(mw mavenVersion) bool {
numberOfTokens := max(len(mv.tokens), len(mw.tokens))

var left mavenVersionToken
Expand Down Expand Up @@ -202,7 +202,7 @@ func splitCharsInclusive(s, chars string) (out []string) {
return
}

func newMavenVersion(str string) MavenVersion {
func newMavenVersion(str string) mavenVersion {
var tokens []mavenVersionToken

// The Maven coordinate is split in tokens between dots ('.'), hyphens ('-')
Expand Down Expand Up @@ -299,9 +299,9 @@ func newMavenVersion(str string) MavenVersion {
i--
}

return MavenVersion{tokens}
return mavenVersion{tokens}
}
func (mv MavenVersion) Compare(w MavenVersion) int {
func (mv mavenVersion) compare(w mavenVersion) int {
if mv.equal(w) {
return 0
}
Expand All @@ -312,10 +312,10 @@ func (mv MavenVersion) Compare(w MavenVersion) int {
return +1
}

func (mv MavenVersion) CompareStr(str string) int {
return mv.Compare(parseMavenVersion(str))
func (mv mavenVersion) CompareStr(str string) int {
return mv.compare(parseMavenVersion(str))
}

func parseMavenVersion(str string) MavenVersion {
func parseMavenVersion(str string) mavenVersion {
return newMavenVersion(str)
}
14 changes: 7 additions & 7 deletions semantic/version-nuget.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package semantic

import "strings"

type NuGetVersion struct {
SemverLikeVersion
type nuGetVersion struct {
semverLikeVersion
}

func (v NuGetVersion) Compare(w NuGetVersion) int {
func (v nuGetVersion) compare(w nuGetVersion) int {
if diff := v.Components.Cmp(w.Components); diff != 0 {
return diff
}

return compareBuildComponents(strings.ToLower(v.Build), strings.ToLower(w.Build))
}

func (v NuGetVersion) CompareStr(str string) int {
return v.Compare(parseNuGetVersion(str))
func (v nuGetVersion) CompareStr(str string) int {
return v.compare(parseNuGetVersion(str))
}

func parseNuGetVersion(str string) NuGetVersion {
return NuGetVersion{ParseSemverLikeVersion(str, 4)}
func parseNuGetVersion(str string) nuGetVersion {
return nuGetVersion{parseSemverLikeVersion(str, 4)}
}
12 changes: 6 additions & 6 deletions semantic/version-packagist.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,22 @@ func comparePackagistComponents(a, b []string) int {
return 0
}

type PackagistVersion struct {
type packagistVersion struct {
Original string
Components []string
}

func parsePackagistVersion(str string) PackagistVersion {
return PackagistVersion{
func parsePackagistVersion(str string) packagistVersion {
return packagistVersion{
str,
strings.Split(canonicalizePackagistVersion(str), "."),
}
}

func (v PackagistVersion) Compare(w PackagistVersion) int {
func (v packagistVersion) compare(w packagistVersion) int {
return comparePackagistComponents(v.Components, w.Components)
}

func (v PackagistVersion) CompareStr(str string) int {
return v.Compare(parsePackagistVersion(str))
func (v packagistVersion) CompareStr(str string) int {
return v.compare(parsePackagistVersion(str))
}
Loading

0 comments on commit 3cf966c

Please sign in to comment.