github.com/gogf/gf@v1.16.9/net/gtrace/gtrace_carrier.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gtrace 8 9 import ( 10 "github.com/gogf/gf/internal/json" 11 "github.com/gogf/gf/util/gconv" 12 ) 13 14 // Carrier is the storage medium used by a TextMapPropagator. 15 type Carrier map[string]interface{} 16 17 func NewCarrier(data ...map[string]interface{}) Carrier { 18 if len(data) > 0 && data[0] != nil { 19 return data[0] 20 } else { 21 return make(map[string]interface{}) 22 } 23 } 24 25 // Get returns the value associated with the passed key. 26 func (c Carrier) Get(k string) string { 27 return gconv.String(c[k]) 28 } 29 30 // Set stores the key-value pair. 31 func (c Carrier) Set(k, v string) { 32 c[k] = v 33 } 34 35 // Keys lists the keys stored in this carrier. 36 func (c Carrier) Keys() []string { 37 keys := make([]string, 0, len(c)) 38 for k := range c { 39 keys = append(keys, k) 40 } 41 return keys 42 } 43 44 func (c Carrier) MustMarshal() []byte { 45 b, err := json.Marshal(c) 46 if err != nil { 47 panic(err) 48 } 49 return b 50 } 51 52 func (c Carrier) String() string { 53 return string(c.MustMarshal()) 54 } 55 56 func (c Carrier) UnmarshalJSON(b []byte) error { 57 carrier := NewCarrier(nil) 58 return json.UnmarshalUseNumber(b, carrier) 59 }