File: payload.go

package info (click to toggle)
golang-github-juju-names 4.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 340 kB
  • sloc: makefile: 14
file content (74 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package names

import (
	"regexp"

	"github.com/juju/utils"
)

const (
	// PayloadTagKind is used as the prefix for the string
	// representation of payload tags.
	PayloadTagKind = "payload"

	// This can be expanded later, as needed.
	payloadClass = "([a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)"
)

var validPayload = regexp.MustCompile("^" + payloadClass + "$")

// IsValidPayload returns whether id is a valid Juju ID for
// a charm payload. The ID must be a valid alpha-numeric (plus hyphens).
func IsValidPayload(id string) bool {
	return validPayload.MatchString(id)
}

// For compatibility with Juju 1.25, UUIDs are also supported.
func isValidPayload(id string) bool {
	return IsValidPayload(id) || utils.IsValidUUIDString(id)
}

// PayloadTag represents a charm payload.
type PayloadTag struct {
	id string
}

// NewPayloadTag returns the tag for a charm's payload with the given id.
func NewPayloadTag(id string) PayloadTag {
	return PayloadTag{
		id: id,
	}
}

// ParsePayloadTag parses a payload tag string.
// So ParsePayloadTag(tag.String()) === tag.
func ParsePayloadTag(tag string) (PayloadTag, error) {
	t, err := ParseTag(tag)
	if err != nil {
		return PayloadTag{}, err
	}
	pt, ok := t.(PayloadTag)
	if !ok {
		return PayloadTag{}, invalidTagError(tag, PayloadTagKind)
	}
	return pt, nil
}

// Kind implements Tag.
func (t PayloadTag) Kind() string {
	return PayloadTagKind
}

// Id implements Tag.Id. It always returns the same ID with which
// it was created. So NewPayloadTag(x).Id() == x for all valid x.
func (t PayloadTag) Id() string {
	return t.id
}

// String implements Tag.
func (t PayloadTag) String() string {
	return tagString(t)
}