add suffix field to ResId

This commit is contained in:
zoncoen
2018-11-14 12:43:49 +09:00
parent a8984578e4
commit a14609f730
4 changed files with 161 additions and 39 deletions

View File

@@ -32,22 +32,46 @@ type ResId struct {
// an untransformed resource has no prefix, fully transformed resource has an arbitrary number of prefixes
// concatenated together.
prefix string
// nameSuffix of the resource
// an untransformed resource has no suffix, fully transformed resource has an arbitrary number of suffixes
// concatenated together.
suffix string
// namespace the resource belongs to
// an untransformed resource has no namespace, fully transformed resource has the namespace from
// the top most overlay
namespace string
}
// NewResIdWithPrefixSuffixNamespace creates new resource identifier with a prefix, suffix and a namespace
func NewResIdWithPrefixSuffixNamespace(k gvk.Gvk, n, p, s, ns string) ResId {
return ResId{gvKind: k, name: n, prefix: p, suffix: s, namespace: ns}
}
// NewResIdWithPrefixNamespace creates new resource identifier with a prefix and a namespace
func NewResIdWithPrefixNamespace(k gvk.Gvk, n, p, ns string) ResId {
return ResId{gvKind: k, name: n, prefix: p, namespace: ns}
}
// NewResIdWithSuffixNamespace creates new resource identifier with a suffix and a namespace
func NewResIdWithSuffixNamespace(k gvk.Gvk, n, s, ns string) ResId {
return ResId{gvKind: k, name: n, suffix: s, namespace: ns}
}
// NewResIdWithPrefixSuffix creates new resource identifier with a prefix and suffix
func NewResIdWithPrefixSuffix(k gvk.Gvk, n, p, s string) ResId {
return ResId{gvKind: k, name: n, prefix: p, suffix: s}
}
// NewResIdWithPrefix creates new resource identifier with a prefix
func NewResIdWithPrefix(k gvk.Gvk, n, p string) ResId {
return ResId{gvKind: k, name: n, prefix: p}
}
// NewResIdWithSuffix creates new resource identifier with a suffix
func NewResIdWithSuffix(k gvk.Gvk, n, s string) ResId {
return ResId{gvKind: k, name: n, suffix: s}
}
// NewResId creates new resource identifier
func NewResId(k gvk.Gvk, n string) ResId {
return ResId{gvKind: k, name: n}
@@ -62,6 +86,7 @@ const (
noNamespace = "noNamespace"
noPrefix = "noPrefix"
noName = "noName"
noSuffix = "noSuffix"
separator = "|"
)
@@ -79,9 +104,13 @@ func (n ResId) String() string {
if nm == "" {
nm = noName
}
s := n.suffix
if s == "" {
s = noSuffix
}
return strings.Join(
[]string{n.gvKind.String(), ns, p, nm}, separator)
[]string{n.gvKind.String(), ns, p, nm, s}, separator)
}
// GvknString of ResId based on GVK and name
@@ -90,7 +119,7 @@ func (n ResId) GvknString() string {
}
// GvknEquals return if two ResId have the same Group/Version/Kind and name
// The comparison excludes prefix
// The comparison excludes prefix and suffix
func (n ResId) GvknEquals(id ResId) bool {
return n.gvKind.Equals(id.gvKind) && n.name == id.name
}
@@ -110,6 +139,11 @@ func (n ResId) Prefix() string {
return n.prefix
}
// Suffix returns name suffix.
func (n ResId) Suffix() string {
return n.suffix
}
// Namespace returns resource namespace.
func (n ResId) Namespace() string {
return n.namespace
@@ -117,12 +151,17 @@ func (n ResId) Namespace() string {
// CopyWithNewPrefix make a new copy from current ResId and append a new prefix
func (n ResId) CopyWithNewPrefix(p string) ResId {
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.concatPrefix(p), namespace: n.namespace}
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.concatPrefix(p), suffix: n.suffix, namespace: n.namespace}
}
// CopyWithNewSuffix make a new copy from current ResId and append a new suffix
func (n ResId) CopyWithNewSuffix(p string) ResId {
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, suffix: n.concatSuffix(p), namespace: n.namespace}
}
// CopyWithNewNamespace make a new copy from current ResId and set a new namespace
func (n ResId) CopyWithNewNamespace(ns string) ResId {
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, namespace: ns}
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, suffix: n.suffix, namespace: ns}
}
// HasSameLeftmostPrefix check if two ResIds have the same
@@ -133,6 +172,14 @@ func (n ResId) HasSameLeftmostPrefix(id ResId) bool {
return prefixes1[0] == prefixes2[0]
}
// HasSameRightmostSuffix check if two ResIds have the same
// right most suffix.
func (n ResId) HasSameRightmostSuffix(id ResId) bool {
suffixes1 := n.suffixList()
suffixes2 := id.suffixList()
return suffixes1[len(suffixes1)-1] == suffixes2[len(suffixes2)-1]
}
func (n ResId) concatPrefix(p string) string {
if p == "" {
return n.prefix
@@ -143,6 +190,20 @@ func (n ResId) concatPrefix(p string) string {
return p + ":" + n.prefix
}
func (n ResId) concatSuffix(s string) string {
if s == "" {
return n.suffix
}
if n.suffix == "" {
return s
}
return n.suffix + ":" + s
}
func (n ResId) prefixList() []string {
return strings.Split(n.prefix, ":")
}
func (n ResId) suffixList() []string {
return strings.Split(n.suffix, ":")
}