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

revert #273

It was accidentally applied to v2 not the devel branch.
Roger Peppe 8 лет назад
Родитель
Сommit
4341420a14
5 измененных файлов с 9 добавлено и 124 удалено
  1. 0 5
      decode.go
  2. 1 60
      decode_test.go
  3. 3 9
      encode.go
  4. 2 3
      encode_test.go
  5. 3 47
      resolve.go

+ 0 - 5
decode.go

@@ -453,11 +453,6 @@ func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
 			out.SetFloat(resolved)
 			out.SetFloat(resolved)
 			good = true
 			good = true
 		}
 		}
-	case reflect.Struct:
-		if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
-			out.Set(resolvedv)
-			good = true
-		}
 	case reflect.Ptr:
 	case reflect.Ptr:
 		if out.Type().Elem() == reflect.TypeOf(resolved) {
 		if out.Type().Elem() == reflect.TypeOf(resolved) {
 			// TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
 			// TODO DOes this make sense? When is out a Ptr except when decoding a nil value?

+ 1 - 60
decode_test.go

@@ -572,66 +572,7 @@ var unmarshalTests = []struct {
 	},
 	},
 	{
 	{
 		"a: 2015-02-24T18:19:39Z\n",
 		"a: 2015-02-24T18:19:39Z\n",
-		map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
-	},
-
-	// Timestamps
-	{
-		// Date only.
-		"a: 2015-01-01\n",
-		map[string]interface{}{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
-	},
-	{
-		// RFC3339
-		"a: 2015-02-24T18:19:39.12Z\n",
-		map[string]interface{}{"a": time.Date(2015, 2, 24, 18, 19, 39, .12e9, time.UTC)},
-	},
-	{
-		// RFC3339 with short dates.
-		"a: 2015-2-3T3:4:5Z",
-		map[string]interface{}{"a": time.Date(2015, 2, 3, 3, 4, 5, 0, time.UTC)},
-	},
-	{
-		// ISO8601 lower case t
-		"a: 2015-02-24t18:19:39Z\n",
-		map[string]interface{}{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
-	},
-	{
-		// space separate, no time zone
-		"a: 2015-02-24 18:19:39\n",
-		map[string]interface{}{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
-	},
-	// Some cases not currently handled. Uncomment these when
-	// the code is fixed.
-	//	{
-	//		// space separated with time zone
-	//		"a: 2001-12-14 21:59:43.10 -5",
-	//		map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
-	//	},
-	//	{
-	//		// arbitrary whitespace between fields
-	//		"a: 2001-12-14 \t\t \t21:59:43.10 \t Z",
-	//		map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
-	//	},
-	{
-		// explicit string tag
-		"a: !!str 2015-01-01",
-		map[string]interface{}{"a": "2015-01-01"},
-	},
-	{
-		// explicit timestamp tag on quoted string
-		"a: !!timestamp \"2015-01-01\"",
-		map[string]interface{}{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
-	},
-	{
-		// explicit timestamp tag on unquoted string
-		"a: !!timestamp 2015-01-01",
-		map[string]interface{}{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
-	},
-	{
-		// quoted string that's a valid timestamp
-		"a: \"2015-01-01\"",
-		map[string]interface{}{"a": "2015-01-01"},
+		map[string]time.Time{"a": time.Unix(1424801979, 0).In(time.UTC)},
 	},
 	},
 
 
 	// Encode empty lists as zero-length slices.
 	// Encode empty lists as zero-length slices.

+ 3 - 9
encode.go

@@ -251,17 +251,11 @@ func (e *encoder) stringv(tag string, in reflect.Value) {
 			failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
 			failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
 		}
 		}
 	}
 	}
-	switch {
-	case rtag == yaml_TIMESTAMP_TAG:
-		// TODO with the current code, there's no way for tag to be non-empty,
-		// but what should this function do if (for example) tag is yaml_BOOL_TAG
-		// and rtag is something incompatible with it?
-		style = yaml_PLAIN_SCALAR_STYLE
-	case tag == "" && (rtag != yaml_STR_TAG || isBase60Float(s)):
+	if tag == "" && (rtag != yaml_STR_TAG || isBase60Float(s)) {
 		style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
 		style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
-	case strings.Contains(s, "\n"):
+	} else if strings.Contains(s, "\n") {
 		style = yaml_LITERAL_SCALAR_STYLE
 		style = yaml_LITERAL_SCALAR_STYLE
-	default:
+	} else {
 		style = yaml_PLAIN_SCALAR_STYLE
 		style = yaml_PLAIN_SCALAR_STYLE
 	}
 	}
 	e.emitScalar(s, "", tag, style)
 	e.emitScalar(s, "", tag, style)

+ 2 - 3
encode_test.go

@@ -303,7 +303,7 @@ var marshalTests = []struct {
 		"a: 1.2.3.4\n",
 		"a: 1.2.3.4\n",
 	},
 	},
 	{
 	{
-		map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
+		map[string]time.Time{"a": time.Unix(1424801979, 0)},
 		"a: 2015-02-24T18:19:39Z\n",
 		"a: 2015-02-24T18:19:39Z\n",
 	},
 	},
 
 
@@ -327,8 +327,7 @@ var marshalTests = []struct {
 func (s *S) TestMarshal(c *C) {
 func (s *S) TestMarshal(c *C) {
 	defer os.Setenv("TZ", os.Getenv("TZ"))
 	defer os.Setenv("TZ", os.Getenv("TZ"))
 	os.Setenv("TZ", "UTC")
 	os.Setenv("TZ", "UTC")
-	for i, item := range marshalTests {
-		c.Logf("test %d: %q", i, item.data)
+	for _, item := range marshalTests {
 		data, err := yaml.Marshal(item.value)
 		data, err := yaml.Marshal(item.value)
 		c.Assert(err, IsNil)
 		c.Assert(err, IsNil)
 		c.Assert(string(data), Equals, item.data)
 		c.Assert(string(data), Equals, item.data)

+ 3 - 47
resolve.go

@@ -6,7 +6,6 @@ import (
 	"regexp"
 	"regexp"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
-	"time"
 	"unicode/utf8"
 	"unicode/utf8"
 )
 )
 
 
@@ -76,7 +75,7 @@ func longTag(tag string) string {
 
 
 func resolvableTag(tag string) bool {
 func resolvableTag(tag string) bool {
 	switch tag {
 	switch tag {
-	case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG, yaml_TIMESTAMP_TAG:
+	case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG:
 		return true
 		return true
 	}
 	}
 	return false
 	return false
@@ -126,15 +125,6 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
 
 
 		case 'D', 'S':
 		case 'D', 'S':
 			// Int, float, or timestamp.
 			// Int, float, or timestamp.
-			// Only try values as a timestamp if the value is unquoted or there's an explicit
-			// !!timestamp tag.
-			if tag == "" || tag == yaml_TIMESTAMP_TAG {
-				t, ok := parseTimestamp(in)
-				if ok {
-					return yaml_TIMESTAMP_TAG, t
-				}
-			}
-
 			plain := strings.Replace(in, "_", "", -1)
 			plain := strings.Replace(in, "_", "", -1)
 			intv, err := strconv.ParseInt(plain, 0, 64)
 			intv, err := strconv.ParseInt(plain, 0, 64)
 			if err == nil {
 			if err == nil {
@@ -177,6 +167,8 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
 					}
 					}
 				}
 				}
 			}
 			}
+			// XXX Handle timestamps here.
+
 		default:
 		default:
 			panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
 			panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
 		}
 		}
@@ -214,39 +206,3 @@ func encodeBase64(s string) string {
 	}
 	}
 	return string(out[:k])
 	return string(out[:k])
 }
 }
