Skip to content

Commit 6c7cc58

Browse files
committed
Initial commit
0 parents  commit 6c7cc58

7 files changed

Lines changed: 443 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
example/example

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: go
2+
go:
3+
- 1.1
4+
- 1.2.2
5+
- 1.3.3
6+
- tip

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Igor Dolzhikov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
Go Tags
2+
=======
3+
4+
A tags package for use with Go (golang) services
5+
6+
[![Build Status](https://travis-ci.org/takama/tags.png?branch=master)](https://travis-ci.org/takama/tags)
7+
[![GoDoc](https://godoc.org/github.com/takama/tags?status.svg)](https://godoc.org/github.com/takama/tags)
8+
9+
### Definitions
10+
11+
"Tags" - a strings which used for tag any object
12+
"non-strict Tags" - a strings which match to strings in data ("new" -> "new")
13+
"strict Tags" - all strict tags have prefix "+" for strict match ("+new")
14+
and "-" for strict mismatch ("-old")
15+
16+
All strict Tags applied with logical operator "AND" between each other
17+
All non-strict Tags applied with logical operator "OR" between all tags
18+
19+
### Example
20+
21+
```go
22+
package main
23+
24+
import (
25+
"fmt"
26+
27+
"github.com/takama/tags"
28+
)
29+
30+
// Product is struct with tags
31+
type Product struct {
32+
Name string
33+
Description string
34+
Tags tags.Tags
35+
}
36+
37+
func main() {
38+
product := Product{
39+
Name: "the tee",
40+
Description: "the boutle of ice black tee with sugar",
41+
Tags: tags.Tags{"ice", "black", "sugar"},
42+
}
43+
44+
fmt.Println("Product:", product.Description)
45+
46+
fmt.Println("Is this tee black or green?")
47+
query := tags.Tags{"black", "green"}
48+
if product.Tags.IsTagged(query) {
49+
fmt.Println("Yes, the tee is black.")
50+
} else {
51+
fmt.Println("No, the tee has not black or green options.")
52+
}
53+
54+
fmt.Println("Is this tee green with sugar?")
55+
query = tags.Tags{"+green", "+sugar"}
56+
if product.Tags.IsTagged(query) {
57+
fmt.Println("Yes, the tee is green with sugar.")
58+
} else {
59+
fmt.Println("No, the tee with sugar, but is not green.")
60+
}
61+
62+
fmt.Println("Is this tee hot?")
63+
query = tags.Tags{"-ice"}
64+
if product.Tags.IsTagged(query) {
65+
fmt.Println("Yes, the tee is hot.")
66+
} else {
67+
fmt.Println("No, it is ice tee.")
68+
}
69+
}
70+
```
71+
72+
## Author
73+
74+
[Igor Dolzhikov](https://github.com/takama)
75+
76+
## Contributors
77+
78+
All the contributors are welcome. If you would like to be the contributor please accept some rules.
79+
- The pull requests will be accepted only in "develop" branch
80+
- All modifications or additions should be tested
81+
- Sorry, I'll not accept code with any dependency, only standard library
82+
83+
Thank you for your understanding!
84+
85+
## License
86+
87+
[MIT Public License](https://github.com/takama/tags/blob/master/LICENSE)

example/example.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/takama/tags"
7+
)
8+
9+
// Product is struct with tags
10+
type Product struct {
11+
Name string
12+
Description string
13+
Tags tags.Tags
14+
}
15+
16+
func main() {
17+
product := Product{
18+
Name: "the tee",
19+
Description: "the boutle of ice black tee with sugar",
20+
Tags: tags.Tags{"ice", "black", "sugar"},
21+
}
22+
23+
fmt.Println("Product:", product.Description)
24+
25+
fmt.Println("Is this tee black or green?")
26+
query := tags.Tags{"black", "green"}
27+
if product.Tags.IsTagged(query) {
28+
fmt.Println("Yes, the tee is black.")
29+
} else {
30+
fmt.Println("No, the tee has not black or green options.")
31+
}
32+
33+
fmt.Println("Is this tee green with sugar?")
34+
query = tags.Tags{"+green", "+sugar"}
35+
if product.Tags.IsTagged(query) {
36+
fmt.Println("Yes, the tee is green with sugar.")
37+
} else {
38+
fmt.Println("No, the tee with sugar, but is not green.")
39+
}
40+
41+
fmt.Println("Is this tee hot?")
42+
query = tags.Tags{"-ice"}
43+
if product.Tags.IsTagged(query) {
44+
fmt.Println("Yes, the tee is hot.")
45+
} else {
46+
fmt.Println("No, it is ice tee.")
47+
}
48+
}

tags.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2014 Igor Dolzhikov. All rights reserved.
2+
// Use of this source code is governed by a license
3+
// that can be found in the LICENSE file.
4+
5+
/*
6+
Package tags 0.1.0
7+
8+
Definition:
9+
10+
"Tags" - a strings which used for tag any object
11+
"non-strict Tags" - a strings which match to strings in data ("new" -> "new")
12+
"strict Tags" - all strict tags have prefix "+" for strict match ("+new")
13+
and "-" for strict mismatch ("-old")
14+
15+
All strict Tags applied with logical operator "AND" between each other
16+
All non-strict Tags applied with logical operator "OR" between all tags
17+
18+
Example:
19+
20+
package main
21+
22+
import (
23+
"fmt"
24+
25+
"github.com/takama/tags"
26+
)
27+
28+
// Product is struct with tags
29+
type Product struct {
30+
Name string
31+
Description string
32+
Tags tags.Tags
33+
}
34+
35+
func main() {
36+
product := Product{
37+
Name: "the tee",
38+
Description: "the boutle of ice black tee with sugar",
39+
Tags: tags.Tags{"ice", "black", "sugar"},
40+
}
41+
42+
fmt.Println("Product:", product.Description)
43+
44+
fmt.Println("Is this tee black or green?")
45+
query := tags.Tags{"black", "green"}
46+
if product.Tags.IsTagged(query) {
47+
fmt.Println("Yes, the tee is black.")
48+
} else {
49+
fmt.Println("No, the tee has not black or green options.")
50+
}
51+
52+
fmt.Println("Is this tee green with sugar?")
53+
query = tags.Tags{"+green", "+sugar"}
54+
if product.Tags.IsTagged(query) {
55+
fmt.Println("Yes, the tee is green with sugar.")
56+
} else {
57+
fmt.Println("No, the tee with sugar, but is not green.")
58+
}
59+
60+
fmt.Println("Is this tee hot?")
61+
query = tags.Tags{"-ice"}
62+
if product.Tags.IsTagged(query) {
63+
fmt.Println("Yes, the tee is hot.")
64+
} else {
65+
fmt.Println("No, it is ice tee.")
66+
}
67+
}
68+
69+
70+
Go Tags
71+
*/
72+
package tags
73+
74+
import (
75+
"strings"
76+
)
77+
78+
// Tags a slice of strings which used for tag any object
79+
type Tags []string
80+
81+
// IsTagged method checks if elements in a data compliant for tags in query
82+
func (t Tags) IsTagged(query Tags) bool {
83+
if len(query) == 0 {
84+
return true
85+
}
86+
if len(t) == 0 {
87+
return false
88+
}
89+
for _, qTag := range query {
90+
for _, tag := range t {
91+
if tag == qTag {
92+
return true
93+
}
94+
}
95+
}
96+
97+
var strict uint8
98+
for _, qTag := range query {
99+
if strings.HasPrefix(qTag, "+") || strings.HasPrefix(qTag, "-") {
100+
strict++
101+
}
102+
}
103+
if strict == 0 {
104+
return false
105+
}
106+
for _, qTag := range query {
107+
if strings.HasPrefix(qTag, "+") {
108+
for _, tag := range t {
109+
if tag == strings.TrimPrefix(qTag, "+") {
110+
strict--
111+
break
112+
}
113+
}
114+
}
115+
if strings.HasPrefix(qTag, "-") {
116+
fit := true
117+
for _, tag := range t {
118+
if tag == strings.TrimPrefix(qTag, "-") {
119+
fit = false
120+
break
121+
}
122+
}
123+
if fit {
124+
strict--
125+
}
126+
}
127+
}
128+
129+
return strict == 0
130+
}

0 commit comments

Comments
 (0)