codeql icon indicating copy to clipboard operation
codeql copied to clipboard

Go: Use new type for all semantic versions

Open mbg opened this issue 1 year ago • 1 comments

Context

Throughout the Go autobuilder, we handle semantic versions. The semver package has no dedicated type for them and all functions from the package work on string values. This makes it difficult to understand where in the code we have something that we know is a valid semantic version and where we might have a string value that isn't. This is made worse by semver requiring all semantic versions to have a v prefix.

What this PR does

We introduce a new SemVer interface, backed by an implementation based on a private type, which is intended to guarantee that a SemVer value is a valid semantic version. Since the type is not exported, the NewSemVer function must be used to construct SemVer values. This function expects any version string as input and formats it according to semver's requirements.

All existing code in the autobuilder is modified to use the SemVer type whereever we would previously have used functions from the semver package.

How to review

Best reviewed commit-by-commit.

mbg avatar May 08 '24 22:05 mbg

After the set of most recent changes, some things to check:

  • Do we miss out on Canonical anywhere now that it is no longer in NewSemVer?
  • Is there anywhere else that needs StandardSemVer()?

mbg avatar May 15 '24 11:05 mbg