config: add support for float64

This commit is contained in:
Alex Crawford 2014-11-21 15:43:48 -08:00
parent 3275ead1ec
commit 4a0019c669
6 changed files with 64 additions and 9 deletions

View File

@ -125,7 +125,7 @@ func toNode(v interface{}, c context, n *node) {
n.children = append(n.children, cn) n.children = append(n.children, cn)
c.Increment() c.Increment()
} }
case reflect.String, reflect.Int, reflect.Bool: case reflect.String, reflect.Int, reflect.Bool, reflect.Float64:
default: default:
panic(fmt.Sprintf("toNode(): unhandled kind %s", vv.Kind())) panic(fmt.Sprintf("toNode(): unhandled kind %s", vv.Kind()))
} }

View File

@ -61,7 +61,7 @@ func checkNodeStructure(n, g node, r *Report) {
toNode(reflect.New(c).Elem().Interface(), context{}, &cg) toNode(reflect.New(c).Elem().Interface(), context{}, &cg)
checkNodeStructure(cn, cg, r) checkNodeStructure(cn, cg, r)
} }
case reflect.String, reflect.Int, reflect.Bool: case reflect.String, reflect.Int, reflect.Float64, reflect.Bool:
default: default:
panic(fmt.Sprintf("checkNodeStructure(): unhandled kind %s", g.Kind())) panic(fmt.Sprintf("checkNodeStructure(): unhandled kind %s", g.Kind()))
} }
@ -92,7 +92,7 @@ func checkNodeValidity(n, g node, r *Report) {
toNode(reflect.New(c).Elem().Interface(), context{}, &cg) toNode(reflect.New(c).Elem().Interface(), context{}, &cg)
checkNodeValidity(cn, cg, r) checkNodeValidity(cn, cg, r)
} }
case reflect.String, reflect.Int, reflect.Bool: case reflect.String, reflect.Int, reflect.Float64, reflect.Bool:
default: default:
panic(fmt.Sprintf("checkNodeValidity(): unhandled kind %s", g.Kind())) panic(fmt.Sprintf("checkNodeValidity(): unhandled kind %s", g.Kind()))
} }
@ -104,10 +104,10 @@ func checkNodeValidity(n, g node, r *Report) {
func isCompatible(n, g reflect.Kind) bool { func isCompatible(n, g reflect.Kind) bool {
switch g { switch g {
case reflect.String: case reflect.String:
return n == reflect.String || n == reflect.Int || n == reflect.Bool return n == reflect.String || n == reflect.Int || n == reflect.Float64 || n == reflect.Bool
case reflect.Struct: case reflect.Struct:
return n == reflect.Struct || n == reflect.Map return n == reflect.Struct || n == reflect.Map
case reflect.Bool, reflect.Slice: case reflect.Bool, reflect.Slice, reflect.Int, reflect.Float64:
return n == g return n == g
default: default:
panic(fmt.Sprintf("isCompatible(): unhandled kind %s", g)) panic(fmt.Sprintf("isCompatible(): unhandled kind %s", g))

View File

@ -31,9 +31,9 @@ func dropinContents(e interface{}) string {
var out string var out string
for i := 0; i < et.NumField(); i++ { for i := 0; i < et.NumField(); i++ {
if val := ev.Field(i).String(); val != "" { if val := ev.Field(i).Interface(); !config.IsZero(val) {
key := et.Field(i).Tag.Get("env") key := et.Field(i).Tag.Get("env")
out += fmt.Sprintf("Environment=\"%s=%s\"\n", key, val) out += fmt.Sprintf("Environment=\"%s=%v\"\n", key, val)
} }
} }

55
system/env_test.go Normal file
View File

@ -0,0 +1,55 @@
package system
import (
"testing"
)
func TestDropinContents(t *testing.T) {
tests := []struct {
Config interface{}
Contents string
}{
{
struct{}{},
"",
},
{
struct {
A string `env:"A"`
B int `env:"B"`
C bool `env:"C"`
D float64 `env:"D"`
}{
"hi", 1, true, 0.12345,
},
`[Service]
Environment="A=hi"
Environment="B=1"
Environment="C=true"
Environment="D=0.12345"
`,
},
{
struct {
A float64 `env:"A"`
B float64 `env:"B"`
C float64 `env:"C"`
D float64 `env:"D"`
}{
0.000001, 1, 0.9999999, 0.1,
},
`[Service]
Environment="A=1e-06"
Environment="B=1"
Environment="C=0.9999999"
Environment="D=0.1"
`,
},
}
for _, tt := range tests {
if c := dropinContents(tt.Config); c != tt.Contents {
t.Errorf("bad contents (%+v): want %q, got %q", tt, tt.Contents, c)
}
}
}

View File

@ -67,7 +67,7 @@ Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
} { } {
units := Etcd{tt.config}.Units() units := Etcd{tt.config}.Units()
if !reflect.DeepEqual(tt.units, units) { if !reflect.DeepEqual(tt.units, units) {
t.Errorf("bad units (%q): want %#v, got %#v", tt.config, tt.units, units) t.Errorf("bad units (%+v): want %#v, got %#v", tt.config, tt.units, units)
} }
} }
} }

View File

@ -48,7 +48,7 @@ Environment="FLEET_PUBLIC_IP=12.34.56.78"
} { } {
units := Fleet{tt.config}.Units() units := Fleet{tt.config}.Units()
if !reflect.DeepEqual(units, tt.units) { if !reflect.DeepEqual(units, tt.units) {
t.Errorf("bad units (%q): want %#v, got %#v", tt.config, tt.units, units) t.Errorf("bad units (%+v): want %#v, got %#v", tt.config, tt.units, units)
} }
} }
} }