Просмотр исходного кода

Convert int to float when explicitly tagged.

Closes #312.
Gustavo Niemeyer 8 лет назад
Родитель
Сommit
9a4310b1ca
2 измененных файлов с 19 добавлено и 0 удалено
  1. 6 0
      decode_test.go
  2. 13 0
      resolve.go

+ 6 - 0
decode_test.go

@@ -405,6 +405,12 @@ var unmarshalTests = []struct {
 	{
 		"v: !!float '1.1'",
 		map[string]interface{}{"v": 1.1},
+	}, {
+		"v: !!float 0",
+		map[string]interface{}{"v": float64(0)},
+	}, {
+		"v: !!float -1",
+		map[string]interface{}{"v": float64(-1)},
 	}, {
 		"v: !!null ''",
 		map[string]interface{}{"v": nil},

+ 13 - 0
resolve.go

@@ -92,6 +92,19 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
 		switch tag {
 		case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
 			return
+		case yaml_FLOAT_TAG:
+			if rtag == yaml_INT_TAG {
+				switch v := out.(type) {
+				case int64:
+					rtag = yaml_FLOAT_TAG
+					out = float64(v)
+					return
+				case int:
+					rtag = yaml_FLOAT_TAG
+					out = float64(v)
+					return
+				}
+			}
 		}
 		failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
 	}()