Skip to content

Commit

Permalink
👔 update: netutil - update add new func ToKindWithFunc() and add some…
Browse files Browse the repository at this point in the history
… new consts
  • Loading branch information
inhere committed Jul 27, 2023
1 parent 5b354e6 commit cac36d3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
15 changes: 6 additions & 9 deletions netutil/httpctype/content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ const (
JS = "application/javascript; charset=utf-8"
JS2 = "text/javascript; charset=utf-8"

MSGPACK = "application/x-msgpack; charset=utf-8"
MSGPACK2 = "application/msgpack; charset=utf-8"

PROTOBUF = "application/x-protobuf"

Form = "application/x-www-form-urlencoded"
// FormUtf8 for form data with charset=utf-8
FormUtf8 = "application/x-www-form-urlencoded; charset=utf-8"
Form = "application/x-www-form-urlencoded; charset=utf-8"
// FormData for upload file
FormData = "multipart/form-data"
DataForm = FormData

// Binary represents content type application/octet-stream
Binary = "application/octet-stream"
Binary = "application/octet-stream"
PROTOBUF = "application/x-protobuf"
MSGPACK = "application/x-msgpack; charset=utf-8"
MSGPACK2 = "application/msgpack; charset=utf-8"
)
1 change: 1 addition & 0 deletions netutil/httpctype/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const (
KindFormData = "dataForm"
KindJSON = "json"
KindXML = "xml"
KindYAML = "yaml"
)
20 changes: 13 additions & 7 deletions netutil/httpctype/mime_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ package httpctype
// there are some Content-Type MIME of the most common data formats.
const (
MIMEHTML = "text/html"
MIMEHtml = MIMEHTML
MIMEText = "text/plain" // equals MIMEPlain
MIMEPlain = "text/plain"
MIMEJSON = "application/json"
MIMEXML = "application/xml"
MIMEXML2 = "text/xml"
MIMEYAML = "application/x-yaml"
MIMEPlain = MIMEText

MIMEPOSTForm = "application/x-www-form-urlencoded"
MIMEMultiDataForm = "multipart/form-data"
MIMEJSON = "application/json"

MIMEYAML = "application/x-yaml"
MIMEYaml = MIMEYAML
MIMEXML = "application/xml"
MIMEXML2 = "text/xml"

MIMEForm = "application/x-www-form-urlencoded"
MIMEPOSTForm = MIMEForm
MIMEDataForm = "multipart/form-data"
MIMEMultiDataForm = MIMEDataForm

MIMEPROTOBUF = "application/x-protobuf"
MIMEMSGPACK = "application/x-msgpack"
Expand Down
12 changes: 11 additions & 1 deletion netutil/httpctype/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ func ToKind(cType, defaultType string) string {
return defaultType
}

return ToKindWithFunc(cType, func(_ string) string {
return defaultType
})
}

// ToKindWithFunc match base kind name by content-type, with a fallback func
func ToKindWithFunc(cType string, fbFunc func(cType string) string) string {
// JSON body request: "application/json"
if strings.Contains(cType, "/json") {
return KindJSON
Expand All @@ -29,5 +36,8 @@ func ToKind(cType, defaultType string) string {
return KindXML
}

return defaultType
if fbFunc != nil {
return fbFunc(cType)
}
return ""
}
9 changes: 9 additions & 0 deletions netutil/httpctype/util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httpctype_test

import (
"strings"
"testing"

"github.com/gookit/goutil/netutil/httpctype"
Expand All @@ -25,4 +26,12 @@ func TestToKind(t *testing.T) {
for _, tt := range tests {
assert.Equal(t, tt.want, httpctype.ToKind(tt.cType, tt.defaultType))
}

assert.Eq(t, "", httpctype.ToKindWithFunc("not-match", nil))
assert.Eq(t, httpctype.KindYAML, httpctype.ToKindWithFunc(httpctype.YAML, func(cType string) string {
if strings.Contains(cType, "/x-yaml") {
return httpctype.KindYAML
}
return ""
}))
}

0 comments on commit cac36d3

Please sign in to comment.