Added hash functions to various low level models.

This commit is contained in:
Dave Shanley
2022-10-25 09:44:42 -04:00
parent 1e0b45217a
commit 4671bca168
8 changed files with 131 additions and 2 deletions

View File

@@ -1,7 +1,9 @@
package v3
import (
"crypto/sha256"
"github.com/pb33f/libopenapi/datamodel/low"
"strings"
)
// ServerVariable represents a low-level OpenAPI 3+ ServerVariable object.
@@ -16,3 +18,18 @@ type ServerVariable struct {
Default low.NodeReference[string]
Description low.NodeReference[string]
}
// Hash will return a consistent SHA256 Hash of the ServerVariable object
func (s *ServerVariable) Hash() [32]byte {
var f []string
for k := range s.Enum {
f = append(f, s.Enum[k].Value)
}
if !s.Default.IsEmpty() {
f = append(f, s.Default.Value)
}
if !s.Description.IsEmpty() {
f = append(f, s.Description.Value)
}
return sha256.Sum256([]byte(strings.Join(f, "|")))
}