feat(merge_component): ✨ Add third souce for diff
Add third source (only from recipe, WIP commit) and attach debugger for checking functions
This commit is contained in:
parent
860dc05cde
commit
1b96e298ee
8 changed files with 439 additions and 175 deletions
|
|
@ -1079,78 +1079,17 @@ func (d *Data) MergeRecipe(country, filename, changeKey string) (string, error)
|
|||
}
|
||||
|
||||
// get recipe
|
||||
// var sourceRecipe models.Recipe
|
||||
sourceRecipe := d.GetRecipe(country, filename)
|
||||
// copy(d.GetRecipe(country, filename), &sourceRecipe)
|
||||
|
||||
// check address
|
||||
fmt.Println("[Source] source === recipe? ", sourceRecipe == d.GetRecipe(country, filename))
|
||||
|
||||
// apply value to its source
|
||||
d.SetValuesToRecipe(sourceRecipe.Recipe01, patchValue)
|
||||
// updating version
|
||||
sourceRecipe.MachineSetting.ConfigNumber += 1
|
||||
newVersionStr := strconv.Itoa(sourceRecipe.MachineSetting.ConfigNumber)
|
||||
|
||||
// save to local and redis
|
||||
|
||||
// create new file name
|
||||
updatedFilename := ""
|
||||
prefixLocalFile := "coffeethai02_"
|
||||
if country != "tha" {
|
||||
updatedFilename = prefixLocalFile + newVersionStr + "_" + country + ".json"
|
||||
} else {
|
||||
updatedFilename = prefixLocalFile + newVersionStr + ".json"
|
||||
}
|
||||
|
||||
fullUpdatedFilename := path.Join("./cofffeemachineConfig", country, updatedFilename)
|
||||
|
||||
// create new file
|
||||
|
||||
// handle case if file already exists, add version by 1 then search new filename in loop
|
||||
// list all files in dir
|
||||
directory := path.Join("./cofffeemachineConfig", country)
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("MergeRecipe: Error when read dir", zap.Error(err))
|
||||
return "ReadDirError", fmt.Errorf("error when read dir: %v", err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if file.Name() == updatedFilename {
|
||||
// add version by 1
|
||||
sourceRecipe.MachineSetting.ConfigNumber += 1
|
||||
newVersionStr = strconv.Itoa(sourceRecipe.MachineSetting.ConfigNumber)
|
||||
|
||||
if country != "tha" {
|
||||
updatedFilename = prefixLocalFile + newVersionStr + "_" + country + ".json"
|
||||
} else {
|
||||
updatedFilename = prefixLocalFile + newVersionStr + ".json"
|
||||
}
|
||||
|
||||
fullUpdatedFilename = path.Join("./cofffeemachineConfig", country, updatedFilename)
|
||||
}
|
||||
}
|
||||
|
||||
file, err := os.Create(fullUpdatedFilename)
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("MergeRecipe: Error when create new file", zap.Error(err))
|
||||
return "CreateFileError", fmt.Errorf("error when create new file: %v", err)
|
||||
}
|
||||
|
||||
// write file
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", " ")
|
||||
err = encoder.Encode(sourceRecipe)
|
||||
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("MergeRecipe: Error when write file", zap.Error(err))
|
||||
return "WriteFileError", fmt.Errorf("error when write file: %v", err)
|
||||
}
|
||||
|
||||
// set cache
|
||||
err = d.redisClient.SetToKey(updatedFilename, sourceRecipe)
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("MergeRecipe: Error when set cache", zap.Error(err))
|
||||
return "SetCacheError", fmt.Errorf("error when set cache: %v", err)
|
||||
}
|
||||
|
||||
return updatedFilename, nil
|
||||
return d.finalizedVersion(country, sourceRecipe)
|
||||
}
|
||||
|
||||
func (d *Data) MergeRecipeNoCache(country string, filename string, updatedRecipe models.Recipe01) (string, error) {
|
||||
|
|
@ -1159,6 +1098,11 @@ func (d *Data) MergeRecipeNoCache(country string, filename string, updatedRecipe
|
|||
|
||||
// apply value to its source
|
||||
d.SetValuesToRecipe(sourceRecipe.Recipe01, updatedRecipe)
|
||||
|
||||
return d.finalizedVersion(country, sourceRecipe)
|
||||
}
|
||||
|
||||
func (d *Data) finalizedVersion(country string, sourceRecipe *models.Recipe) (string, error) {
|
||||
// updating version
|
||||
sourceRecipe.MachineSetting.ConfigNumber += 1
|
||||
newVersionStr := strconv.Itoa(sourceRecipe.MachineSetting.ConfigNumber)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue