Skip to content

Commit

Permalink
PR wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSeptimus-Klotho committed Aug 19, 2024
1 parent 1a3cdf2 commit 7949de9
Show file tree
Hide file tree
Showing 25 changed files with 399 additions and 189 deletions.
4 changes: 2 additions & 2 deletions pkg/k2/constructs/dynamic_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"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/constructs/template/properties"
"github.com/klothoplatform/klotho/pkg/k2/model"
"github.com/klothoplatform/klotho/pkg/templateutils"
"go.uber.org/zap"
Expand Down Expand Up @@ -52,7 +52,7 @@ func (ctx DynamicValueContext) ExecuteUnmarshal(tmpl string, data any, value any
}

func (ctx DynamicValueContext) Unmarshal(data *bytes.Buffer, v any) error {
return execution.UnmarshalAny(data, v)
return properties.UnmarshalAny(data, v)
}

// ExecuteTemplateUnmarshal executes the template tmpl using data as input and unmarshals the value into v
Expand Down
18 changes: 0 additions & 18 deletions pkg/k2/constructs/template/execution/unmarshal.go

This file was deleted.

29 changes: 17 additions & 12 deletions pkg/k2/constructs/template/inputs/properties_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"regexp"
"strings"

"github.com/google/uuid"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/properties"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/property"
"gopkg.in/yaml.v3"
Expand All @@ -22,37 +21,45 @@ type (
Name string `json:"name" yaml:"name"`
// Type defines the type of the property
Type string `json:"type" yaml:"type"`

// Description defines the description of the property
Description string `json:"description" yaml:"description"`

// DefaultValue defines the default value of the property
DefaultValue any `json:"default_value" yaml:"default_value"`

// Required defines whether the property is required
Required bool `json:"required" yaml:"required"`

//OperationalRule *knowledgebase.PropertyRule `json:"operational_rule" yaml:"operational_rule"`

// Properties defines the sub properties of a key_value_list, map, list, or set
Properties InputTemplateMap `json:"properties" yaml:"properties"`

// MinLength defines the minimum length of a string, list, set, or map (number of entries)
MinLength *int `yaml:"min_length"`
// MaxLength defines the maximum length of a string, list, set, or map (number of entries)
MaxLength *int `yaml:"max_length"`

// MinValue defines the minimum value of an int or float
MinValue *float64 `yaml:"min_value"`
// MaxValue defines the maximum value of an int or float
MaxValue *float64 `yaml:"max_value"`

// UniqueItems defines whether the items in a list or set must be unique
UniqueItems *bool `yaml:"unique_items"`
// UniqueKeys defines whether the keys in a map must be unique (default true)
UniqueKeys *bool `yaml:"unique_keys"`

SanitizeTmpl string `yaml:"sanitize"`
// SanitizeTmpl is a go template to sanitize user input when setting the property
SanitizeTmpl string `yaml:"sanitize"`
// AllowedValues defines an enumeration of allowed values for a string, int, float, or bool
AllowedValues []string `yaml:"allowed_values"`

KeyProperty *InputTemplate `yaml:"key_property"`
// KeyProperty is the property of the keys in a key_value_list or map
KeyProperty *InputTemplate `yaml:"key_property"`
// ValueProperty is the property of the values in a key_value_list or map
ValueProperty *InputTemplate `yaml:"value_property"`

// ItemProperty is the property of the items in a list or set
ItemProperty *InputTemplate `yaml:"item_property"`

// Path is the path to the property in the template
// this field is derived and is not part of the yaml
Path string `json:"-" yaml:"-"`
}

