package misc
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/archsh/go.uuid"
|
|
)
|
|
|
|
type HashMap map[string]string
|
|
|
|
func (h HashMap) HasKey(k string) bool {
|
|
if nil == h {
|
|
return false
|
|
}
|
|
_, ok := h[k]
|
|
return ok
|
|
}
|
|
|
|
func (h HashMap) GetInt(k string, defaults ...int) (int, bool) {
|
|
d := 0
|
|
if len(defaults) > 0 {
|
|
d = defaults[0]
|
|
}
|
|
if nil == h {
|
|
return d, false
|
|
}
|
|
if ns, ok := h[k]; ok {
|
|
if n, e := strconv.Atoi(ns); e == nil {
|
|
return n, true
|
|
}
|
|
}
|
|
return d, false
|
|
}
|
|
|
|
func (h HashMap) GetInt64(k string, defaults ...int64) (int64, bool) {
|
|
var d int64 = 0
|
|
if len(defaults) > 0 {
|
|
d = defaults[0]
|
|
}
|
|
if nil == h {
|
|
return d, false
|
|
}
|
|
if ns, ok := h[k]; ok {
|
|
if n, e := strconv.ParseInt(ns, 10, 64); e == nil {
|
|
return n, true
|
|
}
|
|
}
|
|
return d, false
|
|
}
|
|
|
|
func (h HashMap) GetUInt(k string, defaults ...uint) (uint, bool) {
|
|
var d uint = 0
|
|
if len(defaults) > 0 {
|
|
d = defaults[0]
|
|
}
|
|
if nil == h {
|
|
return d, false
|
|
}
|
|
if ns, ok := h[k]; ok {
|
|
if n, e := strconv.ParseUint(ns, 10, 32); e == nil {
|
|
return uint(n), true
|
|
}
|
|
}
|
|
return d, false
|
|
}
|
|
|
|
func (h HashMap) GetString(k string, defaults ...string) (string, bool) {
|
|
d := ""
|
|
if len(defaults) > 0 {
|
|
d = defaults[0]
|
|
}
|
|
if nil == h {
|
|
return d, false
|
|
}
|
|
if ns, ok := h[k]; ok {
|
|
return ns, true
|
|
}
|
|
return d, false
|
|
}
|
|
|
|
func (h HashMap) GetBool(k string, defaults ...bool) (bool, bool) {
|
|
d := false
|
|
if len(defaults) > 0 {
|
|
d = defaults[0]
|
|
}
|
|
if nil == h {
|
|
return d, false
|
|
}
|
|
if ns, ok := h[k]; ok {
|
|
switch strings.ToLower(ns) {
|
|
case "t", "true", "yes", "ok":
|
|
return true, true
|
|
case "f", "false", "no":
|
|
return false, true
|
|
}
|
|
}
|
|
return d, false
|
|
}
|
|
|
|
func (h HashMap) GetUUID(k string, defaults ...uuid.UUID) (uuid.UUID, bool) {
|
|
d := uuid.UUID{}
|
|
if len(defaults) > 0 {
|
|
d = defaults[0]
|
|
}
|
|
if nil == h {
|
|
return d, false
|
|
}
|
|
if ns, ok := h[k]; ok {
|
|
if uid, e := uuid.FromString(ns); nil == e {
|
|
return uid, true
|
|
}
|
|
}
|
|
return d, false
|
|
}
|