-
Notifications
You must be signed in to change notification settings - Fork 39
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
Construct Template Enhancements #993
Merged
Merged
Changes from all commits
Commits
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
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
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
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
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package construct | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/klothoplatform/klotho/pkg/reflectutil" | ||
|
||
"github.com/klothoplatform/klotho/pkg/set" | ||
"github.com/klothoplatform/klotho/pkg/yaml_util" | ||
) | ||
|
@@ -25,14 +28,22 @@ func (r *Resource) SetProperty(pathStr string, value any) error { | |
} | ||
|
||
// GetProperty is a wrapper around [PropertyPath.Get] for convenience. | ||
// It returns ErrPropertyDoesNotExist if the property does not exist. | ||
func (r *Resource) GetProperty(pathStr string) (any, error) { | ||
path, err := r.PropertyPath(pathStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return path.Get(), nil | ||
if value, ok := path.Get(); ok { | ||
return value, nil | ||
} | ||
// Backwards compatibility: if the property does not exist, return nil instead of an error. | ||
return nil, nil | ||
} | ||
|
||
// ErrPropertyDoesNotExist is returned when a property does not exist. | ||
var ErrPropertyDoesNotExist = errors.New("property does not exist") | ||
|
||
// AppendProperty is a wrapper around [PropertyPath.Append] for convenience. | ||
func (r *Resource) AppendProperty(pathStr string, value any) error { | ||
path, err := r.PropertyPath(pathStr) | ||
|
@@ -51,6 +62,13 @@ func (r *Resource) RemoveProperty(pathStr string, value any) error { | |
return path.Remove(value) | ||
} | ||
|
||
func (r *Resource) PropertyPath(pathStr string) (PropertyPath, error) { | ||
if r.Properties == nil { | ||
r.Properties = Properties{} | ||
} | ||
return r.Properties.PropertyPath(pathStr) | ||
} | ||
|
||
func (p Properties) Equals(other any) (equal bool) { | ||
otherProps, ok := other.(Properties) | ||
if !ok { | ||
|
@@ -67,8 +85,8 @@ func (p Properties) Equals(other any) (equal bool) { | |
equal = false | ||
return StopWalk | ||
} | ||
v := path.Get() | ||
otherV := otherPath.Get() | ||
v, _ := path.Get() | ||
otherV, _ := otherPath.Get() | ||
Comment on lines
+88
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this check the error to see if they match? Eg, if both are non-existent |
||
|
||
if v == nil || otherV == nil { | ||
equal = v == otherV | ||
|
@@ -93,9 +111,44 @@ func (p Properties) Equals(other any) (equal bool) { | |
return equal | ||
} | ||
|
||
func (p Properties) SetProperty(pathStr string, value any) error { | ||
path, err := p.PropertyPath(pathStr) | ||
if err != nil { | ||
return err | ||
} | ||
return path.Set(value) | ||
} | ||
|
||
func (p *Properties) GetProperty(pathStr string) (any, error) { | ||
path, err := p.PropertyPath(pathStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if value, ok := path.Get(); ok { | ||
return value, nil | ||
} | ||
return nil, ErrPropertyDoesNotExist | ||
} | ||
|
||
func (p Properties) AppendProperty(pathStr string, value any) error { | ||
path, err := p.PropertyPath(pathStr) | ||
if err != nil { | ||
return err | ||
} | ||
return path.Append(value) | ||
} | ||
|
||
func (p Properties) RemoveProperty(pathStr string, value any) error { | ||
path, err := p.PropertyPath(pathStr) | ||
if err != nil { | ||
return err | ||
} | ||
return path.Remove(value) | ||
} | ||
|
||
type ( | ||
PropertyPathItem interface { | ||
Get() any | ||
Get() (value any, ok bool) | ||
DavidSeptimus-Klotho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Set(value any) error | ||
Remove(value any) error | ||
Append(value any) error | ||
|
@@ -126,54 +179,10 @@ type ( | |
} | ||
) | ||
|
||
func splitPath(path string) []string { | ||
var parts []string | ||
bracket := 0 | ||
lastPartIdx := 0 | ||
for i := 0; i < len(path); i++ { | ||
switch path[i] { | ||
case '.': | ||
if bracket == 0 { | ||
if i > lastPartIdx { | ||
parts = append(parts, path[lastPartIdx:i]) | ||
} | ||
lastPartIdx = i | ||
} | ||
|
||
case '[': | ||
if bracket == 0 { | ||
if i > lastPartIdx { | ||
parts = append(parts, path[lastPartIdx:i]) | ||
} | ||
lastPartIdx = i | ||
} | ||
bracket++ | ||
|
||
case ']': | ||
bracket-- | ||
if bracket == 0 { | ||
parts = append(parts, path[lastPartIdx:i+1]) | ||
lastPartIdx = i + 1 | ||
} | ||
} | ||
if i == len(path)-1 && lastPartIdx <= i { | ||
parts = append(parts, path[lastPartIdx:]) | ||
} | ||
} | ||
return parts | ||
} | ||
|
||
func (r *Resource) PropertyPath(pathStr string) (PropertyPath, error) { | ||
if r.Properties == nil { | ||
r.Properties = Properties{} | ||
} | ||
return r.Properties.PropertyPath(pathStr) | ||
} | ||
|
||
// PropertyPath interprets a string path to index (potentially deeply) into [Resource.Properties] | ||
// which can be used to get, set, append, or remove values. | ||
func (p Properties) PropertyPath(pathStr string) (PropertyPath, error) { | ||
pathParts := splitPath(pathStr) | ||
pathParts := reflectutil.SplitPath(pathStr) | ||
if len(pathParts) == 0 { | ||
return nil, fmt.Errorf("empty path") | ||
} | ||
|
@@ -512,15 +521,15 @@ func (i *mapValuePathItem) Remove(value any) (err error) { | |
return nil | ||
} | ||
|
||
func (i *mapValuePathItem) Get() any { | ||
func (i *mapValuePathItem) Get() (any, bool) { | ||
if !i.m.IsValid() { | ||
return nil | ||
return nil, false | ||
} | ||
v := i.m.MapIndex(i.key) | ||
if !v.IsValid() { | ||
return nil | ||
return nil, false | ||
} | ||
return v.Interface() | ||
return v.Interface(), true | ||
} | ||
|
||
func (i *mapValuePathItem) parent() PropertyPathItem { | ||
|
@@ -531,8 +540,8 @@ func (i *mapValuePathItem) Key() PropertyPathItem { | |
return (*mapKeyPathItem)(i) | ||
} | ||
|
||
func (i *mapKeyPathItem) Get() any { | ||
return i.key.Interface() | ||
func (i *mapKeyPathItem) Get() (any, bool) { | ||
return i.key.Interface(), true | ||
} | ||
|
||
func (i *mapKeyPathItem) Set(value any) (err error) { | ||
|
@@ -605,8 +614,14 @@ func (i *arrayIndexPathItem) Remove(value any) (err error) { | |
return nil | ||
} | ||
|
||
func (i *arrayIndexPathItem) Get() any { | ||
return i.a.Index(i.index).Interface() | ||
func (i *arrayIndexPathItem) Get() (any, bool) { | ||
if !i.a.IsValid() || !reflectutil.IsAnyOf(reflectutil.GetConcreteElement(i.a), reflect.Slice, reflect.Array) { | ||
return nil, false | ||
} | ||
if i.a.Len() <= i.index { | ||
return nil, false | ||
} | ||
return i.a.Index(i.index).Interface(), true | ||
} | ||
|
||
func (i *arrayIndexPathItem) parent() PropertyPathItem { | ||
|
@@ -624,14 +639,14 @@ func (i PropertyPath) Append(value any) error { | |
} | ||
|
||
// Remove removes the value at this path item. If value is nil, it is interpreted | ||
// to remove the item itself. Non-nil value'd remove is only supported on array items, to | ||
// to remove the item itself. Non-nil valued remove is only supported on array items, to | ||
// remove a value from the array. | ||
func (i PropertyPath) Remove(value any) error { | ||
return i[len(i)-1].Remove(value) | ||
} | ||
|
||
// Get returns the value at this path item. | ||
func (i PropertyPath) Get() any { | ||
func (i PropertyPath) Get() (any, bool) { | ||
return i[len(i)-1].Get() | ||
} | ||
|
||
|
@@ -700,7 +715,7 @@ func (r *Resource) WalkProperties(fn WalkPropertiesFunc) error { | |
} | ||
|
||
// WalkProperties walks the properties of the resource, calling fn for each property. If fn returns | ||
// SkipProperty, the property and its decendants (if a map or array type) is skipped. If fn returns | ||
// SkipProperty, the property and its descendants (if a map or array type) are skipped. If fn returns | ||
// StopWalk, the walk is stopped. | ||
// NOTE: does not walk over the _keys_ of any maps, only values. | ||
func (p Properties) WalkProperties(fn WalkPropertiesFunc) error { | ||
|
@@ -733,7 +748,11 @@ func (p Properties) WalkProperties(fn WalkPropertiesFunc) error { | |
} | ||
|
||
added := make(set.Set[string]) | ||
v := reflect.ValueOf(current.Get()) | ||
rv, ok := current.Get() | ||
if !ok { | ||
continue | ||
} | ||
v := reflect.ValueOf(rv) | ||
switch v.Kind() { | ||
case reflect.Map: | ||
keys, err := mapKeys(v) | ||
|
Oops, something went wrong.
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.
Was this causing problems? I don't mind breaking and internal API's backwards compatibility. If it does break the functional tests, then can you throw up a ticket for this cleanup?
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.
IIRC, I did this, so I wouldn't have to introduce error handling in places that were relying on the previous behavior.