diff --git a/dataext/syncSet.go b/dataext/syncSet.go index fae5c82..31197fb 100644 --- a/dataext/syncSet.go +++ b/dataext/syncSet.go @@ -7,6 +7,9 @@ type SyncSet[TData comparable] struct { lock sync.Mutex } +// Add adds `value` to the set +// returns true if the value was actually inserted +// returns false if the value already existed func (s *SyncSet[TData]) Add(value TData) bool { s.lock.Lock() defer s.lock.Unlock() @@ -15,10 +18,10 @@ func (s *SyncSet[TData]) Add(value TData) bool { s.data = make(map[TData]bool) } - _, ok := s.data[value] + _, existsInPreState := s.data[value] s.data[value] = true - return !ok + return !existsInPreState } func (s *SyncSet[TData]) AddAll(values []TData) { diff --git a/exerr/data.go b/exerr/data.go index e42135e..8f23340 100644 --- a/exerr/data.go +++ b/exerr/data.go @@ -1,6 +1,7 @@ package exerr import ( + "gogs.mikescher.com/BlackForestBytes/goext/dataext" "gogs.mikescher.com/BlackForestBytes/goext/langext" ) @@ -37,25 +38,32 @@ type ErrorType struct { //goland:noinspection GoUnusedGlobalVariable var ( - TypeInternal = ErrorType{"INTERNAL_ERROR", langext.Ptr(500)} - TypePanic = ErrorType{"PANIC", langext.Ptr(500)} - TypeNotImplemented = ErrorType{"NOT_IMPLEMENTED", langext.Ptr(500)} + TypeInternal = NewType("INTERNAL_ERROR", langext.Ptr(500)) + TypePanic = NewType("PANIC", langext.Ptr(500)) + TypeNotImplemented = NewType("NOT_IMPLEMENTED", langext.Ptr(500)) - TypeWrap = ErrorType{"Wrap", nil} + TypeWrap = NewType("Wrap", nil) - TypeBindFailURI = ErrorType{"BINDFAIL_URI", langext.Ptr(400)} - TypeBindFailQuery = ErrorType{"BINDFAIL_QUERY", langext.Ptr(400)} - TypeBindFailJSON = ErrorType{"BINDFAIL_JSON", langext.Ptr(400)} - TypeBindFailFormData = ErrorType{"BINDFAIL_FORMDATA", langext.Ptr(400)} - TypeBindFailHeader = ErrorType{"BINDFAIL_HEADER", langext.Ptr(400)} + TypeBindFailURI = NewType("BINDFAIL_URI", langext.Ptr(400)) + TypeBindFailQuery = NewType("BINDFAIL_QUERY", langext.Ptr(400)) + TypeBindFailJSON = NewType("BINDFAIL_JSON", langext.Ptr(400)) + TypeBindFailFormData = NewType("BINDFAIL_FORMDATA", langext.Ptr(400)) + TypeBindFailHeader = NewType("BINDFAIL_HEADER", langext.Ptr(400)) - TypeUnauthorized = ErrorType{"UNAUTHORIZED", langext.Ptr(401)} - TypeAuthFailed = ErrorType{"AUTH_FAILED", langext.Ptr(401)} + TypeUnauthorized = NewType("UNAUTHORIZED", langext.Ptr(401)) + TypeAuthFailed = NewType("AUTH_FAILED", langext.Ptr(401)) // other values come from pkgconfig ) +var registeredTypes = dataext.SyncSet[string]{} + func NewType(key string, defStatusCode *int) ErrorType { + insertOkay := registeredTypes.Add(key) + if !insertOkay { + panic("Cannot register same ErrType ('" + key + "') more than once") + } + return ErrorType{key, defStatusCode} } diff --git a/goextVersion.go b/goextVersion.go index ba3511a..1d43391 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.226" +const GoextVersion = "0.0.227" -const GoextVersionTimestamp = "2023-08-08T14:28:09+0200" +const GoextVersionTimestamp = "2023-08-08T15:28:29+0200"