Skip to content

Commit

Permalink
Initial implementation for generation of to-from (expand-flatten) fun…
Browse files Browse the repository at this point in the history
…ctions (#31)
  • Loading branch information
bendbennett committed Aug 7, 2023
1 parent bb5f51a commit 4fc770c
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 10 deletions.
14 changes: 13 additions & 1 deletion internal/cmd/generate_data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func generateDataSourceCode(spec spec.Specification, outputPath, packageName, ge
log.Fatal(err)
}

// generate "expand" and "flatten" code
modelsToFromBytes, err := g.ModelsToFromBytes()
if err != nil {
log.Fatal(err)
}

// format schema code
formattedDataSourcesSchema, err := format.Format(schemaBytes)
if err != nil {
Expand All @@ -170,8 +176,14 @@ func generateDataSourceCode(spec spec.Specification, outputPath, packageName, ge
log.Fatal(err)
}

// format "expand" and "flatten" code
formattedDataSourcesToFrom, err := format.Format(modelsToFromBytes)
if err != nil {
log.Fatal(err)
}

// write code
err = output.WriteDataSources(formattedDataSourcesSchema, formattedDataSourcesModels, formattedDataSourcesModelObjectHelpers, outputPath)
err = output.WriteDataSources(formattedDataSourcesSchema, formattedDataSourcesModels, formattedDataSourcesModelObjectHelpers, formattedDataSourcesToFrom, outputPath)
if err != nil {
return fmt.Errorf("error writing Go code to output: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions internal/datasource_convert/single_nested_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func convertSingleNestedBlock(b *datasource.SingleNestedBlock) (datasource_gener

Attributes: attributes,
Blocks: blocks,
CustomType: b.CustomType,
Validators: b.Validators,

AssociatedExternalType: b.AssociatedExternalType,
CustomType: b.CustomType,
Validators: b.Validators,
}, nil
}
19 changes: 15 additions & 4 deletions internal/datasource_generate/single_nested_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ type GeneratorSingleNestedBlock struct {

// The "specschema" types are used instead of the types within the attribute
// because support for extracting custom import information is required.
Attributes generatorschema.GeneratorAttributes
Blocks generatorschema.GeneratorBlocks
CustomType *specschema.CustomType
Validators []specschema.ObjectValidator
AssociatedExternalType *specschema.AssociatedExternalType
Attributes generatorschema.GeneratorAttributes
Blocks generatorschema.GeneratorBlocks
CustomType *specschema.CustomType
Validators []specschema.ObjectValidator
}

func (g GeneratorSingleNestedBlock) AssocExtType() *generatorschema.AssocExtType {
return generatorschema.NewAssocExtType(g.AssociatedExternalType)
}

func (g GeneratorSingleNestedBlock) AttrType() attr.Type {
Expand Down Expand Up @@ -52,6 +57,12 @@ func (g GeneratorSingleNestedBlock) Imports() *generatorschema.Imports {
imports.Append(v.Imports())
}

// TODO: This should only be added if model object helper functions are being generated.
imports.Append(generatorschema.AttrImports())

assocExtTypeImports := generatorschema.AssociatedExternalTypeImports(g.AssociatedExternalType)
imports.Append(assocExtTypeImports)

return imports
}

Expand Down
48 changes: 46 additions & 2 deletions internal/datasource_generate/single_nested_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: generatorschema.TypesImport,
},
{
Path: generatorschema.AttrImport,
},
},
},
"custom-type-without-import": {
input: GeneratorSingleNestedBlock{
CustomType: &specschema.CustomType{},
},
expected: []code.Import{},
expected: []code.Import{
{
Path: generatorschema.AttrImport,
},
},
},
"custom-type-with-import-empty-string": {
input: GeneratorSingleNestedBlock{
Expand All @@ -43,7 +50,11 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
},
},
},
expected: []code.Import{},
expected: []code.Import{
{
Path: generatorschema.AttrImport,
},
},
},
"custom-type-with-import": {
input: GeneratorSingleNestedBlock{
Expand All @@ -57,6 +68,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: "github.com/my_account/my_project/attribute",
},
{
Path: generatorschema.AttrImport,
},
},
},
"nested-attribute-list": {
Expand All @@ -73,6 +87,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: generatorschema.TypesImport,
},
{
Path: generatorschema.AttrImport,
},
},
},
"nested-attribute-list-with-custom-type": {
Expand All @@ -94,6 +111,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: "github.com/my_account/my_project/nested_list",
},
{
Path: generatorschema.AttrImport,
},
},
},
"nested-list-with-custom-type-with-element-with-custom-type": {
Expand Down Expand Up @@ -127,6 +147,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: "github.com/my_account/my_project/bool",
},
{
Path: generatorschema.AttrImport,
},
},
},
"nested-attribute-object": {
Expand Down Expand Up @@ -170,6 +193,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: "github.com/my_account/my_project/nested_object",
},
{
Path: generatorschema.AttrImport,
},
},
},
"nested-object-with-custom-type-with-attribute-with-custom-type": {
Expand Down Expand Up @@ -206,6 +232,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: "github.com/my_account/my_project/bool",
},
{
Path: generatorschema.AttrImport,
},
},
},
"nested-block-with-custom-type": {
Expand All @@ -227,6 +256,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: "github.com/my_account/my_project/nested_block",
},
{
Path: generatorschema.AttrImport,
},
},
},
"validator-custom-nil": {
Expand All @@ -240,6 +272,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: generatorschema.TypesImport,
},
{
Path: generatorschema.AttrImport,
},
},
},
"validator-custom-import-nil": {
Expand All @@ -253,6 +288,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: generatorschema.TypesImport,
},
{
Path: generatorschema.AttrImport,
},
},
},
"validator-custom-import-empty-string": {
Expand All @@ -272,6 +310,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: generatorschema.TypesImport,
},
{
Path: generatorschema.AttrImport,
},
},
},
"validator-custom-import": {
Expand Down Expand Up @@ -309,6 +350,9 @@ func TestGeneratorSingleNestedBlock_Imports(t *testing.T) {
{
Path: "github.com/myproject/myvalidators/validator",
},
{
Path: generatorschema.AttrImport,
},
},
},
}
Expand Down
7 changes: 6 additions & 1 deletion internal/output/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"path/filepath"
)

