Skip to content

Commit 292ce20

Browse files
authored
Implement handling of nested primitives with associated external types (#73)
* Handling nested primitives that have an associated external type (#71) * Adding changelog entry (#71)
1 parent 99e04d2 commit 292ce20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1271
-68
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: ENHANCEMENTS
2+
body: Adds usage of To/From methods for primitive attributes with an associated external
3+
type into To/From methods of nested attributes and blocks
4+
time: 2023-10-19T14:49:38.39524+01:00
5+
custom:
6+
Issue: "73"

internal/datasource_generate/bool_attribute.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,45 @@ func (g GeneratorBoolAttribute) ToFromFunctions(name string) ([]byte, error) {
168168

169169
return b, nil
170170
}
171+
172+
// AttrType returns a string representation of a basetypes.BoolTypable type.
173+
func (g GeneratorBoolAttribute) AttrType(name generatorschema.FrameworkIdentifier) string {
174+
if g.AssociatedExternalType != nil {
175+
return fmt.Sprintf("%sType{}", name.ToPascalCase())
176+
}
177+
178+
return "basetypes.BoolType{}"
179+
}
180+
181+
// AttrValue returns a string representation of a basetypes.BoolValuable type.
182+
func (g GeneratorBoolAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string {
183+
if g.AssociatedExternalType != nil {
184+
return fmt.Sprintf("%sValue", name.ToPascalCase())
185+
}
186+
187+
return "basetypes.BoolValue"
188+
}
189+
190+
func (g GeneratorBoolAttribute) To() generatorschema.ToFromConversion {
191+
if g.AssociatedExternalType != nil {
192+
return generatorschema.ToFromConversion{
193+
AssocExtType: g.AssociatedExternalType,
194+
}
195+
}
196+
197+
return generatorschema.ToFromConversion{
198+
Default: "ValueBoolPointer",
199+
}
200+
}
201+
202+
func (g GeneratorBoolAttribute) From() generatorschema.ToFromConversion {
203+
if g.AssociatedExternalType != nil {
204+
return generatorschema.ToFromConversion{
205+
AssocExtType: g.AssociatedExternalType,
206+
}
207+
}
208+
209+
return generatorschema.ToFromConversion{
210+
Default: "BoolPointerValue",
211+
}
212+
}

internal/datasource_generate/float64_attribute.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,45 @@ func (g GeneratorFloat64Attribute) ToFromFunctions(name string) ([]byte, error)
167167

168168
return b, nil
169169
}
170+
171+
// AttrType returns a string representation of a basetypes.Float64Typable type.
172+
func (g GeneratorFloat64Attribute) AttrType(name generatorschema.FrameworkIdentifier) string {
173+
if g.AssociatedExternalType != nil {
174+
return fmt.Sprintf("%sType{}", name.ToPascalCase())
175+
}
176+
177+
return "basetypes.Float64Type{}"
178+
}
179+
180+
// AttrValue returns a string representation of a basetypes.Float64Valuable type.
181+
func (g GeneratorFloat64Attribute) AttrValue(name generatorschema.FrameworkIdentifier) string {
182+
if g.AssociatedExternalType != nil {
183+
return fmt.Sprintf("%sValue", name.ToPascalCase())
184+
}
185+
186+
return "basetypes.Float64Value"
187+
}
188+
189+
func (g GeneratorFloat64Attribute) To() generatorschema.ToFromConversion {
190+
if g.AssociatedExternalType != nil {
191+
return generatorschema.ToFromConversion{
192+
AssocExtType: g.AssociatedExternalType,
193+
}
194+
}
195+
196+
return generatorschema.ToFromConversion{
197+
Default: "ValueFloat64Pointer",
198+
}
199+
}
200+
201+
func (g GeneratorFloat64Attribute) From() generatorschema.ToFromConversion {
202+
if g.AssociatedExternalType != nil {
203+
return generatorschema.ToFromConversion{
204+
AssocExtType: g.AssociatedExternalType,
205+
}
206+
}
207+
208+
return generatorschema.ToFromConversion{
209+
Default: "Float64PointerValue",
210+
}
211+
}

internal/datasource_generate/int64_attribute.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,45 @@ func (g GeneratorInt64Attribute) ToFromFunctions(name string) ([]byte, error) {
167167

168168
return b, nil
169169
}
170+
171+
// AttrType returns a string representation of a basetypes.Int64Typable type.
172+
func (g GeneratorInt64Attribute) AttrType(name generatorschema.FrameworkIdentifier) string {
173+
if g.AssociatedExternalType != nil {
174+
return fmt.Sprintf("%sType{}", name.ToPascalCase())
175+
}
176+
177+
return "basetypes.Int64Type{}"
178+
}
179+
180+
// AttrValue returns a string representation of a basetypes.Int64Valuable type.
181+
func (g GeneratorInt64Attribute) AttrValue(name generatorschema.FrameworkIdentifier) string {
182+
if g.AssociatedExternalType != nil {
183+
return fmt.Sprintf("%sValue", name.ToPascalCase())
184+
}
185+
186+
return "basetypes.Int64Value"
187+
}
188+
189+
func (g GeneratorInt64Attribute) To() generatorschema.ToFromConversion {
190+
if g.AssociatedExternalType != nil {
191+
return generatorschema.ToFromConversion{
192+
AssocExtType: g.AssociatedExternalType,
193+
}
194+
}
195+
196+
return generatorschema.ToFromConversion{
197+
Default: "ValueInt64Pointer",
198+
}
199+
}
200+
201+
func (g GeneratorInt64Attribute) From() generatorschema.ToFromConversion {
202+
if g.AssociatedExternalType != nil {
203+
return generatorschema.ToFromConversion{
204+
AssocExtType: g.AssociatedExternalType,
205+
}
206+
}
207+
208+
return generatorschema.ToFromConversion{
209+
Default: "Int64PointerValue",
210+
}
211+
}

internal/datasource_generate/list_nested_attribute.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ func (g GeneratorListNestedAttribute) ToFromFunctions(name string) ([]byte, erro
209209
return nil, nil
210210
}
211211

212+
var buf bytes.Buffer
213+
212214
toFuncs := g.NestedObject.Attributes.ToFuncs()
213215

214216
fromFuncs := g.NestedObject.Attributes.FromFuncs()
@@ -221,5 +223,23 @@ func (g GeneratorListNestedAttribute) ToFromFunctions(name string) ([]byte, erro
221223
return nil, err
222224
}
223225

224-
return b, nil
226+
buf.Write(b)
227+
228+
attributeKeys := g.NestedObject.Attributes.SortedKeys()
229+
230+
// Recursively call ToFromFunctions() for each attribute that implements
231+
// ToFrom interface.
232+
for _, k := range attributeKeys {
233+
if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok {
234+
b, err := c.ToFromFunctions(k)
235+
236+
if err != nil {
237+
return nil, err
238+
}
239+
240+
buf.Write(b)
241+
}
242+
}
243+
244+
return buf.Bytes(), nil
225245
}

internal/datasource_generate/list_nested_block.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ func (g GeneratorListNestedBlock) ToFromFunctions(name string) ([]byte, error) {
287287
return nil, nil
288288
}
289289

290+
var buf bytes.Buffer
291+
290292
toFuncs := g.NestedObject.Attributes.ToFuncs()
291293

292294
fromFuncs := g.NestedObject.Attributes.FromFuncs()
@@ -299,5 +301,23 @@ func (g GeneratorListNestedBlock) ToFromFunctions(name string) ([]byte, error) {
299301
return nil, err
300302
}
301303

302-
return b, nil
304+
buf.Write(b)
305+
306+
attributeKeys := g.NestedObject.Attributes.SortedKeys()
307+
308+
// Recursively call ToFromFunctions() for each attribute that implements
309+
// ToFrom interface.
310+
for _, k := range attributeKeys {
311+
if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok {
312+
b, err := c.ToFromFunctions(k)
313+
314+
if err != nil {
315+
return nil, err
316+
}
317+
318+
buf.Write(b)
319+
}
320+
}
321+
322+
return buf.Bytes(), nil
303323
}

