module

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2019 License: BSD-3-Clause Imports: 6 Imported by: 345

Documentation

Overview

Package module defines the module.Version type along with support code.

The module.Version type is a simple Path, Version pair:

type Version struct {
	Path string
	Version string
}

There are no restrictions imposed directly by use of this structure, but additional checking functions, most notably Check, verify that a particular path, version pair is valid.

Escaped Paths

Module paths appear as substrings of file system paths (in the download cache) and of web server URLs in the proxy protocol. In general we cannot rely on file systems to be case-sensitive, nor can we rely on web servers, since they read from file systems. That is, we cannot rely on the file system to keep rsc.io/QUOTE and rsc.io/quote separate. Windows and macOS don't. Instead, we must never require two different casings of a file path. Because we want the download cache to match the proxy protocol, and because we want the proxy protocol to be possible to serve from a tree of static files (which might be stored on a case-insensitive file system), the proxy protocol must never require two different casings of a URL path either.

One possibility would be to make the escaped form be the lowercase hexadecimal encoding of the actual path bytes. This would avoid ever needing different casings of a file path, but it would be fairly illegible to most programmers when those paths appeared in the file system (including in file paths in compiler errors and stack traces) in web server logs, and so on. Instead, we want a safe escaped form that leaves most paths unaltered.

The safe escaped form is to replace every uppercase letter with an exclamation mark followed by the letter's lowercase equivalent.

For example,

github.com/Azure/azure-sdk-for-go ->  github.com/!azure/azure-sdk-for-go.
github.com/GoogleCloudPlatform/cloudsql-proxy -> github.com/!google!cloud!platform/cloudsql-proxy
github.com/Sirupsen/logrus -> github.com/!sirupsen/logrus.

Import paths that avoid upper-case letters are left unchanged. Note that because import paths are ASCII-only and avoid various problematic punctuation (like : < and >), the escaped form is also ASCII-only and avoids the same problematic punctuation.

Import paths have never allowed exclamation marks, so there is no need to define how to escape a literal !.

Unicode Restrictions

Today, paths are disallowed from using Unicode.

Although paths are currently disallowed from using Unicode, we would like at some point to allow Unicode letters as well, to assume that file systems and URLs are Unicode-safe (storing UTF-8), and apply the !-for-uppercase convention for escaping them in the file system. But there are at least two subtle considerations.

First, note that not all case-fold equivalent distinct runes form an upper/lower pair. For example, U+004B ('K'), U+006B ('k'), and U+212A ('K' for Kelvin) are three distinct runes that case-fold to each other. When we do add Unicode letters, we must not assume that upper/lower are the only case-equivalent pairs. Perhaps the Kelvin symbol would be disallowed entirely, for example. Or perhaps it would escape as "!!k", or perhaps as "(212A)".

Second, it would be nice to allow Unicode marks as well as letters, but marks include combining marks, and then we must deal not only with case folding but also normalization: both U+00E9 ('é') and U+0065 U+0301 ('e' followed by combining acute accent) look the same on the page and are treated by some file systems as the same path. If we do allow Unicode marks in paths, there must be some kind of normalization to allow only one canonical encoding of any character used in an import path.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalVersion

func CanonicalVersion(v string) string

CanonicalVersion returns the canonical form of the version string v. It is the same as semver.Canonical(v) except that it preserves the special build suffix "+incompatible".

func Check

func Check(path, version string) error

Check checks that a given module path, version pair is valid. In addition to the path being a valid module path and the version being a valid semantic version, the two must correspond. For example, the path "yaml/v2" only corresponds to semantic versions beginning with "v2.".

func CheckFilePath

func CheckFilePath(path string) error

CheckFilePath checks that a slash-separated file path is valid. The definition of a valid file path is the same as the definition of a valid import path except that the set of allowed characters is larger: all Unicode letters, ASCII digits, the ASCII space character (U+0020), and the ASCII punctuation characters “!#$%&()+,-.=@[]^_{}~”. (The excluded punctuation characters, " * < > ? ` ' | / \ and :, have special meanings in certain shells or operating systems.)

