From 70de8e8d04698c3cbb488e7215c6623ca603dcb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 7 Jan 2024 04:18:03 +0100 Subject: [PATCH] v0.0.362 --- dataext/syncMap.go | 113 +++++++++++++++++++++++++++++++++++++++++++++ exerr/data.go | 15 +++--- goextVersion.go | 4 +- sq/converter.go | 35 ++++++++++++++ sq/database.go | 3 ++ 5 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 dataext/syncMap.go diff --git a/dataext/syncMap.go b/dataext/syncMap.go new file mode 100644 index 0000000..4d1db41 --- /dev/null +++ b/dataext/syncMap.go @@ -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 +} diff --git a/exerr/data.go b/exerr/data.go index b487bda..33e6649 100644 --- a/exerr/data.go +++ b/exerr/data.go @@ -71,15 +71,18 @@ var ( // 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 { - insertOkay := registeredTypes.Add(key) - if !insertOkay { - panic("Cannot register same ErrType ('" + key + "') more than once") - } + et := ErrorType{key, defStatusCode} - return ErrorType{key, defStatusCode} + registeredTypes.Set(key, et) + + return et +} + +func ListRegisteredTypes() []ErrorType { + return registeredTypes.GetAllValues() } type LogPrintLevel string diff --git a/goextVersion.go b/goextVersion.go index 545c3fe..9bc989b 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ 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" diff --git a/sq/converter.go b/sq/converter.go index 9583327..dea7cc6 100644 --- a/sq/converter.go +++ b/sq/converter.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "gogs.mikescher.com/BlackForestBytes/goext/exerr" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/rfctime" "reflect" @@ -105,6 +106,40 @@ var ConverterJsonArrToString = NewDBTypeConverter[JsonArr, string](func(v JsonAr 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 { dbTypeString string modelTypeString string diff --git a/sq/database.go b/sq/database.go index 12f4b0f..7a3ac59 100644 --- a/sq/database.go +++ b/sq/database.go @@ -150,4 +150,7 @@ func (db *database) RegisterDefaultConverter() { db.RegisterConverter(ConverterRFC339NanoTimeToString) db.RegisterConverter(ConverterJsonObjToString) db.RegisterConverter(ConverterJsonArrToString) + db.RegisterConverter(ConverterExErrCategoryToString) + db.RegisterConverter(ConverterExErrSeverityToString) + db.RegisterConverter(ConverterExErrTypeToString) }