diff --git a/.changes/unreleased/ENHANCEMENTS-20231019-144938.yaml b/.changes/unreleased/ENHANCEMENTS-20231019-144938.yaml new file mode 100644 index 00000000..1ac616be --- /dev/null +++ b/.changes/unreleased/ENHANCEMENTS-20231019-144938.yaml @@ -0,0 +1,6 @@ +kind: ENHANCEMENTS +body: Adds usage of To/From methods for primitive attributes with an associated external + type into To/From methods of nested attributes and blocks +time: 2023-10-19T14:49:38.39524+01:00 +custom: + Issue: "73" diff --git a/internal/datasource_generate/bool_attribute.go b/internal/datasource_generate/bool_attribute.go index 601ad816..3cdf461b 100644 --- a/internal/datasource_generate/bool_attribute.go +++ b/internal/datasource_generate/bool_attribute.go @@ -168,3 +168,45 @@ func (g GeneratorBoolAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.BoolTypable type. +func (g GeneratorBoolAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.BoolType{}" +} + +// AttrValue returns a string representation of a basetypes.BoolValuable type. +func (g GeneratorBoolAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.BoolValue" +} + +func (g GeneratorBoolAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueBoolPointer", + } +} + +func (g GeneratorBoolAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "BoolPointerValue", + } +} diff --git a/internal/datasource_generate/float64_attribute.go b/internal/datasource_generate/float64_attribute.go index 18da604e..5e34f888 100644 --- a/internal/datasource_generate/float64_attribute.go +++ b/internal/datasource_generate/float64_attribute.go @@ -167,3 +167,45 @@ func (g GeneratorFloat64Attribute) ToFromFunctions(name string) ([]byte, error) return b, nil } + +// AttrType returns a string representation of a basetypes.Float64Typable type. +func (g GeneratorFloat64Attribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.Float64Type{}" +} + +// AttrValue returns a string representation of a basetypes.Float64Valuable type. +func (g GeneratorFloat64Attribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.Float64Value" +} + +func (g GeneratorFloat64Attribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueFloat64Pointer", + } +} + +func (g GeneratorFloat64Attribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "Float64PointerValue", + } +} diff --git a/internal/datasource_generate/int64_attribute.go b/internal/datasource_generate/int64_attribute.go index 3531239d..2539b51e 100644 --- a/internal/datasource_generate/int64_attribute.go +++ b/internal/datasource_generate/int64_attribute.go @@ -167,3 +167,45 @@ func (g GeneratorInt64Attribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.Int64Typable type. +func (g GeneratorInt64Attribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.Int64Type{}" +} + +// AttrValue returns a string representation of a basetypes.Int64Valuable type. +func (g GeneratorInt64Attribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.Int64Value" +} + +func (g GeneratorInt64Attribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueInt64Pointer", + } +} + +func (g GeneratorInt64Attribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "Int64PointerValue", + } +} diff --git a/internal/datasource_generate/list_nested_attribute.go b/internal/datasource_generate/list_nested_attribute.go index ee21aa49..5fda1523 100644 --- a/internal/datasource_generate/list_nested_attribute.go +++ b/internal/datasource_generate/list_nested_attribute.go @@ -209,6 +209,8 @@ func (g GeneratorListNestedAttribute) ToFromFunctions(name string) ([]byte, erro return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -221,5 +223,23 @@ func (g GeneratorListNestedAttribute) ToFromFunctions(name string) ([]byte, erro return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/datasource_generate/list_nested_block.go b/internal/datasource_generate/list_nested_block.go index dc6d2692..51231ec2 100644 --- a/internal/datasource_generate/list_nested_block.go +++ b/internal/datasource_generate/list_nested_block.go @@ -287,6 +287,8 @@ func (g GeneratorListNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -299,5 +301,23 @@ func (g GeneratorListNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/datasource_generate/map_nested_attribute.go b/internal/datasource_generate/map_nested_attribute.go index 0a07bca4..73a12fa3 100644 --- a/internal/datasource_generate/map_nested_attribute.go +++ b/internal/datasource_generate/map_nested_attribute.go @@ -209,6 +209,8 @@ func (g GeneratorMapNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -221,5 +223,23 @@ func (g GeneratorMapNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/datasource_generate/number_attribute.go b/internal/datasource_generate/number_attribute.go index 1d6f91e7..e1c3d977 100644 --- a/internal/datasource_generate/number_attribute.go +++ b/internal/datasource_generate/number_attribute.go @@ -167,3 +167,45 @@ func (g GeneratorNumberAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.NumberTypable type. +func (g GeneratorNumberAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.NumberType{}" +} + +// AttrValue returns a string representation of a basetypes.NumberValuable type. +func (g GeneratorNumberAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.NumberValue" +} + +func (g GeneratorNumberAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueBigFloat", + } +} + +func (g GeneratorNumberAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "NumberValue", + } +} diff --git a/internal/datasource_generate/set_nested_attribute.go b/internal/datasource_generate/set_nested_attribute.go index 0eb5dc33..e71e5f59 100644 --- a/internal/datasource_generate/set_nested_attribute.go +++ b/internal/datasource_generate/set_nested_attribute.go @@ -209,6 +209,8 @@ func (g GeneratorSetNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -221,5 +223,23 @@ func (g GeneratorSetNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/datasource_generate/set_nested_block.go b/internal/datasource_generate/set_nested_block.go index da255aa6..bbc51f81 100644 --- a/internal/datasource_generate/set_nested_block.go +++ b/internal/datasource_generate/set_nested_block.go @@ -287,6 +287,8 @@ func (g GeneratorSetNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -299,5 +301,23 @@ func (g GeneratorSetNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/datasource_generate/single_nested_attribute.go b/internal/datasource_generate/single_nested_attribute.go index 92022421..c98ec5c3 100644 --- a/internal/datasource_generate/single_nested_attribute.go +++ b/internal/datasource_generate/single_nested_attribute.go @@ -212,6 +212,8 @@ func (g GeneratorSingleNestedAttribute) ToFromFunctions(name string) ([]byte, er return nil, nil } + var buf bytes.Buffer + toFuncs := g.Attributes.ToFuncs() fromFuncs := g.Attributes.FromFuncs() @@ -224,5 +226,23 @@ func (g GeneratorSingleNestedAttribute) ToFromFunctions(name string) ([]byte, er return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/datasource_generate/single_nested_block.go b/internal/datasource_generate/single_nested_block.go index 66bd6558..43f0c981 100644 --- a/internal/datasource_generate/single_nested_block.go +++ b/internal/datasource_generate/single_nested_block.go @@ -301,6 +301,8 @@ func (g GeneratorSingleNestedBlock) ToFromFunctions(name string) ([]byte, error) return nil, nil } + var buf bytes.Buffer + toFuncs := g.Attributes.ToFuncs() fromFuncs := g.Attributes.FromFuncs() @@ -313,5 +315,23 @@ func (g GeneratorSingleNestedBlock) ToFromFunctions(name string) ([]byte, error) return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/datasource_generate/string_attribute.go b/internal/datasource_generate/string_attribute.go index 247e622a..eba93ec4 100644 --- a/internal/datasource_generate/string_attribute.go +++ b/internal/datasource_generate/string_attribute.go @@ -167,3 +167,45 @@ func (g GeneratorStringAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.StringTypable type. +func (g GeneratorStringAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.StringType{}" +} + +// AttrValue returns a string representation of a basetypes.StringValuable type. +func (g GeneratorStringAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.StringValue" +} + +func (g GeneratorStringAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueStringPointer", + } +} + +func (g GeneratorStringAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "StringPointerValue", + } +} diff --git a/internal/provider_generate/bool_attribute.go b/internal/provider_generate/bool_attribute.go index 47dc5fd5..ef906d24 100644 --- a/internal/provider_generate/bool_attribute.go +++ b/internal/provider_generate/bool_attribute.go @@ -168,3 +168,45 @@ func (g GeneratorBoolAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.BoolTypable type. +func (g GeneratorBoolAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.BoolType{}" +} + +// AttrValue returns a string representation of a basetypes.BoolValuable type. +func (g GeneratorBoolAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.BoolValue" +} + +func (g GeneratorBoolAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueBoolPointer", + } +} + +func (g GeneratorBoolAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "BoolPointerValue", + } +} diff --git a/internal/provider_generate/float64_attribute.go b/internal/provider_generate/float64_attribute.go index 128a1340..816eff67 100644 --- a/internal/provider_generate/float64_attribute.go +++ b/internal/provider_generate/float64_attribute.go @@ -167,3 +167,45 @@ func (g GeneratorFloat64Attribute) ToFromFunctions(name string) ([]byte, error) return b, nil } + +// AttrType returns a string representation of a basetypes.Float64Typable type. +func (g GeneratorFloat64Attribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.Float64Type{}" +} + +// AttrValue returns a string representation of a basetypes.Float64Valuable type. +func (g GeneratorFloat64Attribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.Float64Value" +} + +func (g GeneratorFloat64Attribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueFloat64Pointer", + } +} + +func (g GeneratorFloat64Attribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "Float64PointerValue", + } +} diff --git a/internal/provider_generate/int64_attribute.go b/internal/provider_generate/int64_attribute.go index e9845c6f..22575009 100644 --- a/internal/provider_generate/int64_attribute.go +++ b/internal/provider_generate/int64_attribute.go @@ -167,3 +167,45 @@ func (g GeneratorInt64Attribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.Int64Typable type. +func (g GeneratorInt64Attribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.Int64Type{}" +} + +// AttrValue returns a string representation of a basetypes.Int64Valuable type. +func (g GeneratorInt64Attribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.Int64Value" +} + +func (g GeneratorInt64Attribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueInt64Pointer", + } +} + +func (g GeneratorInt64Attribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "Int64PointerValue", + } +} diff --git a/internal/provider_generate/list_nested_attribute.go b/internal/provider_generate/list_nested_attribute.go index bc91e731..b74a430e 100644 --- a/internal/provider_generate/list_nested_attribute.go +++ b/internal/provider_generate/list_nested_attribute.go @@ -209,6 +209,8 @@ func (g GeneratorListNestedAttribute) ToFromFunctions(name string) ([]byte, erro return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -221,5 +223,23 @@ func (g GeneratorListNestedAttribute) ToFromFunctions(name string) ([]byte, erro return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/provider_generate/list_nested_block.go b/internal/provider_generate/list_nested_block.go index 550abd79..87247e4c 100644 --- a/internal/provider_generate/list_nested_block.go +++ b/internal/provider_generate/list_nested_block.go @@ -287,6 +287,8 @@ func (g GeneratorListNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -299,5 +301,23 @@ func (g GeneratorListNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/provider_generate/map_nested_attribute.go b/internal/provider_generate/map_nested_attribute.go index d22c17da..df29bb74 100644 --- a/internal/provider_generate/map_nested_attribute.go +++ b/internal/provider_generate/map_nested_attribute.go @@ -209,6 +209,8 @@ func (g GeneratorMapNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -221,5 +223,23 @@ func (g GeneratorMapNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/provider_generate/number_attribute.go b/internal/provider_generate/number_attribute.go index 0f744abb..5a4f9098 100644 --- a/internal/provider_generate/number_attribute.go +++ b/internal/provider_generate/number_attribute.go @@ -167,3 +167,45 @@ func (g GeneratorNumberAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.NumberTypable type. +func (g GeneratorNumberAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.NumberType{}" +} + +// AttrValue returns a string representation of a basetypes.NumberValuable type. +func (g GeneratorNumberAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.NumberValue" +} + +func (g GeneratorNumberAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueBigFloat", + } +} + +func (g GeneratorNumberAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "NumberValue", + } +} diff --git a/internal/provider_generate/set_nested_attribute.go b/internal/provider_generate/set_nested_attribute.go index 79a29818..2da67f98 100644 --- a/internal/provider_generate/set_nested_attribute.go +++ b/internal/provider_generate/set_nested_attribute.go @@ -209,6 +209,8 @@ func (g GeneratorSetNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -221,5 +223,23 @@ func (g GeneratorSetNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/provider_generate/set_nested_block.go b/internal/provider_generate/set_nested_block.go index ff7c6295..1a5fd1d7 100644 --- a/internal/provider_generate/set_nested_block.go +++ b/internal/provider_generate/set_nested_block.go @@ -287,6 +287,8 @@ func (g GeneratorSetNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -299,5 +301,23 @@ func (g GeneratorSetNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/provider_generate/single_nested_attribute.go b/internal/provider_generate/single_nested_attribute.go index 426315dc..772834d0 100644 --- a/internal/provider_generate/single_nested_attribute.go +++ b/internal/provider_generate/single_nested_attribute.go @@ -212,6 +212,8 @@ func (g GeneratorSingleNestedAttribute) ToFromFunctions(name string) ([]byte, er return nil, nil } + var buf bytes.Buffer + toFuncs := g.Attributes.ToFuncs() fromFuncs := g.Attributes.FromFuncs() @@ -224,5 +226,23 @@ func (g GeneratorSingleNestedAttribute) ToFromFunctions(name string) ([]byte, er return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/provider_generate/single_nested_block.go b/internal/provider_generate/single_nested_block.go index 259b4ff7..94036cf5 100644 --- a/internal/provider_generate/single_nested_block.go +++ b/internal/provider_generate/single_nested_block.go @@ -301,6 +301,8 @@ func (g GeneratorSingleNestedBlock) ToFromFunctions(name string) ([]byte, error) return nil, nil } + var buf bytes.Buffer + toFuncs := g.Attributes.ToFuncs() fromFuncs := g.Attributes.FromFuncs() @@ -313,5 +315,23 @@ func (g GeneratorSingleNestedBlock) ToFromFunctions(name string) ([]byte, error) return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/provider_generate/string_attribute.go b/internal/provider_generate/string_attribute.go index 37bd15ca..637fa99e 100644 --- a/internal/provider_generate/string_attribute.go +++ b/internal/provider_generate/string_attribute.go @@ -167,3 +167,45 @@ func (g GeneratorStringAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.StringTypable type. +func (g GeneratorStringAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.StringType{}" +} + +// AttrValue returns a string representation of a basetypes.StringValuable type. +func (g GeneratorStringAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.StringValue" +} + +func (g GeneratorStringAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueStringPointer", + } +} + +func (g GeneratorStringAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "StringPointerValue", + } +} diff --git a/internal/resource_generate/bool_attribute.go b/internal/resource_generate/bool_attribute.go index 12ef3495..121026d0 100644 --- a/internal/resource_generate/bool_attribute.go +++ b/internal/resource_generate/bool_attribute.go @@ -213,3 +213,45 @@ func (g GeneratorBoolAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.BoolTypable type. +func (g GeneratorBoolAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.BoolType{}" +} + +// AttrValue returns a string representation of a basetypes.BoolValuable type. +func (g GeneratorBoolAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.BoolValue" +} + +func (g GeneratorBoolAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueBoolPointer", + } +} + +func (g GeneratorBoolAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "BoolPointerValue", + } +} diff --git a/internal/resource_generate/float64_attribute.go b/internal/resource_generate/float64_attribute.go index 228fc36b..d7d967f2 100644 --- a/internal/resource_generate/float64_attribute.go +++ b/internal/resource_generate/float64_attribute.go @@ -213,3 +213,45 @@ func (g GeneratorFloat64Attribute) ToFromFunctions(name string) ([]byte, error) return b, nil } + +// AttrType returns a string representation of a basetypes.Float64Typable type. +func (g GeneratorFloat64Attribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.Float64Type{}" +} + +// AttrValue returns a string representation of a basetypes.Float64Valuable type. +func (g GeneratorFloat64Attribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.Float64Value" +} + +func (g GeneratorFloat64Attribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueFloat64Pointer", + } +} + +func (g GeneratorFloat64Attribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "Float64PointerValue", + } +} diff --git a/internal/resource_generate/int64_attribute.go b/internal/resource_generate/int64_attribute.go index 6e7099d7..593ca61d 100644 --- a/internal/resource_generate/int64_attribute.go +++ b/internal/resource_generate/int64_attribute.go @@ -212,3 +212,45 @@ func (g GeneratorInt64Attribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.Int64Typable type. +func (g GeneratorInt64Attribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.Int64Type{}" +} + +// AttrValue returns a string representation of a basetypes.Int64Valuable type. +func (g GeneratorInt64Attribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.Int64Value" +} + +func (g GeneratorInt64Attribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueInt64Pointer", + } +} + +func (g GeneratorInt64Attribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "Int64PointerValue", + } +} diff --git a/internal/resource_generate/list_nested_attribute.go b/internal/resource_generate/list_nested_attribute.go index 31a0e759..b61cbdec 100644 --- a/internal/resource_generate/list_nested_attribute.go +++ b/internal/resource_generate/list_nested_attribute.go @@ -234,6 +234,8 @@ func (g GeneratorListNestedAttribute) ToFromFunctions(name string) ([]byte, erro return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -246,5 +248,23 @@ func (g GeneratorListNestedAttribute) ToFromFunctions(name string) ([]byte, erro return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/resource_generate/list_nested_block.go b/internal/resource_generate/list_nested_block.go index 47431c53..98ccc117 100644 --- a/internal/resource_generate/list_nested_block.go +++ b/internal/resource_generate/list_nested_block.go @@ -304,6 +304,8 @@ func (g GeneratorListNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -316,5 +318,23 @@ func (g GeneratorListNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/resource_generate/map_nested_attribute.go b/internal/resource_generate/map_nested_attribute.go index 2b8a391d..8e883d06 100644 --- a/internal/resource_generate/map_nested_attribute.go +++ b/internal/resource_generate/map_nested_attribute.go @@ -234,6 +234,8 @@ func (g GeneratorMapNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -246,5 +248,23 @@ func (g GeneratorMapNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/resource_generate/number_attribute.go b/internal/resource_generate/number_attribute.go index 69e6e040..762d0f18 100644 --- a/internal/resource_generate/number_attribute.go +++ b/internal/resource_generate/number_attribute.go @@ -201,3 +201,45 @@ func (g GeneratorNumberAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.NumberTypable type. +func (g GeneratorNumberAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.NumberType{}" +} + +// AttrValue returns a string representation of a basetypes.NumberValuable type. +func (g GeneratorNumberAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.NumberValue" +} + +func (g GeneratorNumberAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueBigFloat", + } +} + +func (g GeneratorNumberAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "NumberValue", + } +} diff --git a/internal/resource_generate/set_nested_attribute.go b/internal/resource_generate/set_nested_attribute.go index 745f960d..60d955cf 100644 --- a/internal/resource_generate/set_nested_attribute.go +++ b/internal/resource_generate/set_nested_attribute.go @@ -234,6 +234,8 @@ func (g GeneratorSetNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -246,5 +248,23 @@ func (g GeneratorSetNestedAttribute) ToFromFunctions(name string) ([]byte, error return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/resource_generate/set_nested_block.go b/internal/resource_generate/set_nested_block.go index 3758adc0..35ff7979 100644 --- a/internal/resource_generate/set_nested_block.go +++ b/internal/resource_generate/set_nested_block.go @@ -302,6 +302,8 @@ func (g GeneratorSetNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, nil } + var buf bytes.Buffer + toFuncs := g.NestedObject.Attributes.ToFuncs() fromFuncs := g.NestedObject.Attributes.FromFuncs() @@ -314,5 +316,23 @@ func (g GeneratorSetNestedBlock) ToFromFunctions(name string) ([]byte, error) { return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.NestedObject.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/resource_generate/single_nested_attribute.go b/internal/resource_generate/single_nested_attribute.go index 6f804ba8..cc50e94a 100644 --- a/internal/resource_generate/single_nested_attribute.go +++ b/internal/resource_generate/single_nested_attribute.go @@ -232,6 +232,8 @@ func (g GeneratorSingleNestedAttribute) ToFromFunctions(name string) ([]byte, er return nil, nil } + var buf bytes.Buffer + toFuncs := g.Attributes.ToFuncs() fromFuncs := g.Attributes.FromFuncs() @@ -244,5 +246,23 @@ func (g GeneratorSingleNestedAttribute) ToFromFunctions(name string) ([]byte, er return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/resource_generate/single_nested_block.go b/internal/resource_generate/single_nested_block.go index 46966046..c16b3cf5 100644 --- a/internal/resource_generate/single_nested_block.go +++ b/internal/resource_generate/single_nested_block.go @@ -311,6 +311,8 @@ func (g GeneratorSingleNestedBlock) ToFromFunctions(name string) ([]byte, error) return nil, nil } + var buf bytes.Buffer + toFuncs := g.Attributes.ToFuncs() fromFuncs := g.Attributes.FromFuncs() @@ -323,5 +325,23 @@ func (g GeneratorSingleNestedBlock) ToFromFunctions(name string) ([]byte, error) return nil, err } - return b, nil + buf.Write(b) + + attributeKeys := g.Attributes.SortedKeys() + + // Recursively call ToFromFunctions() for each attribute that implements + // ToFrom interface. + for _, k := range attributeKeys { + if c, ok := g.Attributes[k].(generatorschema.ToFrom); ok { + b, err := c.ToFromFunctions(k) + + if err != nil { + return nil, err + } + + buf.Write(b) + } + } + + return buf.Bytes(), nil } diff --git a/internal/resource_generate/string_attribute.go b/internal/resource_generate/string_attribute.go index 77c25aae..381183bb 100644 --- a/internal/resource_generate/string_attribute.go +++ b/internal/resource_generate/string_attribute.go @@ -212,3 +212,45 @@ func (g GeneratorStringAttribute) ToFromFunctions(name string) ([]byte, error) { return b, nil } + +// AttrType returns a string representation of a basetypes.StringTypable type. +func (g GeneratorStringAttribute) AttrType(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sType{}", name.ToPascalCase()) + } + + return "basetypes.StringType{}" +} + +// AttrValue returns a string representation of a basetypes.StringValuable type. +func (g GeneratorStringAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string { + if g.AssociatedExternalType != nil { + return fmt.Sprintf("%sValue", name.ToPascalCase()) + } + + return "basetypes.StringValue" +} + +func (g GeneratorStringAttribute) To() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "ValueStringPointer", + } +} + +func (g GeneratorStringAttribute) From() generatorschema.ToFromConversion { + if g.AssociatedExternalType != nil { + return generatorschema.ToFromConversion{ + AssocExtType: g.AssociatedExternalType, + } + } + + return generatorschema.ToFromConversion{ + Default: "StringPointerValue", + } +} diff --git a/internal/schema/associated_external_type.go b/internal/schema/associated_external_type.go index 6a043ec1..cd8f5e8f 100644 --- a/internal/schema/associated_external_type.go +++ b/internal/schema/associated_external_type.go @@ -5,6 +5,8 @@ package schema import ( "strings" + "unicode" + "unicode/utf8" "github.com/hashicorp/terraform-plugin-codegen-spec/code" "github.com/hashicorp/terraform-plugin-codegen-spec/schema" @@ -99,3 +101,15 @@ func (a *AssocExtType) ToPascalCase() string { return ucName } + +func (a *AssocExtType) ToCamelCase() string { + pascal := a.ToPascalCase() + + // Grab first rune and lower case it + firstLetter, size := utf8.DecodeRuneInString(pascal) + if firstLetter == utf8.RuneError && size <= 1 { + return pascal + } + + return string(unicode.ToLower(firstLetter)) + pascal[size:] +} diff --git a/internal/schema/attributes.go b/internal/schema/attributes.go index 0658b928..e9983165 100644 --- a/internal/schema/attributes.go +++ b/internal/schema/attributes.go @@ -62,6 +62,11 @@ func (g GeneratorAttributes) AttrTypes() (map[string]string, error) { for _, k := range attributeKeys { name := FrameworkIdentifier(k) + if a, ok := g[k].(AttrType); ok { + attrTypes[k] = a.AttrType(name) + continue + } + switch g[k].GeneratorSchemaType() { case GeneratorBoolAttribute: attrTypes[k] = "basetypes.BoolType{}" @@ -135,6 +140,11 @@ func (g GeneratorAttributes) AttrValues() (map[string]string, error) { attrValues := make(map[string]string, len(g)) for _, k := range attributeKeys { + if a, ok := g[k].(AttrValue); ok { + attrValues[k] = a.AttrValue(FrameworkIdentifier(k)) + continue + } + switch g[k].GeneratorSchemaType() { case GeneratorBoolAttribute: attrValues[k] = "basetypes.BoolValue" @@ -170,23 +180,14 @@ func (g GeneratorAttributes) AttrValues() (map[string]string, error) { // FromFuncs returns a mapping of attribute names to string representations of the // function that converts a Go value to a framework value. -func (g GeneratorAttributes) FromFuncs() map[string]string { +func (g GeneratorAttributes) FromFuncs() map[string]ToFromConversion { attributeKeys := g.SortedKeys() - fromFuncs := make(map[string]string, len(g)) + fromFuncs := make(map[string]ToFromConversion, len(g)) for _, k := range attributeKeys { - switch g[k].GeneratorSchemaType() { - case GeneratorBoolAttribute: - fromFuncs[k] = "BoolPointerValue" - case GeneratorFloat64Attribute: - fromFuncs[k] = "Float64PointerValue" - case GeneratorInt64Attribute: - fromFuncs[k] = "Int64PointerValue" - case GeneratorNumberAttribute: - fromFuncs[k] = "NumberValue" - case GeneratorStringAttribute: - fromFuncs[k] = "StringPointerValue" + if a, ok := g[k].(From); ok { + fromFuncs[k] = a.From() } } @@ -224,23 +225,14 @@ func (g GeneratorAttributes) Schema() (string, error) { // ToFuncs returns a mapping of attribute names to string representations of the // function that converts a framework value to a Go value. -func (g GeneratorAttributes) ToFuncs() map[string]string { +func (g GeneratorAttributes) ToFuncs() map[string]ToFromConversion { attributeKeys := g.SortedKeys() - toFuncs := make(map[string]string, len(g)) + toFuncs := make(map[string]ToFromConversion, len(g)) for _, k := range attributeKeys { - switch g[k].GeneratorSchemaType() { - case GeneratorBoolAttribute: - toFuncs[k] = "ValueBoolPointer" - case GeneratorFloat64Attribute: - toFuncs[k] = "ValueFloat64Pointer" - case GeneratorInt64Attribute: - toFuncs[k] = "ValueInt64Pointer" - case GeneratorNumberAttribute: - toFuncs[k] = "ValueBigFloat" - case GeneratorStringAttribute: - toFuncs[k] = "ValueStringPointer" + if a, ok := g[k].(To); ok { + toFuncs[k] = a.To() } } diff --git a/internal/schema/templates/object_from.gotmpl b/internal/schema/templates/object_from.gotmpl index c1face19..39ff2b92 100644 --- a/internal/schema/templates/object_from.gotmpl +++ b/internal/schema/templates/object_from.gotmpl @@ -5,10 +5,26 @@ var diags diag.Diagnostics if apiObject == nil { return New{{.Name}}ValueNull(), diags } +{{- range $key, $value := .FromFuncs}} +{{- if $value.AssocExtType}} + +{{$key.ToCamelCase}}Val, d := {{$key.ToPascalCase}}Value{}.From{{$value.AssocExtType.ToPascalCase}}(ctx, apiObject.{{$key.ToPascalCase}}) + +diags.Append(d...) + +if diags.HasError() { +return New{{$.Name}}ValueNull(), diags +} +{{- end}} +{{- end}} return {{.Name}}Value{ -{{- range $key, $value := .FromFuncs }} -{{$key.ToPascalCase}}: types.{{$value}}(apiObject.{{$key.ToPascalCase}}), +{{- range $key, $value := .FromFuncs}} +{{- if $value.AssocExtType}} +{{$key.ToPascalCase}}: {{$key.ToCamelCase}}Val, +{{- else if $value.Default}} +{{$key.ToPascalCase}}: types.{{$value.Default}}(apiObject.{{$key.ToPascalCase}}), +{{- end}} {{- end}} state: attr.ValueStateKnown, }, diags diff --git a/internal/schema/templates/object_to.gotmpl b/internal/schema/templates/object_to.gotmpl index 0f11cf63..737e8ec0 100644 --- a/internal/schema/templates/object_to.gotmpl +++ b/internal/schema/templates/object_to.gotmpl @@ -13,10 +13,26 @@ diags.Append(diag.NewErrorDiagnostic( return nil, diags } +{{- range $key, $value := .ToFuncs}} +{{- if $value.AssocExtType}} + +{{$value.AssocExtType.ToCamelCase}}, d := v.{{$key.ToPascalCase}}.To{{$value.AssocExtType.ToPascalCase}}(ctx) + +diags.Append(d...) + +if diags.HasError() { +return nil, diags +} +{{- end}} +{{- end}} return &{{.AssocExtType.TypeReference}}{ -{{- range $key, $value := .ToFuncs }} -{{$key.ToPascalCase}}: v.{{$key.ToPascalCase}}.{{$value}}(), +{{- range $key, $value := .ToFuncs}} +{{- if $value.AssocExtType}} +{{$key.ToPascalCase}}: {{$value.AssocExtType.ToCamelCase}}, +{{- else if $value.Default}} +{{$key.ToPascalCase}}: v.{{$key.ToPascalCase}}.{{$value.Default}}(), +{{- end}} {{- end}} }, diags } \ No newline at end of file diff --git a/internal/schema/to_from_object.go b/internal/schema/to_from_object.go index 516e8c51..b0e5ed00 100644 --- a/internal/schema/to_from_object.go +++ b/internal/schema/to_from_object.go @@ -11,24 +11,24 @@ import ( type ToFromObject struct { Name FrameworkIdentifier AssocExtType *AssocExtType - FromFuncs map[FrameworkIdentifier]string - ToFuncs map[FrameworkIdentifier]string + ToFuncs map[FrameworkIdentifier]ToFromConversion + FromFuncs map[FrameworkIdentifier]ToFromConversion templates map[string]string } -func NewToFromObject(name string, assocExtType *AssocExtType, toFuncs, fromFuncs map[string]string) ToFromObject { +func NewToFromObject(name string, assocExtType *AssocExtType, toFuncs, fromFuncs map[string]ToFromConversion) ToFromObject { t := map[string]string{ "from": ObjectFromTemplate, "to": ObjectToTemplate, } - tf := make(map[FrameworkIdentifier]string, len(toFuncs)) + tf := make(map[FrameworkIdentifier]ToFromConversion, len(toFuncs)) for k, v := range toFuncs { tf[FrameworkIdentifier(k)] = v } - ff := make(map[FrameworkIdentifier]string, len(fromFuncs)) + ff := make(map[FrameworkIdentifier]ToFromConversion, len(fromFuncs)) for k, v := range fromFuncs { ff[FrameworkIdentifier(k)] = v @@ -78,7 +78,7 @@ func (o ToFromObject) renderTo() ([]byte, error) { err = t.Execute(&buf, struct { Name string AssocExtType *AssocExtType - ToFuncs map[FrameworkIdentifier]string + ToFuncs map[FrameworkIdentifier]ToFromConversion }{ Name: o.Name.ToPascalCase(), AssocExtType: o.AssocExtType, @@ -104,7 +104,7 @@ func (o ToFromObject) renderFrom() ([]byte, error) { err = t.Execute(&buf, struct { Name string AssocExtType *AssocExtType - FromFuncs map[FrameworkIdentifier]string + FromFuncs map[FrameworkIdentifier]ToFromConversion }{ Name: o.Name.ToPascalCase(), AssocExtType: o.AssocExtType, diff --git a/internal/schema/to_from_object_test.go b/internal/schema/to_from_object_test.go index 3c534a9d..9fcb0f56 100644 --- a/internal/schema/to_from_object_test.go +++ b/internal/schema/to_from_object_test.go @@ -17,22 +17,21 @@ func TestToFromObject_renderFrom(t *testing.T) { testCases := map[string]struct { name string assocExtType *AssocExtType - fromFuncs map[string]string + fromFuncs map[string]ToFromConversion expected []byte expectedError error }{ "default": { name: "Example", assocExtType: &AssocExtType{ - &schema.AssociatedExternalType{ - Import: &code.Import{ - Path: "example.com/apisdk", - }, + AssociatedExternalType: &schema.AssociatedExternalType{ Type: "*apisdk.Type", }, }, - fromFuncs: map[string]string{ - "bool_attribute": "BoolPointerValue", + fromFuncs: map[string]ToFromConversion{ + "bool_attribute": { + Default: "BoolPointerValue", + }, }, expected: []byte(` func (v ExampleValue) FromApisdkType(ctx context.Context, apiObject *apisdk.Type) (ExampleValue, diag.Diagnostics) { @@ -47,6 +46,45 @@ BoolAttribute: types.BoolPointerValue(apiObject.BoolAttribute), state: attr.ValueStateKnown, }, diags } +`), + }, + "nested-assoc-ext-type": { + name: "Example", + assocExtType: &AssocExtType{ + AssociatedExternalType: &schema.AssociatedExternalType{ + Type: "*apisdk.Type", + }, + }, + fromFuncs: map[string]ToFromConversion{ + "bool_attribute": { + AssocExtType: &AssocExtType{ + AssociatedExternalType: &schema.AssociatedExternalType{ + Type: "*api.BoolAttribute", + }, + }, + }, + }, + expected: []byte(` +func (v ExampleValue) FromApisdkType(ctx context.Context, apiObject *apisdk.Type) (ExampleValue, diag.Diagnostics) { +var diags diag.Diagnostics + +if apiObject == nil { +return NewExampleValueNull(), diags +} + +boolAttributeVal, d := BoolAttributeValue{}.FromApiBoolAttribute(ctx, apiObject.BoolAttribute) + +diags.Append(d...) + +if diags.HasError() { +return NewExampleValueNull(), diags +} + +return ExampleValue{ +BoolAttribute: boolAttributeVal, +state: attr.ValueStateKnown, +}, diags +} `), }, } @@ -78,7 +116,7 @@ func TestToFromObject_renderTo(t *testing.T) { testCases := map[string]struct { name string assocExtType *AssocExtType - toFuncs map[string]string + toFuncs map[string]ToFromConversion expected []byte expectedError error }{ @@ -92,8 +130,10 @@ func TestToFromObject_renderTo(t *testing.T) { Type: "*apisdk.Type", }, }, - toFuncs: map[string]string{ - "bool_attribute": "ValueBoolPointer", + toFuncs: map[string]ToFromConversion{ + "bool_attribute": { + Default: "ValueBoolPointer", + }, }, expected: []byte(`func (v ExampleValue) ToApisdkType(ctx context.Context) (*apisdk.Type, diag.Diagnostics) { var diags diag.Diagnostics @@ -114,6 +154,54 @@ return nil, diags return &apisdk.Type{ BoolAttribute: v.BoolAttribute.ValueBoolPointer(), }, diags +}`), + }, + "nested-assoc-ext-type": { + name: "Example", + assocExtType: &AssocExtType{ + &schema.AssociatedExternalType{ + Import: &code.Import{ + Path: "example.com/apisdk", + }, + Type: "*apisdk.Type", + }, + }, + toFuncs: map[string]ToFromConversion{ + "bool_attribute": { + AssocExtType: &AssocExtType{ + AssociatedExternalType: &schema.AssociatedExternalType{ + Type: "*api.BoolAttribute", + }, + }, + }, + }, + expected: []byte(`func (v ExampleValue) ToApisdkType(ctx context.Context) (*apisdk.Type, diag.Diagnostics) { +var diags diag.Diagnostics + +if v.IsNull() { +return nil, diags +} + +if v.IsUnknown() { +diags.Append(diag.NewErrorDiagnostic( +"ExampleValue Value Is Unknown", +` + "`" + `"ExampleValue" is unknown.` + "`" + `, +)) + +return nil, diags +} + +apiBoolAttribute, d := v.BoolAttribute.ToApiBoolAttribute(ctx) + +diags.Append(d...) + +if diags.HasError() { +return nil, diags +} + +return &apisdk.Type{ +BoolAttribute: apiBoolAttribute, +}, diags }`), }, } diff --git a/internal/schema/types.go b/internal/schema/types.go index a1461bf4..b0035bbb 100644 --- a/internal/schema/types.go +++ b/internal/schema/types.go @@ -38,6 +38,14 @@ type GeneratorAttribute interface { Schema(FrameworkIdentifier) (string, error) } +type AttrType interface { + AttrType(FrameworkIdentifier) string +} + +type AttrValue interface { + AttrValue(FrameworkIdentifier) string +} + type GeneratorBlock interface { Equal(GeneratorBlock) bool GeneratorSchemaType() Type @@ -60,6 +68,19 @@ type ToFrom interface { ToFromFunctions(name string) ([]byte, error) } +type ToFromConversion struct { + Default string + AssocExtType *AssocExtType +} + +type To interface { + To() ToFromConversion +} + +type From interface { + From() ToFromConversion +} + type Type int64 const (