Browse code

engine: fix engine.Env.Encode() to stop auto-guessing types.

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)

Solomon Hykes authored on 2014/04/04 08:15:56
Showing 2 changed files
... ...
@@ -194,25 +194,7 @@ func (env *Env) SetAuto(k string, v interface{}) {
194 194
 }
195 195
 
196 196
 func (env *Env) Encode(dst io.Writer) error {
197
-	m := make(map[string]interface{})
198
-	for k, v := range env.Map() {
199
-		var val interface{}
200
-		if err := json.Unmarshal([]byte(v), &val); err == nil {
201
-			// FIXME: we fix-convert float values to int, because
202
-			// encoding/json decodes integers to float64, but cannot encode them back.
203
-			// (See http://golang.org/src/pkg/encoding/json/decode.go#L46)
204
-			if fval, isFloat := val.(float64); isFloat {
205
-				val = int(fval)
206
-			}
207
-			m[k] = val
208
-		} else {
209
-			m[k] = v
210
-		}
211
-	}
212
-	if err := json.NewEncoder(dst).Encode(&m); err != nil {
213
-		return err
214
-	}
215
-	return nil
197
+	return json.NewEncoder(dst).Encode(env.Map())
216 198
 }
217 199
 
218 200
 func (env *Env) WriteTo(dst io.Writer) (n int64, err error) {
... ...
@@ -95,3 +95,21 @@ func TestEnviron(t *testing.T) {
95 95
 		t.Fatalf("bar not found in the environ")
96 96
 	}
97 97
 }
98
+
99
+func TestEnvWriteTo(t *testing.T) {
100
+	e := &Env{}
101
+	inputKey := "Version"
102
+	inputVal := "42.1"
103
+	e.Set(inputKey, inputVal)
104
+	out := NewOutput()
105
+	e2, err := out.AddEnv()
106
+	if err != nil {
107
+		t.Fatal(err)
108
+	}
109
+	e.WriteTo(out)
110
+	result := e2.Get(inputKey)
111
+	expected := inputVal
112
+	if expected != result {
113
+		t.Fatalf("%#v\n", result)
114
+	}
115
+}