Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New OpenAPI codes #11

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
65 changes: 65 additions & 0 deletions generics/sync_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package generics

import "sync"

type SyncMap[K comparable, V any] struct {
// type conversion protection, see atomic.Pointer
_ [0]*K
_ [0]*V

m sync.Map
}

func (m *SyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
return m.m.CompareAndDelete(key, old)
}

func (m *SyncMap[K, V]) CompareAndSwap(key K, old V, newValue V) bool {
return m.m.CompareAndSwap(key, old, newValue)
}

func (m *SyncMap[K, V]) Delete(key K) {
m.m.Delete(key)
}

func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) {
if v, o := m.m.Load(key); o {
return v.(V), true
} else {
return
}
}

func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
if v, l := m.m.LoadAndDelete(key); l {
return v.(V), true
} else {
return
}
}

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
if a, l := m.m.LoadOrStore(key, value); l {
return a.(V), true
} else {
return value, false
}
}

func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) {
m.m.Range(func(key, value any) bool {
return f(key.(K), value.(V))
})
}

func (m *SyncMap[K, V]) Store(key K, value V) {
m.m.Store(key, value)
}

func (m *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) {
if p, l := m.m.Swap(key, value); l {
return p.(V), true
} else {
return
}
}
102 changes: 102 additions & 0 deletions generics/sync_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package generics_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/free5gc/util/generics"
)

func TestSyncMap(t *testing.T) {
var m generics.SyncMap[int, string]

// Load and Store
m.Store(1, "test")
v, e := m.Load(1)
assert.True(t, e)
assert.Equal(t, "test", v)
_, e = m.Load(2)
assert.False(t, e)

// Delete
m.Store(2, "test2")
m.Delete(1)
_, e = m.Load(1)
assert.False(t, e)
m.Delete(3)
v, e = m.Load(2)
assert.True(t, e)
assert.Equal(t, "test2", v)

// LoadOrStore
v, e = m.LoadOrStore(1, "test")
assert.False(t, e)
assert.Equal(t, "test", v)
v, e = m.Load(1)
assert.True(t, e)
assert.Equal(t, "test", v)
v, e = m.LoadOrStore(2, "testX")
assert.True(t, e)
assert.Equal(t, "test2", v)
v, e = m.Load(2)
assert.True(t, e)
assert.Equal(t, "test2", v)

// LoadAndDelete
v, e = m.LoadAndDelete(1)
assert.True(t, e)
assert.Equal(t, "test", v)
_, e = m.Load(1)
assert.False(t, e)
_, e = m.LoadAndDelete(1)
assert.False(t, e)

// Swap
_, e = m.Swap(1, "test")
assert.False(t, e)
v, e = m.Load(1)
assert.True(t, e)
assert.Equal(t, "test", v)
v, e = m.Swap(2, "testX")
assert.True(t, e)
assert.Equal(t, "test2", v)
v, e = m.Load(2)
assert.True(t, e)
assert.Equal(t, "testX", v)

// CompareAndDelete
m.Delete(1)
m.Store(2, "test2")
assert.False(t, m.CompareAndDelete(2, "testX"))
v, e = m.Load(2)
assert.True(t, e)
assert.Equal(t, "test2", v)
assert.True(t, m.CompareAndDelete(2, "test2"))
_, e = m.Load(2)
assert.False(t, e)

// CompareAndSwap
m.Store(2, "test2")
assert.False(t, m.CompareAndSwap(1, "test", "testX"))
_, e = m.Load(1)
assert.False(t, e)
assert.False(t, m.CompareAndSwap(2, "testX", "testY"))
v, e = m.Load(2)
assert.True(t, e)
assert.Equal(t, "test2", v)
assert.True(t, m.CompareAndSwap(2, "test2", "testY"))
v, e = m.Load(2)
assert.True(t, e)
assert.Equal(t, "testY", v)

m.Range(func(key int, value string) bool {
switch key {
case 2:
assert.Equal(t, "testY", value)
default:
assert.Fail(t, "Invalid key %d", key)
}
return true
})
}
56 changes: 35 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,55 +1,69 @@
module github.com/free5gc/util

go 1.17
go 1.22.0

require (
github.com/ShouheiNishi/openapi5g v0.0.0-20241106074548-e84ac6a60950
github.com/evanphx/json-patch v0.5.2
github.com/gin-gonic/gin v1.9.1
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/mitchellh/mapstructure v1.4.2
github.com/pkg/errors v0.9.1
github.com/samber/lo v1.47.0
github.com/sirupsen/logrus v1.8.1
github.com/smartystreets/goconvey v1.6.4
github.com/stretchr/testify v1.8.3
github.com/stretchr/testify v1.9.0
github.com/tim-ywliu/nested-logrus-formatter v1.3.2
go.mongodb.org/mongo-driver v1.8.4
golang.org/x/net v0.17.0
golang.org/x/net v0.30.0
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/free5gc/openapi v1.0.8 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading