Skip to content
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

Implement handling of nested primitives with associated external types #73

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20231019-144938.yaml
Original file line number Diff line number Diff line change
@@ -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"
42 changes: 42 additions & 0 deletions internal/datasource_generate/bool_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
42 changes: 42 additions & 0 deletions internal/datasource_generate/float64_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
42 changes: 42 additions & 0 deletions internal/datasource_generate/int64_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
22 changes: 21 additions & 1 deletion internal/datasource_generate/list_nested_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
22 changes: 21 additions & 1 deletion internal/datasource_generate/list_nested_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
22 changes: 21 additions & 1 deletion internal/datasource_generate/map_nested_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
42 changes: 42 additions & 0 deletions internal/datasource_generate/number_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
22 changes: 21 additions & 1 deletion internal/datasource_generate/set_nested_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
22 changes: 21 additions & 1 deletion internal/datasource_generate/set_nested_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
22 changes: 21 additions & 1 deletion internal/datasource_generate/single_nested_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
Loading
Loading