Skip to content

Commit

Permalink
Merge pull request #2145 from influxdb/fix-toml-duration
Browse files Browse the repository at this point in the history
Encode toml durations correctly
  • Loading branch information
corylanou committed Apr 2, 2015
2 parents f99d582 + 176e05e commit d29a77e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.9.0-rc19 [unreleased]

### Bugfixes
- [#2145](https://github.com/influxdb/influxdb/pull/2145): Encode toml durations correctly which fixes default configuration generation `influxd config`.

## v0.9.0-rc18 [2015-03-31]

### Bugfixes
Expand Down
9 changes: 9 additions & 0 deletions cmd/influxd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ func (s *Size) UnmarshalText(text []byte) error {
// Duration is a TOML wrapper type for time.Duration.
type Duration time.Duration

func (d Duration) String() string {
return time.Duration(d).String()
}

// UnmarshalText parses a TOML value into a duration value.
func (d *Duration) UnmarshalText(text []byte) error {
// Ignore if there is no value set.
Expand All @@ -344,6 +348,11 @@ func (d *Duration) UnmarshalText(text []byte) error {
return nil
}

// MarshalText converts a duration to a string for decoding toml
func (d Duration) MarshalText() (text []byte, err error) {
return []byte(d.String()), nil
}

// ParseConfigFile parses a configuration file at a given path.
func ParseConfigFile(path string) (*Config, error) {
c, err := NewConfig()
Expand Down
19 changes: 19 additions & 0 deletions cmd/influxd/config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main_test

import (
"bytes"
"reflect"
"strings"
"testing"
"time"

"github.com/BurntSushi/toml"
main "github.com/influxdb/influxdb/cmd/influxd"
)

Expand Down Expand Up @@ -141,6 +143,10 @@ func TestParseConfig(t *testing.T) {
t.Fatalf("cluster dir mismatch: %v", c.Cluster.Dir)
}

if c.Statistics.WriteInterval.String() != "1m0s" {
t.Fatalf("Statistics.WriteInterval mismatch: %v", c.Statistics.WriteInterval)
}

// TODO: UDP Servers testing.
/*
c.Assert(config.UdpServers, HasLen, 1)
Expand All @@ -150,6 +156,19 @@ func TestParseConfig(t *testing.T) {
*/
}

func TestEncodeConfig(t *testing.T) {
c := main.Config{}
c.Statistics.WriteInterval = main.Duration(time.Minute)
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(&c); err != nil {
t.Fatal("Failed to encode: ", err)
}
got, search := buf.String(), `write-interval = "1m0s"`
if !strings.Contains(got, search) {
t.Fatalf("Encoding config failed.\nfailed to find %s in:\n%s\n", search, got)
}
}

// Testing configuration file.
const testFile = `
# Welcome to the InfluxDB configuration file.
Expand Down

0 comments on commit d29a77e

Please sign in to comment.