improve package gjson for automatic content type checking

This commit is contained in:
Jack
2020-08-11 23:36:40 +08:00
parent eacad9b453
commit fcb13bd8ee
2 changed files with 10 additions and 8 deletions

View File

@ -207,16 +207,19 @@ func checkDataType(content []byte) string {
return "json"
} else if gregex.IsMatch(`^<.+>[\S\s]+<.+>$`, content) {
return "xml"
} else if gregex.IsMatch(`[\s\t\n]*[\w\-]+\s*:\s*".+"`, content) ||
gregex.IsMatch(`[\s\t\n]*[\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) {
return "yml"
} else if !gregex.IsMatch(`^[\s\t\n\r]*;.+`, content) &&
!gregex.IsMatch(`[\s\t\n\r]+;.+`, content) &&
(gregex.IsMatch(`[\s\t\n\r]*[\w\-\."]+\s*=\s*".+"`, content) || gregex.IsMatch(`[\s\t\n\r]*[\w\-\."]+\s*=\s*\w+`, content)) {
return "toml"
} else if gregex.IsMatch(`\[[\w]+\]`, content) &&
gregex.IsMatch(`[\s\t\n\[\]]*[\w\-]+\s*=\s*.+`, content) &&
!gregex.IsMatch(`[\s\t\n]*[\w\-]+\s*=*\"*.+\"`, content) {
!gregex.IsMatch(`^[\s\t\n\r]*#.+`, content) &&
!gregex.IsMatch(`[\s\t\n\r]+#.+`, content) &&
(gregex.IsMatch(`[\s\t\n\r]*[\w\-\."]+\s*=\s*".+"`, content) || gregex.IsMatch(`[\s\t\n\r]*[\w\-\."]+\s*=\s*\w+`, content)) {
// Must contain "[xxx]" section.
return "ini"
} else if gregex.IsMatch(`[\s\t\n]*[\w\-\."]+\s*=\s*.+`, content) {
return "toml"
} else {
return ""
}

View File

@ -215,8 +215,7 @@ func Test_Load_Ini(t *testing.T) {
;注释
[addr]
#注释
[addr]
ip = 127.0.0.1
port=9001
enable=true