4cf6be0f07
This change adds a handful of property binding tests. It also fixes: * AsName should assert IsName. * Enumerate properties stably, so that it is deterministic. * Do not issue errors about unrecognized properties for the special `mu/extension` type. It's entire purpose in life is to offer an entirely custom set of properties, which the provider is meant to validate. * Default to an empty map if properties are missing. * Add a "/" to the end of the namespace from the workspace, if present. And rearranges some code: * Rename the LiteralX types to XLiteral; e.g., StringLiteral instead of LiteralString. I kept typing XLiteral erroneously. * Eliminate the Mu prefix on all of the predefined type and service functions and types. It's superfluous and reads nicer this way. * Swap the order of "expected" vs. "got" in the error message about incorrect property types. It used to say "got %v, expected %v"; I personally find that it is more helpful if it says "expected %v, got %v". YMMV.
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
package ast
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/marapongo/mu/pkg/util"
|
|
)
|
|
|
|
// NameDelimiter is what delimits Namespace and Name parts.
|
|
const NameDelimiter = "/"
|
|
|
|
var NameRegexp = regexp.MustCompile(NameRegexps)
|
|
var NameRegexps = "(" + NamePartRegexps + "\\" + NameDelimiter + ")*" + NamePartRegexps
|
|
var NamePartRegexps = "[A-Za-z_][A-Za-z0-9_]*"
|
|
|
|
// IsName checks whether a string is a legal Name.
|
|
func IsName(s string) bool {
|
|
return NameRegexp.FindString(s) == s
|
|
}
|
|
|
|
// AsName converts a given string to a Name, asserting its validity.
|
|
func AsName(s string) Name {
|
|
util.AssertMF(IsName(s), "Expected string '%v' to be a name (%v)", s, NameRegexps)
|
|
return Name(s)
|
|
}
|
|
|
|
// Simple extracts the name portion of a Name (dropping any Namespace).
|
|
func (nm Name) Simple() Name {
|
|
ix := strings.LastIndex(string(nm), NameDelimiter)
|
|
if ix == -1 {
|
|
return nm
|
|
}
|
|
return nm[ix+1:]
|
|
}
|
|
|
|
// Namespace extracts the namespace portion of a Name (dropping the Name); this may be empty.
|
|
func (nm Name) Namespace() Name {
|
|
ix := strings.LastIndex(string(nm), NameDelimiter)
|
|
if ix == -1 {
|
|
return ""
|
|
}
|
|
return nm[:ix]
|
|
}
|