Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: Initial Path Expression Support #396

Merged
merged 5 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changelog/396.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:feature
path: Introduced attribute path expressions
```

```release-note:enhancement
tfsdk: Added `AttributePathExpression` field to `ModifyAttributePlanRequest` and `ValidateAttributeRequest` types
```

```release-note:enhancement
tfsdk: Added `PathMatches` method to `Config`, `Plan`, and `State` types
```
35 changes: 35 additions & 0 deletions internal/fromtftypes/attribute_path_step.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fromtftypes

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// AttributePathStep returns the path.PathStep equivalent of a
// tftypes.AttributePathStep. An error is returned instead of diag.Diagnostics
// so callers can include appropriate logical context about when the error
// occurred.
func AttributePathStep(ctx context.Context, tfType tftypes.AttributePathStep, attrType attr.Type) (path.PathStep, error) {
switch tfType := tfType.(type) {
case tftypes.AttributeName:
return path.PathStepAttributeName(string(tfType)), nil
case tftypes.ElementKeyInt:
return path.PathStepElementKeyInt(int64(tfType)), nil
case tftypes.ElementKeyString:
return path.PathStepElementKeyString(string(tfType)), nil
case tftypes.ElementKeyValue:
attrValue, err := Value(ctx, tftypes.Value(tfType), attrType)

if err != nil {
return nil, fmt.Errorf("unable to create PathStepElementKeyValue from tftypes.Value: %w", err)
}

return path.PathStepElementKeyValue{Value: attrValue}, nil
default:
return nil, fmt.Errorf("unknown tftypes.AttributePathStep: %#v", tfType)
}
}
83 changes: 83 additions & 0 deletions internal/fromtftypes/attribute_path_step_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package fromtftypes_test

import (
"context"
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fromtftypes"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestAttributePathStep(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
tfType tftypes.AttributePathStep
attrType attr.Type
expected path.PathStep
expectedError error
}{
"nil": {
tfType: nil,
expected: nil,
expectedError: fmt.Errorf("unknown tftypes.AttributePathStep: <nil>"),
},
"PathStepAttributeName": {
tfType: tftypes.AttributeName("test"),
expected: path.PathStepAttributeName("test"),
},
"PathStepElementKeyInt": {
tfType: tftypes.ElementKeyInt(1),
expected: path.PathStepElementKeyInt(1),
},
"PathStepElementKeyString": {
tfType: tftypes.ElementKeyString("test"),
expected: path.PathStepElementKeyString("test"),
},
"PathStepElementKeyValue": {
tfType: tftypes.ElementKeyValue(tftypes.NewValue(tftypes.String, "test")),
attrType: types.StringType,
expected: path.PathStepElementKeyValue{Value: types.String{Value: "test"}},
},
"PathStepElementKeyValue-error": {
tfType: tftypes.ElementKeyValue(tftypes.NewValue(tftypes.String, "test")),
attrType: types.BoolType,
expected: nil,
expectedError: fmt.Errorf("unable to create PathStepElementKeyValue from tftypes.Value: unable to convert tftypes.Value (tftypes.String<\"test\">) to attr.Value: can't unmarshal tftypes.String into *bool, expected boolean"),
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got, err := fromtftypes.AttributePathStep(context.Background(), testCase.tfType, testCase.attrType)

if err != nil {
if testCase.expectedError == nil {
t.Fatalf("expected no error, got: %s", err)
}

if !strings.Contains(err.Error(), testCase.expectedError.Error()) {
t.Fatalf("expected error %q, got: %s", testCase.expectedError, err)
}
}

if err == nil && testCase.expectedError != nil {
t.Fatalf("got no error, tfType: %s", testCase.expectedError)
}

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}
3 changes: 3 additions & 0 deletions internal/fromtftypes/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package fromtftypes contains functions to convert from terraform-plugin-go
// tftypes types to framework types.
package fromtftypes
24 changes: 24 additions & 0 deletions internal/fromtftypes/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fromtftypes

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Value returns the attr.Value equivalent to the tftypes.Value.
func Value(ctx context.Context, tfType tftypes.Value, attrType attr.Type) (attr.Value, error) {
if attrType == nil {
return nil, fmt.Errorf("unable to convert tftypes.Value (%s) to attr.Value: missing attr.Type", tfType.String())
}

attrValue, err := attrType.ValueFromTerraform(ctx, tfType)

if err != nil {
return nil, fmt.Errorf("unable to convert tftypes.Value (%s) to attr.Value: %w", tfType.String(), err)
}

return attrValue, nil
}
Loading