github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/trigger/context.go (about)

     1  package trigger
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     7  )
     8  
     9  type key int
    10  var handlerKey key
    11  
    12  type HandlerInfo struct {
    13  	Name string
    14  }
    15  
    16  // NewHandlerContext add the handler info to a new child context
    17  func NewHandlerContext(parentCtx context.Context, config *HandlerConfig) context.Context {
    18  	if config != nil && config.Name != "" {
    19  		return context.WithValue(parentCtx, handlerKey, &HandlerInfo{Name:config.Name})
    20  	}
    21  	return parentCtx
    22  }
    23  
    24  // HandlerFromContext returns the handler info stored in the context, if any.
    25  func HandlerFromContext(ctx context.Context) (*HandlerInfo, bool) {
    26  	u, ok := ctx.Value(handlerKey).(*HandlerInfo)
    27  	return u, ok
    28  }
    29  
    30  // DEPRECATED
    31  var ctxDataKey key
    32  
    33  type ContextData struct {
    34  	Attrs      []*data.Attribute
    35  	HandlerCfg *HandlerConfig
    36  }
    37  
    38  // NewContext returns a new Context that carries the trigger data.
    39  // DEPRECATED
    40  func NewContext(parentCtx context.Context, attrs []*data.Attribute) context.Context {
    41  	ctxData := &ContextData{Attrs: attrs}
    42  	return context.WithValue(parentCtx, ctxDataKey, ctxData)
    43  }
    44  
    45  // DEPRECATED
    46  func NewInitialContext(attrs []*data.Attribute, config *HandlerConfig) context.Context {
    47  	return context.WithValue(context.Background(), ctxDataKey, &ContextData{Attrs: attrs, HandlerCfg: config})
    48  }
    49  
    50  // NewContext returns a new Context that carries the trigger data.
    51  // DEPRECATED
    52  func NewContextWithData(parentCtx context.Context, contextData *ContextData) context.Context {
    53  	return context.WithValue(parentCtx, ctxDataKey, contextData)
    54  }
    55  
    56  // DEPRECATED
    57  func ExtractContextData(ctx context.Context) (*ContextData, bool) {
    58  	if ctx == nil {
    59  		return nil, false
    60  	}
    61  	ctxDataVal := ctx.Value(ctxDataKey)
    62  	if ctxDataVal == nil {
    63  		return nil, false
    64  	}
    65  	ctxData, ok := ctxDataVal.(*ContextData)
    66  	return ctxData, ok
    67  }