Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,24 @@ func TestDecoder(t *testing.T) {
value: map[string]interface{}{"a": "50cent_of_dollar"},
},

// Unconventional keys
{
source: "1: v\n",
value: map[int]string{1: "v"},
},
{
source: "1.1: v\n",
value: map[float64]string{1.1: "v"},
},
{
source: "true: v\n",
value: map[bool]string{true: "v"},
},
{
source: "2015-01-01T00:00:00Z: v\n",
value: map[time.Time]string{time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC): "v"},
},

// Nulls
{
source: "null",
Expand Down
8 changes: 7 additions & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,15 @@ func (e *Encoder) encodeMap(ctx context.Context, value reflect.Value, column int
anchorNode.Value = encoded
encoded = anchorNode
}

kn, err := e.encodeValue(ctx, reflect.ValueOf(key), column)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable k already holds reflect.ValueOf(key).

keyNode, ok := kn.(ast.MapKeyNode)
Comment on lines +716 to +717
Copy link

Copilot AI Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The variable name kn is ambiguous; consider renaming it to rawNode or encodedKeyNode to clarify its purpose.

Suggested change
kn, err := e.encodeValue(ctx, reflect.ValueOf(key), column)
keyNode, ok := kn.(ast.MapKeyNode)
encodedKeyNode, err := e.encodeValue(ctx, reflect.ValueOf(key), column)
keyNode, ok := encodedKeyNode.(ast.MapKeyNode)
Copilot uses AI. Check for mistakes.
if !ok || err != nil {
keyNode = e.encodeString(fmt.Sprint(key), column)
}
node.Values = append(node.Values, ast.MappingValue(
nil,
e.encodeString(keyText, column),
keyNode,
encoded,
))
e.setSmartAnchor(vRef, keyText)
Expand Down
20 changes: 20 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ func TestEncoder(t *testing.T) {
map[string]string{"a": "-"},
nil,
},
{
"1: v\n",
map[int]string{1: "v"},
nil,
},
{
"1.1: v\n",
map[float64]string{1.1: "v"},
nil,
},
{
"true: v\n",
map[bool]string{true: "v"},
nil,
},
{
"2015-01-01T00:00:00Z: v\n",
map[time.Time]string{time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC): "v"},
nil,
},
{
"123\n",
123,
Expand Down
Loading