waveterm/pkg/waveobj/metamap.go
2024-07-30 12:33:28 -07:00

75 lines
1.3 KiB
Go

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package waveobj
type MetaMapType map[string]any
func (m MetaMapType) GetString(key string, def string) string {
if v, ok := m[key]; ok {
if s, ok := v.(string); ok {
return s
}
}
return def
}
func (m MetaMapType) GetBool(key string, def bool) bool {
if v, ok := m[key]; ok {
if b, ok := v.(bool); ok {
return b
}
}
return def
}
func (m MetaMapType) GetInt(key string, def int) int {
if v, ok := m[key]; ok {
if fval, ok := v.(float64); ok {
return int(fval)
}
}
return def
}
func (m MetaMapType) GetFloat(key string, def float64) float64 {
if v, ok := m[key]; ok {
if fval, ok := v.(float64); ok {
return fval
}
}
return def
}
func (m MetaMapType) GetMap(key string) MetaMapType {
if v, ok := m[key]; ok {
if mval, ok := v.(map[string]any); ok {
return MetaMapType(mval)
}
}
return nil
}
func (m MetaMapType) GetArray(key string) []any {
if v, ok := m[key]; ok {
if aval, ok := v.([]any); ok {
return aval
}
}
return nil
}
func (m MetaMapType) GetStringArray(key string) []string {
arr := m.GetArray(key)
if len(arr) == 0 {
return nil
}
rtn := make([]string, 0, len(arr))
for _, v := range arr {
if s, ok := v.(string); ok {
rtn = append(rtn, s)
}
}
return rtn
}