parent
ad9e72cf4d
commit
11643decd0
@ -0,0 +1,122 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project active3
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name active.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 04/05/2024
|
||||||
|
//
|
||||||
|
// 🔒 Copyright 2024 activething.com
|
||||||
|
//
|
||||||
|
// 📜 LICENSE --------------------------------------------------------------
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
package erstr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"typs/strs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
|
||||||
|
labelError string = "error"
|
||||||
|
labelSep string = " : "
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
|
||||||
|
stringer interface { String () string }
|
||||||
|
unwrapper interface { Unwrap () error }
|
||||||
|
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var (
|
||||||
|
|
||||||
|
G = struct {
|
||||||
|
LabelSep string
|
||||||
|
LineLabelFmt string
|
||||||
|
LineErrorFmt string
|
||||||
|
|
||||||
|
Transformers strs.Transformers
|
||||||
|
}{
|
||||||
|
|
||||||
|
LabelSep : labelSep,
|
||||||
|
LineLabelFmt : "%s" + labelSep + "%s\n",
|
||||||
|
LineErrorFmt : labelError + labelSep + "%s\n",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func StringifyError (text string, err error) string {
|
||||||
|
ln := []string { "", text }
|
||||||
|
|
||||||
|
var nl string
|
||||||
|
ne := err
|
||||||
|
for ne != nil {
|
||||||
|
if st,is := ne.(stringer); is {
|
||||||
|
nl = st.String()
|
||||||
|
}
|
||||||
|
if uw,is := ne.(unwrapper); is {
|
||||||
|
ne = uw.Unwrap()
|
||||||
|
} else {
|
||||||
|
ne = nil
|
||||||
|
if strings.Index(nl,labelError) != -1 {
|
||||||
|
ln[0]= G.Transformers.Transform(nl)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ln = append(ln, G.Transformers.Transform(nl))
|
||||||
|
}
|
||||||
|
if len(ln[0]) == 0 {
|
||||||
|
ln[0]=G.Transformers.Transform(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(ln,"")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func FormatLineError(strErr string) string {
|
||||||
|
return fmt.Sprintf(G.LineErrorFmt,strErr) }
|
||||||
|
|
||||||
|
func FormatLine (label, text string) string {
|
||||||
|
return fmt.Sprintf(G.LineLabelFmt,label,text) }
|
||||||
|
|
||||||
|
|
||||||
|
func RemoveLabel (text string) string {
|
||||||
|
if strings.Index(text,G.LabelSep) == -1 {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
l := strings.Split(text,"\n")
|
||||||
|
b := bytes.Buffer{}
|
||||||
|
for _,t := range l {
|
||||||
|
if p := strings.Index(t,G.LabelSep); p != -1 {
|
||||||
|
b.WriteString(t[p+len(G.LabelSep):])
|
||||||
|
b.WriteByte('\n')
|
||||||
|
} else {
|
||||||
|
b.WriteString(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,121 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project active3
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name stringify.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 06/05/2024
|
||||||
|
//
|
||||||
|
// 🔒 Copyright 2024 activething.com
|
||||||
|
//
|
||||||
|
// 📜 LICENSE --------------------------------------------------------------
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
package erstr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"typs/strs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
|
||||||
|
Stringify struct {
|
||||||
|
LabelSep string
|
||||||
|
LineLabelFmt string
|
||||||
|
LineErrorFmt string
|
||||||
|
Transformers strs.Transformers
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorStringifyFnc func (stringify *Stringify)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func NewStringify (options ...ErrorStringifyFnc) *Stringify {
|
||||||
|
s := &Stringify{
|
||||||
|
LabelSep : labelSep,
|
||||||
|
LineLabelFmt : "%s" + labelSep + "%s\n",
|
||||||
|
LineErrorFmt : labelError + labelSep + "%s\n",
|
||||||
|
}
|
||||||
|
for _,o := range options {
|
||||||
|
o(s)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (s *Stringify) Stringify (text string, err error) string {
|
||||||
|
ln := []string { "", text }
|
||||||
|
var nl string
|
||||||
|
ne := err
|
||||||
|
for ne != nil {
|
||||||
|
if st,is := ne.(stringer); is {
|
||||||
|
nl = st.String()
|
||||||
|
}
|
||||||
|
if uw,is := ne.(unwrapper); is {
|
||||||
|
ne = uw.Unwrap()
|
||||||
|
} else {
|
||||||
|
ne = nil
|
||||||
|
if strings.Index(nl,labelError) != -1 {
|
||||||
|
ln[0]= s.Transformers.Transform(nl)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ln = append(ln, s.Transformers.Transform(nl))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ln[0]) == 0 {
|
||||||
|
ln[0]=s.Transformers.Transform(err.Error())
|
||||||
|
}
|
||||||
|
if len(ln) == 0 {
|
||||||
|
return ln[0]
|
||||||
|
}
|
||||||
|
b := bytes.Buffer{}
|
||||||
|
for _,l := range ln {
|
||||||
|
b.WriteString(l)
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stringify) FormatLineError(strErr string) string {
|
||||||
|
return fmt.Sprintf(s.LineErrorFmt,strErr) }
|
||||||
|
|
||||||
|
func (s *Stringify) FormatLine (label, text string) string {
|
||||||
|
return fmt.Sprintf(s.LineLabelFmt,label,text) }
|
||||||
|
|
||||||
|
|
||||||
|
func (s *Stringify) RemoveLabel (text string) string {
|
||||||
|
if strings.Index(text,s.LabelSep) == -1 {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
l := strings.Split(text,"\n")
|
||||||
|
b := bytes.Buffer{}
|
||||||
|
for _,t := range l {
|
||||||
|
if p := strings.Index(t,s.LabelSep); p != -1 {
|
||||||
|
b.WriteString(t[p+len(s.LabelSep):])
|
||||||
|
b.WriteByte('\n')
|
||||||
|
} else {
|
||||||
|
b.WriteString(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,161 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project active3
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name grepo.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 06/05/2024
|
||||||
|
//
|
||||||
|
// 🔒 Copyright 2024 activething.com
|
||||||
|
//
|
||||||
|
// 📜 LICENSE --------------------------------------------------------------
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
package reps
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"syns"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
|
||||||
|
GRepo [K comparable, V any] struct {
|
||||||
|
syns.LockerRW
|
||||||
|
items map[K]V
|
||||||
|
def V
|
||||||
|
validatorFn func(item V) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
GRepoFnc[K comparable, V any] func (r *GRepo[K,V])
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func NewGRepo [K comparable, V any](def V, safe bool, options ...GRepoFnc[K,V]) *GRepo[K,V] {
|
||||||
|
lk := syns.LockerRW (syns.NopLocker{})
|
||||||
|
if safe {
|
||||||
|
lk = &sync.RWMutex{}
|
||||||
|
}
|
||||||
|
rp := &GRepo[K,V]{
|
||||||
|
LockerRW : lk,
|
||||||
|
items : make(map[K]V),
|
||||||
|
validatorFn : func(_ V) bool { return true },
|
||||||
|
}
|
||||||
|
for _,o := range options {
|
||||||
|
o(rp)
|
||||||
|
}
|
||||||
|
return rp
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func (r *GRepo[_,_]) Len () int {
|
||||||
|
r.RLock()
|
||||||
|
l := len(r.items)
|
||||||
|
r.RUnlock()
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (r *GRepo[K, V]) Has (key K) bool {
|
||||||
|
r.RLock()
|
||||||
|
_,hs := r.items[key]
|
||||||
|
r.RUnlock()
|
||||||
|
return hs
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (r *GRepo[K, V]) Get (key K) (V,bool) {
|
||||||
|
r.RLock()
|
||||||
|
v,hs := r.items[key]
|
||||||
|
r.RUnlock()
|
||||||
|
return v,hs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *GRepo[K, V]) GetOrDefault (key K) V {
|
||||||
|
r.RLock()
|
||||||
|
v,hs := r.items[key]
|
||||||
|
if !hs {
|
||||||
|
v = r.def
|
||||||
|
}
|
||||||
|
r.RUnlock()
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (r *GRepo[K, V]) Reg (key K, item V) error {
|
||||||
|
r.Lock()
|
||||||
|
if r.validatorFn != nil && !r.validatorFn(item) {
|
||||||
|
r.Unlock()
|
||||||
|
return G.ErrInvalidItem
|
||||||
|
}
|
||||||
|
if _,hs := r.items[key]; hs {
|
||||||
|
r.Unlock()
|
||||||
|
return G.ErrDuplicatedItem
|
||||||
|
}
|
||||||
|
r.items[key]=item
|
||||||
|
r.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (r *GRepo[K, V]) Set (key K, item V) {
|
||||||
|
r.Lock()
|
||||||
|
if r.validatorFn != nil && !r.validatorFn(item) {
|
||||||
|
r.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.items[key]=item
|
||||||
|
r.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (r *GRepo[K, _]) Del (key ...K) {
|
||||||
|
r.Lock()
|
||||||
|
for _,k := range key {
|
||||||
|
delete(r.items, k)
|
||||||
|
}
|
||||||
|
r.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (r *GRepo[K,V]) GetAll (all bool, key...K) ([]V,int) {
|
||||||
|
c := 0
|
||||||
|
var l []V
|
||||||
|
for _,k := range key {
|
||||||
|
v,hs := r.items[k]
|
||||||
|
if hs {
|
||||||
|
l = append(l, v)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if all {
|
||||||
|
return nil,-1
|
||||||
|
}
|
||||||
|
c-=1
|
||||||
|
}
|
||||||
|
return l,c
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func RepoWithValidator[K comparable,T any] (validator func(item T) bool) GRepoFnc[K,T] {
|
||||||
|
return func(r *GRepo[K,T]) {
|
||||||
|
r.validatorFn = validator } }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
module reps
|
||||||
|
|
||||||
|
go 1.22
|
||||||
Loading…
Reference in new issue