github.com/MontFerret/ferret@v0.18.0/pkg/runtime/core/param.go (about)

     1  package core
     2  
     3  import "context"
     4  
     5  type key int
     6  
     7  const paramsKey key = 0
     8  
     9  func ParamsWith(ctx context.Context, params map[string]Value) context.Context {
    10  	return context.WithValue(ctx, paramsKey, params)
    11  }
    12  
    13  func ParamsFrom(ctx context.Context) (map[string]Value, error) {
    14  	val := ctx.Value(paramsKey)
    15  
    16  	param, ok := val.(map[string]Value)
    17  
    18  	if !ok {
    19  		return nil, Error(ErrNotFound, "parameters")
    20  	}
    21  
    22  	return param, nil
    23  }
    24  
    25  func ParamFrom(ctx context.Context, name string) (Value, error) {
    26  	params, err := ParamsFrom(ctx)
    27  
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	param, found := params[name]
    33  
    34  	if !found {
    35  		return nil, Error(ErrNotFound, "parameter."+name)
    36  	}
    37  
    38  	return param, nil
    39  }