David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2015 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package names |
| 5 | |
| 6 | import ( |
| 7 | "regexp" |
| 8 | |
| 9 | "github.com/juju/utils" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | // PayloadTagKind is used as the prefix for the string |
| 14 | // representation of payload tags. |
| 15 | PayloadTagKind = "payload" |
| 16 | |
| 17 | // This can be expanded later, as needed. |
| 18 | payloadClass = "([a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)" |
| 19 | ) |
| 20 | |
| 21 | var validPayload = regexp.MustCompile("^" + payloadClass + "$") |
| 22 | |
| 23 | // IsValidPayload returns whether id is a valid Juju ID for |
| 24 | // a charm payload. The ID must be a valid alpha-numeric (plus hyphens). |
| 25 | func IsValidPayload(id string) bool { |
| 26 | return validPayload.MatchString(id) |
| 27 | } |
| 28 | |
| 29 | // For compatibility with Juju 1.25, UUIDs are also supported. |
| 30 | func isValidPayload(id string) bool { |
| 31 | return IsValidPayload(id) || utils.IsValidUUIDString(id) |
| 32 | } |
| 33 | |
| 34 | // PayloadTag represents a charm payload. |
| 35 | type PayloadTag struct { |
| 36 | id string |
| 37 | } |
| 38 | |
| 39 | // NewPayloadTag returns the tag for a charm's payload with the given id. |
| 40 | func NewPayloadTag(id string) PayloadTag { |
| 41 | return PayloadTag{ |
| 42 | id: id, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // ParsePayloadTag parses a payload tag string. |
| 47 | // So ParsePayloadTag(tag.String()) === tag. |
| 48 | func ParsePayloadTag(tag string) (PayloadTag, error) { |
| 49 | t, err := ParseTag(tag) |
| 50 | if err != nil { |
| 51 | return PayloadTag{}, err |
| 52 | } |
| 53 | pt, ok := t.(PayloadTag) |
| 54 | if !ok { |
| 55 | return PayloadTag{}, invalidTagError(tag, PayloadTagKind) |
| 56 | } |
| 57 | return pt, nil |
| 58 | } |
| 59 | |
| 60 | // Kind implements Tag. |
| 61 | func (t PayloadTag) Kind() string { |
| 62 | return PayloadTagKind |
| 63 | } |
| 64 | |
| 65 | // Id implements Tag.Id. It always returns the same ID with which |
| 66 | // it was created. So NewPayloadTag(x).Id() == x for all valid x. |
| 67 | func (t PayloadTag) Id() string { |
| 68 | return t.id |
| 69 | } |
| 70 | |
| 71 | // String implements Tag. |
| 72 | func (t PayloadTag) String() string { |
| 73 | return tagString(t) |
| 74 | } |