Skip to content

Commit b729be6

Browse files
authored
esc: handle nil in MakeSecret (#518)
1 parent fe886d0 commit b729be6

File tree

2 files changed

+119
-6
lines changed

2 files changed

+119
-6
lines changed

value.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,21 @@ func NewSecret[T ValueType](v T) Value {
7373
}
7474

7575
func (v Value) MakeSecret() Value {
76-
switch v := v.Value.(type) {
76+
switch vv := v.Value.(type) {
77+
case nil:
78+
copy := v
79+
copy.Secret = true
80+
return copy
7781
case bool:
78-
return NewSecret(v)
82+
return NewSecret(vv)
7983
case json.Number:
80-
return NewSecret(v)
84+
return NewSecret(vv)
8185
case string:
82-
return NewSecret(v)
86+
return NewSecret(vv)
8387
case []Value:
84-
return NewSecret(v)
88+
return NewSecret(vv)
8589
case map[string]Value:
86-
return NewSecret(v)
90+
return NewSecret(vv)
8791
default:
8892
panic("invalid value")
8993
}

value_test.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2023, Pulumi Corporation.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package esc
16+
17+
import (
18+
"encoding/json"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestNewSecret(t *testing.T) {
25+
cases := []struct {
26+
name string
27+
newSecret func() Value
28+
expected Value
29+
}{
30+
{
31+
name: "bool",
32+
newSecret: func() Value { return NewSecret(true) },
33+
expected: Value{Value: true, Secret: true},
34+
},
35+
{
36+
name: "number",
37+
newSecret: func() Value { return NewSecret(json.Number("3.14")) },
38+
expected: Value{Value: json.Number("3.14"), Secret: true},
39+
},
40+
{
41+
name: "string",
42+
newSecret: func() Value { return NewSecret("hello") },
43+
expected: Value{Value: "hello", Secret: true},
44+
},
45+
{
46+
name: "array",
47+
newSecret: func() Value { return NewSecret([]Value{NewValue([]Value{NewValue("hello"), NewValue("world")})}) },
48+
expected: Value{Value: []Value{{Value: []Value{{Value: "hello", Secret: true}, {Value: "world", Secret: true}}, Secret: true}}, Secret: true},
49+
},
50+
{
51+
name: "object",
52+
newSecret: func() Value {
53+
return NewSecret(map[string]Value{"nest": NewValue(map[string]Value{"hello": NewValue("world")})})
54+
},
55+
expected: Value{Value: map[string]Value{"nest": {Value: map[string]Value{"hello": {Value: "world", Secret: true}}, Secret: true}}, Secret: true},
56+
},
57+
}
58+
for _, c := range cases {
59+
t.Run(c.name, func(t *testing.T) {
60+
actual := c.newSecret()
61+
assert.Equal(t, actual, c.expected)
62+
})
63+
}
64+
}
65+
66+
func TestMakeSecret(t *testing.T) {
67+
cases := []struct {
68+
name string
69+
value Value
70+
expected Value
71+
}{
72+
{
73+
name: "zero",
74+
value: Value{},
75+
expected: Value{Secret: true},
76+
},
77+
{
78+
name: "bool",
79+
value: NewValue(true),
80+
expected: Value{Value: true, Secret: true},
81+
},
82+
{
83+
name: "number",
84+
value: NewValue(json.Number("3.14")),
85+
expected: Value{Value: json.Number("3.14"), Secret: true},
86+
},
87+
{
88+
name: "string",
89+
value: NewValue("hello"),
90+
expected: Value{Value: "hello", Secret: true},
91+
},
92+
{
93+
name: "array",
94+
value: NewValue([]Value{NewValue([]Value{NewValue("hello"), NewValue("world")})}),
95+
expected: Value{Value: []Value{{Value: []Value{{Value: "hello", Secret: true}, {Value: "world", Secret: true}}, Secret: true}}, Secret: true},
96+
},
97+
{
98+
name: "object",
99+
value: NewValue(map[string]Value{"nest": NewValue(map[string]Value{"hello": NewValue("world")})}),
100+
expected: Value{Value: map[string]Value{"nest": {Value: map[string]Value{"hello": {Value: "world", Secret: true}}, Secret: true}}, Secret: true},
101+
},
102+
}
103+
for _, c := range cases {
104+
t.Run(c.name, func(t *testing.T) {
105+
actual := c.value.MakeSecret()
106+
assert.Equal(t, actual, c.expected)
107+
})
108+
}
109+
}

0 commit comments

Comments
 (0)