github.com/searKing/golang/go@v1.2.117/context/tags.mime.go (about) 1 // Copyright 2021 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 ( 8 "context" 9 "net/textproto" 10 ) 11 12 // ExtractMIMIETags returns a pre-existing Tags object in the Context. 13 // If the context wasn't set in a tag interceptor, a no-op Tag storage is returned that will *not* be propagated in context. 14 func ExtractMIMIETags(ctx context.Context, key any) (tags textproto.MIMEHeader, has bool) { 15 t, ok := ctx.Value(key).(textproto.MIMEHeader) 16 if !ok { 17 return textproto.MIMEHeader{}, false 18 } 19 20 return t, true 21 } 22 23 // ExtractOrCreateMIMETags extracts or create tags from context by key 24 func ExtractOrCreateMIMETags(ctx context.Context, key any) ( 25 ctx_ context.Context, stags textproto.MIMEHeader) { 26 tags, has := ExtractMIMIETags(ctx, key) 27 if has { 28 return ctx, tags 29 } 30 tags = textproto.MIMEHeader{} 31 return WithMIMETags(ctx, key, tags), tags 32 } 33 34 // WithMIMETags create tags from context by key 35 func WithMIMETags(ctx context.Context, key any, tags textproto.MIMEHeader) context.Context { 36 return context.WithValue(ctx, key, tags) 37 }