pulumi/pkg/ast/versions.go
joeduffy c84512510a Implement dependency versions
This change implements dependency versions, including semantic analysis, per the
checkin 83030685c3.

There's quite a bit in here but at a top-level this parses and validates dependency
references of the form

        [[proto://]base.url]namespace/.../name[@version]

and verifies that the components are correct, as well as binding them to symbols.

These references can appear in two places at the moment:

* Service types.
* Cluster dependencies.

As part of this change, a number of supporting changes have been made:

* Parse Workspaces using a full-blown parser, parser analysis, and semantic analysis.
  This allows us to share logic around the validation of common AST types.  This also
  moves some of the logic around loading workspace.yaml files back to the parser, where
  it can be unified with the way we load Mu.yaml files.

* New ast.Version and ast.VersionSpec types.  The former represents a precise version
  -- either a specific semantic version or a short or long Git SHA hash -- and the
  latter represents a range -- either a Version, "latest", or a semantic range.

* New ast.Ref and ast.RefParts types.  The former is an unparsed string that is
  thought to contain a Ref, while the latter is a validated Ref that has been parsed
  into its components (Proto, Base, Name, and Version).

* Added some type assertions to ensure certain structs implement certain interfaces,
  to speed up finding errors.  (And remove the coercions that zero-fill vtbl slots.)

* Be consistent about prefixing error types with Error or Warning.

* Organize the core compiler driver's logic into three methods, FE, sema, and BE.

* A bunch of tests for some of the above ...  more to come in an upcoming change.
2016-11-22 16:58:23 -08:00

56 lines
1.7 KiB
Go

// Copyright 2016 Marapongo, Inc. All rights reserved.
package ast
import (
"errors"
"regexp"
"github.com/blang/semver"
// TODO(joe): consider supporting the sugared NPM-style semvers, like tilde and caret ranges.
)
var sha1HashRegexps = "[0-9a-fA-F]"
var shortSHA1HashRegexp = regexp.MustCompile(sha1HashRegexps + "{7}")
var longSHA1HashRegexp = regexp.MustCompile(sha1HashRegexps + "{40}")
// Check ensures the given Version is valid; if it is, Check returns nil; if not, an error describing why is returned.
func (v Version) Check() error {
// The only three legal values for Version are: a semantic version or a Git SHA hash (short or long).
// Not that ranges are explicitly disallowed with Versions; for those, you'd use a VersionSpec.
vs := string(v)
if vs == "" {
return errors.New("Missing version")
}
if shortSHA1HashRegexp.FindString(vs) == vs {
return nil
}
if longSHA1HashRegexp.FindString(vs) == vs {
return nil
}
_, err := semver.Parse(vs)
return err
}
// LatestVersion indicates that the latest known source version should be used.
const LatestVersion VersionSpec = "latest"
// Check ensures the given Version is valid; if it is, Check returns nil; if not, an error describing why is returned.
func (v VersionSpec) Check() error {
// More legal values are permitted here. Any valid Version is also a valid VersionSpec. However, VersionSpecs
// permit the special LatestVersionSpec string, in addition to semantic version ranges.
if v == LatestVersion {
return nil
}
if err := Version(v).Check(); err == nil {
return nil
}
vs := string(v)
if vs == "" {
return errors.New("Missing version")
}
_, err := semver.ParseRange(vs)
return err
}