Expand Down Expand Up @@ -302,9 +309,7 @@ var fieldConversion = map[string]FieldConverterFunc{
if sanitizeTmpl == "" {
return nil
}
// generate random uuid as the name of the template
name := uuid.New().String()
tmpl, err := property.NewSanitizationTmpl(name, sanitizeTmpl)
tmpl, err := property.NewSanitizationTmpl(kp.Details().Name, sanitizeTmpl)
if err != nil {
return err
}
Expand Down
227 changes: 219 additions & 8 deletions pkg/k2/constructs/template/inputs/properties_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/properties"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/property"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_ConvertProperty(t *testing.T) {
Expand All @@ -15,26 +16,236 @@ func Test_ConvertProperty(t *testing.T) {
expected property.Property
}{
{
name: "Get string property type",
name: "Convert string property type",
property: InputTemplate{
Type: "string",
Name: "test",
Path: "test",
Required: true,
AllowedValues: []string{"test1", "test2"},
SanitizeTmpl: "test",
DefaultValue: "test",
},
expected: &properties.StringProperty{
SharedPropertyFields: properties.SharedPropertyFields{
DefaultValue: "test",
},
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
AllowedValues: []string{"test1", "test2"},
},
},
{
name: "Convert int property type",
property: InputTemplate{
Type: "int",
Name: "test",
Path: "test",
Required: true,
},
expected: &properties.IntProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
},
},
{
name: "Convert float property type",
property: InputTemplate{
Type: "float",
Name: "test",
Path: "test",
Required: true,
},
expected: &properties.FloatProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
},
},
{
name: "Convert bool property type",
property: InputTemplate{
Type: "bool",
Name: "test",
Path: "test",
Required: true,
},
expected: &properties.BoolProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
},
},
{
name: "Convert map property type",
property: InputTemplate{
Type: "map(string,string)",
Name: "test",
Path: "test",
Required: true,
KeyProperty: &InputTemplate{
Type: "string",
},
ValueProperty: &InputTemplate{
Type: "string",
},
},
expected: &properties.MapProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
Properties: map[string]property.Property{},
KeyProperty: &properties.StringProperty{},
ValueProperty: &properties.StringProperty{},
},
},
{
name: "Convert list property type",
property: InputTemplate{
Type: "list(string)",
Name: "test",
Path: "test",
Required: true,
},
expected: &properties.ListProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
ItemProperty: &properties.StringProperty{},
Properties: map[string]property.Property{},
},
},
{
name: "Convert set property type",
property: InputTemplate{
Type: "set(string)",
Name: "test",
Path: "test",
Required: true,
ItemProperty: &InputTemplate{
Type: "string",
},
},
expected: &properties.SetProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
ItemProperty: &properties.StringProperty{},
Properties: map[string]property.Property{},
},
},
{
name: "Convert key_value_list property type",
property: InputTemplate{
Type: "key_value_list(string,string)",
Name: "test",
Path: "test",
Required: true,
},
expected: &properties.KeyValueListProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
KeyProperty: &properties.StringProperty{PropertyDetails: property.PropertyDetails{Name: "Key"}},
ValueProperty: &properties.StringProperty{PropertyDetails: property.PropertyDetails{Name: "Value"}},
},
},
{
name: "Convert key_value_list property type with custom key and value properties",
property: InputTemplate{
Type: "key_value_list(string,string)",
Name: "test",
Path: "test",
KeyProperty: &InputTemplate{
Type: "string",
Name: "CustomKeyKey",
},
ValueProperty: &InputTemplate{
Type: "string",
Name: "CustomValueKey",
},
},
expected: &properties.KeyValueListProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Path: "test",
},
KeyProperty: &properties.StringProperty{PropertyDetails: property.PropertyDetails{Name: "CustomKeyKey"}},
ValueProperty: &properties.StringProperty{PropertyDetails: property.PropertyDetails{Name: "CustomValueKey"}},
},
},
{
name: "Convert construct property type",
property: InputTemplate{
Type: "construct",
Name: "test",
Path: "test",
Required: true,
},
expected: &properties.ConstructProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
},
},
{
name: "Convert any property type",
property: InputTemplate{
Type: "any",
Name: "test",
Path: "test",
Required: true,
},
expected: &properties.AnyProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
},
},
{
name: "Convert path property type",
property: InputTemplate{
Type: "path",
Name: "test",
Path: "test",
Required: true,
},
expected: &properties.PathProperty{
PropertyDetails: property.PropertyDetails{
Name: "test",
Required: true,
Path: "test",
},
},
expected: &properties.StringProperty{},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)
actual, err := test.property.Convert()
if assert.NoError(err, "Expected no error, but got: %v", err) {
return
}
assert.Equal(actual, test.expected, "expected %v, got %v", test.expected, actual)
require.NoError(t, err)
assert.EqualValuesf(t, actual, test.expected, "expected %v, got %v", test.expected, actual)
})
}
}
6 changes: 3 additions & 3 deletions pkg/k2/constructs/template/properties/any_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package properties

import (
"fmt"

"github.com/klothoplatform/klotho/pkg/construct"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/execution"
"github.com/klothoplatform/klotho/pkg/k2/constructs/template/property"
)

Expand Down Expand Up @@ -42,14 +42,14 @@ func (a *AnyProperty) Clone() property.Property {
return &clone
}

func (a *AnyProperty) GetDefaultValue(ctx execution.Context, data any) (any, error) {
func (a *AnyProperty) GetDefaultValue(ctx property.ExecutionContext, data any) (any, error) {
if a.DefaultValue == nil {
return nil, nil
}
return a.Parse(a.DefaultValue, ctx, data)
}

func (a *AnyProperty) Parse(value any, ctx execution.Context, data any) (any, error) {
func (a *AnyProperty) Parse(value any, ctx property.ExecutionContext, data any) (any, error) {
if val, ok := value.(string); ok {
// check if its any other template string
var result any
Expand Down
Loading

0 comments on commit 7949de9

Please sign in to comment.