Skip to content

Commit 9b88446

Browse files
authored
convert: handle unknown values (#50)
we were not handling `unknown` values correctly for some of the types, that could cause a runtime error. Fix: https://linear.app/pomerium/issue/ENG-2088/value-conversion-error-occurs-after-upgrading-tp-to-v0011-and-changing
1 parent 4ca612f commit 9b88446

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

internal/provider/convert.go

+19-16
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func FromStringMap(m map[string]string) types.Map {
7676

7777
// ToStringList converts a types.List to Settings_StringList and handles diagnostics internally
7878
func ToStringListFromSet(ctx context.Context, dst **pb.Settings_StringList, set types.Set, diagnostics *diag.Diagnostics) {
79-
if set.IsNull() {
79+
if set.IsNull() || set.IsUnknown() {
8080
*dst = nil
8181
return
8282
}
@@ -90,7 +90,7 @@ func ToStringListFromSet(ctx context.Context, dst **pb.Settings_StringList, set
9090

9191
// ToStringMap converts a types.Map to map[string]string and handles diagnostics internally
9292
func ToStringMap(ctx context.Context, dst *map[string]string, m types.Map, diagnostics *diag.Diagnostics) {
93-
if m.IsNull() {
93+
if m.IsNull() || m.IsUnknown() {
9494
*dst = nil
9595
return
9696
}
@@ -103,25 +103,28 @@ func ToStringMap(ctx context.Context, dst *map[string]string, m types.Map, diagn
103103
}
104104

105105
func ToStringSliceFromSet(ctx context.Context, dst *[]string, set types.Set, diagnostics *diag.Diagnostics) {
106-
*dst = make([]string, 0)
107-
if !set.IsNull() {
108-
var values []string
109-
diagnostics.Append(set.ElementsAs(ctx, &values, false)...)
110-
if !diagnostics.HasError() {
111-
*dst = values
112-
}
106+
if set.IsNull() || set.IsUnknown() {
107+
*dst = nil
108+
return
109+
}
110+
var values []string
111+
diagnostics.Append(set.ElementsAs(ctx, &values, false)...)
112+
if !diagnostics.HasError() {
113+
*dst = values
113114
}
114115
}
115116

116117
// ToStringSliceFromList converts a types.List to string slice and handles diagnostics internally
117118
func ToStringSliceFromList(ctx context.Context, dst *[]string, list types.List, diagnostics *diag.Diagnostics) {
118-
*dst = make([]string, 0)
119-
if !list.IsNull() {
120-
var values []string
121-
diagnostics.Append(list.ElementsAs(ctx, &values, false)...)
122-
if !diagnostics.HasError() {
123-
*dst = values
124-
}
119+
if list.IsNull() || list.IsUnknown() {
120+
*dst = nil
121+
return
122+
}
123+
124+
var values []string
125+
diagnostics.Append(list.ElementsAs(ctx, &values, false)...)
126+
if !diagnostics.HasError() {
127+
*dst = values
125128
}
126129
}
127130

internal/provider/convert_test.go

+32-10
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ func TestToStringListFromSet(t *testing.T) {
152152
assert.Nil(t, s)
153153
},
154154
},
155+
{
156+
name: "unknown set",
157+
input: types.SetUnknown(types.StringType),
158+
validate: func(t *testing.T, s *pb.Settings_StringList) {
159+
assert.Nil(t, s)
160+
},
161+
},
155162
{
156163
name: "empty list",
157164
input: types.SetValueMust(types.StringType, []attr.Value{}),
@@ -491,11 +498,16 @@ func TestToStringSliceFromSet(t *testing.T) {
491498
{
492499
name: "null set",
493500
input: types.SetNull(types.StringType),
494-
expected: []string{},
501+
expected: nil,
502+
},
503+
{
504+
name: "unknown set",
505+
input: types.SetUnknown(types.StringType),
506+
expected: nil,
495507
},
496508
{
497509
name: "empty set",
498-
input: types.SetValueMust(types.StringType, []attr.Value{}),
510+
input: types.SetValueMust(types.StringType, nil),
499511
expected: []string{},
500512
},
501513
{
@@ -595,7 +607,12 @@ func TestToStringSliceFromList(t *testing.T) {
595607
{
596608
name: "null list",
597609
input: types.ListNull(types.StringType),
598-
expected: []string{},
610+
expected: nil,
611+
},
612+
{
613+
name: "unknown list",
614+
input: types.ListUnknown(types.StringType),
615+
expected: nil,
599616
},
600617
{
601618
name: "empty list",
@@ -633,7 +650,12 @@ func TestToStringMap(t *testing.T) {
633650
{
634651
name: "null map",
635652
input: types.MapNull(types.StringType),
636-
expected: nil, // Changed from empty map to nil to match implementation
653+
expected: nil,
654+
},
655+
{
656+
name: "unknown map",
657+
input: types.MapUnknown(types.StringType),
658+
expected: nil,
637659
},
638660
{
639661
name: "empty map",
@@ -780,10 +802,10 @@ func TestToRouteStringList(t *testing.T) {
780802
0,
781803
},
782804
{
783-
"invalid",
784-
types.SetValueMust(types.BoolType, []attr.Value{types.BoolValue(true)}),
805+
"unknown",
806+
types.SetUnknown(types.StringType),
785807
nil,
786-
1,
808+
0,
787809
},
788810
{
789811
"empty",
@@ -827,10 +849,10 @@ func TestToSettingsStringList(t *testing.T) {
827849
0,
828850
},
829851
{
830-
"invalid",
831-
types.SetValueMust(types.BoolType, []attr.Value{types.BoolValue(true)}),
852+
"unknown",
853+
types.SetUnknown(types.StringType),
832854
nil,
833-
1,
855+
0,
834856
},
835857
{
836858
"empty",

0 commit comments

Comments
 (0)