Skip to content

Commit a7a830e

Browse files
authored
tfprotov5+tfprotov6: Remove unnecessary AttributePath_Step error return (#369)
1 parent 9cb265c commit a7a830e

26 files changed

+295
-806
lines changed

tfprotov5/internal/toproto/attribute_path.go

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,89 @@
44
package toproto
55

66
import (
7-
"errors"
7+
"fmt"
88

99
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
1010
"github.com/hashicorp/terraform-plugin-go/tftypes"
1111
)
1212

13-
var ErrUnknownAttributePathStepType = errors.New("unknown type of AttributePath_Step")
14-
15-
func AttributePath(in *tftypes.AttributePath) (*tfplugin5.AttributePath, error) {
13+
func AttributePath(in *tftypes.AttributePath) *tfplugin5.AttributePath {
1614
if in == nil {
17-
return nil, nil
18-
}
19-
20-
steps, err := AttributePath_Steps(in.Steps())
21-
22-
if err != nil {
23-
return nil, err
15+
return nil
2416
}
2517

2618
resp := &tfplugin5.AttributePath{
27-
Steps: steps,
19+
Steps: AttributePath_Steps(in.Steps()),
2820
}
2921

30-
return resp, nil
22+
return resp
3123
}
3224

33-
func AttributePaths(in []*tftypes.AttributePath) ([]*tfplugin5.AttributePath, error) {
25+
func AttributePaths(in []*tftypes.AttributePath) []*tfplugin5.AttributePath {
3426
resp := make([]*tfplugin5.AttributePath, 0, len(in))
3527

3628
for _, a := range in {
37-
attr, err := AttributePath(a)
38-
39-
if err != nil {
40-
return resp, err
41-
}
42-
43-
resp = append(resp, attr)
29+
resp = append(resp, AttributePath(a))
4430
}
4531

46-
return resp, nil
32+
return resp
4733
}
4834

49-
func AttributePath_Step(step tftypes.AttributePathStep) (*tfplugin5.AttributePath_Step, error) {
50-
var resp tfplugin5.AttributePath_Step
51-
if name, ok := step.(tftypes.AttributeName); ok {
52-
resp.Selector = &tfplugin5.AttributePath_Step_AttributeName{
53-
AttributeName: string(name),
54-
}
55-
return &resp, nil
35+
func AttributePath_Step(step tftypes.AttributePathStep) *tfplugin5.AttributePath_Step {
36+
if step == nil {
37+
return nil
5638
}
57-
if key, ok := step.(tftypes.ElementKeyString); ok {
58-
resp.Selector = &tfplugin5.AttributePath_Step_ElementKeyString{
59-
ElementKeyString: string(key),
39+
40+
switch step := step.(type) {
41+
case tftypes.AttributeName:
42+
return &tfplugin5.AttributePath_Step{
43+
Selector: &tfplugin5.AttributePath_Step_AttributeName{
44+
AttributeName: string(step),
45+
},
6046
}
61-
return &resp, nil
62-
}
63-
if key, ok := step.(tftypes.ElementKeyInt); ok {
64-
resp.Selector = &tfplugin5.AttributePath_Step_ElementKeyInt{
65-
ElementKeyInt: int64(key),
47+
case tftypes.ElementKeyInt:
48+
return &tfplugin5.AttributePath_Step{
49+
Selector: &tfplugin5.AttributePath_Step_ElementKeyInt{
50+
ElementKeyInt: int64(step),
51+
},
6652
}
67-
return &resp, nil
68-
}
69-
if _, ok := step.(tftypes.ElementKeyValue); ok {
70-
// the protocol has no equivalent of an ElementKeyValue, so we
71-
// return nil for both the step and the error here, to signal
72-
// that we've hit a step we can't convey back to Terraform
73-
return nil, nil
53+
case tftypes.ElementKeyString:
54+
return &tfplugin5.AttributePath_Step{
55+
Selector: &tfplugin5.AttributePath_Step_ElementKeyString{
56+
ElementKeyString: string(step),
57+
},
58+
}
59+
case tftypes.ElementKeyValue:
60+
// The protocol has no equivalent of an ElementKeyValue, so this
61+
// returns nil for the step to signal a step we cannot convey back
62+
// to Terraform.
63+
return nil
7464
}
75-
return nil, ErrUnknownAttributePathStepType
65+
66+
// It is not currently possible to create tftypes.AttributePathStep
67+
// implementations outside the tftypes package and these implementations
68+
// should rarely change, if ever, since they are critical to how
69+
// Terraform understands attribute paths. If this panic was reached, it
70+
// implies that a new step type was introduced and needs to be
71+
// implemented as a new case above or that this logic needs to be
72+
// otherwise changed to handle some new attribute path system.
73+
panic(fmt.Sprintf("unimplemented tftypes.AttributePathStep type: %T", step))
7674
}
7775

78-
func AttributePath_Steps(in []tftypes.AttributePathStep) ([]*tfplugin5.AttributePath_Step, error) {
76+
func AttributePath_Steps(in []tftypes.AttributePathStep) []*tfplugin5.AttributePath_Step {
7977
resp := make([]*tfplugin5.AttributePath_Step, 0, len(in))
78+
8079
for _, step := range in {
81-
if step == nil {
82-
resp = append(resp, nil)
83-
continue
84-
}
85-
s, err := AttributePath_Step(step)
86-
if err != nil {
87-
return resp, err
88-
}
89-
// in the face of a set, the protocol has no way to represent
90-
// the index, so we just bail and return the prefix we can
91-
// return.
80+
s := AttributePath_Step(step)
81+
82+
// In the face of a ElementKeyValue or missing step, Terraform has no
83+
// way to represent the attribute path, so only return the prefix.
9284
if s == nil {
93-
return resp, nil
85+
return resp
9486
}
87+
9588
resp = append(resp, s)
9689
}
97-
return resp, nil
90+
91+
return resp
9892
}

tfprotov5/internal/toproto/attribute_path_test.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ func TestAttributePath(t *testing.T) {
5050
t.Run(name, func(t *testing.T) {
5151
t.Parallel()
5252

53-
// Intentionally not checking the error return as it is impossible
54-
// to implement a test case which would raise an error. This return
55-
// will be removed in preference of a panic a future change.
56-
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/365
57-
got, _ := toproto.AttributePath(testCase.in)
53+
got := toproto.AttributePath(testCase.in)
5854

5955
// Protocol Buffers generated types must have unexported fields
6056
// ignored or cmp.Diff() will raise an error. This is easier than
@@ -137,11 +133,7 @@ func TestAttributePaths(t *testing.T) {
137133
t.Run(name, func(t *testing.T) {
138134
t.Parallel()
139135

140-
// Intentionally not checking the error return as it is impossible
141-
// to implement a test case which would raise an error. This return
142-
// will be removed in preference of a panic a future change.
143-
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/365
144-
got, _ := toproto.AttributePaths(testCase.in)
136+
got := toproto.AttributePaths(testCase.in)
145137

146138
// Protocol Buffers generated types must have unexported fields
147139
// ignored or cmp.Diff() will raise an error. This is easier than
@@ -206,11 +198,7 @@ func TestAttributePath_Step(t *testing.T) {
206198
t.Run(name, func(t *testing.T) {
207199
t.Parallel()
208200

209-
// Intentionally not checking the error return as it is impossible
210-
// to implement a test case which would raise an error. This return
211-
// will be removed in preference of a panic a future change.
212-
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/365
213-
got, _ := toproto.AttributePath_Step(testCase.in)
201+
got := toproto.AttributePath_Step(testCase.in)
214202

215203
// Protocol Buffers generated types must have unexported fields
216204
// ignored or cmp.Diff() will raise an error. This is easier than
@@ -280,11 +268,7 @@ func TestAttributePath_Steps(t *testing.T) {
280268
t.Run(name, func(t *testing.T) {
281269
t.Parallel()
282270

283-
// Intentionally not checking the error return as it is impossible
284-
// to implement a test case which would raise an error. This return
285-
// will be removed in preference of a panic a future change.
286-
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/365
287-
got, _ := toproto.AttributePath_Steps(testCase.in)
271+
got := toproto.AttributePath_Steps(testCase.in)
288272

289273
// Protocol Buffers generated types must have unexported fields
290274
// ignored or cmp.Diff() will raise an error. This is easier than

tfprotov5/internal/toproto/data_source.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,27 @@ func GetMetadata_DataSourceMetadata(in *tfprotov5.DataSourceMetadata) *tfplugin5
2020
return resp
2121
}
2222

23-
func ValidateDataSourceConfig_Response(in *tfprotov5.ValidateDataSourceConfigResponse) (*tfplugin5.ValidateDataSourceConfig_Response, error) {
23+
func ValidateDataSourceConfig_Response(in *tfprotov5.ValidateDataSourceConfigResponse) *tfplugin5.ValidateDataSourceConfig_Response {
2424
if in == nil {
25-
return nil, nil
26-
}
27-
28-
diags, err := Diagnostics(in.Diagnostics)
29-
30-
if err != nil {
31-
return nil, err
25+
return nil
3226
}
3327

3428
resp := &tfplugin5.ValidateDataSourceConfig_Response{
35-
Diagnostics: diags,
29+
Diagnostics: Diagnostics(in.Diagnostics),
3630
}
3731

38-
return resp, nil
32+
return resp
3933
}
4034

41-
func ReadDataSource_Response(in *tfprotov5.ReadDataSourceResponse) (*tfplugin5.ReadDataSource_Response, error) {
35+
func ReadDataSource_Response(in *tfprotov5.ReadDataSourceResponse) *tfplugin5.ReadDataSource_Response {
4236
if in == nil {
43-
return nil, nil
44-
}
45-
46-
diags, err := Diagnostics(in.Diagnostics)
47-
48-
if err != nil {
49-
return nil, err
37+
return nil
5038
}
5139

5240
resp := &tfplugin5.ReadDataSource_Response{
53-
Diagnostics: diags,
41+
Diagnostics: Diagnostics(in.Diagnostics),
5442
State: DynamicValue(in.State),
5543
}
5644

57-
return resp, nil
45+
return resp
5846
}

tfprotov5/internal/toproto/data_source_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ func TestReadDataSource_Response(t *testing.T) {
107107
t.Run(name, func(t *testing.T) {
108108
t.Parallel()
109109

110-
// Intentionally not checking the error return as it is impossible
111-
// to implement a test case which would raise an error. This return
112-
// will be removed in preference of a panic a future change.
113-
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/365
114-
got, _ := toproto.ReadDataSource_Response(testCase.in)
110+
got := toproto.ReadDataSource_Response(testCase.in)
115111

116112
// Protocol Buffers generated types must have unexported fields
117113
// ignored or cmp.Diff() will raise an error. This is easier than
@@ -167,11 +163,7 @@ func TestValidateDataSourceConfig_Response(t *testing.T) {
167163
t.Run(name, func(t *testing.T) {
168164
t.Parallel()
169165

170-
// Intentionally not checking the error return as it is impossible
171-
// to implement a test case which would raise an error. This return
172-
// will be removed in preference of a panic a future change.
173-
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/365
174-
got, _ := toproto.ValidateDataSourceConfig_Response(testCase.in)
166+
got := toproto.ValidateDataSourceConfig_Response(testCase.in)
175167

176168
// Protocol Buffers generated types must have unexported fields
177169
// ignored or cmp.Diff() will raise an error. This is easier than

tfprotov5/internal/toproto/diagnostic.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,34 @@ import (
1010
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
1111
)
1212

13-
func Diagnostic(in *tfprotov5.Diagnostic) (*tfplugin5.Diagnostic, error) {
13+
func Diagnostic(in *tfprotov5.Diagnostic) *tfplugin5.Diagnostic {
1414
if in == nil {
15-
return nil, nil
16-
}
17-
18-
attribute, err := AttributePath(in.Attribute)
19-
20-
if err != nil {
21-
return nil, err
15+
return nil
2216
}
2317

2418
resp := &tfplugin5.Diagnostic{
25-
Attribute: attribute,
19+
Attribute: AttributePath(in.Attribute),
2620
Detail: ForceValidUTF8(in.Detail),
2721
FunctionArgument: in.FunctionArgument,
2822
Severity: Diagnostic_Severity(in.Severity),
2923
Summary: ForceValidUTF8(in.Summary),
3024
}
3125

32-
return resp, nil
26+
return resp
3327
}
3428

3529
func Diagnostic_Severity(in tfprotov5.DiagnosticSeverity) tfplugin5.Diagnostic_Severity {
3630
return tfplugin5.Diagnostic_Severity(in)
3731
}
3832

39-
func Diagnostics(in []*tfprotov5.Diagnostic) ([]*tfplugin5.Diagnostic, error) {
33+
func Diagnostics(in []*tfprotov5.Diagnostic) []*tfplugin5.Diagnostic {
4034
resp := make([]*tfplugin5.Diagnostic, 0, len(in))
4135

4236
for _, diag := range in {
43-
d, err := Diagnostic(diag)
44-
45-
if err != nil {
46-
return resp, err
47-
}
48-
49-
resp = append(resp, d)
37+
resp = append(resp, Diagnostic(diag))
5038
}
5139

52-
return resp, nil
40+
return resp
5341
}
5442

5543
// ForceValidUTF8 returns a string guaranteed to be valid UTF-8 even if the

tfprotov5/internal/toproto/diagnostic_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ func TestDiagnostic(t *testing.T) {
105105
t.Run(name, func(t *testing.T) {
106106
t.Parallel()
107107

108-
// Intentionally not checking the error return as it is impossible
109-
// to implement a test case which would raise an error. This return
110-
// will be removed in preference of a panic a future change.
111-
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/365
112-
got, _ := toproto.Diagnostic(testCase.in)
108+
got := toproto.Diagnostic(testCase.in)
113109

114110
// Protocol Buffers generated types must have unexported fields
115111
// ignored or cmp.Diff() will raise an error. This is easier than
@@ -187,11 +183,7 @@ func TestDiagnostics(t *testing.T) {
187183
t.Run(name, func(t *testing.T) {
188184
t.Parallel()
189185

190-
// Intentionally not checking the error return as it is impossible
191-
// to implement a test case which would raise an error. This return
192-
// will be removed in preference of a panic a future change.
193-
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/365
194-
got, _ := toproto.Diagnostics(testCase.in)
186+
got := toproto.Diagnostics(testCase.in)
195187

196188
// Protocol Buffers generated types must have unexported fields
197189
// ignored or cmp.Diff() will raise an error. This is easier than

tfprotov5/internal/toproto/function.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,17 @@ import (
1010
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
1111
)
1212

13-
func CallFunction_Response(in *tfprotov5.CallFunctionResponse) (*tfplugin5.CallFunction_Response, error) {
13+
func CallFunction_Response(in *tfprotov5.CallFunctionResponse) *tfplugin5.CallFunction_Response {
1414
if in == nil {
15-
return nil, nil
16-
}
17-
18-
diags, err := Diagnostics(in.Diagnostics)
19-
20-
if err != nil {
21-
return nil, err
15+
return nil
2216
}
2317

2418
resp := &tfplugin5.CallFunction_Response{
25-
Diagnostics: diags,
19+
Diagnostics: Diagnostics(in.Diagnostics),
2620
Result: DynamicValue(in.Result),
2721
}
2822

29-
return resp, nil
23+
return resp
3024
}
3125

3226
func Function(in *tfprotov5.Function) (*tfplugin5.Function, error) {
@@ -136,14 +130,8 @@ func GetFunctions_Response(in *tfprotov5.GetFunctionsResponse) (*tfplugin5.GetFu
136130
return nil, nil
137131
}
138132

139-
diags, err := Diagnostics(in.Diagnostics)
140-
141-
if err != nil {
142-
return nil, err
143-
}
144-
145133
resp := &tfplugin5.GetFunctions_Response{
146-
Diagnostics: diags,
134+
Diagnostics: Diagnostics(in.Diagnostics),
147135
Functions: make(map[string]*tfplugin5.Function, len(in.Functions)),
148136
}
149137

0 commit comments

Comments
 (0)