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