Skip to content

Commit b8d7755

Browse files
oguzkilcaningydotnet
authored andcommitted
Allow timestamp scalars to unmarshal into strings
constructTimestamp only handled time.Time and interface{} targets. A plain scalar resolving to the timestamp tag left a string field at its zero value instead of the raw text. This adds a string case that writes the scalar directly, the same fallback other scalar tags already have.
1 parent 766a692 commit b8d7755

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

internal/libyaml/constructor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ func (c *Constructor) constructTimestamp(n *Node, resolved any, out reflect.Valu
394394
out.Set(resolvedv)
395395
return true
396396
}
397+
case reflect.String:
398+
// Allow timestamp values to be constructed as string.
399+
out.SetString(n.Value)
400+
return true
397401
case reflect.Interface:
398402
out.Set(reflect.ValueOf(resolved))
399403
return true

yaml_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,46 @@ var unmarshalTests = []struct {
556556
"a: 2015-01-01",
557557
map[string]any{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
558558
},
559+
{
560+
// implicit timestamp tag into string.
561+
"a: 2015-01-01",
562+
map[string]string{"a": "2015-01-01"},
563+
},
564+
{
565+
// implicit timestamp tag on quoted string into string.
566+
"a: \"2015-01-01\"",
567+
map[string]string{"a": "2015-01-01"},
568+
},
569+
{
570+
// implicit timestamp tag with time and fraction into string.
571+
"a: 2015-02-24T18:19:39.12Z",
572+
map[string]string{"a": "2015-02-24T18:19:39.12Z"},
573+
},
574+
{
575+
// implicit timestamp tag with time and fraction on quoted string into string.
576+
"a: \"2015-02-24T18:19:39.12Z\"",
577+
map[string]string{"a": "2015-02-24T18:19:39.12Z"},
578+
},
579+
{
580+
// explicit timestamp tag on unquoted string into string.
581+
"a: !!timestamp 2015-01-01",
582+
map[string]string{"a": "2015-01-01"},
583+
},
584+
{
585+
// explicit timestamp tag into string.
586+
"a: !!timestamp \"2015-01-01\"",
587+
map[string]string{"a": "2015-01-01"},
588+
},
589+
{
590+
// explicit timestamp tag with time and fraction on unquoted string into string.
591+
"a: !!timestamp 2015-02-24T18:19:39.12Z",
592+
map[string]string{"a": "2015-02-24T18:19:39.12Z"},
593+
},
594+
{
595+
// explicit timestamp tag with time and fraction into string.
596+
"a: !!timestamp \"2015-02-24T18:19:39.12Z\"",
597+
map[string]string{"a": "2015-02-24T18:19:39.12Z"},
598+
},
559599

560600
// UTF-16-LE
561601
{

0 commit comments

Comments
 (0)