tagit

package
v0.0.0-...-8c59519 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2015 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package tagit/bson is a package that exports a type that can be used to add tag lists to your structs.

The tagit.Tags is designed to be used as an field in a struct (composition) like so:

type Article {
	Tags *tagit.Tags `json:"tags"`
}

By using tagit.Tags you will be able to use json.Marshal and json.Unmarshal on your type.

You will be able to use mgo/bson with this package. The type tagit.Tags knows how to be marshalled and unmarshalled to/from bson.

When using tagit.Tags you will have to initialize it with the tagit.NewTags() function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tags

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

Tags struct that holds tags and have a simple interface for working with the tags. By composing this the list of tags will be able to be marshalled/unmarshalled to/from JSON or bson.

func NewTags

func NewTags(tags ...string) *Tags

NewTags is a constructor for Tags. You should initialize your tags with this function. This function can accept variable number of string agruments as an initial set of tags for the created object.

func (*Tags) Add

func (t *Tags) Add(tag string)

Add adds a tag to the list.

func (*Tags) All

func (t *Tags) All() []string

All returns all tags as a slice of strings. The order of the tags in the slice may vary between different calls.

func (*Tags) Count

func (t *Tags) Count() int

Count counts the number of tags and return it as int.

func (*Tags) GetBSON

func (t *Tags) GetBSON() (interface{}, error)

GetBSON is actually implementing the bson.Getter interface so this can be Marshaled into bson. This may be added to the basic tagit implementation because it doesn't require any new imports.

func (*Tags) Has

func (t *Tags) Has(tag string) bool

Has checks if a tags is in the list. Returns boolean.

func (*Tags) MarshalJSON

func (t *Tags) MarshalJSON() ([]byte, error)

MarshalJSON to implement json.Marshaller. Returns the tags as JSON list of strings.

func (*Tags) Remove

func (t *Tags) Remove(tag string)

Remove removes a tag from the list.

func (*Tags) SetBSON

func (t *Tags) SetBSON(raw bson.Raw) error

SetBSON is implementing the bson.Setter interface so this can be Unmarshaled from bson. To implement the Setter interface we need to import the bson package, so we can have the bson.Raw type available.

func (*Tags) String

func (t *Tags) String() (res string)

String to implement fmt.Stringer. Return tags as comma separated list of words.

func (*Tags) UnmarshalJSON

func (t *Tags) UnmarshalJSON(json []byte) error

UnmarshalJSON to implement json.Unmarshaller. Expects JSON list of strings. Adds the tags to the list. Will not remove the current tags in the list.

Jump to

Keyboard shortcuts

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