Building out schema comparison mechanism

Which has led to a new wider hashing capability for the low level API. hashing makes it very easy to determine changes quickly, without having to run comparisons to discover changes, could really speed things up moving forward.
This commit is contained in:
Dave Shanley
2022-10-08 14:09:46 -04:00
parent 7f61a7624d
commit 4b9c5fba1e
13 changed files with 1276 additions and 64 deletions

View File

@@ -4,7 +4,10 @@
package base
import (
"crypto/sha256"
"github.com/pb33f/libopenapi/datamodel/low"
"sort"
"strings"
)
// Discriminator is only used by OpenAPI 3+ documents, it represents a polymorphic discriminator used for schemas
@@ -29,3 +32,21 @@ func (d *Discriminator) FindMappingValue(key string) *low.ValueReference[string]
}
return nil
}
// Hash will return a consistent SHA256 Hash of the Discriminator object
func (d *Discriminator) Hash() [32]byte {
// calculate a hash from every property.
f := []string{d.PropertyName.Value}
propertyKeys := make([]string, 0, len(d.Mapping))
for i := range d.Mapping {
propertyKeys = append(propertyKeys, i.Value)
}
sort.Strings(propertyKeys)
for k := range propertyKeys {
prop := d.FindMappingValue(propertyKeys[k])
f = append(f, prop.Value)
}
return sha256.Sum256([]byte(strings.Join(f, "|")))
}