Skip to content

Commit f690852

Browse files
committed
Defaults for boolean config options not handled well by Go parser
1 parent 8a79292 commit f690852

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

config.common.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
global:
88
useObjectStatus: true
99
useResetQStats: false
10+
usePublications: true
1011
logLevel: INFO
1112
metaprefix: ""
1213
pollInterval: 30s

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func VerifyConfig(cm *Config, fullCf interface{}) error {
368368
log.Debugf("VerifyConfig Error : %+v", err)
369369
}
370370

371-
//log.Fatalf("Exiting immediately") // Used this temporarily to save time
371+
// log.Fatalf("Exiting immediately") // Used this temporarily to save time
372372

373373
return err
374374
}

pkg/config/configyaml.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import (
2525
"fmt"
2626
"gopkg.in/yaml.v2"
2727
"io/ioutil"
28+
"strconv"
2829
)
2930

3031
type ConfigYGlobal struct {
31-
UseObjectStatus bool `yaml:"useObjectStatus" default:true`
32-
UseResetQStats bool `yaml:"useResetQStats" default:false`
33-
UsePublications bool `yaml:"usePublications" default:true`
32+
UseObjectStatus string `yaml:"useObjectStatus" default:"true"`
33+
UseResetQStats string `yaml:"useResetQStats" default:"false"`
34+
UsePublications string `yaml:"usePublications" default:"true"`
3435
LogLevel string `yaml:"logLevel"`
3536
MetaPrefix string
3637
PollInterval string `yaml:"pollInterval"`
@@ -41,7 +42,7 @@ type ConfigYGlobal struct {
4142
type ConfigYConnection struct {
4243
QueueManager string `yaml:"queueManager"`
4344
User string
44-
Client bool `yaml:"clientConnection"`
45+
Client string `yaml:"clientConnection" default:"false"`
4546
Password string
4647
ReplyQueue string `yaml:"replyQueue"`
4748
CcdtUrl string `yaml:"ccdtUrl"`
@@ -54,26 +55,40 @@ type ConfigYObjects struct {
5455
Channels []string
5556
Topics []string
5657
Subscriptions []string
57-
ShowInactiveChannels bool `yaml:"showInactiveChannels"`
58+
ShowInactiveChannels string `yaml:"showInactiveChannels" default:"false"`
5859
}
5960

6061
func ReadConfigFile(f string, cmy interface{}) error {
6162

6263
data, e2 := ioutil.ReadFile(f)
6364
if e2 == nil {
6465
e2 = yaml.Unmarshal(data, cmy)
66+
fmt.Printf("CMY: %+v\n", cmy)
6567
}
6668

6769
return e2
6870
}
6971

72+
// The Go YAML parsing is not what you might expect for booleans - you are
73+
// apparently unable to set a default of "true" for missing fields. So we read ut
74+
// as a string and parse that. The caller also sends in the default value if the string
75+
// cannot be decoded.
76+
func asBool(s string, def bool) bool {
77+
b, err := strconv.ParseBool(s)
78+
if err == nil {
79+
return b
80+
} else {
81+
return def
82+
}
83+
}
84+
7085
// This handles the configuration parameters that are common to all the collectors. The individual
7186
// collectors call similar code for their own specific attributes
7287
func CopyYamlConfig(cm *Config, cyg ConfigYGlobal, cyc ConfigYConnection, cyo ConfigYObjects) {
73-
cm.CC.UseStatus = CopyParmIfNotSetBool("global", "useObjectStatus", cyg.UseObjectStatus)
74-
cm.CC.UseResetQStats = CopyParmIfNotSetBool("global", "useResetQStats", cyg.UseResetQStats)
75-
cm.CC.UsePublications = CopyParmIfNotSetBool("global", "usePublications", cyg.UsePublications)
76-
cm.CC.ShowInactiveChannels = CopyParmIfNotSetBool("objects", "showInactiveChannels", cyo.ShowInactiveChannels)
88+
cm.CC.UseStatus = CopyParmIfNotSetBool("global", "useObjectStatus", asBool(cyg.UseObjectStatus, true))
89+
cm.CC.UseResetQStats = CopyParmIfNotSetBool("global", "useResetQStats", asBool(cyg.UseResetQStats, false))
90+
cm.CC.UsePublications = CopyParmIfNotSetBool("global", "usePublications", asBool(cyg.UsePublications, true))
91+
cm.CC.ShowInactiveChannels = CopyParmIfNotSetBool("objects", "showInactiveChannels", asBool(cyo.ShowInactiveChannels, false))
7792

7893
cm.LogLevel = CopyParmIfNotSetStr("global", "logLevel", cyg.LogLevel)
7994
cm.MetaPrefix = CopyParmIfNotSetStr("global", "metaprefix", cyg.MetaPrefix)
@@ -86,7 +101,7 @@ func CopyYamlConfig(cm *Config, cyg ConfigYGlobal, cyc ConfigYConnection, cyo Co
86101
cm.CC.CcdtUrl = CopyParmIfNotSetStr("connection", "ccdtUrl", cyc.CcdtUrl)
87102
cm.CC.ConnName = CopyParmIfNotSetStr("connection", "connName", cyc.ConnName)
88103
cm.CC.Channel = CopyParmIfNotSetStr("connection", "channel", cyc.Channel)
89-
cm.CC.ClientMode = CopyParmIfNotSetBool("connection", "clientConnection", cyc.Client)
104+
cm.CC.ClientMode = CopyParmIfNotSetBool("connection", "clientConnection", asBool(cyc.Client, false))
90105
cm.CC.UserId = CopyParmIfNotSetStr("connection", "user", cyc.User)
91106
cm.CC.Password = CopyParmIfNotSetStr("connection", "password", cyc.Password)
92107
cm.ReplyQ = CopyParmIfNotSetStr("connection", "replyQueue", cyc.ReplyQueue)

0 commit comments

Comments
 (0)