dubeejw commented on a change in pull request #2424: (Review) Leading Slash (CLI issue #2328) URL: https://github.com/apache/incubator-openwhisk/pull/2424#discussion_r127495007
########## File path: tools/cli/go-whisk-cli/commands/qualified_name.go ########## @@ -0,0 +1,207 @@ +/* + * Copyright 2015-2016 IBM Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package commands + +import ( + "errors" + "fmt" + "strings" + "../../go-whisk/whisk" + "../wski18n" +) + +type QualifiedName struct { + namespace string // namespace. does not include leading '/'. may be "" (i.e. default namespace) + packageName string // package. may be "". does not include leading/trailing '/' + entity string // entity. should not be "" + entityName string // pkg+entity +} + +// GetFullQualifiedName() returns a full qualified name in proper string format +// from qualifiedName with proper syntax. +// Example: /namespace/[package/]entity +func (qualifiedName *QualifiedName) GetFullQualifiedName() string { + output := []string{} + + if len(qualifiedName.namespace) > 0 { + output = append(output, "/", qualifiedName.namespace, "/") + } + if len(qualifiedName.packageName) > 0 { + output = append(output, qualifiedName.packageName, "/") + } + output = append(output, qualifiedName.entity) + + return strings.Join(output, "") +} + +// GetPackageName() returns the package name from qualifiedName without a +// leading '/' +func (qualifiedName *QualifiedName) GetPackageName() string { + return qualifiedName.packageName +} + +// GetEntityName() returns the entity name ([package/]entity) of qualifiedName +// without a leading '/' +func (qualifiedName *QualifiedName) GetEntityName() string { + return qualifiedName.entityName +} + +// GetEntity() returns the name of entity in qualifiedName without a leading '/' +func (qualifiedName *QualifiedName) GetEntity() string { + return qualifiedName.entity +} + +// GetNamespace() returns the name of the namespace in qualifiedName without +// a leading '/' +func (qualifiedName *QualifiedName) GetNamespace() string { + return qualifiedName.namespace +} + +// addLeadSlash(name) returns a (possibly fully qualified) resource name, +// inserting a leading '/' if it is of 3 parts (namespace/package/action) +// and lacking the leading '/'. +func addLeadSlash(name string) string { + parts := strings.Split(name, "/") + if len(parts) == 3 && parts[0] != "" { + name = "/" + name + } + return name +} + +// NewQualifiedName(name) initializes and constructs a (possibly fully qualified) +// QualifiedName struct. +// +// NOTE: If the given qualified name is None, then this is a default qualified +// name and it is resolved from properties. +// NOTE: If the namespace is missing from the qualified name, the namespace +// is also resolved from the property file. +// +// Examples: +// foo => qualifiedName {namespace: "_", entityName: foo} +// pkg/foo => qualifiedName {namespace: "_", entityName: pkg/foo} +// /ns/foo => qualifiedName {namespace: ns, entityName: foo} +// /ns/pkg/foo => qualifiedName {namespace: ns, entityName: pkg/foo} +func NewQualifiedName(name string) (*QualifiedName, error) { + qualifiedName := new(QualifiedName) + + // If name has a preceding delimiter (/), or if it has two delimiters with a + // leading non-empty string, then it contains a namespace. Otherwise the name + // does not specify a namespace, so default the namespace to the namespace + // value set in the properties file; if that is not set, use "_" + name = addLeadSlash(name) + parts := strings.Split(name, "/") + if strings.HasPrefix(name, "/") { + qualifiedName.namespace = parts[1] + + if len(parts) < 2 || len(parts) > 4 { + whisk.Debug(whisk.DbgError, "A valid qualified name was not detected\n") Review comment: This code is duplicated below. Can move the body of the if block to a function and just call that where needed. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
