Skip to content

Commit

Permalink
Merge pull request #37 from ivixvi/refactor-operators
Browse files Browse the repository at this point in the history
Refactor: operators
  • Loading branch information
ivixvi authored Feb 9, 2025
2 parents 1358066 + 2ff8687 commit d8b52fa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 95 deletions.
69 changes: 23 additions & 46 deletions adder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (r *adder) addMapSlice(scopedMap map[string]interface{}, scopedAttr string,
}
oldMaps, ok := areEveryItemsMap(oldSlice)
if !ok {
// WARN: unexpected current value
scopedMap[scopedAttr] = newValue
return scopedMap, true
}
Expand Down Expand Up @@ -60,35 +59,28 @@ func (r *adder) addMap(scopedMap map[string]interface{}, scopedAttr string, newV

func (r *adder) addSlice(scopedMap map[string]interface{}, scopedAttr string, newValue []interface{}) (map[string]interface{}, bool) {
oldSlice, ok := scopedMap[scopedAttr].([]interface{})
// oldSlice is nil
if !ok {
scopedMap[scopedAttr] = newValue
return scopedMap, true
}

// Complex MultiValued
if newMaps, ok := areEveryItemsMap(newValue); ok {
return r.addMapSlice(scopedMap, scopedAttr, newMaps)
}

// Singular MultiValued
changed := false
if oldMaps, ok := areEveryItemsMap(oldSlice); ok {
if newMaps, ok := areEveryItemsMap(newValue); ok {
for _, newMap := range newMaps {
if !containsMap(oldMaps, newMap) {
oldMaps = append(oldMaps, newMap)
changed = true
}
}
if changed {
scopedMap[scopedAttr] = oldMaps
}
return scopedMap, changed
}
} else {
for _, newItem := range newValue {
if !containsItem(oldSlice, newItem) {
oldSlice = append(oldSlice, newItem)
changed = true
}
}
if changed {
scopedMap[scopedAttr] = oldSlice
for _, newItem := range newValue {
if !containsItem(oldSlice, newItem) {
oldSlice = append(oldSlice, newItem)
changed = true
}
}
if changed {
scopedMap[scopedAttr] = oldSlice
}
return scopedMap, changed
}

Expand All @@ -102,14 +94,7 @@ func (r *adder) addValue(scopedMap map[string]interface{}, scopedAttr string, ne

func (r *adder) ByValueForItem(scopedSlice []interface{}, value interface{}) ([]interface{}, bool) {
changed := false
found := false
for _, oldValue := range scopedSlice {
if oldValue == value {
found = true
break
}
}
if !found {
if !containsItem(scopedSlice, value) {
changed = true
scopedSlice = append(scopedSlice, value)
}
Expand All @@ -122,16 +107,12 @@ func (r *adder) ByValueExpressionForItem(scopedMaps []map[string]interface{}, ex
changed := false
newValues := []map[string]interface{}{}
for _, oldValue := range scopedMaps {
if !isMatchExpression(oldValue, expr) {
newValues = append(newValues, oldValue)
if isMatchExpression(oldValue, expr) && !eqMap(oldValue, newValue) {
var merger map[string]interface{}
merger, changed = mergeMap(oldValue, newValue)
newValues = append(newValues, merger)
} else {
if !eqMap(oldValue, newValue) {
var merger map[string]interface{}
merger, changed = mergeMap(oldValue, newValue)
newValues = append(newValues, merger)
} else {
newValues = append(newValues, oldValue)
}
newValues = append(newValues, oldValue)
}
}
return newValues, changed
Expand All @@ -146,19 +127,15 @@ func (r *adder) ByValueExpressionForAttribute(scopedMaps []map[string]interface{
newValues := []map[string]interface{}{}
found := false
for _, oldValue := range scopedMaps {
if !isMatchExpression(oldValue, expr) {
newValues = append(newValues, oldValue)
} else {
if isMatchExpression(oldValue, expr) {
found = true
oldAttrValue, ok := oldValue[subAttr]
if !ok || oldAttrValue != value {
changed = true
oldValue[subAttr] = value
newValues = append(newValues, oldValue)
} else {
newValues = append(newValues, oldValue)
}
}
newValues = append(newValues, oldValue)
}
if !found {
changed = true
Expand Down
83 changes: 34 additions & 49 deletions replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (r *replacer) replaceMapSlice(scopedMap map[string]interface{}, scopedAttr
return scopedMap, true
}
oldMaps, ok := areEveryItemsMap(oldSlice)
// WARN: !ok -> unexpected current value
if !ok || len(oldMaps) != len(newValue) {
scopedMap[scopedAttr] = newValue
return scopedMap, true
Expand All @@ -54,27 +53,25 @@ func (r *replacer) replaceMap(scopedMap map[string]interface{}, scopedAttr strin

func (r *replacer) replaceSlice(scopedMap map[string]interface{}, scopedAttr string, newValue []interface{}) (map[string]interface{}, bool) {
oldSlice, ok := scopedMap[scopedAttr].([]interface{})
// oldSlice is nil
if !ok || len(oldSlice) != len(newValue) {
scopedMap[scopedAttr] = newValue
return scopedMap, true
}
if oldMaps, ok := areEveryItemsMap(oldSlice); ok {
if newMaps, ok := areEveryItemsMap(newValue); ok {
for _, newMap := range newMaps {
if !containsMap(oldMaps, newMap) {
scopedMap[scopedAttr] = newValue
return scopedMap, true
}
}
}
} else {
for _, newItem := range newValue {
if !containsItem(oldSlice, newItem) {
scopedMap[scopedAttr] = newValue
return scopedMap, true
}

// Complex MultiValued
if newMaps, ok := areEveryItemsMap(newValue); ok {
return r.replaceMapSlice(scopedMap, scopedAttr, newMaps)
}

// Singular MultiValued
for _, newItem := range newValue {
if !containsItem(oldSlice, newItem) {
scopedMap[scopedAttr] = newValue
return scopedMap, true
}
}

return scopedMap, false
}

Expand All @@ -88,60 +85,48 @@ func (r *replacer) replaceValue(scopedMap map[string]interface{}, scopedAttr str

func (r *replacer) ByValueForItem(scopedSlice []interface{}, value interface{}) ([]interface{}, bool) {
changed := false
found := false
for _, oldValue := range scopedSlice {
if oldValue == value {
found = true
break
}
}
if !found {
if containsItem(scopedSlice, value) {
changed = true
scopedSlice = append(scopedSlice, value)
}
return scopedSlice, changed
}

func (r *replacer) ByValueExpressionForItem(scopedMaps []map[string]interface{}, expr filter.Expression, value interface{}) ([]map[string]interface{}, bool) {
switch newValue := value.(type) {
case map[string]interface{}:
changed := false
newValues := []map[string]interface{}{}
for _, oldValue := range scopedMaps {
if !isMatchExpression(oldValue, expr) {
newValues = append(newValues, oldValue)
} else {
if !eqMap(oldValue, newValue) {
changed = true
newValues = append(newValues, newValue)
} else {
newValues = append(newValues, oldValue)
}
}
}
return newValues, changed
default:
// unexpected input
newValue, ok := value.(map[string]interface{})

// unexpected input
if !ok {
return scopedMaps, false
}

changed := false
newValues := []map[string]interface{}{}
for _, oldValue := range scopedMaps {
if isMatchExpression(oldValue, expr) && !eqMap(oldValue, newValue) {
changed = true
newValues = append(newValues, newValue)
} else {
newValues = append(newValues, oldValue)
}

}
return newValues, changed

}

func (r *replacer) ByValueExpressionForAttribute(scopedMaps []map[string]interface{}, expr filter.Expression, subAttr string, value interface{}) ([]map[string]interface{}, bool) {
changed := false
newValues := []map[string]interface{}{}
for _, oldValue := range scopedMaps {
if !isMatchExpression(oldValue, expr) {
newValues = append(newValues, oldValue)
} else {
if isMatchExpression(oldValue, expr) {
oldAttrValue, ok := oldValue[subAttr]
if !ok || oldAttrValue != value {
changed = true
oldValue[subAttr] = value
newValues = append(newValues, oldValue)
} else {
newValues = append(newValues, oldValue)
}
}
newValues = append(newValues, oldValue)
}
return newValues, changed
}

0 comments on commit d8b52fa

Please sign in to comment.