Have MakeKey fail if namespace contains a colon

This helper method is only really used for testing, but we should not
allow it to create a Key who's namespace has a colon (as ParseKey
would not build something like this).
This commit is contained in:
Matt Ellis 2018-03-08 11:52:48 -08:00
parent 5dfd720bc3
commit 731463c282
3 changed files with 10 additions and 7 deletions

View file

@ -15,6 +15,6 @@ import (
func TestPrettyKeyForProject(t *testing.T) {
proj := &workspace.Project{Name: tokens.PackageName("test-package"), Runtime: "nodejs"}
assert.Equal(t, "foo", prettyKeyForProject(config.MakeKey("test-package", "foo"), proj))
assert.Equal(t, "other-package:bar", prettyKeyForProject(config.MakeKey("other-package", "bar"), proj))
assert.Equal(t, "foo", prettyKeyForProject(config.MustMakeKey("test-package", "foo"), proj))
assert.Equal(t, "other-package:bar", prettyKeyForProject(config.MustMakeKey("other-package", "bar"), proj))
}

View file

@ -64,10 +64,10 @@ var _ Provider = (*awsOpsProvider)(nil)
var (
// AWS config keys
regionKey = config.MakeKey("aws", "region")
accessKey = config.MakeKey("aws", "accessKey")
secretKey = config.MakeKey("aws", "secretKey") // nolint: gas
token = config.MakeKey("aws", "token")
regionKey = config.MustMakeKey("aws", "region")
accessKey = config.MustMakeKey("aws", "accessKey")
secretKey = config.MustMakeKey("aws", "secretKey") // nolint: gas
token = config.MustMakeKey("aws", "token")
)
const (

View file

@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/contract"
)
type Key struct {
@ -16,7 +17,9 @@ type Key struct {
name string
}
func MakeKey(namespace string, name string) Key {
// MustMakeKey constructs a config.Key for a given namespace and name. The namespace may not contain a `:`
func MustMakeKey(namespace string, name string) Key {
contract.Requiref(!strings.Contains(":", namespace), "namespace", "may not contain a colon")
return Key{namespace: namespace, name: name}
}