Skip to content

Commit

Permalink
Modify imports that are included during code generation (#41)
Browse files Browse the repository at this point in the history
* Import for context is required for simple schemas containing only primitive attributes

* Import for basetypes is required when generating custom type and value types for nested attributes/blocks
  • Loading branch information
bendbennett authored Sep 11, 2023
1 parent d6b8fc6 commit 7daebab
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ type GeneratorSchema struct {
func (g GeneratorSchema) ImportsString() (string, error) {
imports := NewImports()

// Both context and terraform-plugin-framework/diag packages are required if
// model object helpers are generated. Refer to the logic in
// ModelsObjectHelpersBytes() method.
for _, a := range g.Attributes {
if a == nil {
continue
}

imports.Add([]code.Import{
{
Path: ContextImport,
},
}...)

if _, ok := a.(Attributes); ok {
imports.Add([]code.Import{
{
Path: ContextImport,
},
{
Path: FmtImport,
},
Expand All @@ -56,23 +56,26 @@ func (g GeneratorSchema) ImportsString() (string, error) {
{
Path: TfTypesImport,
},
{
Path: BaseTypesImport,
},
}...)
}
}

// Both context and terraform-plugin-framework/diag packages are required if
// model object helpers are generated. Refer to the logic in
// ModelsObjectHelpersBytes() method.
for _, b := range g.Blocks {
if b == nil {
continue
}

imports.Add([]code.Import{
{
Path: ContextImport,
},
}...)

if _, ok := b.(Blocks); ok {
imports.Add([]code.Import{
{
Path: ContextImport,
},
{
Path: FmtImport,
},
Expand All @@ -88,6 +91,9 @@ func (g GeneratorSchema) ImportsString() (string, error) {
{
Path: TfTypesImport,
},
{
Path: BaseTypesImport,
},
}...)
}
}
Expand Down

0 comments on commit 7daebab

Please sign in to comment.