Selaa lähdekoodia

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 11 vuotta sitten
vanhempi
commit
76057addb2
2 muutettua tiedostoa jossa 19 lisäystä ja 19 poistoa
  1. 1 19
      engine/env.go
  2. 18 0
      engine/env_test.go

+ 1 - 19
engine/env.go

@@ -194,25 +194,7 @@ func (env *Env) SetAuto(k string, v interface{}) {
 }
 
 func (env *Env) Encode(dst io.Writer) error {
-	m := make(map[string]interface{})
-	for k, v := range env.Map() {
-		var val interface{}
-		if err := json.Unmarshal([]byte(v), &val); err == nil {
-			// FIXME: we fix-convert float values to int, because
-			// encoding/json decodes integers to float64, but cannot encode them back.
-			// (See http://golang.org/src/pkg/encoding/json/decode.go#L46)
-			if fval, isFloat := val.(float64); isFloat {
-				val = int(fval)
-			}
-			m[k] = val
-		} else {
-			m[k] = v
-		}
-	}
-	if err := json.NewEncoder(dst).Encode(&m); err != nil {
-		return err
-	}
-	return nil
+	return json.NewEncoder(dst).Encode(env.Map())
 }
 
 func (env *Env) WriteTo(dst io.Writer) (n int64, err error) {

+ 18 - 0
engine/env_test.go

@@ -95,3 +95,21 @@ func TestEnviron(t *testing.T) {
 		t.Fatalf("bar not found in the environ")
 	}
 }
+
+func TestEnvWriteTo(t *testing.T) {
+	e := &Env{}
+	inputKey := "Version"
+	inputVal := "42.1"
+	e.Set(inputKey, inputVal)
+	out := NewOutput()
+	e2, err := out.AddEnv()
+	if err != nil {
+		t.Fatal(err)
+	}
+	e.WriteTo(out)
+	result := e2.Get(inputKey)
+	expected := inputVal
+	if expected != result {
+		t.Fatalf("%#v\n", result)
+	}
+}