英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论