Skip to content

Commit 87aed70

Browse files
authored
Refactor merging functionality + ensure descriptions are populated if possible (#50)
* move logic over to new package * move logic over to new package * add initial merge refactor * add more tests * add license headers
1 parent 900db91 commit 87aed70

File tree

11 files changed

+3656
-143
lines changed

11 files changed

+3656
-143
lines changed

internal/cmd/testdata/github/generated_framework_ir.json

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6824,31 +6824,36 @@
68246824
{
68256825
"name": "allow_auto_merge",
68266826
"bool": {
6827-
"computed_optional_required": "computed"
6827+
"computed_optional_required": "computed",
6828+
"description": "Whether to allow Auto-merge to be used on pull requests."
68286829
}
68296830
},
68306831
{
68316832
"name": "allow_merge_commit",
68326833
"bool": {
6833-
"computed_optional_required": "computed"
6834+
"computed_optional_required": "computed",
6835+
"description": "Whether to allow merge commits for pull requests."
68346836
}
68356837
},
68366838
{
68376839
"name": "allow_rebase_merge",
68386840
"bool": {
6839-
"computed_optional_required": "computed"
6841+
"computed_optional_required": "computed",
6842+
"description": "Whether to allow rebase merges for pull requests."
68406843
}
68416844
},
68426845
{
68436846
"name": "allow_squash_merge",
68446847
"bool": {
6845-
"computed_optional_required": "computed"
6848+
"computed_optional_required": "computed",
6849+
"description": "Whether to allow squash merges for pull requests."
68466850
}
68476851
},
68486852
{
68496853
"name": "allow_update_branch",
68506854
"bool": {
6851-
"computed_optional_required": "computed"
6855+
"computed_optional_required": "computed",
6856+
"description": "Whether or not a pull request head branch that is behind its base branch can always be updated even if it is not required to be up to date before merging."
68526857
}
68536858
},
68546859
{
@@ -6860,7 +6865,8 @@
68606865
{
68616866
"name": "archived",
68626867
"bool": {
6863-
"computed_optional_required": "computed"
6868+
"computed_optional_required": "computed",
6869+
"description": "Whether the repository is archived."
68646870
}
68656871
},
68666872
{
@@ -6932,13 +6938,15 @@
69326938
{
69336939
"name": "default_branch",
69346940
"string": {
6935-
"computed_optional_required": "computed"
6941+
"computed_optional_required": "computed",
6942+
"description": "The default branch of the repository."
69366943
}
69376944
},
69386945
{
69396946
"name": "delete_branch_on_merge",
69406947
"bool": {
6941-
"computed_optional_required": "computed"
6948+
"computed_optional_required": "computed",
6949+
"description": "Whether to delete head branches when pull requests are merged"
69426950
}
69436951
},
69446952
{
@@ -6956,7 +6964,8 @@
69566964
{
69576965
"name": "disabled",
69586966
"bool": {
6959-
"computed_optional_required": "computed"
6967+
"computed_optional_required": "computed",
6968+
"description": "Returns whether or not this repository disabled."
69606969
}
69616970
},
69626971
{
@@ -7022,13 +7031,15 @@
70227031
{
70237032
"name": "has_downloads",
70247033
"bool": {
7025-
"computed_optional_required": "computed"
7034+
"computed_optional_required": "computed",
7035+
"description": "Whether downloads are enabled."
70267036
}
70277037
},
70287038
{
70297039
"name": "has_issues",
70307040
"bool": {
7031-
"computed_optional_required": "computed"
7041+
"computed_optional_required": "computed",
7042+
"description": "Whether issues are enabled."
70327043
}
70337044
},
70347045
{
@@ -7040,13 +7051,15 @@
70407051
{
70417052
"name": "has_projects",
70427053
"bool": {
7043-
"computed_optional_required": "computed"
7054+
"computed_optional_required": "computed",
7055+
"description": "Whether projects are enabled."
70447056
}
70457057
},
70467058
{
70477059
"name": "has_wiki",
70487060
"bool": {
7049-
"computed_optional_required": "computed"
7061+
"computed_optional_required": "computed",
7062+
"description": "Whether the wiki is enabled."
70507063
}
70517064
},
70527065
{
@@ -7070,13 +7083,15 @@
70707083
{
70717084
"name": "id",
70727085
"int64": {
7073-
"computed_optional_required": "computed"
7086+
"computed_optional_required": "computed",
7087+
"description": "Unique identifier of the repository"
70747088
}
70757089
},
70767090
{
70777091
"name": "is_template",
70787092
"bool": {
7079-
"computed_optional_required": "computed"
7093+
"computed_optional_required": "computed",
7094+
"description": "Whether this repository acts as a template that can be used to generate new repositories."
70807095
}
70817096
},
70827097
{
@@ -7156,7 +7171,8 @@
71567171
{
71577172
"name": "name",
71587173
"string": {
7159-
"computed_optional_required": "computed"
7174+
"computed_optional_required": "computed",
7175+
"description": "The name of the repository."
71607176
}
71617177
},
71627178
{
@@ -7314,7 +7330,8 @@
73147330
"computed_optional_required": "computed"
73157331
}
73167332
}
7317-
]
7333+
],
7334+
"description": "A GitHub user."
73187335
}
73197336
},
73207337
{
@@ -7358,7 +7375,8 @@
73587375
{
73597376
"name": "private",
73607377
"bool": {
7361-
"computed_optional_required": "computed"
7378+
"computed_optional_required": "computed",
7379+
"description": "Whether the repository is private or public."
73627380
}
73637381
},
73647382
{
@@ -7382,7 +7400,8 @@
73827400
{
73837401
"name": "size",
73847402
"int64": {
7385-
"computed_optional_required": "computed"
7403+
"computed_optional_required": "computed",
7404+
"description": "The size of the repository. Size is calculated hourly. When a repository is initially created, the size is 0."
73867405
}
73877406
},
73887407
{
@@ -7495,13 +7514,15 @@
74957514
{
74967515
"name": "use_squash_pr_title_as_default",
74977516
"bool": {
7498-
"computed_optional_required": "computed"
7517+
"computed_optional_required": "computed",
7518+
"description": "Whether a squash merge commit can use the pull request title as default. **This property has been deprecated. Please use `squash_merge_commit_title` instead."
74997519
}
75007520
},
75017521
{
75027522
"name": "visibility",
75037523
"string": {
7504-
"computed_optional_required": "computed"
7524+
"computed_optional_required": "computed",
7525+
"description": "The repository visibility: public, private, or internal."
75057526
}
75067527
},
75077528
{
@@ -8433,7 +8454,8 @@
84338454
"description": "Whether to require contributors to sign off on web-based commits"
84348455
}
84358456
}
8436-
]
8457+
],
8458+
"description": "A repository on GitHub."
84378459
}
84388460
},
84398461
{

internal/cmd/testdata/petstore3/generated_framework_ir.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@
167167
{
168168
"name": "id",
169169
"int64": {
170-
"computed_optional_required": "computed_optional"
170+
"computed_optional_required": "computed_optional",
171+
"description": "ID of order that needs to be fetched"
171172
}
172173
},
173174
{
@@ -237,7 +238,8 @@
237238
{
238239
"name": "id",
239240
"int64": {
240-
"computed_optional_required": "computed_optional"
241+
"computed_optional_required": "computed_optional",
242+
"description": "ID of pet to return"
241243
}
242244
},
243245
{
@@ -349,7 +351,8 @@
349351
{
350352
"name": "username",
351353
"string": {
352-
"computed_optional_required": "computed_optional"
354+
"computed_optional_required": "computed_optional",
355+
"description": "The name that needs to be fetched. Use user1 for testing. "
353356
}
354357
}
355358
]

internal/mapper/datasource_mapper.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
1111
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
12+
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/merge"
1213
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/oas"
1314
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/util"
1415
"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
@@ -117,48 +118,11 @@ func generateDataSourceSchema(dataSource explorer.DataSource) (*datasource.Schem
117118
}
118119
}
119120

120-
dataSourceAttributes := mergeDataSourceAttributes(
121+
dataSourceAttributes := merge.MergeDataSourceAttributes(
121122
readParameterAttributes,
122123
*readResponseAttributes,
123124
)
124125

125126
dataSourceSchema.Attributes = *dataSourceAttributes
126127
return dataSourceSchema, nil
127128
}
128-
129-
// mainSlice takes priority in the merge, will have each subsequent mergeAttributeSlice applied in sequence
130-
// - No re-ordering of the mainSlice is done, so will append new attributes as they are encountered
131-
func mergeDataSourceAttributes(mainSlice []datasource.Attribute, mergeAttributeSlices ...[]datasource.Attribute) *[]datasource.Attribute {
132-
for _, attributeSlice := range mergeAttributeSlices {
133-
134-
for _, compareAttribute := range attributeSlice {
135-
isNewAttribute := true
136-
137-
for mainIndex, mainAttribute := range mainSlice {
138-
if mainAttribute.Name == compareAttribute.Name {
139-
// Handle types that require nested merging
140-
if mainAttribute.SingleNested != nil && compareAttribute.SingleNested != nil {
141-
mergedAttributes := mergeDataSourceAttributes(mainAttribute.SingleNested.Attributes, compareAttribute.SingleNested.Attributes)
142-
mainSlice[mainIndex].SingleNested.Attributes = *mergedAttributes
143-
} else if mainAttribute.ListNested != nil && compareAttribute.ListNested != nil {
144-
mergedAttributes := mergeDataSourceAttributes(mainAttribute.ListNested.NestedObject.Attributes, compareAttribute.ListNested.NestedObject.Attributes)
145-
mainSlice[mainIndex].ListNested.NestedObject.Attributes = *mergedAttributes
146-
} else if mainAttribute.List != nil && compareAttribute.List != nil {
147-
mergedElementType := mergeElementType(mainAttribute.List.ElementType, compareAttribute.List.ElementType)
148-
mainSlice[mainIndex].List.ElementType = mergedElementType
149-
}
150-
151-
isNewAttribute = false
152-
break
153-
}
154-
}
155-
156-
if isNewAttribute {
157-
// Add this back to the original slice to avoid adding duplicate attributes from different mergeAttributeSlices
158-
mainSlice = append(mainSlice, compareAttribute)
159-
}
160-
}
161-
162-
}
163-
return &mainSlice
164-
}

internal/mapper/element_type_mapper.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)