@@ -28,10 +28,15 @@ type Tag struct {
2828 Value string
2929}
3030
31+ type tagContent struct {
32+ value string
33+ m metadatas
34+ }
35+
3136// Map is a map of tags. Use New to create a context containing
3237// a new Map.
3338type Map struct {
34- m map [Key ]string
39+ m map [Key ]tagContent
3540}
3641
3742// Value returns the value for the key if a value for the key exists.
@@ -40,7 +45,7 @@ func (m *Map) Value(k Key) (string, bool) {
4045 return "" , false
4146 }
4247 v , ok := m .m [k ]
43- return v , ok
48+ return v . value , ok
4449}
4550
4651func (m * Map ) String () string {
@@ -62,29 +67,29 @@ func (m *Map) String() string {
6267 return buffer .String ()
6368}
6469
65- func (m * Map ) insert (k Key , v string ) {
70+ func (m * Map ) insert (k Key , v string , md metadatas ) {
6671 if _ , ok := m .m [k ]; ok {
6772 return
6873 }
69- m .m [k ] = v
74+ m .m [k ] = tagContent { value : v , m : md }
7075}
7176
72- func (m * Map ) update (k Key , v string ) {
77+ func (m * Map ) update (k Key , v string , md metadatas ) {
7378 if _ , ok := m .m [k ]; ok {
74- m .m [k ] = v
79+ m .m [k ] = tagContent { value : v , m : md }
7580 }
7681}
7782
78- func (m * Map ) upsert (k Key , v string ) {
79- m .m [k ] = v
83+ func (m * Map ) upsert (k Key , v string , md metadatas ) {
84+ m .m [k ] = tagContent { value : v , m : md }
8085}
8186
8287func (m * Map ) delete (k Key ) {
8388 delete (m .m , k )
8489}
8590
8691func newMap () * Map {
87- return & Map {m : make (map [Key ]string )}
92+ return & Map {m : make (map [Key ]tagContent )}
8893}
8994
9095// Mutator modifies a tag map.
@@ -95,13 +100,17 @@ type Mutator interface {
95100// Insert returns a mutator that inserts a
96101// value associated with k. If k already exists in the tag map,
97102// mutator doesn't update the value.
98- func Insert (k Key , v string ) Mutator {
103+ // Metadata applies metadata to the tag. It is optional.
104+ // Metadatas are applied in the order in which it is provided.
105+ // If more than one metadata updates the same attribute then
106+ // the update from the last metadata prevails.
107+ func Insert (k Key , v string , mds ... Metadata ) Mutator {
99108 return & mutator {
100109 fn : func (m * Map ) (* Map , error ) {
101110 if ! checkValue (v ) {
102111 return nil , errInvalidValue
103112 }
104- m .insert (k , v )
113+ m .insert (k , v , createMetadatas ( mds ... ) )
105114 return m , nil
106115 },
107116 }
@@ -110,13 +119,17 @@ func Insert(k Key, v string) Mutator {
110119// Update returns a mutator that updates the
111120// value of the tag associated with k with v. If k doesn't
112121// exists in the tag map, the mutator doesn't insert the value.
113- func Update (k Key , v string ) Mutator {
122+ // Metadata applies metadata to the tag. It is optional.
123+ // Metadatas are applied in the order in which it is provided.
124+ // If more than one metadata updates the same attribute then
125+ // the update from the last metadata prevails.
126+ func Update (k Key , v string , mds ... Metadata ) Mutator {
114127 return & mutator {
115128 fn : func (m * Map ) (* Map , error ) {
116129 if ! checkValue (v ) {
117130 return nil , errInvalidValue
118131 }
119- m .update (k , v )
132+ m .update (k , v , createMetadatas ( mds ... ) )
120133 return m , nil
121134 },
122135 }
@@ -126,18 +139,37 @@ func Update(k Key, v string) Mutator {
126139// value of the tag associated with k with v. It inserts the
127140// value if k doesn't exist already. It mutates the value
128141// if k already exists.
129- func Upsert (k Key , v string ) Mutator {
142+ // Metadata applies metadata to the tag. It is optional.
143+ // Metadatas are applied in the order in which it is provided.
144+ // If more than one metadata updates the same attribute then
145+ // the update from the last metadata prevails.
146+ func Upsert (k Key , v string , mds ... Metadata ) Mutator {
130147 return & mutator {
131148 fn : func (m * Map ) (* Map , error ) {
132149 if ! checkValue (v ) {
133150 return nil , errInvalidValue
134151 }
135- m .upsert (k , v )
152+ m .upsert (k , v , createMetadatas ( mds ... ) )
136153 return m , nil
137154 },
138155 }
139156}
140157
158+ func createMetadatas (mds ... Metadata ) metadatas {
159+ var metas metadatas
160+ if len (mds ) > 0 {
161+ for _ , md := range mds {
162+ if md != nil {
163+ md (& metas )
164+ }
165+ }
166+ } else {
167+ WithTTL (TTLUnlimitedPropagation )(& metas )
168+ }
169+ return metas
170+
171+ }
172+
141173// Delete returns a mutator that deletes
142174// the value associated with k.
143175func Delete (k Key ) Mutator {
@@ -160,10 +192,10 @@ func New(ctx context.Context, mutator ...Mutator) (context.Context, error) {
160192 if ! checkKeyName (k .Name ()) {
161193 return ctx , fmt .Errorf ("key:%q: %v" , k , errInvalidKeyName )
162194 }
163- if ! checkValue (v ) {
195+ if ! checkValue (v . value ) {
164196 return ctx , fmt .Errorf ("key:%q value:%q: %v" , k .Name (), v , errInvalidValue )
165197 }
166- m .insert (k , v )
198+ m .insert (k , v . value , v . m )
167199 }
168200 }
169201 var err error
0 commit comments