func WriteDataSources(dataSourcesSchema, dataSourcesModels, dataSourcesModelObjectHelpers map[string][]byte, outputDir string) error {
func WriteDataSources(dataSourcesSchema, dataSourcesModels, dataSourcesModelObjectHelpers, dataSourcesToFrom map[string][]byte, outputDir string) error {
for k, v := range dataSourcesSchema {
filename := fmt.Sprintf("%s_data_source_gen.go", k)

Expand All @@ -34,6 +34,11 @@ func WriteDataSources(dataSourcesSchema, dataSourcesModels, dataSourcesModelObje
if err != nil {
return err
}

_, err = f.Write(dataSourcesToFrom[k])
if err != nil {
return err
}
}

return nil
Expand Down
37 changes: 37 additions & 0 deletions internal/schema/associated_external_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package schema

import (
"strings"

"github.com/hashicorp/terraform-plugin-codegen-spec/code"
"github.com/hashicorp/terraform-plugin-codegen-spec/schema"
)

type AssocExtType struct {
imp *code.Import
typ string
}

func NewAssocExtType(assocExtType *schema.AssociatedExternalType) *AssocExtType {
if assocExtType == nil {
return nil
}

return &AssocExtType{
imp: assocExtType.Import,
typ: assocExtType.Type,
}
}

func (a AssocExtType) Type() string {
return a.typ
}

func (a AssocExtType) TypeReference() string {
tr, _ := strings.CutPrefix(a.typ, "*")

return tr
}
33 changes: 33 additions & 0 deletions internal/schema/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

const (
AttrImport = "github.com/hashicorp/terraform-plugin-framework/attr"
BaseTypesImport = "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
ContextImport = "context"
DiagImport = "github.com/hashicorp/terraform-plugin-framework/diag"
PlanModifierImport = "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
Expand Down Expand Up @@ -123,6 +124,16 @@ func GetElementTypeImports(e specschema.ElementType) *Imports {
}
}

func AttrImports() *Imports {
imports := NewImports()

imports.Add(code.Import{
Path: AttrImport,
})

return imports
}

func GetAttrTypesImports(customType *specschema.CustomType, attrTypes []specschema.ObjectAttributeType) *Imports {
imports := NewImports()

Expand Down Expand Up @@ -297,3 +308,25 @@ func CustomValidatorImports(cv *specschema.CustomValidator) *Imports {

return imports
}

func AssociatedExternalTypeImports(a *specschema.AssociatedExternalType) *Imports {
imports := NewImports()

if a == nil {
return imports
}

if !a.HasImport() {
return imports
}

if len(a.Import.Path) > 0 {
imports.Add(*a.Import)

imports.Add(code.Import{
Path: BaseTypesImport,
})
}

return imports
}
Loading

0 comments on commit 4fc770c

Please sign in to comment.