internal/datasource_generate/map_nested_attribute.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ func (g GeneratorMapNestedAttribute) ToFromFunctions(name string) ([]byte, error
209209
return nil, nil
210210
}
211211

212+
var buf bytes.Buffer
213+
212214
toFuncs := g.NestedObject.Attributes.ToFuncs()
213215

214216
fromFuncs := g.NestedObject.Attributes.FromFuncs()
@@ -221,5 +223,23 @@ func (g GeneratorMapNestedAttribute) ToFromFunctions(name string) ([]byte, error
221223
return nil, err
222224
}
223225

224-
return b, nil
226+
buf.Write(b)
227+
228+
attributeKeys := g.NestedObject.Attributes.SortedKeys()
229+
230+
// Recursively call ToFromFunctions() for each attribute that implements
231+
// ToFrom interface.
232+
for _, k := range attributeKeys {
233+
if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok {
234+
b, err := c.ToFromFunctions(k)
235+
236+
if err != nil {
237+
return nil, err
238+
}
239+
240+
buf.Write(b)
241+
}
242+
}
243+
244+
return buf.Bytes(), nil
225245
}

internal/datasource_generate/number_attribute.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,45 @@ func (g GeneratorNumberAttribute) ToFromFunctions(name string) ([]byte, error) {
167167

168168
return b, nil
169169
}
170+
171+
// AttrType returns a string representation of a basetypes.NumberTypable type.
172+
func (g GeneratorNumberAttribute) AttrType(name generatorschema.FrameworkIdentifier) string {
173+
if g.AssociatedExternalType != nil {
174+
return fmt.Sprintf("%sType{}", name.ToPascalCase())
175+
}
176+
177+
return "basetypes.NumberType{}"
178+
}
179+
180+
// AttrValue returns a string representation of a basetypes.NumberValuable type.
181+
func (g GeneratorNumberAttribute) AttrValue(name generatorschema.FrameworkIdentifier) string {
182+
if g.AssociatedExternalType != nil {
183+
return fmt.Sprintf("%sValue", name.ToPascalCase())
184+
}
185+
186+
return "basetypes.NumberValue"
187+
}
188+
189+
func (g GeneratorNumberAttribute) To() generatorschema.ToFromConversion {
190+
if g.AssociatedExternalType != nil {
191+
return generatorschema.ToFromConversion{
192+
AssocExtType: g.AssociatedExternalType,
193+
}
194+
}
195+
196+
return generatorschema.ToFromConversion{
197+
Default: "ValueBigFloat",
198+
}
199+
}
200+
201+
func (g GeneratorNumberAttribute) From() generatorschema.ToFromConversion {
202+
if g.AssociatedExternalType != nil {
203+
return generatorschema.ToFromConversion{
204+
AssocExtType: g.AssociatedExternalType,
205+
}
206+
}
207+
208+
return generatorschema.ToFromConversion{
209+
Default: "NumberValue",
210+
}
211+
}

