Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfeng-db committed Aug 21, 2024
1 parent 457dc94 commit 6b38834
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions pluginframework/tfschema/customizable_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package tfschema

import (
"fmt"
)

type CustomizableSchemaPluginFramework struct {
attr AttributeBuilder
}

func ConstructCustomizableSchema(attributes map[string]AttributeBuilder) *CustomizableSchemaPluginFramework {
attr := AttributeBuilder(SingleNestedAttribute{Attributes: attributes})
return &CustomizableSchemaPluginFramework{attr: attr}
}

// Converts CustomizableSchema into a map from string to Attribute.
func (s *CustomizableSchemaPluginFramework) ToAttributeMap() map[string]AttributeBuilder {
return attributeToMap(&s.attr)
}

func attributeToMap(attr *AttributeBuilder) map[string]AttributeBuilder {
var m map[string]AttributeBuilder
switch attr := (*attr).(type) {
case SingleNestedAttribute:
m = attr.Attributes
case ListNestedAttribute:
m = attr.NestedObject.Attributes
case MapNestedAttribute:
m = attr.NestedObject.Attributes
default:
panic(fmt.Errorf("cannot convert to map, attribute is not nested"))
}

return m
}

func (s *CustomizableSchemaPluginFramework) AddValidator(v any, path ...string) *CustomizableSchemaPluginFramework {
cb := func(attr AttributeBuilder) AttributeBuilder {
return attr.AddValidators(v)

Check failure on line 39 in pluginframework/tfschema/customizable_schema.go

View workflow job for this annotation

GitHub Actions / tests

attr.AddValidators undefined (type AttributeBuilder has no field or method AddValidators) (compile)
}

navigateSchemaWithCallback(&s.attr, cb, path...)

return s
}

func (s *CustomizableSchemaPluginFramework) SetOptional(path ...string) *CustomizableSchemaPluginFramework {
cb := func(attr AttributeBuilder) AttributeBuilder {
return attr.SetOptional()
}

navigateSchemaWithCallback(&s.attr, cb, path...)

return s
}

func (s *CustomizableSchemaPluginFramework) SetRequired(path ...string) *CustomizableSchemaPluginFramework {
cb := func(attr AttributeBuilder) AttributeBuilder {
return attr.SetRequired()
}

navigateSchemaWithCallback(&s.attr, cb, path...)

return s
}

func (s *CustomizableSchemaPluginFramework) SetSensitive(path ...string) *CustomizableSchemaPluginFramework {
cb := func(attr AttributeBuilder) AttributeBuilder {
return attr.SetSensitive()
}

navigateSchemaWithCallback(&s.attr, cb, path...)
return s
}

func (s *CustomizableSchemaPluginFramework) SetDeprecated(msg string, path ...string) *CustomizableSchemaPluginFramework {
cb := func(attr AttributeBuilder) AttributeBuilder {
return attr.SetDeprecated(msg)
}

navigateSchemaWithCallback(&s.attr, cb, path...)

return s
}

func (s *CustomizableSchemaPluginFramework) SetComputed(path ...string) *CustomizableSchemaPluginFramework {
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 *CustomizableSchemaPluginFramework) SetReadOnly(path ...string) *CustomizableSchemaPluginFramework {
cb := func(attr AttributeBuilder) AttributeBuilder {
return attr.SetReadOnly()
}

navigateSchemaWithCallback(&s.attr, cb, path...)

return s
}

// Helper function for navigating through schema attributes, 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)
}

0 comments on commit 6b38834

Please sign in to comment.