1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // Tag holds a key and value pair.
15 // It is normally used when passing around lists of tags.
22 // TagMap is the interface to a collection of Tags indexed by key.
23 type TagMap interface {
24 // Find returns the tag that matches the supplied key.
28 // TagList is the interface to something that provides an iterable
30 // Iteration should start from 0 and continue until Valid returns false.
31 type TagList interface {
32 // Valid returns true if the index is within range for the list.
33 // It does not imply the tag at that index will itself be valid.
35 // Tag returns the tag at the given index.
39 // tagList implements TagList for a list of Tags.
44 // tagFilter wraps a TagList filtering out specific tags.
45 type tagFilter struct {
50 // tagMap implements TagMap for a simple list of tags.
55 // tagMapChain implements TagMap for a list of underlying TagMap.
56 type tagMapChain struct {
60 // TagOfValue creates a new tag from the key and value.
61 // This method is for implementing new key types, tag creation should
62 // normally be done with the Of method of the key.
63 func TagOfValue(k Key, value interface{}) Tag { return Tag{key: k, untyped: value} }
65 // UnpackValue assumes the tag was built using TagOfValue and returns the value
66 // that was passed to that constructor.
67 // This method is for implementing new key types, for type safety normal
68 // access should be done with the From method of the key.
69 func (t Tag) UnpackValue() interface{} { return t.untyped }
71 // TagOf64 creates a new tag from a key and a uint64. This is often
72 // used for non uint64 values that can be packed into a uint64.
73 // This method is for implementing new key types, tag creation should
74 // normally be done with the Of method of the key.
75 func TagOf64(k Key, v uint64) Tag { return Tag{key: k, packed: v} }
77 // Unpack64 assumes the tag was built using TagOf64 and returns the value that
78 // was passed to that constructor.
79 // This method is for implementing new key types, for type safety normal
80 // access should be done with the From method of the key.
81 func (t Tag) Unpack64() uint64 { return t.packed }
83 // TagOfString creates a new tag from a key and a string.
84 // This method is for implementing new key types, tag creation should
85 // normally be done with the Of method of the key.
86 func TagOfString(k Key, v string) Tag {
87 hdr := (*reflect.StringHeader)(unsafe.Pointer(&v))
90 packed: uint64(hdr.Len),
91 untyped: unsafe.Pointer(hdr.Data),
95 // UnpackString assumes the tag was built using TagOfString and returns the
96 // value that was passed to that constructor.
97 // This method is for implementing new key types, for type safety normal
98 // access should be done with the From method of the key.
99 func (t Tag) UnpackString() string {
101 hdr := (*reflect.StringHeader)(unsafe.Pointer(&v))
102 hdr.Data = uintptr(t.untyped.(unsafe.Pointer))
103 hdr.Len = int(t.packed)
104 return *(*string)(unsafe.Pointer(hdr))
107 // Valid returns true if the Tag is a valid one (it has a key).
108 func (t Tag) Valid() bool { return t.key != nil }
110 // Key returns the key of this Tag.
111 func (t Tag) Key() Key { return t.key }
113 // Format is used for debug printing of tags.
114 func (t Tag) Format(f fmt.State, r rune) {
116 io.WriteString(f, `nil`)
119 io.WriteString(f, t.Key().Name())
120 io.WriteString(f, "=")
122 t.Key().Format(f, buf[:0], t)
125 func (l *tagList) Valid(index int) bool {
126 return index >= 0 && index < len(l.tags)
129 func (l *tagList) Tag(index int) Tag {
133 func (f *tagFilter) Valid(index int) bool {
134 return f.underlying.Valid(index)
137 func (f *tagFilter) Tag(index int) Tag {
138 tag := f.underlying.Tag(index)
139 for _, f := range f.keys {
147 func (l tagMap) Find(key Key) Tag {
148 for _, tag := range l.tags {
149 if tag.Key() == key {
156 func (c tagMapChain) Find(key Key) Tag {
157 for _, src := range c.maps {
166 var emptyList = &tagList{}
168 func NewTagList(tags ...Tag) TagList {
172 return &tagList{tags: tags}
175 func Filter(l TagList, keys ...Key) TagList {
179 return &tagFilter{keys: keys, underlying: l}
182 func NewTagMap(tags ...Tag) TagMap {
183 return tagMap{tags: tags}
186 func MergeTagMaps(srcs ...TagMap) TagMap {
188 for _, src := range srcs {
190 nonNil = append(nonNil, src)
193 if len(nonNil) == 1 {
196 return tagMapChain{maps: nonNil}