44 lines
980 B
Go
44 lines
980 B
Go
package tools
|
|
|
|
import (
|
|
"encoding/json"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func ParseBytes(in []byte) any {
|
|
return ParseString(string(in))
|
|
}
|
|
|
|
func ParseString(in string) any {
|
|
s := strings.TrimSpace(in)
|
|
if s == "" {
|
|
return s
|
|
}
|
|
|
|
yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$")
|
|
yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$")
|
|
null := regexp.MustCompile("^(~|null|Null|NULL|none|None|NONE|nil|Nil|NIL)$")
|
|
var j json.RawMessage
|
|
if null.MatchString(s) {
|
|
return nil
|
|
} else if yesno.MatchString(s) {
|
|
if yes.MatchString(s) {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
} else if i, err := strconv.Atoi(s); err == nil {
|
|
return i
|
|
} else if f, err := strconv.ParseFloat(s, 64); err == nil {
|
|
return f
|
|
} else if t, err := time.Parse(time.RFC3339, s); err == nil {
|
|
return t
|
|
} else if err := json.Unmarshal([]byte(s), &j); err == nil {
|
|
return j
|
|
}
|
|
|
|
return s
|
|
}
|