improve package gjson for automatic content type checking

This commit is contained in:
Jack
2020-08-11 23:46:21 +08:00
parent fcb13bd8ee
commit 46e45ca84b
2 changed files with 17 additions and 3 deletions

View File

@ -207,8 +207,8 @@ func checkDataType(content []byte) string {
return "json"
} else if gregex.IsMatch(`^<.+>[\S\s]+<.+>$`, content) {
return "xml"
} else if gregex.IsMatch(`[\s\t\n\r]*[\w\-]+\s*:\s*".+"`, content) ||
gregex.IsMatch(`[\s\t\n\r]*[\w\-]+\s*:\s*\w+`, content) {
} else if (gregex.IsMatch(`^[\s\t\n\r]*[\w\-]+\s*:\s*".+"`, content) || gregex.IsMatch(`^[\s\t\n\r]*[\w\-]+\s*:\s*\w+`, content)) ||
(gregex.IsMatch(`[\s\t\n\r]+[\w\-]+\s*:\s*".+"`, content) || gregex.IsMatch(`[\s\t\n\r]+[\w\-]+\s*:\s*\w+`, content)) {
return "yml"
} else if !gregex.IsMatch(`^[\s\t\n\r]*;.+`, content) &&
!gregex.IsMatch(`[\s\t\n\r]+;.+`, content) &&

View File

@ -12,11 +12,25 @@ import (
)
func Test_checkDataType(t *testing.T) {
data := []byte(`
gtest.C(t, func(t *gtest.T) {
data := []byte(`
bb = """
dig := dig; END;"""
`)
t.Assert(checkDataType(data), "toml")
})
gtest.C(t, func(t *gtest.T) {
data := []byte(`
# 模板引擎目录
viewpath = "/home/www/templates/"
# MySQL数据库配置
[redis]
dd = 11
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`)
t.Assert(checkDataType(data), "toml")
})
}