unmarshaler.go 464 B

1234567891011121314151617181920212223242526
  1. package contextx
  2. import (
  3. "context"
  4. "github.com/tal-tech/go-zero/core/mapping"
  5. )
  6. const contextTagKey = "ctx"
  7. var unmarshaler = mapping.NewUnmarshaler(contextTagKey)
  8. type contextValuer struct {
  9. context.Context
  10. }
  11. func (cv contextValuer) Value(key string) (interface{}, bool) {
  12. v := cv.Context.Value(key)
  13. return v, v != nil
  14. }
  15. func For(ctx context.Context, v interface{}) error {
  16. return unmarshaler.UnmarshalValuer(contextValuer{
  17. Context: ctx,
  18. }, v)
  19. }