From 915545bb9d8f88dfcfca60f59e02d31c6e859ff7 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Wed, 27 May 2020 10:48:03 -0400 Subject: [PATCH 1/2] Allow null values for jsonencode --- cty/function/stdlib/json.go | 5 +++++ cty/function/stdlib/json_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/cty/function/stdlib/json.go b/cty/function/stdlib/json.go index 07901c65..6ee3a87e 100644 --- a/cty/function/stdlib/json.go +++ b/cty/function/stdlib/json.go @@ -12,6 +12,7 @@ var JSONEncodeFunc = function.New(&function.Spec{ Name: "val", Type: cty.DynamicPseudoType, AllowDynamicType: true, + AllowNull: true, }, }, Type: function.StaticReturnType(cty.String), @@ -24,6 +25,10 @@ var JSONEncodeFunc = function.New(&function.Spec{ return cty.UnknownVal(retType), nil } + if val.IsNull() { + return cty.NullVal(retType), nil + } + buf, err := json.Marshal(val, val.Type()) if err != nil { return cty.NilVal, err diff --git a/cty/function/stdlib/json_test.go b/cty/function/stdlib/json_test.go index e67ba6e0..9e5a2df0 100644 --- a/cty/function/stdlib/json_test.go +++ b/cty/function/stdlib/json_test.go @@ -52,6 +52,11 @@ func TestJSONEncode(t *testing.T) { cty.DynamicVal, cty.UnknownVal(cty.String), }, + // We allow null values + { + cty.NullVal(cty.String), + cty.NullVal(cty.String), + }, } for _, test := range tests { From 0c4042c3171c81ba0be9c44311404fd433b19ff1 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Wed, 27 May 2020 14:55:16 -0400 Subject: [PATCH 2/2] We should return string null for JSON purposes --- cty/function/stdlib/json.go | 2 +- cty/function/stdlib/json_test.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cty/function/stdlib/json.go b/cty/function/stdlib/json.go index 6ee3a87e..02770a65 100644 --- a/cty/function/stdlib/json.go +++ b/cty/function/stdlib/json.go @@ -26,7 +26,7 @@ var JSONEncodeFunc = function.New(&function.Spec{ } if val.IsNull() { - return cty.NullVal(retType), nil + return cty.StringVal("null"), nil } buf, err := json.Marshal(val, val.Type()) diff --git a/cty/function/stdlib/json_test.go b/cty/function/stdlib/json_test.go index 9e5a2df0..5c1348fc 100644 --- a/cty/function/stdlib/json_test.go +++ b/cty/function/stdlib/json_test.go @@ -52,10 +52,9 @@ func TestJSONEncode(t *testing.T) { cty.DynamicVal, cty.UnknownVal(cty.String), }, - // We allow null values { cty.NullVal(cty.String), - cty.NullVal(cty.String), + cty.StringVal("null"), }, }