Skip to content

Commit 753e83f

Browse files
authored
Fix provider code generation when its schema has no attributes (#87)
The context import was previously missing.
1 parent b66db47 commit 753e83f

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

internal/cmd/generate_provider_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func TestGenerateProviderCommand(t *testing.T) {
2121
irInputPath: "testdata/custom_and_external/ir.json",
2222
goldenFileDir: "testdata/custom_and_external/provider_output",
2323
},
24+
"provider_no_attributes": {
25+
irInputPath: "testdata/provider_no_attributes/ir.json",
26+
goldenFileDir: "testdata/provider_no_attributes/provider_output",
27+
},
2428
}
2529
for name, testCase := range testCases {
2630
name, testCase := name, testCase
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"provider": {
3+
"name": "example"
4+
},
5+
"version": "0.1"
6+
}

internal/cmd/testdata/provider_no_attributes/provider_output/example_provider_gen.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/schema/schema.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ type GeneratorSchema struct {
2727
func (g GeneratorSchema) Imports() (string, error) {
2828
imports := NewImports()
2929

30+
imports.Add(
31+
code.Import{
32+
Path: ContextImport,
33+
},
34+
)
35+
3036
for _, a := range g.Attributes {
3137
if a == nil {
3238
continue
3339
}
3440

35-
imports.Add([]code.Import{
36-
{
37-
Path: ContextImport,
38-
},
39-
}...)
40-
4141
if _, ok := a.(Attributes); ok {
4242
imports.Add([]code.Import{
4343
{

0 commit comments

Comments
 (0)