Skip to content

Commit

Permalink
More PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSeptimus-Klotho committed Aug 17, 2024
1 parent 6a7724a commit dc177a1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 57 deletions.
7 changes: 4 additions & 3 deletions pkg/construct/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package construct
import (
"errors"
"fmt"
"github.com/klothoplatform/klotho/pkg/reflectutil"
"reflect"
"sort"
"strconv"
"strings"

"github.com/klothoplatform/klotho/pkg/reflectutil"

"github.com/klothoplatform/klotho/pkg/set"
"github.com/klothoplatform/klotho/pkg/yaml_util"
)
Expand Down Expand Up @@ -638,7 +639,7 @@ 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)
Expand Down Expand Up @@ -714,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 {
Expand Down
44 changes: 23 additions & 21 deletions pkg/k2/constructs/construct_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"errors"
"fmt"

"github.com/klothoplatform/klotho/pkg/async"
template2 "github.com/klothoplatform/klotho/pkg/k2/constructs/template"
inputs2 "github.com/klothoplatform/klotho/pkg/k2/constructs/template/property"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/property"

"reflect"
"slices"
"strings"
Expand Down Expand Up @@ -102,7 +104,7 @@ func (ce *ConstructEvaluator) evaluateInputRules(o InfraOwner) error {
return nil
}

func (ce *ConstructEvaluator) evaluateInputRule(dv *DynamicValueData, rule template2.InputRuleTemplate) error {
func (ce *ConstructEvaluator) evaluateInputRule(dv *DynamicValueData, rule template.InputRuleTemplate) error {
if rule.ForEach != "" {
return ce.evaluateForEachRule(dv, rule)
}
Expand Down Expand Up @@ -400,11 +402,11 @@ func (ce *ConstructEvaluator) evaluateResources(o InfraOwner) error {
}

ri := o.GetTemplateResourcesIterator()
ri.ForEach(func(key string, resource template2.ResourceTemplate) error {
ri.ForEach(func(key string, resource template.ResourceTemplate) error {
var r *Resource
r, err = ce.resolveResource(dv, key, resource)
if err != nil {
return template2.StopIteration
return template.StopIteration
}
o.SetResource(key, r)
return nil
Expand All @@ -415,7 +417,7 @@ func (ce *ConstructEvaluator) evaluateResources(o InfraOwner) error {
return nil
}

func GetPropertyFunc(ps *template2.PropertySource, path string) func(string) any {
func GetPropertyFunc(ps *template.PropertySource, path string) func(string) any {
return func(key string) any {
i, ok := ps.GetProperty(fmt.Sprintf("%s.%s", path, key))
if !ok {
Expand All @@ -425,7 +427,7 @@ func GetPropertyFunc(ps *template2.PropertySource, path string) func(string) any
}
}

func (ce *ConstructEvaluator) evaluateForEachRule(dv *DynamicValueData, rule template2.InputRuleTemplate) error {
func (ce *ConstructEvaluator) evaluateForEachRule(dv *DynamicValueData, rule template.InputRuleTemplate) error {
parentPrefix := dv.resourceKeyPrefix

ctx := DynamicValueContext{
Expand Down Expand Up @@ -463,7 +465,7 @@ func (ce *ConstructEvaluator) evaluateForEachRule(dv *DynamicValueData, rule tem
}

ri := rule.Do.ResourcesIterator()
ri.ForEach(func(key string, resource template2.ResourceTemplate) error {
ri.ForEach(func(key string, resource template.ResourceTemplate) error {
if dv.resourceKeyPrefix != "" {
key = fmt.Sprintf("%s.%s", dv.resourceKeyPrefix, key)
}
Expand Down Expand Up @@ -495,7 +497,7 @@ func (ce *ConstructEvaluator) evaluateForEachRule(dv *DynamicValueData, rule tem
return nil
}

func (ce *ConstructEvaluator) evaluateIfRule(dv *DynamicValueData, rule template2.InputRuleTemplate) error {
func (ce *ConstructEvaluator) evaluateIfRule(dv *DynamicValueData, rule template.InputRuleTemplate) error {
parentPrefix := dv.resourceKeyPrefix

prefix, err := ce.interpolateValue(dv, rule.Prefix)
Expand Down Expand Up @@ -531,15 +533,15 @@ func (ce *ConstructEvaluator) evaluateIfRule(dv *DynamicValueData, rule template
}
executeThen := boolResult

var body template2.ConditionalExpressionTemplate
var body template.ConditionalExpressionTemplate
if executeThen && rule.Then != nil {
body = *rule.Then
} else if rule.Else != nil {
body = *rule.Else
}

ri := body.ResourcesIterator()
ri.ForEach(func(key string, resource template2.ResourceTemplate) error {
ri.ForEach(func(key string, resource template.ResourceTemplate) error {
if dv.resourceKeyPrefix != "" {
key = fmt.Sprintf("%s.%s", dv.resourceKeyPrefix, key)
}
Expand Down Expand Up @@ -569,7 +571,7 @@ func (ce *ConstructEvaluator) evaluateIfRule(dv *DynamicValueData, rule template
return nil
}

func (ce *ConstructEvaluator) resolveResource(dv *DynamicValueData, key string, rt template2.ResourceTemplate) (*Resource, error) {
func (ce *ConstructEvaluator) resolveResource(dv *DynamicValueData, key string, rt template.ResourceTemplate) (*Resource, error) {
// update the resource if it already exists
if dv.currentOwner == nil {
return nil, fmt.Errorf("current owner is nil")
Expand All @@ -584,7 +586,7 @@ func (ce *ConstructEvaluator) resolveResource(dv *DynamicValueData, key string,
return nil, fmt.Errorf("could not interpolate resource %s: %w", key, err)
}

resTmpl := tmpl.(template2.ResourceTemplate)
resTmpl := tmpl.(template.ResourceTemplate)
typeParts := strings.Split(resTmpl.Type, ":")
if len(typeParts) != 2 && resTmpl.Type != "" {
return nil, fmt.Errorf("invalid resource type: %s", resTmpl.Type)
Expand Down Expand Up @@ -633,7 +635,7 @@ func (ce *ConstructEvaluator) resolveResource(dv *DynamicValueData, key string,
return resource, nil
}

func (ce *ConstructEvaluator) resolveEdge(dv *DynamicValueData, edge template2.EdgeTemplate) (*Edge, error) {
func (ce *ConstructEvaluator) resolveEdge(dv *DynamicValueData, edge template.EdgeTemplate) (*Edge, error) {
from, err := ce.interpolateValue(dv, edge.From)
if err != nil {
return nil, err
Expand All @@ -654,15 +656,15 @@ func (ce *ConstructEvaluator) resolveEdge(dv *DynamicValueData, edge template2.E
}

return &Edge{
From: from.(template2.ResourceRef),
To: to.(template2.ResourceRef),
From: from.(template.ResourceRef),
To: to.(template.ResourceRef),
Data: data.(construct.EdgeData),
}, nil
}

func (ce *ConstructEvaluator) evaluateOutputs(o InfraOwner) error {
// sort the keys of the outputs alphabetically to ensure deterministic ordering
sortKeys := func(m map[string]template2.OutputTemplate) []string {
sortKeys := func(m map[string]template.OutputTemplate) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
Expand All @@ -684,15 +686,15 @@ func (ce *ConstructEvaluator) evaluateOutputs(o InfraOwner) error {
return fmt.Errorf("failed to interpolate value for output %s: %w", key, err)
}

outputTemplate, ok := output.(template2.OutputTemplate)
outputTemplate, ok := output.(template.OutputTemplate)
if !ok {
return fmt.Errorf("invalid output template for output %s", key)
}

var value any
var ref construct.PropertyRef

r, ok := outputTemplate.Value.(template2.ResourceRef)
r, ok := outputTemplate.Value.(template.ResourceRef)
if !ok {
value = outputTemplate.Value
} else {
Expand Down Expand Up @@ -741,13 +743,13 @@ func (ce *ConstructEvaluator) convertInputs(inputs map[string]model.Input) (cons
}

type HasInputs interface {
ForEachInput(f func(input inputs2.Property) error) error
ForEachInput(f func(input property.Property) error) error
GetInputs() construct.Properties
}

func (ce *ConstructEvaluator) initializeInputs(c HasInputs, i construct.Properties) error {
var inputErrors error
_ = c.ForEachInput(func(input inputs2.Property) error {
_ = c.ForEachInput(func(input property.Property) error {
v, err := i.GetProperty(input.Details().Path)
if err == nil {
if (v == nil || v == input.ZeroValue()) && input.Details().Required {
Expand Down
5 changes: 3 additions & 2 deletions pkg/k2/constructs/construct_evaluator_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package constructs

import (
"github.com/klothoplatform/klotho/pkg/k2/constructs/template"
"reflect"
"testing"

"github.com/klothoplatform/klotho/pkg/k2/constructs/template"

"github.com/klothoplatform/klotho/pkg/construct"
"github.com/klothoplatform/klotho/pkg/k2/model"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -305,7 +306,7 @@ func TestInterpolateValue(t *testing.T) {
},
{
name: "Go template evaluated before interpolation string",
rawValue: "Hello ${inputs:{{\"stringInput\"}}}",
rawValue: `Hello ${inputs:{{"stringInput"}}}`,
data: DynamicValueData{currentOwner: mockConstruct},
expected: "Hello Hello",
},
Expand Down
7 changes: 3 additions & 4 deletions pkg/k2/constructs/construct_marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package constructs

import (
"fmt"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template"
"github.com/klothoplatform/klotho/pkg/reflectutil"
"reflect"
"sort"

"github.com/klothoplatform/klotho/pkg/k2/constructs/template"
"github.com/klothoplatform/klotho/pkg/reflectutil"

"github.com/klothoplatform/klotho/pkg/construct"
"github.com/klothoplatform/klotho/pkg/engine/constraints"
"github.com/klothoplatform/klotho/pkg/k2/model"
Expand Down Expand Up @@ -217,8 +218,6 @@ func (m *ConstructMarshaller) marshalRefs(o InfraOwner, rawVal any) (any, error)
}
ref.Index(i).Set(reflect.ValueOf(serializedField))
}
default:
// Do nothing
}

return rawVal, nil
Expand Down
13 changes: 7 additions & 6 deletions pkg/k2/constructs/construct_marshaller_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package constructs

import (
"github.com/klothoplatform/klotho/pkg/k2/constructs/template"
"reflect"
"testing"

"github.com/klothoplatform/klotho/pkg/k2/constructs/template"
"github.com/stretchr/testify/require"

"github.com/klothoplatform/klotho/pkg/async"
"github.com/klothoplatform/klotho/pkg/construct"
"github.com/klothoplatform/klotho/pkg/engine/constraints"
Expand Down Expand Up @@ -521,12 +523,11 @@ func TestConstructMarshaller_marshalRefs(t *testing.T) {
ConstructEvaluator: evaluator,
}
got, err := marshaller.marshalRefs(tt.args.o, tt.args.rawVal)
if tt.wantErr && assert.Error(t, err, "ConstructMarshaller.marshalRefs() error = %v, wantErr %v", err, tt.wantErr) {
t.FailNow()
} else if !tt.wantErr && !assert.NoError(t, err, "ConstructMarshaller.marshalRefs() error = %v, wantErr %v", err, tt.wantErr) {
t.FailNow()
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}

if tt.name == "marshal unsupported type" {
// Since we can't compare function types directly, we use reflection to check the type
if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/k2/constructs/construct_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package constructs

import (
"testing"

"github.com/klothoplatform/klotho/pkg/k2/constructs/template"
properties2 "github.com/klothoplatform/klotho/pkg/k2/constructs/template/properties"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/property"
"github.com/klothoplatform/klotho/pkg/k2/model"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"testing"

"github.com/klothoplatform/klotho/pkg/construct"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -42,8 +44,8 @@ func TestGetInput(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, err := c.GetInputValue(tt.key)
if tt.wantErr && !assert.Error(t, err) {
assert.FailNow(t, "expected error")
if tt.wantErr {
require.Error(t, err)
}
assert.Equal(t, tt.expected, value)
})
Expand Down Expand Up @@ -99,9 +101,7 @@ resources:
properties:
prop2: value2
`)
if !assert.NoError(t, err) {
t.FailNow()
}
require.NoError(t, err)

c := &Construct{
ConstructTemplate: *mockTemplate,
Expand Down
27 changes: 12 additions & 15 deletions pkg/k2/constructs/dynamic_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"slices"
"strings"
"text/template"

"github.com/klothoplatform/klotho/pkg/async"
"github.com/klothoplatform/klotho/pkg/construct"
template2 "github.com/klothoplatform/klotho/pkg/k2/constructs/template"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/execution"
"github.com/klothoplatform/klotho/pkg/k2/model"
"github.com/klothoplatform/klotho/pkg/templateutils"
"go.uber.org/zap"
"reflect"
"slices"
"strings"
"text/template"
)

type DynamicValueContext struct {
Expand Down Expand Up @@ -284,6 +285,12 @@ func SelectItem(src any) DynamicValueSelection {
}

// Next returns the next value in the selection and whether there are more values
// If the selection is a map, the key is also returned.
// If the selection is a slice, the index is returned instead.
// If there are no more values, the second return value is false.
//
// This function is intended to be used by an orchestration layer across multiple go templates
// and is unavailable inside the templates themselves
func (s *DynamicValueSelection) Next() (any, bool) {
srcValue := reflect.ValueOf(s.Source)

Expand Down Expand Up @@ -311,15 +318,5 @@ func (s *DynamicValueSelection) Next() (any, bool) {
}

func stringValue(v any) string {
if v == nil {
return ""
}
switch v := v.(type) {
case string:
return v
case fmt.Stringer:
return v.String()
default:
return fmt.Sprintf("%v", v)
}
return fmt.Sprintf("%v", v)
}

0 comments on commit dc177a1

Please sign in to comment.