my-log/tools/parse.go
Dan Jones 96c3b2ff30 Use ParseDate in Parse
Also limit to English, or we get lots of false positives
2024-02-25 13:12:13 -06:00

43 lines
957 B
Go

package tools
import (
"encoding/json"
"regexp"
"strconv"
"strings"
)
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 := ParseDate(s); err == nil {
return t
} else if err := json.Unmarshal([]byte(s), &j); err == nil {
return j
}
return s
}