From fc4354fadcb04f24e6b9782ded51f30b61fa92f2 Mon Sep 17 00:00:00 2001 From: Andrew Kroh Date: Fri, 12 Sep 2025 19:21:52 -0400 Subject: [PATCH] fix: resolve encode_json error for ref.Val lists Fixes JSON marshaling failure when encoding lists containing map[ref.Val]ref.Val types. The error "json: unsupported type: map[ref.Val]ref.Val" occurred when calling encode_json() on expressions like [0, 1].map(i, {"a":i}).encode_json(). The fix extends the protobuf conversion case to handle both map[ref.Val]ref.Val (existing) and []ref.Val (new) types. --- lib/json.go | 2 +- testdata/json_encode.txt | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/json.go b/lib/json.go index f23484c..fa08b32 100644 --- a/lib/json.go +++ b/lib/json.go @@ -251,7 +251,7 @@ func encodeJSON(val ref.Val) ref.Val { switch under := val.Value().(type) { case map[string]any: v = under - case map[ref.Val]ref.Val: + case map[ref.Val]ref.Val, []ref.Val: pb, err := val.ConvertToNative(structpbValueType) if err != nil { return types.NewErr("failed proto conversion: %v", err) diff --git a/testdata/json_encode.txt b/testdata/json_encode.txt index 0fea196..0f22481 100644 --- a/testdata/json_encode.txt +++ b/testdata/json_encode.txt @@ -23,6 +23,7 @@ cmp stdout want.txt }), "plain text".encode_json(), encode_json("plain text"), + [0, 1].map(i, {"a":i}).encode_json() ] -- want.txt -- [ @@ -32,5 +33,6 @@ cmp stdout want.txt "{\"a\":{\"b\":{\"c\":1}}}", "{\"a\":{\"b\":{\"c\":1}}}", "\"plain text\"", - "\"plain text\"" + "\"plain text\"", + "[{\"a\":0},{\"a\":1}]" ]