-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed artifact_registry_repository permadiff in unordered array #9376
Changes from 4 commits
5d2ec71
27fe321
f9607ee
8a2d5cc
5d80838
3bc1882
c03db5f
f3c2461
f66a055
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<%# The license inside this block applies to this file | ||
# Copyright 2023 Google Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
-%> | ||
func upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bool { | ||
log.Printf("%v, %v, %v\n", k, old, new) | ||
o, n := d.GetChange("virtual_repository_config.0.upstream_policies") | ||
oMap, ok := o.([]any) | ||
if !ok { | ||
return false | ||
} | ||
nMap, ok := n.([]any) | ||
if !ok { | ||
return false | ||
} | ||
hashFunc := func(val any) string { | ||
x := val.(map[string]any) | ||
return fmt.Sprintf("[id:%v priority:%v repository:%v]", x["id"], x["priority"], x["repository"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is equivalent to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if golang is consistent with map key order within the same process. This was to prevent two maps from outputting different strings with the same information. |
||
} | ||
|
||
return arrayDiffSuppress(oMap, nMap, hashFunc) | ||
} | ||
|
||
func arrayDiffSuppress(old, new []any, hashFunc func(x any) string) bool { | ||
oldMap := map[string]bool{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're just making a set here we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced with schema.Set. |
||
newMap := map[string]bool{} | ||
for _, x := range old { | ||
key := hashFunc(x) | ||
oldMap[key] = true | ||
} | ||
for _, y := range new { | ||
key := hashFunc(y) | ||
newMap[key] = true | ||
} | ||
if len(oldMap) != len(newMap) { | ||
return false | ||
} | ||
for x := range oldMap { | ||
if !newMap[x] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless we have a reason to do this by hand, it would probably be better to use the sdk provided set methods such as schema.NewSet and Set.Equal to ensure we don't miss anything with casting the types around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed! Switched over to using schema.Set.