MSP-5223 update compare actual data config and update request data config
This commit is contained in:
1 parent
867fd98bce
commit
28be68322c
3 files changed
+24
-9
No files matched your search
@@ -19,6 +19,7 @@ require (
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/tidwall/gjson v1.18.0
|
||||
github.com/txix-open/etp/v4 v4.0.1
|
||||
github.com/txix-open/go-cmp v0.0.0-20240401171944-16b203530f60
|
||||
github.com/txix-open/isp-kit v1.46.0
|
||||
github.com/xeipuuv/gojsonschema v1.2.0
|
||||
golang.org/x/sync v0.12.0
|
||||
|
||||
@@ -262,6 +262,8 @@ github.com/txix-open/bellows v1.2.0 h1:CXv8nQaZtB/micraeRilYyj/gtfv+bqBgP5aPYQgj
|
||||
github.com/txix-open/bellows v1.2.0/go.mod h1:qbKCy+RTgD30Qpw1fyb3y3jp5Y9mGhLLxgae1l0W92o=
|
||||
github.com/txix-open/etp/v4 v4.0.1 h1:5VSYgGsvnMz0tvUHsjVWFETI0qWBTCF1O3ARCHaCIgA=
|
||||
github.com/txix-open/etp/v4 v4.0.1/go.mod h1:FIu7TUDdwcgmtmF6tFTV1bGA2fRY/iVaPQdsGzvqYc8=
|
||||
github.com/txix-open/go-cmp v0.0.0-20240401171944-16b203530f60 h1:7SZ72lNU69g8eiT5rcRh1ubATZPs/J05NEIN7E47TYM=
|
||||
github.com/txix-open/go-cmp v0.0.0-20240401171944-16b203530f60/go.mod h1:jzpq0uwcdGq0OSNXZ6vv6UJkCOU/fLPgeoz+KkGDYgo=
|
||||
github.com/txix-open/grmq v1.7.0 h1:nwenrlxEjXpK0EBCa/WbIxK+y0mF2pZxNKbheFjQVsI=
|
||||
github.com/txix-open/grmq v1.7.0/go.mod h1:DAfbV4jLyxQBLG8OYJVy8kCbhucdlu/C/wl+nUDc9p8=
|
||||
github.com/txix-open/isp-kit v1.46.0 h1:Rqw5X+501vEwMv6oGvC6sYU1++1XiTvxt6Ja/CD7lRw=
|
||||
|
||||
+21
-9
@@ -6,6 +6,8 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/go-cmp/cmp"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
"isp-config-service/domain"
|
||||
@@ -144,7 +146,11 @@ func (c Config) CreateUpdateConfig(
|
||||
return nil, entity.ErrConfigNotFound
|
||||
}
|
||||
|
||||
if oldConfig.Name == req.Name && isEqualData(oldConfig.Data, req.Data) {
|
||||
isMatch, err := isEqualConfigs(oldConfig, req)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "compare data configs")
|
||||
}
|
||||
if isMatch {
|
||||
c.logger.Info(ctx, "no changes in config; skip update")
|
||||
result := configToDto(*oldConfig, nil)
|
||||
return &result, nil
|
||||
@@ -391,16 +397,22 @@ func validateConfig(config []byte, schema []byte) (map[string]string, error) {
|
||||
return details, nil
|
||||
}
|
||||
|
||||
func isEqualData(oldData, reqData []byte) bool {
|
||||
if len(oldData) != len(reqData) {
|
||||
return false
|
||||
func isEqualConfigs(oldConfig *entity.Config, reqConfig domain.CreateUpdateConfigRequest) (bool, error) {
|
||||
if oldConfig.Name != reqConfig.Name {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
for i := range oldData {
|
||||
if oldData[i] != reqData[i] {
|
||||
return false
|
||||
}
|
||||
oldDataMap := make(map[string]any)
|
||||
err := json.Unmarshal(oldConfig.Data, &oldDataMap)
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "unmarshal actual config data")
|
||||
}
|
||||
|
||||
return true
|
||||
reqDataMap := make(map[string]any)
|
||||
err = json.Unmarshal(reqConfig.Data, &reqDataMap)
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "unmarshal request config data")
|
||||
}
|
||||
|
||||
return cmp.Equal(oldDataMap, reqDataMap), nil
|
||||
}
|
||||
Reference in new issue
Block a user