tags

package module
v0.0.0-...-e0633e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 5 Imported by: 0

README

Tags

A Go library for creating and manipulating tags.

NB: This project is a work in progress.

Roadmap

  • rewrite DMSd using the library
  • write tests
  • write package documentation (godoc)
  • update README.md

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must[T MustConstraint](t T, err error) T

Must takes a value of Tag, []Tag, TagGroup or []TagGroup and an error and either panics (if error != nil) or returns the value.

Types

type LessFunc

type LessFunc func(Tag, Tag) bool

LessFunc is used to sort tags by the *Func methods.

type MatchFunc

type MatchFunc func(Tag) bool

MatchFunc is used to match tags by the *Func methods.

type MustConstraint

type MustConstraint interface {
	Tag | []Tag | TagGroup | []TagGroup
}

MustConstraint is a type constraint for the Must function.

type Tag

type Tag struct {
	// contains filtered or unexported fields
}

Tag can be a label (a tag without a value), a single value tag (a tag with a name and one value) or a multiple value tag (a tag with a name and more than one value).

func New

func New(name string, values ...string) (Tag, error)

New creates a tag with the name and values.

The name cannot be an empty string. Empty-string values will be removed. Repeating values will be removed, i.e. values will be made unique.

You can also use the convenience functions to create tags: NewLabel, NewSingleValue or NewMultiValue.

func NewLabel

func NewLabel(name string) (Tag, error)

NewLabel creates a label tag (a tag without a value).

The name cannot be an empty string.

func NewMultiValue

func NewMultiValue(name string, values ...string) (Tag, error)

NewMultiValue creates a multiple value tag (a tag with more than one value).

The name and values cannot be empty strings. Repeating values will be removed, i.e. values will be made unique. At least two unique values are required.

func NewSingleValue

func NewSingleValue(name, value string) (Tag, error)

NewSingleValue creates a single value tag (a tag with one value).

The name and value cannot be empty strings.

func Parse

func Parse(tag string) (Tag, error)

Parse tries to parse a string representation of a tag and returns the corresponding Tag or an error.

The string must be in the name[:value,...] format.

Examples:

Must(Parse("label")) -> Tag{name: "label", values: nil}
Must(Parse("single:value")) -> Tag{name: "single", values: []string{"value"}}
Must(Parse("multi:value1,value2")) -> Tag{name: "multi", values: []string{"value1", "value2"}}

This function is the reverse of the Tag.String method.

func (Tag) HasFunc

func (t Tag) HasFunc(fn MatchFunc) bool

HasFunc returns true if the tag matches the fn.

func (Tag) HasName

func (t Tag) HasName(name string) bool

HasName returns true if the tag has the name.

func (Tag) HasValues

func (t Tag) HasValues(values ...string) bool

HasValues returns true if the tag has all the values.

func (Tag) IsLabel

func (t Tag) IsLabel() bool

IsLabel returns true if the tag is a label (a tag without a value).

func (Tag) IsMultiValue

func (t Tag) IsMultiValue() bool

IsMultiValue returns true if the tag is a multiple value tag.

func (Tag) IsSingleValue

func (t Tag) IsSingleValue() bool

IsSingleValue returns true if the tag is a single value tag.

func (Tag) Name

func (t Tag) Name() string

Name returns the tag name.

func (Tag) String

func (t Tag) String() string

String returns a string representation of the tag in the name[:value,...] format.

Examples:

Must(NewLabel("label")).String() -> "label"
Must(NewSingleValue("single", "value").String() -> "single:value"
Must(NewMultiValue("multi", "value1", "value2").String() -> "multi:value1,value2"

This method is the reverse of the Parse function.

func (Tag) Value

func (t Tag) Value() string

Value returns the tag value.

If the tag has no values (it's a label) it returns an empty string.

If the tag has multiple values (it's a multiple value tag) it returns just the first value. To get all values use the Tag.Values method.

func (Tag) Values

func (t Tag) Values() []string

Values returns all values of the tag.

If the tag is a label, it returns an empty slice.

type TagGroup

type TagGroup struct {
	// contains filtered or unexported fields
}

TagGroup is a group of related tags.

func NewGroup

func NewGroup(name string, tags ...Tag) (TagGroup, error)

NewGroup creates a group with the specified name and adds the provided tags to it.

The group name cannot be an empty string. The tag names must be unique, see the TagGroup.Add method docs.

func NewGroupWithGeneratedName

func NewGroupWithGeneratedName(tags ...Tag) TagGroup

NewGroupWithGeneratedName creates a group with a generated name and adds the specified tags to it.

The tag names must be unique, see the TagGroup.Add method docs.

func (*TagGroup) Add

func (g *TagGroup) Add(tags ...Tag)

Add adds tags to the group.

If there are multiple tags with the same Tag.Name, only the last one will be added, i.e. the tag names must be unique.

func (*TagGroup) Contains

func (g *TagGroup) Contains(tags ...Tag) bool

Contains returns true if the group contains the tags. The tags must match by both name and values.

func (*TagGroup) ContainsFunc

func (g *TagGroup) ContainsFunc(fn MatchFunc) bool

ContainsFunc returns true if the group contains tags matching the fn. The tags must match by both name and values.

func (*TagGroup) ContainsNames

func (g *TagGroup) ContainsNames(names ...string) bool

ContainsNames returns true if the group contains tags matching the names.

func (*TagGroup) ContainsValues

func (g *TagGroup) ContainsValues(values ...string) bool

ContainsValues returns true if the group contains tags matching all the values, i.e. only tags that have all the values are considered matches.

func (*TagGroup) FindFunc

func (g *TagGroup) FindFunc(fn MatchFunc) (found []Tag)

FindFunc returns tags matching the fn.

func (*TagGroup) FindNames

func (g *TagGroup) FindNames(names ...string) []Tag

FindNames returns tags matching the names.

func (*TagGroup) FindValues

func (g *TagGroup) FindValues(values ...string) []Tag

FindValues returns tags matching all the values, i.e. only tags that have all the values are considered matches.

func (*TagGroup) Name

func (g *TagGroup) Name() string

Name returns the group name.

func (*TagGroup) Remove

func (g *TagGroup) Remove(tags ...Tag)

Remove removes the matching tags from the group. The tags must match by both name and values.

func (*TagGroup) RemoveFunc

func (g *TagGroup) RemoveFunc(fn MatchFunc)

RemoveFunc removes tags matching the fn from the group.

func (*TagGroup) RemoveNames

func (g *TagGroup) RemoveNames(names ...string)

RemoveNames removes tags matching the names from the group.

func (*TagGroup) RemoveValues

func (g *TagGroup) RemoveValues(values ...string)

RemoveValues removes tags matching all the values from the group, i.e. only tags that have all the values are considered matches.

func (*TagGroup) Rename

func (g *TagGroup) Rename(newName string) error

Rename renames the group. The newName cannot be an empty string.

func (*TagGroup) SortFunc

func (g *TagGroup) SortFunc(fn LessFunc)

SortFunc sorts the tags by fn.

func (*TagGroup) SortNames

func (g *TagGroup) SortNames(desc bool)

SortNames sorts the tags by their name in ascending (desc == false) or descending (desc == true) order.

func (*TagGroup) Tags

func (g *TagGroup) Tags() []Tag

Tags returns the group tags.

Tags can be added to the group with the TagGroup.Add method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL