tagit

package module
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: 3 Imported by: 0

README

tagit

BuildStatus Coverage Status GoDoc status

tagit is a package that exports two types that can be used to add tag lists to your structs.

Example

go get github.com/ndyakov/tagit

type Article struct {
	Name string      `json:"name"`
	Tags *tagit.Tags `json:"tags"`
}

func NewArticle(name) *Article {
	return &Article{Name: name, Tags: tagit.NewTags()}
}

func main() {
	a := NewArticle("Tagit!")
	a.Tags.Add("example")
	tags := a.Tags.All()  // ['example']
	a.Tags.Has("example") // true
	a.Tags.Count()        // 1
}

You can also initialize *tagit.Tags with initial set of tags.

func main() {
	tags := tagit.NewTags("wow", "such", "tags")
	tags.All()  // ['wow', "such", "tags"]
	tags.Has("example") // false
	tags.Has("wow")     // true
	tags.Count()        // 3
}
Taggable

This may be removed, because I don't see why anyone whould like to use it instead of Tags

The first of them (tagit.Taggable) can be used as anonymous field in a struct:

	type Article struct {
		tagit.Taggable
	}

This type provides barebones tag operations and cannot be Marshaled/Unmarshaled by itself.

Tags

The other one (tagit.Tags) is designed to be used as an field in a struct (as composite) and is the prefered way of using taggit:

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

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

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

type Tags
  func NewTags(tags ...string) *Tags
  func (t *Tags) Add(tag string)
  func (t *Tags) All() []string
  func (t *Tags) Count() int
  func (t *Tags) Has(tag string) bool
  func (t *Tags) MarshalJSON() ([]byte, error)
  func (t *Tags) Remove(tag string)
  func (t *Tags) String() (res string)
  func (t *Tags) UnmarshalJSON(json []byte) error

Tagit + bson

GoDoc

import "github.com/ndyakov/tagit/bson"

If you want to use this with mgo (the mongo driver for golang) you can use the tagit/bson packet that has a Tags type similar to the one in the root packet but with the possibility to be Marshaled to BSON and Unmarshaled from BSON. This can be used with mgo.

	type Artwork struct {
		Name string      `bson:"name, omitempty" json:"name"`
		Tags *tagit.Tags `bson:"tags, omitempty" json:"tags"`
	}

tagit/bson's Tags type obeys the same interface as tagit.Tags.

Why are there different packages with almost the same type?

You may wonder why there are few different packages with almost the same Tags type an which one you should use.

The main reason is that working with bson, for example, needs additional imports and I want to keep the root package as slim as possible. Althought this will be harder to maintain I think it is a reasonable solutions for small package as this one.

Contributions

Before contributing please execute:

  • gofmt
  • golint

License

The MIT License (MIT)

Copyright (c) 2015 Nedyalko Dyakov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package tagit is a package that exports two types that can be used to add tag lists to your structs. The first of them (tagit.Taggable) can be used as anonymous field in a struct:

type Article {
	tagit.Taggable
}

The other one (tagit.Tags) is designed to be used as an field in a struct (composition) and is the prefered way of using taggit:

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.

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 Taggable

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

Taggable is a simple type that you can add as anonymous field to your struct and then tag/untag instances of your type

func (*Taggable) HasTag

func (t *Taggable) HasTag(tag string) bool

HasTag checks if a tag is available in the tag list.

func (*Taggable) NumTags

func (t *Taggable) NumTags() int

NumTags counts the number of tags in the list.

func (*Taggable) Tag

func (t *Taggable) Tag(tag string)

Tag adds tag to the list.

func (*Taggable) Tags

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

Tags returns the tags list as slice of strings. Be aware that the order of the elements may differ between calls.

func (*Taggable) Untag

func (t *Taggable) Untag(tag string)

Untag removes tag from the list.

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.

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) 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) 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.

Directories

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

Jump to

Keyboard shortcuts

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