-
Notifications
You must be signed in to change notification settings - Fork 31
/
diff_suppress.go
65 lines (54 loc) · 1.3 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package hcl
import (
"bytes"
"encoding/json"
"reflect"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func equalLineByLine(s1, s2 string) bool {
parts1 := strings.Split(strings.TrimSpace(s1), "\n")
parts2 := strings.Split(strings.TrimSpace(s2), "\n")
if len(parts1) != len(parts2) {
return false
}
for idx := range parts1 {
part1 := strings.TrimSpace(parts1[idx])
part2 := strings.TrimSpace(parts2[idx])
if part1 != part2 {
return false
}
}
return true
}
func SuppressEOT(k, old, new string, d *schema.ResourceData) bool {
return equalLineByLine(old, new)
}
func JSONStringsEqual(s1, s2 string) bool {
b1 := bytes.NewBufferString("")
if err := json.Compact(b1, []byte(s1)); err != nil {
return false
}
b2 := bytes.NewBufferString("")
if err := json.Compact(b2, []byte(s2)); err != nil {
return false
}
return JSONBytesEqual(b1.Bytes(), b2.Bytes())
}
func JSONBytesEqual(b1, b2 []byte) bool {
var o1 interface{}
if err := json.Unmarshal(b1, &o1); err != nil {
return false
}
var o2 interface{}
if err := json.Unmarshal(b2, &o2); err != nil {
return false
}
return reflect.DeepEqual(o1, o2)
}
func SuppressJSONorEOT(k, old, new string, d *schema.ResourceData) bool {
if JSONStringsEqual(old, new) {
return true
}
return equalLineByLine(old, new)
}