-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
diff_suppress.go
42 lines (35 loc) · 1.64 KB
/
diff_suppress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package apimanagement
import (
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
)
// XmlWithDotNetInterpolationsDiffSuppress is a Diff Suppress Func for when the XML contains
// .net interpolations, and thus isn't valid XML to parse
// whilst really we should be parsing the XML Tokens and skipping over the error - in practice
func XmlWithDotNetInterpolationsDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// try parsing this as valid xml if we can, to handle ordering differences
same := suppress.XmlDiff(k, old, new, d)
if same {
return same
}
// otherwise best-effort this via string comparison
oldVal := normalizeXmlWithDotNetInterpolationsString(old)
newVal := normalizeXmlWithDotNetInterpolationsString(new)
return oldVal == newVal
}
// normalizeXmlWithDotNetInterpolationsString is intended as a fallback to diff two xml strings
// containing .net interpolations, which means that they aren't directly valid xml
// whilst we /could/ xml.EscapeString these that encodes the entire string, rather than the expression
// we could do that as a potential extension, but this seems sufficient in testing :shrug:
func normalizeXmlWithDotNetInterpolationsString(input string) string {
value := input
value = strings.ReplaceAll(value, "\n", "")
value = strings.ReplaceAll(value, "\r", "")
value = strings.ReplaceAll(value, "\t", "")
value = strings.ReplaceAll(value, " ", "")
value = strings.ReplaceAll(value, " ", "")
value = strings.ReplaceAll(value, " ", "")
value = strings.ReplaceAll(value, """, "\"")
return value
}