github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/payload/id.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package payload
     5  
     6  import (
     7  	"strings"
     8  )
     9  
    10  // TODO(ericsnow) Add ValidateID()?
    11  
    12  // TODO(ericsnow) Add a "composite" ID type?
    13  
    14  // BuildID composes an ID from a class and an ID.
    15  func BuildID(class, id string) string {
    16  	if id == "" {
    17  		// TODO(natefinch) remove this special case when we can be sure the ID
    18  		// is never empty (and fix the tests).
    19  		return class
    20  	}
    21  	return class + "/" + id
    22  }
    23  
    24  // ParseID extracts the payload name and details ID from the provided string.
    25  // The format is expected to be name/pluginID. If no separator is found, the
    26  // whole string is assumed to be the name.
    27  func ParseID(id string) (name, pluginID string) {
    28  	parts := strings.SplitN(id, "/", 2)
    29  	if len(parts) == 2 {
    30  		return parts[0], parts[1]
    31  	}
    32  	return id, ""
    33  }