Skip to content

Commit 89e7ee5

Browse files
authored
tfprotov5+tfprotov6: Cleanup and unit test fromproto/toproto packages (#367)
Reference: #365 Adjusts the following packages: * `tfprotov5/internal/fromproto` * `tfprotov5/internal/tf5server` * `tfprotov5/internal/toproto` * `tfprotov6/internal/fromproto` * `tfprotov6/internal/tf6server` * `tfprotov6/internal/toproto` With the following changes: - Removed unnecessary response types from `fromproto` - Removed unnecessary request types from `toproto` - Added unit testing for all functionality - Added nil panic protection for all functionality and removed extraneous nil checking in callers (e.g. around `DynamicValue` handling) - Clarified protocol request/response naming in the servers - Added vertical whitespace for readability and consistency As noted in some of the new unit testing, many of the response types can also have their error returns removed, but this will be saved for a followup change to somewhat reduce this already large refactoring.
1 parent bd50706 commit 89e7ee5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+9740
-2366
lines changed

tfprotov5/internal/fromproto/attribute_path.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

tfprotov5/internal/fromproto/data_source.go

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,29 @@ import (
88
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
99
)
1010

11-
func DataSourceMetadata(in *tfplugin5.GetMetadata_DataSourceMetadata) *tfprotov5.DataSourceMetadata {
11+
func ValidateDataSourceConfigRequest(in *tfplugin5.ValidateDataSourceConfig_Request) *tfprotov5.ValidateDataSourceConfigRequest {
1212
if in == nil {
1313
return nil
1414
}
1515

16-
return &tfprotov5.DataSourceMetadata{
17-
TypeName: in.TypeName,
18-
}
19-
}
20-
21-
func ValidateDataSourceConfigRequest(in *tfplugin5.ValidateDataSourceConfig_Request) (*tfprotov5.ValidateDataSourceConfigRequest, error) {
2216
resp := &tfprotov5.ValidateDataSourceConfigRequest{
17+
Config: DynamicValue(in.Config),
2318
TypeName: in.TypeName,
2419
}
25-
if in.Config != nil {
26-
resp.Config = DynamicValue(in.Config)
27-
}
28-
return resp, nil
20+
21+
return resp
2922
}
3023

31-
func ValidateDataSourceConfigResponse(in *tfplugin5.ValidateDataSourceConfig_Response) (*tfprotov5.ValidateDataSourceConfigResponse, error) {
32-
diags, err := Diagnostics(in.Diagnostics)
33-
if err != nil {
34-
return nil, err
24+
func ReadDataSourceRequest(in *tfplugin5.ReadDataSource_Request) *tfprotov5.ReadDataSourceRequest {
25+
if in == nil {
26+
return nil
3527
}
36-
return &tfprotov5.ValidateDataSourceConfigResponse{
37-
Diagnostics: diags,
38-
}, nil
39-
}
4028

41-
func ReadDataSourceRequest(in *tfplugin5.ReadDataSource_Request) (*tfprotov5.ReadDataSourceRequest, error) {
4229
resp := &tfprotov5.ReadDataSourceRequest{
43-
TypeName: in.TypeName,
44-
}
45-
if in.Config != nil {
46-
resp.Config = DynamicValue(in.Config)
47-
}
48-
if in.ProviderMeta != nil {
49-
resp.ProviderMeta = DynamicValue(in.ProviderMeta)
30+
Config: DynamicValue(in.Config),
31+
ProviderMeta: DynamicValue(in.ProviderMeta),
32+
TypeName: in.TypeName,
5033
}
51-
return resp, nil
52-
}
5334

54-
func ReadDataSourceResponse(in *tfplugin5.ReadDataSource_Response) (*tfprotov5.ReadDataSourceResponse, error) {
55-
diags, err := Diagnostics(in.Diagnostics)
56-
if err != nil {
57-
return nil, err
58-
}
59-
resp := &tfprotov5.ReadDataSourceResponse{
60-
Diagnostics: diags,
61-
}
62-
if in.State != nil {
63-
resp.State = DynamicValue(in.State)
64-
}
65-
return resp, nil
35+
return resp
6636
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/google/go-cmp/cmp"
10+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
11+
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/fromproto"
12+
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
13+
)
14+
15+
func TestReadDataSourceRequest(t *testing.T) {
16+
t.Parallel()
17+
18+
testCases := map[string]struct {
19+
in *tfplugin5.ReadDataSource_Request
20+
expected *tfprotov5.ReadDataSourceRequest
21+
}{
22+
"nil": {
23+
in: nil,
24+
expected: nil,
25+
},
26+
"zero": {
27+
in: &tfplugin5.ReadDataSource_Request{},
28+
expected: &tfprotov5.ReadDataSourceRequest{},
29+
},
30+
"Config": {
31+
in: &tfplugin5.ReadDataSource_Request{
32+
Config: testTfplugin5DynamicValue(),
33+
},
34+
expected: &tfprotov5.ReadDataSourceRequest{
35+
Config: testTfprotov5DynamicValue(),
36+
},
37+
},
38+
"ProviderMeta": {
39+
in: &tfplugin5.ReadDataSource_Request{
40+
ProviderMeta: testTfplugin5DynamicValue(),
41+
},
42+
expected: &tfprotov5.ReadDataSourceRequest{
43+
ProviderMeta: testTfprotov5DynamicValue(),
44+
},
45+
},
46+
"TypeName": {
47+
in: &tfplugin5.ReadDataSource_Request{
48+
TypeName: "test",
49+
},
50+
expected: &tfprotov5.ReadDataSourceRequest{
51+
TypeName: "test",
52+
},
53+
},
54+
}
55+
56+
for name, testCase := range testCases {
57+
name, testCase := name, testCase
58+
59+
t.Run(name, func(t *testing.T) {
60+
t.Parallel()
61+
62+
got := fromproto.ReadDataSourceRequest(testCase.in)
63+
64+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
65+
t.Errorf("unexpected difference: %s", diff)
66+
}
67+
})
68+
}
69+
}
70+
71+
func TestValidateDataSourceConfigRequest(t *testing.T) {
72+
t.Parallel()
73+
74+
testCases := map[string]struct {
75+
in *tfplugin5.ValidateDataSourceConfig_Request
76+
expected *tfprotov5.ValidateDataSourceConfigRequest
77+
}{
78+
"nil": {
79+
in: nil,
80+
expected: nil,
81+
},
82+
"zero": {
83+
in: &tfplugin5.ValidateDataSourceConfig_Request{},
84+
expected: &tfprotov5.ValidateDataSourceConfigRequest{},
85+
},
86+
"Config": {
87+
in: &tfplugin5.ValidateDataSourceConfig_Request{
88+
Config: testTfplugin5DynamicValue(),
89+
},
90+
expected: &tfprotov5.ValidateDataSourceConfigRequest{
91+
Config: testTfprotov5DynamicValue(),
92+
},
93+
},
94+
"TypeName": {
95+
in: &tfplugin5.ValidateDataSourceConfig_Request{
96+
TypeName: "test",
97+
},
98+
expected: &tfprotov5.ValidateDataSourceConfigRequest{
99+
TypeName: "test",
100+
},
101+
},
102+
}
103+
104+
for name, testCase := range testCases {
105+
name, testCase := name, testCase
106+
107+
t.Run(name, func(t *testing.T) {
108+
t.Parallel()
109+
110+
got := fromproto.ValidateDataSourceConfigRequest(testCase.in)
111+
112+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
113+
t.Errorf("unexpected difference: %s", diff)
114+
}
115+
})
116+
}
117+
}

tfprotov5/internal/fromproto/diagnostic.go

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// Package fromproto converts Protocol Buffers generated tfplugin5 types into
5+
// terraform-plugin-go tfprotov5 types.
6+
package fromproto

tfprotov5/internal/fromproto/types.go renamed to tfprotov5/internal/fromproto/dynamic_value.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ import (
99
)
1010

1111
func DynamicValue(in *tfplugin5.DynamicValue) *tfprotov5.DynamicValue {
12-
return &tfprotov5.DynamicValue{
12+
if in == nil {
13+
return nil
14+
}
15+
16+
resp := &tfprotov5.DynamicValue{
1317
MsgPack: in.Msgpack,
1418
JSON: in.Json,
1519
}
20+
21+
return resp
1622
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto_test
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/toproto"
10+
"github.com/hashicorp/terraform-plugin-go/tftypes"
11+
)
12+
13+
func testTfplugin5DynamicValue() *tfplugin5.DynamicValue {
14+
return toproto.DynamicValue(testTfprotov5DynamicValue())
15+
}
16+
17+
func testTfprotov5DynamicValue() *tfprotov5.DynamicValue {
18+
dynamicValue, err := tfprotov5.NewDynamicValue(
19+
tftypes.Object{},
20+
tftypes.NewValue(tftypes.Object{}, nil),
21+
)
22+
23+
if err != nil {
24+
panic("unable to create DynamicValue: " + err.Error())
25+
}
26+
27+
return &dynamicValue
28+
}

tfprotov5/internal/fromproto/function.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
99
)
1010

11-
func CallFunctionRequest(in *tfplugin5.CallFunction_Request) (*tfprotov5.CallFunctionRequest, error) {
11+
func CallFunctionRequest(in *tfplugin5.CallFunction_Request) *tfprotov5.CallFunctionRequest {
1212
if in == nil {
13-
return nil, nil
13+
return nil
1414
}
1515

1616
resp := &tfprotov5.CallFunctionRequest{
@@ -22,15 +22,15 @@ func CallFunctionRequest(in *tfplugin5.CallFunction_Request) (*tfprotov5.CallFun
2222
resp.Arguments = append(resp.Arguments, DynamicValue(argument))
2323
}
2424

25-
return resp, nil
25+
return resp
2626
}
2727

28-
func GetFunctionsRequest(in *tfplugin5.GetFunctions_Request) (*tfprotov5.GetFunctionsRequest, error) {
28+
func GetFunctionsRequest(in *tfplugin5.GetFunctions_Request) *tfprotov5.GetFunctionsRequest {
2929
if in == nil {
30-
return nil, nil
30+
return nil
3131
}
3232

3333
resp := &tfprotov5.GetFunctionsRequest{}
3434

35-
return resp, nil
35+
return resp
3636
}

0 commit comments

Comments
 (0)