File: applicationoffer.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 (47 lines) | stat: -rw-r--r-- 1,445 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
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package names

import (
	"regexp"
)

const ApplicationOfferTagKind = "applicationoffer"

const (
	ApplicationOfferSnippet = "(?:[a-z][a-z0-9]*(?:-[a-z0-9]*[a-z][a-z0-9]*)*)"
)

var validApplicationOffer = regexp.MustCompile("^" + ApplicationOfferSnippet + "$")

// IsValidApplicationOffer returns whether name is a valid application offer name.
func IsValidApplicationOffer(name string) bool {
	return validApplicationOffer.MatchString(name)
}

type ApplicationOfferTag struct {
	Name string
}

func (t ApplicationOfferTag) String() string { return t.Kind() + "-" + t.Id() }
func (t ApplicationOfferTag) Kind() string   { return ApplicationOfferTagKind }
func (t ApplicationOfferTag) Id() string     { return t.Name }

// NewApplicationOfferTag returns the tag for the application with the given name.
func NewApplicationOfferTag(applicationOfferName string) ApplicationOfferTag {
	return ApplicationOfferTag{Name: applicationOfferName}
}

// ParseApplicationOfferTag parses a application tag string.
func ParseApplicationOfferTag(applicationOfferTag string) (ApplicationOfferTag, error) {
	tag, err := ParseTag(applicationOfferTag)
	if err != nil {
		return ApplicationOfferTag{}, err
	}
	st, ok := tag.(ApplicationOfferTag)
	if !ok {
		return ApplicationOfferTag{}, invalidTagError(applicationOfferTag, ApplicationOfferTagKind)
	}
	return st, nil
}