Skip to content

Commit 442023b

Browse files
author
Michael Bonifacio
committed
merge origin/main and resolve conflicts
2 parents e0d1476 + 846c366 commit 442023b

66 files changed

Lines changed: 15855 additions & 1270 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pre-commit-config.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
5+
# Run golangci-lint as a pre-commit hook to catch issues before they are pushed
6+
# See https://golangci-lint.run/ for more information
7+
- repo: local
8+
hooks:
9+
- id: golangci-lint
10+
name: Lint Go code
11+
entry: go tool golangci-lint run
12+
language: system
13+
pass_filenames: false
14+
types: [go]

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ go get github.com/pb33f/libopenapi-validator
3434
go run github.com/pb33f/libopenapi-validator/cmd/validate@latest [--regexengine] [--yaml2json] <file>
3535
```
3636

37+
## Install pre-commit hook
38+
39+
To install the pre-commit hook, run the following command in your terminal:
40+
41+
```bash
42+
pre-commit install
43+
```
44+
3745
### Options
3846

3947
#### --regexengine

cache/cache.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cache
66
import (
77
"github.com/pb33f/libopenapi/datamodel/high/base"
88
"github.com/santhosh-tekuri/jsonschema/v6"
9+
"go.yaml.in/yaml/v4"
910
)
1011

1112
// SchemaCacheEntry holds a compiled schema and its intermediate representations.
@@ -16,12 +17,13 @@ type SchemaCacheEntry struct {
1617
ReferenceSchema string // String version of RenderedInline
1718
RenderedJSON []byte
1819
CompiledSchema *jsonschema.Schema
20+
RenderedNode *yaml.Node
1921
}
2022

2123
// SchemaCache defines the interface for schema caching implementations.
22-
// The key is a [32]byte hash of the schema (from schema.GoLow().Hash()).
24+
// The key is a uint64 hash of the schema (from schema.GoLow().Hash()).
2325
type SchemaCache interface {
24-
Load(key [32]byte) (*SchemaCacheEntry, bool)
25-
Store(key [32]byte, value *SchemaCacheEntry)
26-
Range(f func(key [32]byte, value *SchemaCacheEntry) bool)
26+
Load(key uint64) (*SchemaCacheEntry, bool)
27+
Store(key uint64, value *SchemaCacheEntry)
28+
Range(f func(key uint64, value *SchemaCacheEntry) bool)
2729
}

cache/cache_test.go

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ func TestDefaultCache_StoreAndLoad(t *testing.T) {
2929
CompiledSchema: &jsonschema.Schema{},
3030
}
3131

32-
// Create a test key (32-byte hash)
33-
var key [32]byte
34-
copy(key[:], []byte("test-schema-hash-12345678901234"))
32+
// Create a test key (uint64 hash)
33+
key := uint64(0x123456789abcdef0)
3534

3635
// Store the schema
3736
cache.Store(key, testSchema)
@@ -49,8 +48,7 @@ func TestDefaultCache_LoadMissing(t *testing.T) {
4948
cache := NewDefaultCache()
5049

5150
// Try to load a key that doesn't exist
52-
var key [32]byte
53-
copy(key[:], []byte("nonexistent-key-12345678901234"))
51+
key := uint64(0xdeadbeef)
5452

5553
loaded, ok := cache.Load(key)
5654
assert.False(t, ok, "Should not find non-existent key")
@@ -60,7 +58,7 @@ func TestDefaultCache_LoadMissing(t *testing.T) {
6058
func TestDefaultCache_LoadNilCache(t *testing.T) {
6159
var cache *DefaultCache
6260

63-
var key [32]byte
61+
key := uint64(0)
6462
loaded, ok := cache.Load(key)
6563

6664
assert.False(t, ok)
@@ -71,7 +69,7 @@ func TestDefaultCache_StoreNilCache(t *testing.T) {
7169
var cache *DefaultCache
7270

7371
// Should not panic
74-
var key [32]byte
72+
key := uint64(0)
7573
cache.Store(key, &SchemaCacheEntry{})
7674

7775
// Verify nothing was stored (cache is nil)
@@ -82,10 +80,9 @@ func TestDefaultCache_Range(t *testing.T) {
8280
cache := NewDefaultCache()
8381

8482
// Store multiple entries
85-
entries := make(map[[32]byte]*SchemaCacheEntry)
83+
entries := make(map[uint64]*SchemaCacheEntry)
8684
for i := 0; i < 5; i++ {
87-
var key [32]byte
88-
copy(key[:], []byte{byte(i)})
85+
key := uint64(i)
8986

9087
entry := &SchemaCacheEntry{
9188
RenderedInline: []byte{byte(i)},
@@ -97,8 +94,8 @@ func TestDefaultCache_Range(t *testing.T) {
9794

9895
// Range over all entries
9996
count := 0
100-
foundKeys := make(map[[32]byte]bool)
101-
cache.Range(func(key [32]byte, value *SchemaCacheEntry) bool {
97+
foundKeys := make(map[uint64]bool)
98+
cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
10299
count++
103100
foundKeys[key] = true
104101

@@ -118,14 +115,13 @@ func TestDefaultCache_RangeEarlyTermination(t *testing.T) {
118115

119116
// Store multiple entries
120117
for i := 0; i < 10; i++ {
121-
var key [32]byte
122-
copy(key[:], []byte{byte(i)})
118+
key := uint64(i)
123119
cache.Store(key, &SchemaCacheEntry{})
124120
}
125121

126122
// Range but stop after 3 iterations
127123
count := 0
128-
cache.Range(func(key [32]byte, value *SchemaCacheEntry) bool {
124+
cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
129125
count++
130126
return count < 3 // Stop after 3
131127
})
@@ -138,7 +134,7 @@ func TestDefaultCache_RangeNilCache(t *testing.T) {
138134

139135
// Should not panic
140136
called := false
141-
cache.Range(func(key [32]byte, value *SchemaCacheEntry) bool {
137+
cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
142138
called = true
143139
return true
144140
})
@@ -151,7 +147,7 @@ func TestDefaultCache_RangeEmpty(t *testing.T) {
151147

152148
// Range over empty cache
153149
count := 0
154-
cache.Range(func(key [32]byte, value *SchemaCacheEntry) bool {
150+
cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
155151
count++
156152
return true
157153
})
@@ -162,8 +158,7 @@ func TestDefaultCache_RangeEmpty(t *testing.T) {
162158
func TestDefaultCache_Overwrite(t *testing.T) {
163159
cache := NewDefaultCache()
164160

165-
var key [32]byte
166-
copy(key[:], []byte("test-key"))
161+
key := uint64(0x12345678)
167162

168163
// Store first value
169164
first := &SchemaCacheEntry{
@@ -188,10 +183,9 @@ func TestDefaultCache_MultipleKeys(t *testing.T) {
188183
cache := NewDefaultCache()
189184

190185
// Store with different keys
191-
var key1, key2, key3 [32]byte
192-
copy(key1[:], []byte("key1"))
193-
copy(key2[:], []byte("key2"))
194-
copy(key3[:], []byte("key3"))
186+
key1 := uint64(1)
187+
key2 := uint64(2)
188+
key3 := uint64(3)
195189

196190
cache.Store(key1, &SchemaCacheEntry{RenderedInline: []byte("value1")})
197191
cache.Store(key2, &SchemaCacheEntry{RenderedInline: []byte("value2")})
@@ -218,8 +212,7 @@ func TestDefaultCache_ThreadSafety(t *testing.T) {
218212
done := make(chan bool, 10)
219213
for i := 0; i < 10; i++ {
220214
go func(val int) {
221-
var key [32]byte
222-
copy(key[:], []byte{byte(val)})
215+
key := uint64(val)
223216
cache.Store(key, &SchemaCacheEntry{
224217
RenderedInline: []byte{byte(val)},
225218
})
@@ -235,8 +228,7 @@ func TestDefaultCache_ThreadSafety(t *testing.T) {
235228
// Concurrent reads
236229
for i := 0; i < 10; i++ {
237230
go func(val int) {
238-
var key [32]byte
239-
copy(key[:], []byte{byte(val)})
231+
key := uint64(val)
240232
loaded, ok := cache.Load(key)
241233
assert.True(t, ok)
242234
assert.NotNil(t, loaded)
@@ -251,7 +243,7 @@ func TestDefaultCache_ThreadSafety(t *testing.T) {
251243

252244
// Verify all entries exist
253245
count := 0
254-
cache.Range(func(key [32]byte, value *SchemaCacheEntry) bool {
246+
cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
255247
count++
256248
return true
257249
})
@@ -284,20 +276,18 @@ func TestDefaultCache_RangeWithInvalidTypes(t *testing.T) {
284276
cache.m.Store("invalid-key-type", &SchemaCacheEntry{})
285277

286278
// Store an entry with wrong value type
287-
var validKey [32]byte
288-
copy(validKey[:], []byte{1})
279+
validKey := uint64(1)
289280
cache.m.Store(validKey, "invalid-value-type")
290281

291282
// Store a valid entry
292-
var validKey2 [32]byte
293-
copy(validKey2[:], []byte{2})
283+
validKey2 := uint64(2)
294284
validEntry := &SchemaCacheEntry{RenderedInline: []byte("valid")}
295285
cache.Store(validKey2, validEntry)
296286

297287
// Range should skip invalid entries and only process valid ones
298288
count := 0
299289
var seenEntry *SchemaCacheEntry
300-
cache.Range(func(key [32]byte, value *SchemaCacheEntry) bool {
290+
cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
301291
count++
302292
seenEntry = value
303293
return true

cache/default_cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func NewDefaultCache() *DefaultCache {
1515
}
1616

1717
// Load retrieves a schema from the cache.
18-
func (c *DefaultCache) Load(key [32]byte) (*SchemaCacheEntry, bool) {
18+
func (c *DefaultCache) Load(key uint64) (*SchemaCacheEntry, bool) {
1919
if c == nil || c.m == nil {
2020
return nil, false
2121
}
@@ -28,20 +28,20 @@ func (c *DefaultCache) Load(key [32]byte) (*SchemaCacheEntry, bool) {
2828
}
2929

3030
// Store saves a schema to the cache.
31-
func (c *DefaultCache) Store(key [32]byte, value *SchemaCacheEntry) {
31+
func (c *DefaultCache) Store(key uint64, value *SchemaCacheEntry) {
3232
if c == nil || c.m == nil {
3333
return
3434
}
3535
c.m.Store(key, value)
3636
}
3737

3838
// Range calls f for each entry in the cache (for testing/inspection).
39-
func (c *DefaultCache) Range(f func(key [32]byte, value *SchemaCacheEntry) bool) {
39+
func (c *DefaultCache) Range(f func(key uint64, value *SchemaCacheEntry) bool) {
4040
if c == nil || c.m == nil {
4141
return
4242
}
4343
c.m.Range(func(k, v interface{}) bool {
44-
key, ok := k.([32]byte)
44+
key, ok := k.(uint64)
4545
if !ok {
4646
return true
4747
}

0 commit comments

Comments
 (0)