mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-11 04:20:24 +00:00
Upgraded comparison funcrtion for maps.
It's now more intelligent and runs async in threads, also added a variation of the map check function to cope with untyped comparison functions.
This commit is contained in:
@@ -201,35 +201,137 @@ func CheckMapForChanges[T any, R any](expLeft, expRight map[low.KeyReference[str
|
|||||||
|
|
||||||
expChanges := make(map[string]R)
|
expChanges := make(map[string]R)
|
||||||
|
|
||||||
// check left example hashes
|
checkLeft := func(k string, doneChan chan bool, f, g map[string]string, p, h map[string]low.ValueReference[T]) {
|
||||||
for k := range lHashes {
|
rhash := g[k]
|
||||||
rhash := rHashes[k]
|
|
||||||
if rhash == "" {
|
if rhash == "" {
|
||||||
|
if p[k].GetValueNode().Value == "" {
|
||||||
|
p[k].GetValueNode().Value = k
|
||||||
|
}
|
||||||
CreateChange(changes, ObjectRemoved, label,
|
CreateChange(changes, ObjectRemoved, label,
|
||||||
lValues[k].GetValueNode(), nil, true,
|
p[k].GetValueNode(), nil, true,
|
||||||
lValues[k].GetValue(), nil)
|
p[k].GetValue(), nil)
|
||||||
continue
|
doneChan <- true
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if lHashes[k] == rHashes[k] {
|
if f[k] == g[k] {
|
||||||
continue
|
doneChan <- true
|
||||||
|
return
|
||||||
}
|
}
|
||||||
// run comparison.
|
// run comparison.
|
||||||
expChanges[k] = compareFunc(lValues[k].Value, rValues[k].Value)
|
expChanges[k] = compareFunc(p[k].Value, h[k].Value)
|
||||||
|
doneChan <- true
|
||||||
|
}
|
||||||
|
|
||||||
|
doneChan := make(chan bool)
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
// check left example hashes
|
||||||
|
for k := range lHashes {
|
||||||
|
count++
|
||||||
|
go checkLeft(k, doneChan, lHashes, rHashes, lValues, rValues)
|
||||||
}
|
}
|
||||||
|
|
||||||
//check right example hashes
|
//check right example hashes
|
||||||
for k := range rHashes {
|
for k := range rHashes {
|
||||||
lhash := lHashes[k]
|
count++
|
||||||
if lhash == "" {
|
go checkRightValue(k, doneChan, lHashes, rValues, changes, label)
|
||||||
CreateChange(changes, ObjectAdded, label,
|
}
|
||||||
nil, rValues[k].GetValueNode(), false,
|
|
||||||
nil, rValues[k].GetValue())
|
// wait for all done signals.
|
||||||
continue
|
completed := 0
|
||||||
|
for completed < count {
|
||||||
|
select {
|
||||||
|
case <-doneChan:
|
||||||
|
completed++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return expChanges
|
return expChanges
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckMapForChangesUntyped checks a left and right low level map for any additions, subtractions or modifications to
|
||||||
|
// values. The compareFunc can be generic and accept any type
|
||||||
|
func CheckMapForChangesUntyped[T any, R any](expLeft, expRight map[low.KeyReference[string]]low.ValueReference[T],
|
||||||
|
changes *[]*Change, label string, compareFunc func(l, r any) R) map[string]R {
|
||||||
|
|
||||||
|
lHashes := make(map[string]string)
|
||||||
|
rHashes := make(map[string]string)
|
||||||
|
lValues := make(map[string]low.ValueReference[T])
|
||||||
|
rValues := make(map[string]low.ValueReference[T])
|
||||||
|
|
||||||
|
for k := range expLeft {
|
||||||
|
lHashes[k.Value] = low.GenerateHashString(expLeft[k].Value)
|
||||||
|
lValues[k.Value] = expLeft[k]
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := range expRight {
|
||||||
|
rHashes[k.Value] = low.GenerateHashString(expRight[k].Value)
|
||||||
|
rValues[k.Value] = expRight[k]
|
||||||
|
}
|
||||||
|
|
||||||
|
expChanges := make(map[string]R)
|
||||||
|
|
||||||
|
checkLeft := func(k string, doneChan chan bool, f, g map[string]string, p, h map[string]low.ValueReference[T]) {
|
||||||
|
rhash := g[k]
|
||||||
|
if rhash == "" {
|
||||||
|
if p[k].GetValueNode().Value == "" {
|
||||||
|
p[k].GetValueNode().Value = k
|
||||||
|
}
|
||||||
|
CreateChange(changes, ObjectRemoved, label,
|
||||||
|
p[k].GetValueNode(), nil, true,
|
||||||
|
p[k].GetValue(), nil)
|
||||||
|
doneChan <- true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if f[k] == g[k] {
|
||||||
|
doneChan <- true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// run comparison.
|
||||||
|
expChanges[k] = compareFunc(p[k].Value, h[k].Value)
|
||||||
|
doneChan <- true
|
||||||
|
}
|
||||||
|
|
||||||
|
doneChan := make(chan bool)
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
// check left example hashes
|
||||||
|
for k := range lHashes {
|
||||||
|
count++
|
||||||
|
go checkLeft(k, doneChan, lHashes, rHashes, lValues, rValues)
|
||||||
|
}
|
||||||
|
|
||||||
|
//check right example hashes
|
||||||
|
for k := range rHashes {
|
||||||
|
count++
|
||||||
|
go checkRightValue(k, doneChan, lHashes, rValues, changes, label)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for all done signals.
|
||||||
|
completed := 0
|
||||||
|
for completed < count {
|
||||||
|
select {
|
||||||
|
case <-doneChan:
|
||||||
|
completed++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return expChanges
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkRightValue[T any](k string, doneChan chan bool, f map[string]string, p map[string]low.ValueReference[T],
|
||||||
|
changes *[]*Change, label string) {
|
||||||
|
|
||||||
|
lhash := f[k]
|
||||||
|
if lhash == "" {
|
||||||
|
if p[k].GetValueNode().Value == "" {
|
||||||
|
p[k].GetValueNode().Value = k
|
||||||
|
}
|
||||||
|
CreateChange(changes, ObjectAdded, label,
|
||||||
|
nil, p[k].GetValueNode(), false,
|
||||||
|
nil, p[k].GetValue())
|
||||||
|
}
|
||||||
|
doneChan <- true
|
||||||
|
}
|
||||||
|
|
||||||
// ExtractStringValueSliceChanges will compare two low level string slices for changes.
|
// ExtractStringValueSliceChanges will compare two low level string slices for changes.
|
||||||
func ExtractStringValueSliceChanges(lParam, rParam []low.ValueReference[string],
|
func ExtractStringValueSliceChanges(lParam, rParam []low.ValueReference[string],
|
||||||
changes *[]*Change, label string, breaking bool) {
|
changes *[]*Change, label string, breaking bool) {
|
||||||
|
|||||||
4
what-changed/model/components_test.go
Normal file
4
what-changed/model/components_test.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package model
|
||||||
Reference in New Issue
Block a user