-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhashing.go
More file actions
223 lines (188 loc) · 5.32 KB
/
hashing.go
File metadata and controls
223 lines (188 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package hashing
import (
"fmt"
"hash/fnv"
"reflect"
"slices"
"strconv"
"strings"
"github.com/speakeasy-api/openapi/internal/interfaces"
"go.yaml.in/yaml/v4"
)
func Hash(v any) string {
hasher := fnv.New64a()
hashableStr := toHashableString(v)
_, _ = hasher.Write([]byte(hashableStr))
return formatHash(hasher.Sum64())
}
// formatHash converts a uint64 hash to a zero-padded 16-character hex string
// without the allocation overhead of fmt.Sprintf.
func formatHash(h uint64) string {
const hexDigits = "0123456789abcdef"
var buf [16]byte
for i := 15; i >= 0; i-- {
buf[i] = hexDigits[h&0xf]
h >>= 4
}
return string(buf[:])
}
type model interface {
GetCoreAny() any
SetCoreAny(core any)
}
func toHashableString(v any) string {
if v == nil {
return ""
}
var builder strings.Builder
typ := reflect.TypeOf(v)
if typ == nil {
return ""
}
switch typ.Kind() {
case reflect.Slice, reflect.Array:
sliceVal := reflect.ValueOf(v)
if typ.Kind() == reflect.Slice && sliceVal.IsNil() {
return ""
}
for i := 0; i < sliceVal.Len(); i++ {
builder.WriteString(toHashableString(sliceVal.Index(i).Interface()))
}
case reflect.Map:
mapVal := reflect.ValueOf(v)
if mapVal.IsNil() {
return ""
}
mapKeys := mapVal.MapKeys()
// Sort keys for deterministic output
slices.SortFunc(mapKeys, func(a, b reflect.Value) int {
return strings.Compare(toHashableString(a.Interface()), toHashableString(b.Interface()))
})
for _, key := range mapKeys {
builder.WriteString(toHashableString(key.Interface()))
builder.WriteString(toHashableString(mapVal.MapIndex(key).Interface()))
}
case reflect.Struct:
// Check if this is a yaml.Node
if node, ok := v.(yaml.Node); ok {
builder.WriteString(yamlNodeToHashableString(&node))
} else {
builder.WriteString(structToHashableString(v))
}
case reflect.Ptr, reflect.Interface:
val := reflect.ValueOf(v)
if val.IsNil() {
return ""
}
// Check if this is a sequenced map interface (for pointer types)
if interfaces.ImplementsInterface[interfaces.SequencedMapInterface](typ) && !interfaces.ImplementsInterface[model](typ) {
builder.WriteString(sequencedMapToHashableString(v))
} else {
builder.WriteString(toHashableString(val.Elem().Interface()))
}
default:
switch v := v.(type) {
case string:
builder.WriteString(v)
case int:
builder.WriteString(strconv.Itoa(v))
case int64:
builder.WriteString(strconv.FormatInt(v, 10))
case float64:
builder.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
case bool:
builder.WriteString(strconv.FormatBool(v))
case uint64:
builder.WriteString(strconv.FormatUint(v, 10))
case *yaml.Node:
builder.WriteString(yamlNodeToHashableString(v))
default:
builder.WriteString(fmt.Sprintf("%v", v))
}
}
return builder.String()
}
type eitherValue interface {
IsLeft() bool
IsRight() bool
}
func structToHashableString(v any) string {
var builder strings.Builder
structVal := reflect.ValueOf(v)
structType := structVal.Type()
for i := 0; i < structVal.NumField(); i++ {
fieldType := structType.Field(i)
fieldVal := structVal.Field(i)
if fieldType.Anonymous {
switch {
case interfaces.ImplementsInterface[interfaces.SequencedMapInterface](reflect.PointerTo(fieldVal.Type())):
// For value embeds, we need to get the address to access the interface methods
if fieldVal.CanAddr() {
builder.WriteString(sequencedMapToHashableString(fieldVal.Addr().Interface()))
} else {
builder.WriteString(sequencedMapToHashableString(fieldVal.Interface()))
}
case interfaces.ImplementsInterface[eitherValue](reflect.PointerTo(fieldVal.Type())):
builder.WriteString(structToHashableString(fieldVal.Interface()))
}
continue
}
if !fieldType.IsExported() {
continue
}
// Ignore extensions field as they are generally metadata and don't impact the equivalence of what we want to match
if fieldType.Name == "Extensions" {
continue
}
val := toHashableString(fieldVal.Interface())
if val == "" {
continue
}
builder.WriteString(fieldType.Name)
builder.WriteString(val)
}
return builder.String()
}
// yamlNodeToHashableString recursively processes a YAML node and its children,
// including only semantic content (Tag, Value, Kind) and excluding positional
// metadata (Line, Column, Style, etc.)
func yamlNodeToHashableString(node *yaml.Node) string {
if node == nil {
return ""
}
var builder strings.Builder
// Include semantic fields only
builder.WriteString("Kind")
builder.WriteString(strconv.Itoa(int(node.Kind)))
if node.Tag != "" {
builder.WriteString("Tag" + node.Tag)
}
if node.Value != "" {
builder.WriteString("Value" + node.Value)
}
// Recursively process children in Content array
for _, child := range node.Content {
builder.WriteString(yamlNodeToHashableString(child))
}
return builder.String()
}
func sequencedMapToHashableString(v any) string {
var builder strings.Builder
seqMap, ok := v.(interfaces.SequencedMapInterface)
if !ok {
return ""
}
keys := slices.Collect(seqMap.KeysAny())
slices.SortFunc(keys, func(a, b any) int {
return strings.Compare(toHashableString(a), toHashableString(b))
})
for _, key := range keys {
val, ok := seqMap.GetAny(key)
if !ok {
continue
}
builder.WriteString(toHashableString(key))
builder.WriteString(toHashableString(val))
}
return builder.String()
}