Skip to content

Commit

Permalink
feat: use go-ucfg instead of json to compare json objects
Browse files Browse the repository at this point in the history
Signed-off-by: disaster37 <[email protected]>
  • Loading branch information
disaster37 committed Mar 8, 2022
1 parent dcfc645 commit ff45bf6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 23 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/elastic/go-ucfg v0.8.4 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-resty/resty/v2 v2.7.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disaster37/go-kibana-rest/v7 v7.12.1-3 h1:d5R8AzbS8QhUSYO2C34rNYCO1rVRJvXwcNCFTzWgf8E=
github.com/disaster37/go-kibana-rest/v7 v7.12.1-3/go.mod h1:akcYbyS5mkIAnA+2Y4lO2/NmfYlynFdzhuFqmf2z6C8=
github.com/elastic/go-ucfg v0.8.4 h1:OAHTnubzXKsYYYWVzl8psLcS5mCbNKjXxtMY41itthk=
github.com/elastic/go-ucfg v0.8.4/go.mod h1:4E8mPOLSUV9hQ7sgLEJ4bvt0KhMuDJa8joDT2QGAEKA=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -690,13 +692,15 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/hjson/hjson-go.v3 v3.0.1/go.mod h1:X6zrTSVeImfwfZLfgQdInl9mWjqPqgH90jom9nym/lw=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
121 changes: 98 additions & 23 deletions kb/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package kb

import (
"encoding/json"
"reflect"
"strings"

"github.com/elastic/go-ucfg"
"github.com/elastic/go-ucfg/diff"
ucfgjson "github.com/elastic/go-ucfg/json"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
)

// suppressEquivalentJSON permit to compare json string
func suppressEquivalentJSON(k, old, new string, d *schema.ResourceData) bool {
var oldObj, newObj interface{}
if err := json.Unmarshal([]byte(old), &oldObj); err != nil {
confOld, err := ucfgjson.NewConfig([]byte(old), ucfg.PathSep("."))
if err != nil {
log.Errorf("Error on suppressEquivalentJSON: %s", err.Error())
return false
}
if err := json.Unmarshal([]byte(new), &newObj); err != nil {
confNew, err := ucfgjson.NewConfig([]byte(new), ucfg.PathSep("."))
if err != nil {
log.Errorf("Error on suppressEquivalentJSON: %s", err.Error())
return false
}

return reflect.DeepEqual(oldObj, newObj)
currentDiff := diff.CompareConfigs(confOld, confNew)
log.Debugf("Diff\n: %s", currentDiff.GoStringer())

return !currentDiff.HasChanged()
}

// suppressEquivalentNDJSON permit to compare ndjson string
Expand All @@ -27,41 +36,55 @@ func suppressEquivalentNDJSON(k, old, new string, d *schema.ResourceData) bool {
// NDJSON mean sthat each line correspond to JSON struct
oldSlice := strings.Split(old, "\n")
newSlice := strings.Split(new, "\n")
oldObjSlice := make([]map[string]interface{}, len(oldSlice))
newObjSlice := make([]map[string]interface{}, len(newSlice))
oldObjSlice := make([]*ucfg.Config, len(oldSlice))
newObjSlice := make([]*ucfg.Config, len(newSlice))
if len(oldSlice) != len(newSlice) {
return false
}

// Convert string line to JSON
for i, oldJSON := range oldSlice {
jsonObj := make(map[string]interface{})
if err := json.Unmarshal([]byte(oldJSON), &jsonObj); err != nil {
config, err := ucfgjson.NewConfig([]byte(oldJSON), ucfg.PathSep("."))
if err != nil {
log.Errorf("Error on suppressEquivalentNDJSON: %s", err.Error())
return false
}
config.Remove("version", -1)
config.Remove("updated_at", -1)

delete(jsonObj, "version")
delete(jsonObj, "updated_at")

oldObjSlice[i] = jsonObj
oldObjSlice[i] = config
}
for i, newJSON := range newSlice {
jsonObj := make(map[string]interface{})
if err := json.Unmarshal([]byte(newJSON), &jsonObj); err != nil {
config, err := ucfgjson.NewConfig([]byte(newJSON), ucfg.PathSep("."))
if err != nil {
log.Errorf("Error on suppressEquivalentNDJSON: %s", err.Error())
return false
}
delete(jsonObj, "version")
delete(jsonObj, "updated_at")
config.Remove("version", -1)
config.Remove("updated_at", -1)

newObjSlice[i] = jsonObj
newObjSlice[i] = config
}

// Compare json obj
for _, oldJSON := range oldObjSlice {
for _, oldConfig := range oldObjSlice {
isFound := false
for _, newJSON := range newObjSlice {
if oldJSON["id"].(string) == newJSON["id"].(string) {
if reflect.DeepEqual(oldJSON, newJSON) == false {
oldId, err := oldConfig.String("id", -1)
if err != nil {
log.Errorf("Error on suppressEquivalentNDJSON: %s", err.Error())
return false
}
for _, newConfig := range newObjSlice {
newId, err := newConfig.String("id", -1)
if err != nil {
log.Errorf("Error on suppressEquivalentNDJSON: %s", err.Error())
return false
}
if oldId == newId {
currentDiff := diff.CompareConfigs(oldConfig, newConfig)
log.Debugf("Diff\n: %s", currentDiff.GoStringer())

if currentDiff.HasChanged() {
return false
}
isFound = true
Expand All @@ -74,6 +97,58 @@ func suppressEquivalentNDJSON(k, old, new string, d *schema.ResourceData) bool {
}
}

/*
// NDJSON mean sthat each line correspond to JSON struct
oldSlice := strings.Split(old, "\n")
newSlice := strings.Split(new, "\n")
oldObjSlice := make([]map[string]interface{}, len(oldSlice))
newObjSlice := make([]map[string]interface{}, len(newSlice))
if len(oldSlice) != len(newSlice) {
return false
}
// Convert string line to JSON
for i, oldJSON := range oldSlice {
jsonObj := make(map[string]interface{})
if err := json.Unmarshal([]byte(oldJSON), &jsonObj); err != nil {
return false
}
delete(jsonObj, "version")
delete(jsonObj, "updated_at")
oldObjSlice[i] = jsonObj
}
for i, newJSON := range newSlice {
jsonObj := make(map[string]interface{})
if err := json.Unmarshal([]byte(newJSON), &jsonObj); err != nil {
return false
}
delete(jsonObj, "version")
delete(jsonObj, "updated_at")
newObjSlice[i] = jsonObj
}
// Compare json obj
for _, oldJSON := range oldObjSlice {
isFound := false
for _, newJSON := range newObjSlice {
if oldJSON["id"].(string) == newJSON["id"].(string) {
if reflect.DeepEqual(oldJSON, newJSON) == false {
return false
}
isFound = true
break
}
}
if isFound == false {
return false
}
}
*/

return true

}

0 comments on commit ff45bf6

Please sign in to comment.