internal/datasource_generate/set_nested_attribute.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ func (g GeneratorSetNestedAttribute) ToFromFunctions(name string) ([]byte, error
209209
return nil, nil
210210
}
211211

212+
var buf bytes.Buffer
213+
212214
toFuncs := g.NestedObject.Attributes.ToFuncs()
213215

214216
fromFuncs := g.NestedObject.Attributes.FromFuncs()
@@ -221,5 +223,23 @@ func (g GeneratorSetNestedAttribute) ToFromFunctions(name string) ([]byte, error
221223
return nil, err
222224
}
223225

224-
return b, nil
226+
buf.Write(b)
227+
228+
attributeKeys := g.NestedObject.Attributes.SortedKeys()
229+
230+
// Recursively call ToFromFunctions() for each attribute that implements
231+
// ToFrom interface.
232+
for _, k := range attributeKeys {
233+
if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok {
234+
b, err := c.ToFromFunctions(k)
235+
236+
if err != nil {
237+
return nil, err
238+
}
239+
240+
buf.Write(b)
241+
}
242+
}
243+
244+
return buf.Bytes(), nil
225245
}

internal/datasource_generate/set_nested_block.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ func (g GeneratorSetNestedBlock) ToFromFunctions(name string) ([]byte, error) {
287287
return nil, nil
288288
}
289289

290+
var buf bytes.Buffer
291+
290292
toFuncs := g.NestedObject.Attributes.ToFuncs()
291293

292294
fromFuncs := g.NestedObject.Attributes.FromFuncs()
@@ -299,5 +301,23 @@ func (g GeneratorSetNestedBlock) ToFromFunctions(name string) ([]byte, error) {
299301
return nil, err
300302
}
301303

302-
return b, nil
304+
buf.Write(b)
305+
306+
attributeKeys := g.NestedObject.Attributes.SortedKeys()
307+
308+
// Recursively call ToFromFunctions() for each attribute that implements
309+
// ToFrom interface.
310+
for _, k := range attributeKeys {
311+
if c, ok := g.NestedObject.Attributes[k].(generatorschema.ToFrom); ok {
312+
b, err := c.ToFromFunctions(k)
313+
314+
if err != nil {
315+
return nil, err
316+
}
317+
318+
buf.Write(b)
319+
}
320+
}
321+
322+
return buf.Bytes(), nil
303323
}

0 commit comments

Comments
 (0)