本文整理汇总了Golang中github.com/codedellemc/libstorage/api/types.Context.Value方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Value方法的具体用法?Golang Context.Value怎么用?Golang Context.Value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/codedellemc/libstorage/api/types.Context
的用法示例。
在下文中一共展示了Context.Value方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Handle
// Handle is the type's Handler function.
func (h *postArgsHandler) Handle(
ctx types.Context,
w http.ResponseWriter,
req *http.Request,
store types.Store) error {
reqObj := ctx.Value("reqObj")
if reqObj == nil {
return fmt.Errorf("missing request object")
}
v := reflect.ValueOf(reqObj).Elem()
t := v.Type()
for i := 0; i < v.NumField(); i++ {
ft := t.Field(i)
fv := v.Field(i).Interface()
switch tfv := fv.(type) {
case nil:
// do nothing
case map[string]interface{}:
store.Set(getFieldName(ft), utils.NewStoreWithData(tfv))
default:
// add it to the store
store.Set(getFieldName(ft), fv)
}
}
return h.handler(ctx, w, req, store)
}
示例2: Services
func (c *client) Services(
ctx types.Context) (map[string]*types.ServiceInfo, error) {
reply := map[string]*types.ServiceInfo{}
url := "/services"
if ctx.Value(ctxInstanceForSvc) != nil {
url = "/services?instance"
}
if _, err := c.httpGet(ctx, url, &reply); err != nil {
return nil, err
}
return reply, nil
}
示例3: ServiceInspect
func (c *client) ServiceInspect(
ctx types.Context, name string) (*types.ServiceInfo, error) {
reply := &types.ServiceInfo{}
url := fmt.Sprintf("/services/%s", name)
if ctx.Value(ctxInstanceForSvc) != nil {
url = fmt.Sprintf("/services/%s?instance", name)
}
if _, err := c.httpGet(ctx, url, &reply); err != nil {
return nil, err
}
return reply, nil
}
示例4: httpDo
func (c *client) httpDo(
ctx types.Context,
method, path string,
payload, reply interface{}) (*http.Response, error) {
reqBody, err := encPayload(payload)
if err != nil {
return nil, err
}
url := fmt.Sprintf("http://%s%s", c.host, path)
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
return nil, err
}
ctx = context.RequireTX(ctx)
tx := context.MustTransaction(ctx)
ctx = ctx.WithValue(transactionHeaderKey, tx)
if iid, ok := context.InstanceID(ctx); ok {
ctx = ctx.WithValue(instanceIDHeaderKey, iid)
} else if iidMap, ok := ctx.Value(
context.AllInstanceIDsKey).(types.InstanceIDMap); ok {
if len(iidMap) > 0 {
var iids []fmt.Stringer
for _, iid := range iidMap {
iids = append(iids, iid)
}
ctx = ctx.WithValue(instanceIDHeaderKey, iids)
}
}
if lds, ok := context.LocalDevices(ctx); ok {
ctx = ctx.WithValue(localDevicesHeaderKey, lds)
} else if ldsMap, ok := ctx.Value(
context.AllLocalDevicesKey).(types.LocalDevicesMap); ok {
if len(ldsMap) > 0 {
var ldsess []fmt.Stringer
for _, lds := range ldsMap {
ldsess = append(ldsess, lds)
}
ctx = ctx.WithValue(localDevicesHeaderKey, ldsess)
}
}
for key := range context.CustomHeaderKeys() {
var headerName string
switch tk := key.(type) {
case string:
headerName = tk
case fmt.Stringer:
headerName = tk.String()
default:
headerName = fmt.Sprintf("%v", key)
}
if headerName == "" {
continue
}
val := ctx.Value(key)
switch tv := val.(type) {
case string:
req.Header.Add(headerName, tv)
case fmt.Stringer:
req.Header.Add(headerName, tv.String())
case []string:
for _, sv := range tv {
req.Header.Add(headerName, sv)
}
case []fmt.Stringer:
for _, sv := range tv {
req.Header.Add(headerName, sv.String())
}
default:
if val != nil {
req.Header.Add(headerName, fmt.Sprintf("%v", val))
}
}
}
c.logRequest(req)
res, err := ctxhttp.Do(ctx, &c.Client, req)
if err != nil {
return nil, err
}
defer c.setServerName(res)
c.logResponse(res)
if res.StatusCode > 299 {
httpErr, err := goof.DecodeHTTPError(res.Body)
if err != nil {
return res, goof.WithField("status", res.StatusCode, "http error")
}
return res, httpErr
//.........这里部分代码省略.........