|
4 | 4 | package toproto |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "errors" |
| 7 | + "fmt" |
8 | 8 |
|
9 | 9 | "github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5" |
10 | 10 | "github.com/hashicorp/terraform-plugin-go/tftypes" |
11 | 11 | ) |
12 | 12 |
|
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 { |
16 | 14 | 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 |
24 | 16 | } |
25 | 17 |
|
26 | 18 | resp := &tfplugin5.AttributePath{ |
27 | | - Steps: steps, |
| 19 | + Steps: AttributePath_Steps(in.Steps()), |
28 | 20 | } |
29 | 21 |
|
30 | | - return resp, nil |
| 22 | + return resp |
31 | 23 | } |
32 | 24 |
|
33 | | -func AttributePaths(in []*tftypes.AttributePath) ([]*tfplugin5.AttributePath, error) { |
| 25 | +func AttributePaths(in []*tftypes.AttributePath) []*tfplugin5.AttributePath { |
34 | 26 | resp := make([]*tfplugin5.AttributePath, 0, len(in)) |
35 | 27 |
|
36 | 28 | 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)) |
44 | 30 | } |
45 | 31 |
|
46 | | - return resp, nil |
| 32 | + return resp |
47 | 33 | } |
48 | 34 |
|
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 |
56 | 38 | } |
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 | + }, |
60 | 46 | } |
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 | + }, |
66 | 52 | } |
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 |
74 | 64 | } |
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)) |
76 | 74 | } |
77 | 75 |
|
78 | | -func AttributePath_Steps(in []tftypes.AttributePathStep) ([]*tfplugin5.AttributePath_Step, error) { |
| 76 | +func AttributePath_Steps(in []tftypes.AttributePathStep) []*tfplugin5.AttributePath_Step { |
79 | 77 | resp := make([]*tfplugin5.AttributePath_Step, 0, len(in)) |
| 78 | + |
80 | 79 | 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. |
92 | 84 | if s == nil { |
93 | | - return resp, nil |
| 85 | + return resp |
94 | 86 | } |
| 87 | + |
95 | 88 | resp = append(resp, s) |
96 | 89 | } |
97 | | - return resp, nil |
| 90 | + |
| 91 | + return resp |
98 | 92 | } |
0 commit comments