blob: c911ffdd5cdb766d5982dd7b8bffcc2f0b7b253d [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2015 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package names
5
6import (
7 "regexp"
8
9 "github.com/juju/utils"
10)
11
12const (
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
21var 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).
25func IsValidPayload(id string) bool {
26 return validPayload.MatchString(id)
27}
28
29// For compatibility with Juju 1.25, UUIDs are also supported.
30func isValidPayload(id string) bool {
31 return IsValidPayload(id) || utils.IsValidUUIDString(id)
32}
33
34// PayloadTag represents a charm payload.
35type PayloadTag struct {
36 id string
37}
38
39// NewPayloadTag returns the tag for a charm's payload with the given id.
40func 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.
48func 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.
61func (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.
67func (t PayloadTag) Id() string {
68 return t.id
69}
70
71// String implements Tag.
72func (t PayloadTag) String() string {
73 return tagString(t)
74}