github.com/searKing/golang/go@v1.2.117/context/tags.map.go (about)

     1  // Copyright 2020 The searKing Author. 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.
     4  
     5  package context
     6  
     7  import "net/textproto"
     8  
     9  //go:generate go-option -type=mapTags
    10  type mapTags struct {
    11  	isMimeKey bool // represents a MIME-style key mapping
    12  
    13  	values map[string]any
    14  }
    15  
    16  func (t *mapTags) Set(key string, value any) {
    17  	if t.isMimeKey {
    18  		key = textproto.CanonicalMIMEHeaderKey(key)
    19  	}
    20  	t.values[key] = value
    21  }
    22  
    23  func (t *mapTags) Get(key string) (any, bool) {
    24  	if t.isMimeKey {
    25  		key = textproto.CanonicalMIMEHeaderKey(key)
    26  	}
    27  	val, ok := t.values[key]
    28  	return val, ok
    29  }
    30  
    31  // Del deletes the values associated with key.
    32  func (t *mapTags) Del(key string) {
    33  	if t.isMimeKey {
    34  		key = textproto.CanonicalMIMEHeaderKey(key)
    35  	}
    36  	delete(t.values, key)
    37  }
    38  
    39  func (t *mapTags) Values() map[string]any {
    40  	return t.values
    41  }
    42  
    43  // WithMapTagsMimeKey represents a MIME-style key mapping
    44  func WithMapTagsMimeKey() MapTagsOption {
    45  	return MapTagsOptionFunc(func(t *mapTags) {
    46  		t.isMimeKey = true
    47  	})
    48  }