在Golang中将元数据传递到上下文中时出现错误。

huangapple go评论87阅读模式
英文:

Errors when passing metadata into context in golang

问题

我正在处理处理GRPC请求的特定问题。我试图根据这个代码示例将我的GRPC请求中的元数据传递到上下文中:https://github.com/go-kit/kit/blob/master/auth/jwt/transport.go#L47。

(以防万一,可以在这里参考contextKey的解释:https://medium.com/@matryer/context-keys-in-go-5312346a868d#.vn10llkse):

以下是我的代码:

type contextKey string

func (c contextKey) String() string {
    return string(c)
}

var Headers := metadata.New(map[string]string{"auth":"","abc": "", "xyz" : ""})

func ToGRPCContext() grpctransport.RequestFunc {
    return func(ctx context.Context, md *metadata.MD) context.Context {
        for _, header := range Headers {
            val, ok := (*md)[header]
            if !ok {
                return ctx
            }
            if len(val) > 0 {
                ctx = context.WithValue(ctx, contextKey(header), val)
            }
        }
        return ctx
    }
}

我试图读取元数据字段(Headers)并将其传递给上下文。

我遇到了以下错误:cannot use header (type []string) as type string in map indexcannot convert header (type []string) to type contextKey。我通过访问索引并执行类似于val, ok := (*md)[header[0]]的操作来修复上述错误。然而,我想将映射的所有元素传递给上下文。

有关如何解决这个问题的建议吗?

英文:

I am working on specific with handling GRPC requests. I am trying to pass the meta from my GRPC request into the context based on this code sample: https://github.com/go-kit/kit/blob/master/auth/jwt/transport.go#L47.

(just in case, the contextKey explanation can be referred here: https://medium.com/@matryer/context-keys-in-go-5312346a868d#.vn10llkse):

Below is my code:

type contextKey string

func (c contextKey) String() string {
    return string(c)
}

var Headers := metadata.New(map[string]string{"auth":"", "abc": "", "xyz" : ""})

func ToGRPCContext() grpctransport.RequestFunc {
    return func(ctx context.Context, md *metadata.MD) context.Context {
	   for _, header := range Headers {
		   val, ok := (*md)[header]
		   if !ok {
			   return ctx
		   }
		   if len(val) > 0 {
			  ctx = context.WithValue(ctx, contextKey(header), val)
		   }
	   }
	   return ctx
    }

}

I am trying to read metadata fields (Headers) and pass it to the context.

I am getting the following errors. cannot use header (type []string) as type string in map index and cannot convert header (type []string) to type contextKey. I had fixed the above errors by accessing the index and doing something like this val, ok := (*md)[header[0]]. However, I want to pass all the elements of the map to the context.

Any suggestions on how to work around this problem?

答案1

得分: 2

我认为你想将标题名称用作上下文键:

for name, header := range Headers {
  val := r.Header.Get(header)
  if len(val) > 0 {
    ctx = context.WithValue(ctx, contextKey(name), val)
  }
}

或者,将标题存储为单个值:

ctx = context.WithValue(ctx, contextKey("headers"), Headers)
英文:

I think you want to use the header name as the context key:

for name, header := range Headers {
  val := r.Header.Get(header)
  if len(val) > 0 {
    ctx = context.WithValue(ctx, contextKey(name), val)
  }
}

Alternatively, store the headers as a single value:

ctx = context.WithValue(ctx, contextKey("headers"), Headers)

huangapple
  • 本文由 发表于 2017年2月17日 02:02:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/42281477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定