v0.0.362
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m27s

This commit is contained in:
Mike Schwörer 2024-01-07 04:18:03 +01:00
parent d38fa60fbc
commit 70de8e8d04
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 162 additions and 8 deletions

113
dataext/syncMap.go Normal file
View File

@ -0,0 +1,113 @@
package dataext
import "sync"
type SyncMap[TKey comparable, TData any] struct {
data map[TKey]TData
lock sync.Mutex
}
func (s *SyncMap[TKey, TData]) Set(key TKey, data TData) {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[TKey]TData)
}
s.data[key] = data
}
func (s *SyncMap[TKey, TData]) SetIfNotContains(key TKey, data TData) bool {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[TKey]TData)
}
if _, existsInPreState := s.data[key]; existsInPreState {
return false
}
s.data[key] = data
return true
}
func (s *SyncMap[TKey, TData]) Get(key TKey) (TData, bool) {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[TKey]TData)
}
if v, ok := s.data[key]; ok {
return v, true
} else {
return *new(TData), false
}
}
func (s *SyncMap[TKey, TData]) Delete(key TKey) bool {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[TKey]TData)
}
_, ok := s.data[key]
delete(s.data, key)
return ok
}
func (s *SyncMap[TKey, TData]) Contains(key TKey) bool {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[TKey]TData)
}
_, ok := s.data[key]
return ok
}
func (s *SyncMap[TKey, TData]) GetAllKeys() []TKey {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[TKey]TData)
}
r := make([]TKey, 0, len(s.data))
for k := range s.data {
r = append(r, k)
}
return r
}
func (s *SyncMap[TKey, TData]) GetAllValues() []TData {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[TKey]TData)
}
r := make([]TData, 0, len(s.data))
for _, v := range s.data {
r = append(r, v)
}
return r
}

View File

@ -71,15 +71,18 @@ var (
// other values come from the downstream application that uses goext // other values come from the downstream application that uses goext
) )
var registeredTypes = dataext.SyncSet[string]{} var registeredTypes = dataext.SyncMap[string, ErrorType]{}
func NewType(key string, defStatusCode *int) ErrorType { func NewType(key string, defStatusCode *int) ErrorType {
insertOkay := registeredTypes.Add(key) et := ErrorType{key, defStatusCode}
if !insertOkay {
panic("Cannot register same ErrType ('" + key + "') more than once") registeredTypes.Set(key, et)
return et
} }
return ErrorType{key, defStatusCode} func ListRegisteredTypes() []ErrorType {
return registeredTypes.GetAllValues()
} }
type LogPrintLevel string type LogPrintLevel string

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.361" const GoextVersion = "0.0.362"
const GoextVersionTimestamp = "2024-01-07T04:01:12+0100" const GoextVersionTimestamp = "2024-01-07T04:18:03+0100"

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rfctime" "gogs.mikescher.com/BlackForestBytes/goext/rfctime"
"reflect" "reflect"
@ -105,6 +106,40 @@ var ConverterJsonArrToString = NewDBTypeConverter[JsonArr, string](func(v JsonAr
return mrsh, nil return mrsh, nil
}) })
var ConverterExErrCategoryToString = NewDBTypeConverter[exerr.ErrorCategory, string](func(v exerr.ErrorCategory) (string, error) {
return v.Category, nil
}, func(v string) (exerr.ErrorCategory, error) {
for _, cat := range exerr.AllCategories {
if cat.Category == v {
return cat, nil
}
}
return exerr.CatUser, errors.New("failed to convert '" + v + "' to exerr.ErrorCategory")
})
var ConverterExErrSeverityToString = NewDBTypeConverter[exerr.ErrorSeverity, string](func(v exerr.ErrorSeverity) (string, error) {
return v.Severity, nil
}, func(v string) (exerr.ErrorSeverity, error) {
for _, sev := range exerr.AllSeverities {
if sev.Severity == v {
return sev, nil
}
}
return exerr.SevErr, errors.New("failed to convert '" + v + "' to exerr.ErrorSeverity")
})
var ConverterExErrTypeToString = NewDBTypeConverter[exerr.ErrorType, string](func(v exerr.ErrorType) (string, error) {
return v.Key, nil
}, func(v string) (exerr.ErrorType, error) {
for _, etp := range exerr.ListRegisteredTypes() {
if etp.Key == v {
return etp, nil
}
}
return exerr.NewType(v, nil), nil
})
type dbTypeConverterImpl[TModelData any, TDBData any] struct { type dbTypeConverterImpl[TModelData any, TDBData any] struct {
dbTypeString string dbTypeString string
modelTypeString string modelTypeString string

View File

@ -150,4 +150,7 @@ func (db *database) RegisterDefaultConverter() {
db.RegisterConverter(ConverterRFC339NanoTimeToString) db.RegisterConverter(ConverterRFC339NanoTimeToString)
db.RegisterConverter(ConverterJsonObjToString) db.RegisterConverter(ConverterJsonObjToString)
db.RegisterConverter(ConverterJsonArrToString) db.RegisterConverter(ConverterJsonArrToString)
db.RegisterConverter(ConverterExErrCategoryToString)
db.RegisterConverter(ConverterExErrSeverityToString)
db.RegisterConverter(ConverterExErrTypeToString)
} }