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) { func TestPrettyKeyForProject(t *testing.T) {
proj := &workspace.Project{Name: tokens.PackageName("test-package"), Runtime: "nodejs"} proj := &workspace.Project{Name: tokens.PackageName("test-package"), Runtime: "nodejs"}
assert.Equal(t, "foo", prettyKeyForProject(config.MakeKey("test-package", "foo"), proj)) assert.Equal(t, "foo", prettyKeyForProject(config.MustMakeKey("test-package", "foo"), proj))
assert.Equal(t, "other-package:bar", prettyKeyForProject(config.MakeKey("other-package", "bar"), 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 ( var (
// AWS config keys // AWS config keys
regionKey = config.MakeKey("aws", "region") regionKey = config.MustMakeKey("aws", "region")
accessKey = config.MakeKey("aws", "accessKey") accessKey = config.MustMakeKey("aws", "accessKey")
secretKey = config.MakeKey("aws", "secretKey") // nolint: gas secretKey = config.MustMakeKey("aws", "secretKey") // nolint: gas
token = config.MakeKey("aws", "token") token = config.MustMakeKey("aws", "token")
) )
const ( const (

View file

@ -9,6 +9,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/tokens" "github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/contract"
) )
type Key struct { type Key struct {
@ -16,7 +17,9 @@ type Key struct {
name string 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} return Key{namespace: namespace, name: name}
} }