-
Notifications
You must be signed in to change notification settings - Fork 3
/
bindingTags.go
54 lines (43 loc) · 1.71 KB
/
bindingTags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package astra
import (
"reflect"
"slices"
"github.com/ls6-events/astra/astTraversal"
)
func ExtractBindingTags(fields map[string]Field) (bindingTags []astTraversal.BindingTagType, uniqueBindings bool) {
for _, field := range fields {
var previousTag astTraversal.BindingTag
for bindingType, bindingTag := range field.StructFieldBindingTags {
if previousTag != (astTraversal.BindingTag{}) && !uniqueBindings {
uniqueBindings = !reflect.DeepEqual(bindingTag, previousTag)
}
if !slices.Contains(bindingTags, bindingType) {
bindingTags = append(bindingTags, bindingType)
}
previousTag = bindingTag
}
}
if len(bindingTags) == 0 {
return astTraversal.BindingTags, uniqueBindings
}
return bindingTags, uniqueBindings
}
func ContentTypeToBindingTag(contentType string) astTraversal.BindingTagType {
mimetypeToBindingTagMap := map[string]astTraversal.BindingTagType{
"application/json": astTraversal.JSONBindingTag,
"application/xml": astTraversal.XMLBindingTag,
"application/x-www-form-urlencoded": astTraversal.FormBindingTag,
"multipart/form-data": astTraversal.FormBindingTag,
"application/yaml": astTraversal.YAMLBindingTag,
}
return mimetypeToBindingTagMap[contentType]
}
func BindingTagToContentTypes(bindingTag astTraversal.BindingTagType) []string {
bindingTagToMimetypeMap := map[astTraversal.BindingTagType][]string{
astTraversal.JSONBindingTag: {"application/json"},
astTraversal.XMLBindingTag: {"application/xml"},
astTraversal.FormBindingTag: {"application/x-www-form-urlencoded", "multipart/form-data"},
astTraversal.YAMLBindingTag: {"application/yaml"},
}
return bindingTagToMimetypeMap[bindingTag]
}