Golang地图比较:部分匹配

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

golang map comparison: partial match

问题

我正在寻找将收到的HTTP请求头与作为结构的一部分存储的预期头映射进行比较的方法:

type Request struct {
    URI     string
    Method  string
    Headers map[string]interface{}
}

我需要确保结构中定义的头在传入的请求中存在。我不关心是否有额外的头我没有预期的,但是结构中存储的所有头都必须存在。

在Golang中是否有一种约定来确定一个映射中的所有项是否存在于另一个映射中?以下是一些示例数据:

{
  "expected_headers": {
    "Content-Type": ["application/json"],
    "Accept-Encoding": ["gzip, deflate"]
  },
  "received_headers": {
    "Content-Type": ["application/json"],
    "Accept-Encoding": ["gzip, deflate"],
    "Accept": ["application/json"]
  }
}

这是一个正面的例子:即测试预期头是否存在于接收到的头中的结果应该为True。

我知道我可以循环遍历预期头的集合,并在接收到的头中查找每个头。然而,我希望有一种更优雅的方法来完成同样的事情。

根据评论,我已经添加了我的解决方案。我自由承认我对Go是全新的(尽管我已经在许多不同的语言中编程了几十年)。我的解决方案对我来说似乎不够优雅。欢迎更好的解决方案!

func contains(theSlice []string, theValue string) bool {
    for _, value := range theSlice {
        if value == theValue {
            return true
        }
    }
    return false
}

func slicesMatch(slice1 []string, slice2 []string) bool {
    for _, value := range slice1 {
        if !(contains(slice2, value)) {
            return false
        }
    }
    return true
}

func headersMatch(expected map[string][]string, actual http.Header) bool {
    for key, value := range expected {
        if !(slicesMatch(value, actual[key])) {
            return false
        }
    }
    return true
}

希望对你有所帮助!

英文:

I am looking to compare received HTTP request headers with a map of expected headers being stored as part of a struct:

type Request struct {
    URI    string
    Method string
    Headers map[string]interface{}
}

I need to make sure that the Headers defined in the struct exist in the incoming request. I don't care if there are extra headers I wasn't expecting, but all headers stored in the struct must be present.

Is there a golang convention for determining whether all items in a map exist in another map? Some example data:

{
  "expected_headers": {
    "Content-Type": ["application/json"],
    "Accept-Encoding": ["gzip, deflate"]
  },
  "received_headers": {
    "Content-Type": ["application/json"],
    "Accept-Encoding": ["gzip, deflate"],
    "Accept": ["application/json"]
}

That is a positive example: i.e. the result of testing if the expected headers are present in the received headers should be True.

I know I could loop over the set of expected_headers and look for each of them in received_headers. However, I'm hoping there is a more elegant way to accomplish the same thing.


Based on the comments, I have added my solution. I freely admit that I'm brand new to Go (though I've been coding in many different languages for decades). My solution does not seem elegant to me. Better solutions welcome!

func contains(theSlice []string, theValue string) bool {
    for _, value := range theSlice {
	    if value == theValue {
		    return true
	    }
    }
    return false
}

func slicesMatch(slice1 []string, slice2 []string) bool {
    for _, value := range slice1 {
	    if !(contains(slice2, value)) {
		    return false
	    }
    }
    return true
}

func headersMatch(expected map[string][]string, actual http.Header) bool {
    for key, value := range expected {
	    if !(slicesMatch(value, actual[key])) {
		    return false
	    }
    }
    return true
}

答案1

得分: 4

在Golang中,是否有一种约定来确定一个映射中的所有项是否存在于另一个映射中?

没有,没有这样的约定。您将需要循环遍历并逐个检查。

英文:

> Is there a golang convention for determining whether all items in a map exist in another map?

No, there isn't. You will have to loop and check one at a time.

huangapple
  • 本文由 发表于 2014年9月5日 00:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/25670712.html
匿名

发表评论

匿名网友

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

确定