/
settings.go
49 lines (40 loc) · 1.22 KB
/
settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"fmt"
"strings"
)
// SettingsAttributes handles the settings attribute values that may be any of several
// types and adds some basic helper methods to make accessing values easier.
type SettingsAttributes map[string]interface{}
// String gets the string representation of the attribute value.
func (ba SettingsAttributes) String(name string) string {
if val, ok := ba[name]; ok {
return fmt.Sprintf("%v", val)
}
return ""
}
// Float64 gets the value as a float64 or 0 if that is not possible.
func (ba SettingsAttributes) Float64(name string) float64 {
if val, ok := ba[name]; ok {
return val.(float64)
}
return 0
}
// Int gets the value as an integer or 0 if that is not possible.
func (ba SettingsAttributes) Int(name string) int {
// Integer values may be interpeted as float64, so get it as that first,
// then coerce down to int.
floatVal := int(ba.Float64(name))
return (floatVal)
}
// Bool gets the value as a boolean or returns false.
func (ba SettingsAttributes) Bool(name string) bool {
maybeBool := ba.String(name)
maybeBool = strings.ToLower(maybeBool)
return (maybeBool == "true" ||
maybeBool == "1" ||
maybeBool == "enabled")
}