Skip to content

Commit

Permalink
Improve configuration parser and add test for the new options. Fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
driskell committed Jul 15, 2014
1 parent 0eaf5fe commit 8f5a07a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
44 changes: 35 additions & 9 deletions spec/courier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
{
"network": {
"ssl ca": "#{@ssl_cert.path}",
"servers": [ "127.0.0.1:#{server_port}" ],
"timeout": 15,
"reconnect": 1
"servers": [ "127.0.0.1:#{server_port}" ]
},
"files": [
{
Expand Down Expand Up @@ -270,9 +268,7 @@
{
"network": {
"ssl ca": "#{@ssl_cert.path}",
"servers": [ "127.0.0.1:#{server_port}" ],
"timeout": 15,
"reconnect": 1
"servers": [ "127.0.0.1:#{server_port}" ]
},
"files": [
{
Expand Down Expand Up @@ -304,9 +300,7 @@
{
"network": {
"ssl ca": "#{@ssl_cert.path}",
"servers": [ "127.0.0.1:#{server_port}" ],
"timeout": 15,
"reconnect": 1
"servers": [ "127.0.0.1:#{server_port}" ]
},
"files": [
{
Expand Down Expand Up @@ -592,4 +586,36 @@
end
end
end

# TODO: We should start using Go tests for things like this
it 'should accept the various general and network configuration elements' do
f = create_log

startup config: <<-config
{
"general": {
"prospect interval": 10,
"spool size": 1024,
"spool timeout": 5,
"log level": "debug"
},
"network": {
"ssl ca": "#{@ssl_cert.path}",
"servers": [ "127.0.0.1:#{server_port}" ],
"timeout": 15,
"reconnect": 1
},
"files": [
{
"paths": [ "#{TEMP_PATH}/logs/log-*" ]
}
]
}
config

f.log 5000

# Receive and check
receive_and_check
end
end
44 changes: 29 additions & 15 deletions src/log-courier/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,34 +361,48 @@ func PopulateConfig(config interface{}, config_path string, raw_config map[strin
} else if field.Type().String() == "time.Duration" {
var duration float64
vduration := reflect.ValueOf(duration)
fail := true
if value.Type().AssignableTo(vduration.Type()) {
duration = value.Float()
if duration < math.MinInt64 || duration > math.MaxInt64 {
err = fmt.Errorf("Option %s%s is not a valid numeric or string duration", config_path, tag)
return
if duration >= math.MinInt64 && duration <= math.MaxInt64 {
field.Set(reflect.ValueOf(time.Duration(int64(duration)) * time.Second))
fail = false
}
field.Set(reflect.ValueOf(time.Duration(int64(duration)) * time.Second))
} else if value.Kind() == reflect.String {
var tduration time.Duration
if tduration, err = time.ParseDuration(value.String()); err != nil {
err = fmt.Errorf("Option %s%s is not a valid numeric or string duration: %s", config_path, tag, err)
return
if tduration, err = time.ParseDuration(value.String()); err == nil {
field.Set(reflect.ValueOf(tduration))
fail = false
}
field.Set(reflect.ValueOf(tduration))
} else {
}
if fail {
err = fmt.Errorf("Option %s%s must be a valid numeric or string duration", config_path, tag)
return
}
} else if field.Kind() == reflect.Int64 {
fail := true
if value.Kind() == reflect.Float64 {
number := value.Float()
if math.Floor(number) == number {
fail = false
field.Set(reflect.ValueOf(int64(number)))
}
}
if fail {
err = fmt.Errorf("Option %s%s is not a valid integer", config_path, tag, field.Type())
return
}
} else if field.Type().String() == "logging.Level" {
fail := true
if value.Kind() == reflect.String {
var llevel logging.Level
if llevel, err = logging.LogLevel(value.String()); err != nil {
err = fmt.Errorf("Option %s%s is not a valid log level (critical, error, warning, notice, info, debug)", config_path, tag)
return
if llevel, err = logging.LogLevel(value.String()); err == nil {
fail = false
field.Set(reflect.ValueOf(llevel))
}
field.Set(reflect.ValueOf(llevel))
} else {
err = fmt.Errorf("Option %s%s must be a valid log level (critical, error, warning, notice, info, debug)", config_path, tag)
}
if fail {
err = fmt.Errorf("Option %s%s is not a valid log level (critical, error, warning, notice, info, debug)", config_path, tag)
return
}
} else {
Expand Down

0 comments on commit 8f5a07a

Please sign in to comment.