-
-// This is a subset of the formats allowed by the regular expression
-// defined at http://yaml.org/type/timestamp.html.
-var allowedTimestampFormats = []string{
-	"2006-1-2T15:4:5Z07:00",
-	"2006-1-2t15:4:5Z07:00", // RFC3339 with lower-case "t".
-	"2006-1-2 15:4:5",       // space separated with no time zone
-	"2006-1-2",              // date only
-	// Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5"
-	// from the set of examples.
-}
-
-// parseTimestamp parses s as a timestamp string and
-// returns the timestamp and reports whether it succeeded.
-// Timestamp formats are defined at http://yaml.org/type/timestamp.html
-func parseTimestamp(s string) (time.Time, bool) {
-	// TODO write code to check all the formats supported by
-	// http://yaml.org/type/timestamp.html instead of using time.Parse.
-
-	// Quick check: all date formats start with YYYY-.
-	i := 0
-	for ; i < len(s); i++ {
-		if c := s[i]; c < '0' || c > '9' {
-			break
-		}
-	}
-	if i != 4 || i == len(s) || s[i] != '-' {
-		return time.Time{}, false
-	}
-	for _, format := range allowedTimestampFormats {
-		if t, err := time.Parse(format, s); err == nil {
-			return t, true
-		}
-	}
-	return time.Time{}, false
-}