CheckFilePath may be less restrictive in the future, but see the top-level package documentation for additional information about subtleties of Unicode.

func CheckImportPath

func CheckImportPath(path string) error

CheckImportPath checks that an import path is valid.

A valid import path consists of one or more valid path elements separated by slashes (U+002F). (It must not begin with nor end in a slash.)

A valid path element is a non-empty string made up of ASCII letters, ASCII digits, and limited ASCII punctuation: + - . _ and ~. It must not begin or end with a dot (U+002E), nor contain two dots in a row.

The element prefix up to the first dot must not be a reserved file name on Windows, regardless of case (CON, com1, NuL, and so on).

CheckImportPath may be less restrictive in the future, but see the top-level package documentation for additional information about subtleties of Unicode.

func CheckPath

func CheckPath(path string) error

CheckPath checks that a module path is valid. A valid module path is a valid import path, as checked by CheckImportPath, with two additional constraints. First, the leading path element (up to the first slash, if any), by convention a domain name, must contain only lower-case ASCII letters, ASCII digits, dots (U+002E), and dashes (U+002D); it must contain at least one dot and cannot start with a dash. Second, for a final path element of the form /vN, where N looks numeric (ASCII digits and dots) must not begin with a leading zero, must not be /v1, and must not contain any dots. For paths beginning with "gopkg.in/", this second requirement is replaced by a requirement that the path follow the gopkg.in server's conventions.

func EscapePath

func EscapePath(path string) (escaped string, err error)

EscapePath returns the escaped form of the given module path. It fails if the module path is invalid.

func EscapeVersion

func EscapeVersion(v string) (escaped string, err error)

EscapeVersion returns the escaped form of the given module version. Versions are allowed to be in non-semver form but must be valid file names and not contain exclamation marks.

func MatchPathMajor

func MatchPathMajor(v, pathMajor string) bool

MatchPathMajor reports whether the semantic version v matches the path major version pathMajor.

func Sort

func Sort(list []Version)

Sort sorts the list by Path, breaking ties by comparing Version fields. The Version fields are interpreted as semantic versions (using semver.Compare) optionally followed by a tie-breaking suffix introduced by a slash character, like in "v0.0.1/go.mod".

func SplitPathVersion

func SplitPathVersion(path string) (prefix, pathMajor string, ok bool)

SplitPathVersion returns prefix and major version such that prefix+pathMajor == path and version is either empty or "/vN" for N >= 2. As a special case, gopkg.in paths are recognized directly; they require ".vN" instead of "/vN", and for all N, not just N >= 2. SplitPathVersion returns with ok = false when presented with a path whose last path element does not satisfy the constraints applied by CheckPath, such as "example.com/pkg/v1" or "example.com/pkg/v1.2".

func UnescapePath

func UnescapePath(escaped string) (path string, err error)

UnescapePath returns the module path for the given escaped path. It fails if the escaped path is invalid or describes an invalid path.

func UnescapeVersion

func UnescapeVersion(escaped string) (v string, err error)

UnescapeVersion returns the version string for the given escaped version. It fails if the escaped form is invalid or describes an invalid version. Versions are allowed to be in non-semver form but must be valid file names and not contain exclamation marks.

Types

type Version

type Version struct {
	// Path is a module path, like "golang.org/x/text" or "rsc.io/quote/v2".
	Path string

	// Version is a module version.
	// By convention, the version string is in canonical semver form,
	// but enforcement is left up to individual APIs.
	Version string `json:",omitempty"`
}

A Version (for clients, a module.Version) is defined by a module path and version pair. These are stored in their plain (unescaped) form.

func (Version) String

func (m Version) String() string

String returns the module version syntax Path@Version.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL