github.com/go-playground/pkg/v5@v5.29.1/context/context.go (about)

     1  package contextext
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  var _ context.Context = (*detachedContext)(nil)
    10  
    11  type detachedContext struct {
    12  	parent context.Context
    13  }
    14  
    15  // Detach returns a new context which continues to have access to its parent values but
    16  // is no longer bound/attached to the parents timeouts nor deadlines.
    17  //
    18  // If a nil context is passed in a new Background context will be used as the parent.
    19  //
    20  // This is useful for when you wish to pass along values such as span or logging information but do not want the
    21  // current operation to be cancelled despite what upstream code/callers think.
    22  func Detach(parent context.Context) detachedContext {
    23  	if parent == nil {
    24  		return detachedContext{parent: context.Background()}
    25  	}
    26  	return detachedContext{parent: parent}
    27  }
    28  
    29  func (c detachedContext) Deadline() (deadline time.Time, ok bool) {
    30  	return
    31  }
    32  
    33  func (c detachedContext) Done() <-chan struct{} {
    34  	return nil
    35  }
    36  
    37  func (c detachedContext) Err() error {
    38  	return nil
    39  }
    40  
    41  func (c detachedContext) Value(key interface{}) interface{} {
    42  	return c.parent.Value(key)
    43  }
    44  
    45  func (c detachedContext) String() string {
    46  	return fmt.Sprintf("%s.Detached", c.parent)
    47  }