github.com/lingyao2333/mo-zero@v1.4.1/core/contextx/unmarshaler.go (about) 1 package contextx 2 3 import ( 4 "context" 5 6 "github.com/lingyao2333/mo-zero/core/mapping" 7 ) 8 9 const contextTagKey = "ctx" 10 11 var unmarshaler = mapping.NewUnmarshaler(contextTagKey) 12 13 type contextValuer struct { 14 context.Context 15 } 16 17 func (cv contextValuer) Value(key string) (interface{}, bool) { 18 v := cv.Context.Value(key) 19 return v, v != nil 20 } 21 22 // For unmarshals ctx into v. 23 func For(ctx context.Context, v interface{}) error { 24 return unmarshaler.UnmarshalValuer(contextValuer{ 25 Context: ctx, 26 }, v) 27 }