Browse code

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

This reverts commit 76057addb255e6f14dd03c276317abc759a15a80.

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/04/05 03:29:56
Showing 2 changed files
... ...
@@ -194,7 +194,25 @@ func (env *Env) SetAuto(k string, v interface{}) {
194 194
 }
195 195
 
196 196
 func (env *Env) Encode(dst io.Writer) error {
197
-	return json.NewEncoder(dst).Encode(env.Map())
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
198 216
 }
199 217
 
200 218
 func (env *Env) WriteTo(dst io.Writer) (n int64, err error) {
... ...
@@ -95,21 +95,3 @@ 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
-}