my-log/tools/parse.go

40 lines
913 B
Go
Raw Permalink Normal View History

2024-02-09 09:44:35 -06:00
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) {
2024-10-07 15:50:02 -05:00
return yes.MatchString(s)
2024-02-09 09:44:35 -06:00
} 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 {
2024-02-09 09:44:35 -06:00
return t
} else if err := json.Unmarshal([]byte(s), &j); err == nil {
return j
}
return s
}