-
Notifications
You must be signed in to change notification settings - Fork 398
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
[Internal] Add CustomizableSchema for Plugin Framework #3927
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
internal/pluginframework/tfschema/customizable_schema.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package tfschema | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/databricks/terraform-provider-databricks/common" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
// CustomizableSchema is a wrapper struct on top of AttributeBuilder that can be used to navigate through nested schema add customizations. | ||
type CustomizableSchema struct { | ||
attr AttributeBuilder | ||
} | ||
|
||
// ConstructCustomizableSchema constructs a CustomizableSchema given a map from string to AttributeBuilder. | ||
func ConstructCustomizableSchema(attributes map[string]AttributeBuilder) *CustomizableSchema { | ||
attr := AttributeBuilder(SingleNestedAttributeBuilder{Attributes: attributes}) | ||
return &CustomizableSchema{attr: attr} | ||
} | ||
|
||
// ToAttributeMap converts CustomizableSchema into a map from string to Attribute. | ||
func (s *CustomizableSchema) ToAttributeMap() map[string]AttributeBuilder { | ||
return attributeToMap(&s.attr) | ||
} | ||
|
||
// attributeToMap converts AttributeBuilder into a map from string to AttributeBuilder. | ||
func attributeToMap(attr *AttributeBuilder) map[string]AttributeBuilder { | ||
var m map[string]AttributeBuilder | ||
switch attr := (*attr).(type) { | ||
case SingleNestedAttributeBuilder: | ||
m = attr.Attributes | ||
case ListNestedAttributeBuilder: | ||
m = attr.NestedObject.Attributes | ||
case MapNestedAttributeBuilder: | ||
m = attr.NestedObject.Attributes | ||
default: | ||
panic(fmt.Errorf("cannot convert to map, attribute is not nested")) | ||
} | ||
|
||
return m | ||
} | ||
|
||
func (s *CustomizableSchema) AddValidator(v any, path ...string) *CustomizableSchema { | ||
cb := func(attr AttributeBuilder) AttributeBuilder { | ||
switch a := attr.(type) { | ||
case BoolAttributeBuilder: | ||
return a.AddValidator(v.(validator.Bool)) | ||
case Float64AttributeBuilder: | ||
return a.AddValidator(v.(validator.Float64)) | ||
case Int64AttributeBuilder: | ||
return a.AddValidator(v.(validator.Int64)) | ||
case ListAttributeBuilder: | ||
return a.AddValidator(v.(validator.List)) | ||
case ListNestedAttributeBuilder: | ||
return a.AddValidator(v.(validator.List)) | ||
case MapAttributeBuilder: | ||
return a.AddValidator(v.(validator.Map)) | ||
case MapNestedAttributeBuilder: | ||
return a.AddValidator(v.(validator.Map)) | ||
case SingleNestedAttributeBuilder: | ||
return a.AddValidator(v.(validator.Object)) | ||
case StringAttributeBuilder: | ||
return a.AddValidator(v.(validator.String)) | ||
default: | ||
panic(fmt.Errorf("cannot add validator, attribute builder type is invalid. %s", common.TerraformBugErrorMessage)) | ||
edwardfeng-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
navigateSchemaWithCallback(&s.attr, cb, path...) | ||
|
||
return s | ||
} | ||
|
||
func (s *CustomizableSchema) SetOptional(path ...string) *CustomizableSchema { | ||
cb := func(attr AttributeBuilder) AttributeBuilder { | ||
return attr.SetOptional() | ||
} | ||
|
||
navigateSchemaWithCallback(&s.attr, cb, path...) | ||
|
||
return s | ||
} | ||
|
||
func (s *CustomizableSchema) SetRequired(path ...string) *CustomizableSchema { | ||
cb := func(attr AttributeBuilder) AttributeBuilder { | ||
return attr.SetRequired() | ||
} | ||
|
||
navigateSchemaWithCallback(&s.attr, cb, path...) | ||
|
||
return s | ||
} | ||
|
||
func (s *CustomizableSchema) SetSensitive(path ...string) *CustomizableSchema { | ||
cb := func(attr AttributeBuilder) AttributeBuilder { | ||
return attr.SetSensitive() | ||
} | ||
|
||
navigateSchemaWithCallback(&s.attr, cb, path...) | ||
return s | ||
} | ||
|
||
func (s *CustomizableSchema) SetDeprecated(msg string, path ...string) *CustomizableSchema { | ||
cb := func(attr AttributeBuilder) AttributeBuilder { | ||
return attr.SetDeprecated(msg) | ||
} | ||
|
||
navigateSchemaWithCallback(&s.attr, cb, path...) | ||
|
||
return s | ||
} | ||
|
||
func (s *CustomizableSchema) SetComputed(path ...string) *CustomizableSchema { | ||
cb := func(attr AttributeBuilder) AttributeBuilder { | ||
return attr.SetComputed() | ||
} | ||
|
||
navigateSchemaWithCallback(&s.attr, cb, path...) | ||
return s | ||
} | ||
|
||
// SetReadOnly sets the schema to be read-only (i.e. computed, non-optional). | ||
// This should be used for fields that are not user-configurable but are returned | ||
// by the platform. | ||
func (s *CustomizableSchema) SetReadOnly(path ...string) *CustomizableSchema { | ||
cb := func(attr AttributeBuilder) AttributeBuilder { | ||
return attr.SetReadOnly() | ||
} | ||
|
||
navigateSchemaWithCallback(&s.attr, cb, path...) | ||
|
||
return s | ||
} | ||
|
||
// navigateSchemaWithCallback navigates through schema attributes and executes callback on the target, panics if path does not exist or invalid. | ||
func navigateSchemaWithCallback(s *AttributeBuilder, cb func(AttributeBuilder) AttributeBuilder, path ...string) (AttributeBuilder, error) { | ||
current_scm := s | ||
for i, p := range path { | ||
m := attributeToMap(current_scm) | ||
|
||
v, ok := m[p] | ||
if !ok { | ||
return nil, fmt.Errorf("missing key %s", p) | ||
} | ||
|
||
if i == len(path)-1 { | ||
m[p] = cb(v) | ||
return m[p], nil | ||
} | ||
current_scm = &v | ||
} | ||
return nil, fmt.Errorf("path %v is incomplete", path) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to find a common interface for all of the validators but it seems like it's hard because each one of these has a separate method like
ValidateFloat64
orValidateBool
doc linkThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mgyucht maybe I missed something here, let me know if you have ideas, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this is the right way to do this. This is the reason we couldn't include the AddValidator method in the AttributeBuilder interface.