Skip to content

Commit

Permalink
Merge resources recursively (GoogleCloudPlatform#7783)
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge committed Apr 20, 2023
1 parent b5c0617 commit 20aae6d
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions tools/missing-test-detector/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,7 @@ func readHCLBlockBody(body hcl.Body, fileBytes []byte) (Resource, error) {
if existing, ok := m[block.Type]; ok {
// Merge the fields from the current block into the existing resource config.
if existingResource, ok := existing.(Resource); ok {
for k, v := range blockConfig {
if _, ok := existingResource[k]; !ok {
existingResource[k] = v
}
}
mergeResources(existingResource, blockConfig)
}
} else {
m[block.Type] = blockConfig
Expand All @@ -292,3 +288,18 @@ func readHCLBlockBody(body hcl.Body, fileBytes []byte) (Resource, error) {
}
return m, nil
}

// Perform a recursive one-way merge of b into a.
func mergeResources(a, b Resource) {
for k, bv := range b {
if av, ok := a[k]; ok {
if avr, ok := av.(Resource); ok {
if bvr, ok := bv.(Resource); ok {
mergeResources(avr, bvr)
}
}
} else {
a[k] = bv
}
}
}

0 comments on commit 20aae6d

Please sign in to comment.