First Commit

packs2
DEV 3 years ago
parent 33e4d8a6ab
commit 848ea1e78c

@ -44,7 +44,9 @@ type (
}
)
func NewBuffer() *Buffer { return &Buffer{} }
func NewBuffer() *Buffer {
return &Buffer{}
}
func (b *Buffer) Len() int {
return len(b.data) - b.off

@ -0,0 +1,55 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name err_causer.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
type (
// Causer
// Define un interface para los objetos que implementan la información sobre un error
// previo causante del error
Causer interface {
Cause() error
}
// CauserFnc
// Función para ser llamada para obtener el error causante
CauserFnc func() error
)
func (f CauserFnc) Cause() error { return f() }
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -0,0 +1,75 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name err_cause.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
import "activething.com/go/gogo/core/opts"
type (
// ErrCause
// Define una estructura para almacenar un error y su causa previa
// Implmenta error, Unwrap
//
ErrCause struct {
error
cause error
}
)
func NewErrCause(err, cause error) *ErrCause {
if err == nil {
if cause == nil {
return nil
}
return &ErrCause{error: cause}
}
return &ErrCause{error: err, cause: cause}
}
func (e ErrCause) WithInfo(options ...opts.OptionFnc[ErrInfo]) *ErrInfo {
return NewErrInfo(e, options...)
}
func (e ErrCause) Stack() StackTrace {
return getStackTrace(e.cause)
}
func (e ErrCause) Cause() error { return e.cause }
func (e ErrCause) Unwrap() error { return e.cause }
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -1,13 +1,14 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name err_code.go
// Created by DEV
// Modified 16/01/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name err_code.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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
@ -19,10 +20,13 @@
// 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 errs
import "activething.com/go/gogo/core/opts"
type (
// ErrCode
@ -40,3 +44,28 @@ type (
)
func (e ErrCode) Error() string { return string(e) }
func (e ErrCode) Stack() StackTrace {
return nil
}
func (e ErrCode) WithCause(cause error) *ErrCause {
return NewErrCause(e, cause)
}
func (e ErrCode) WithInfo(options ...opts.OptionFnc[ErrInfo]) *ErrInfo {
return NewErrInfo(e, options...)
}
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -0,0 +1,78 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name err_group.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
import "activething.com/go/gogo/core/opts"
type ErrGroup ErrList
func (_ ErrGroup) Error() string {
return "core:errs.group-error"
}
func (g ErrGroup) Count() int {
return len(g)
}
func (g ErrGroup) Errors() []error {
return g
}
func (g ErrGroup) Cause() error {
switch len(g) {
case 0:
return nil
case 1:
return g[0]
default:
return ErrGroup(g[1:])
}
}
func (g ErrGroup) Unwrap() error {
return g.Cause()
}
func (g ErrGroup) Stack() StackTrace {
return g.Stack()
}
func (g ErrGroup) WithInfo(options ...opts.OptionFnc[ErrInfo]) *ErrInfo {
return NewErrInfo(g, options...)
}
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -0,0 +1,169 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name err_info.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
import (
"activething.com/go/gogo/core/opts"
"fmt"
"time"
)
type (
// ErrInfo
// Representa un objeto para añadir una capa de información addicional a un error
//
ErrInfo struct {
/*embedded*/ error
date time.Time
source string
help string
message string
link string
details map[string]any
stack StackTrace
}
)
func NewErrInfo(err error, options ...opts.OptionFnc[ErrInfo]) *ErrInfo {
if err == nil {
return nil
}
return opts.Apply(&ErrInfo{error: err}, options...)
}
func (i ErrInfo) Message() string {
return i.message
}
func (i ErrInfo) Source() string {
return i.source
}
func (i ErrInfo) Help() string {
return i.help
}
func (i ErrInfo) Link() string {
return i.link
}
func (i ErrInfo) Stack() StackTrace {
if i.stack != nil {
return i.stack
}
return getStackTrace(i.Cause())
}
func (i ErrInfo) Details() map[string]any {
return i.Details()
}
func (i ErrInfo) Cause() error {
if cs, is := i.error.(Causer); is {
return cs.Cause()
}
return Unwrap(i.error)
}
func (i ErrInfo) Unwrap() error {
return i.Cause()
}
func (i ErrInfo) Date() time.Time { return i.date }
func ErrInfoWithCause(cause error) opts.OptionFnc[ErrInfo] {
return func(i *ErrInfo) {
if cause == nil {
return
}
i.error = NewErrCause(i.error, cause)
}
}
func ErrInfoWithMessage(message string) opts.OptionFnc[ErrInfo] {
return func(i *ErrInfo) {
i.message = message
}
}
func ErrInfoWithMessagef(format string, args ...any) opts.OptionFnc[ErrInfo] {
return func(i *ErrInfo) {
i.message = fmt.Sprintf(format, args...)
}
}
func ErrInfoWithLink(link string) opts.OptionFnc[ErrInfo] {
return func(i *ErrInfo) {
i.link = link
}
}
func ErrInfoWithHelp(help string) opts.OptionFnc[ErrInfo] {
return func(i *ErrInfo) {
i.help = help
}
}
func ErrInfoWithDate() opts.OptionFnc[ErrInfo] {
return func(i *ErrInfo) { i.date = time.Now() }
}
func ErrInfoWithStack(offset ...int) opts.OptionFnc[ErrInfo] {
return func(i *ErrInfo) {
if len(i.stack) > 0 {
return
}
ox := 3
if len(offset) > 0 {
ox = offset[0]
}
i.stack = *NewStackTrace(ox)
}
}
func ErrInfoWithDetail(name string, value any) opts.OptionFnc[ErrInfo] {
return func(i *ErrInfo) {
if i.details == nil {
i.details = make(map[string]any)
}
i.details[name] = value
}
}
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -0,0 +1,135 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name err_list.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
type ErrList []error
func (l ErrList) Clean() ErrList {
for _, e := range l {
if e == nil {
return l.clean()
}
}
return l
}
func (l ErrList) clean() ErrList {
rs := make([]error, 0, len(l))
for _, e := range l {
if e != nil {
rs = append(rs, e)
}
}
return rs
}
func (l ErrList) GetFirst() error {
for _, er := range l {
if er != nil {
return er
}
}
return nil
}
func (l ErrList) GetLast() error {
for i := len(l) - 1; i >= 0; i-- {
if l[i] != nil {
return l[i]
}
}
return nil
}
func (l ErrList) Any() bool {
return l.count(true) > 0
}
func (l ErrList) Count() int {
return _errorsCount(false, l)
}
func (l ErrList) count(ext bool) int {
rs := 0
for _, er := range l {
if er != nil {
if ext {
return 1
}
rs++
}
}
return rs
}
func (l ErrList) Stack() StackTrace {
for _, er := range l {
if er != nil {
st := getStackTrace(er)
if st != nil {
return st
}
}
}
return nil
}
func (l ErrList) ToError() error {
switch _errorsCount(false, l) {
case 0:
return nil
case 1:
return l.GetFirst()
default:
return ErrGroup(l)
}
}
func _errorsCount(exitOnAny bool, els []error) int {
rs := 0
for _, er := range els {
if er != nil {
rs++
if exitOnAny {
break
}
}
}
return rs
}
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -0,0 +1,53 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name err_info.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
type (
ErrorHandler interface {
HandleError(err error)
}
// ErrorHandlerFnc
// Función que implementa el interface ErrorHandler
ErrorHandlerFnc func(err error)
)
func (f ErrorHandlerFnc) HandleError(err error) {
f(err)
}
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -22,3 +22,103 @@
// ------------------------------------------------------------------------
package errs
import (
"errors"
"fmt"
)
type (
//
_W interface{ Unwrap() error }
// Posibles tipos que proveen una posible stack trace
_T interface{ StackTrace() []uintptr }
_C interface{ Callers() []uintptr }
_F interface{ StackFrames() []uintptr }
)
var (
G = struct {
ErrPanic ErrCode
ErrParamNil ErrCode
ErrParamInvalid ErrCode
ErrParamOutOfRange ErrCode
}{
ErrPanic: ErrCode("core:errs.panic-error"),
ErrParamNil: ErrCode("core:errs.param-nil"),
ErrParamInvalid: ErrCode("core:errs.param-invalid"),
ErrParamOutOfRange: ErrCode("core:errs.param-out-of-range"),
}
)
func Is(err, target error) bool {
return errors.Is(err, target)
}
func As(err error, target any) bool {
return errors.As(err, target)
}
func ToError(err any) error {
if err == nil {
return nil
}
switch e := err.(type) {
case error:
return e
case []error:
return ErrList(e).ToError()
default:
return ErrCode(fmt.Sprintf("#{er}"))
}
}
func ToPanic(err any) error {
if err == nil {
return nil
}
return G.ErrPanic.WithCause(ToError(err))
}
func Wrap(err, cause error) error {
if err == nil {
return cause
}
if cause == nil {
return err
}
return *NewErrCause(err, cause)
}
func Unwrap(err error) error {
cs, is := err.(_W)
if !is {
return nil
}
return cs.Unwrap()
}
func getStackTrace(err error) StackTrace {
for err != nil {
switch e := err.(type) {
case StackTracer:
return StackTrace(e.Stack())
case _T:
return StackTrace(e.StackTrace())
case _C:
return StackTrace(e.Callers())
case _F:
return StackTrace(e.StackFrames())
}
err = Unwrap(err)
}
return nil
}
func Test(err error) {
if err != nil {
fmt.Printf("Error %v\n", err.Error())
panic(err)
}
}

@ -0,0 +1,70 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name stack.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
import "runtime"
const (
stackMaxFrames = 32
stackSkipFrames = 2
)
type StackTrace []uintptr
func NewStackTrace(offset int) *StackTrace {
var fm [stackMaxFrames]uintptr
if offset < 0 {
offset = 0
}
cn := runtime.Callers(offset+stackSkipFrames, fm[:])
st := StackTrace(fm[0:cn])
return &st
}
func (s StackTrace) Frames() []StackTraceFrame {
ln := len(s)
if ln == 0 {
return nil
}
st := make([]StackTraceFrame, ln)
for ix := 0; ix < ln; ix++ {
st[ix] = StackTraceFrame(s[ix])
}
return st
}
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -0,0 +1,97 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name stack_trace_frame.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
import (
"path"
"runtime"
"strings"
)
const (
unknown string = "??"
)
type StackTraceFrame uintptr
func (f StackTraceFrame) PC() uintptr { return uintptr(f) - 1 }
func (f StackTraceFrame) FileName() (fl string) {
fl, _ = f.FileLine()
return
}
func (f StackTraceFrame) FileFull() string {
return path.Base(f.FileName())
}
func (f StackTraceFrame) Line() (ln int) {
_, ln = f.FileLine()
return
}
func (f StackTraceFrame) FileLine() (string, int) {
pc := f.PC()
if fn := runtime.FuncForPC(pc); fn != nil {
return fn.FileLine(pc)
}
return unknown, 0
}
func (f StackTraceFrame) FuncName() string {
return f.getFunc(true)
}
func (f StackTraceFrame) FuncFull() string {
return f.getFunc(false)
}
func (f StackTraceFrame) getFunc(cls bool) string {
if fn := runtime.FuncForPC(f.PC()); fn != nil {
nm := fn.Name()
if cls {
ix := strings.LastIndex(nm, "/")
nm = nm[ix+1:]
ix = strings.Index(nm, ".")
return nm[ix+1:]
}
return nm
}
return unknown
}
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -0,0 +1,45 @@
/*
---------------------------------------------------------
Project activething
Active Thing (activething.com) git.activething.com/go
File name stack_tracer.go
Created by DEV
Modified 23/10/2023
Copyright 2023 activething.com
---------------------------------------------------------
// 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 errs
type (
StackTracer interface {
Stack() StackTrace
}
)
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
SIDDHARTA
- HERMANN HESSE -
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

@ -0,0 +1,32 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name gogo.go
// Created by DEV
// Modified 17/01/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// 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 slices
func MakeSlice[T any](size, capacity int, creator func() T) []T {
rs := make([]T, size, capacity)
for i := 0; i < size; i++ {
rs[i] = creator()
}
return rs
}

@ -24,3 +24,26 @@
package slices
type String []string
func MakeString(size, capacity int) String {
return make(String, size, capacity)
}
func (s String) Reset() String {
if s != nil {
return s[:0]
}
return s
}
func (s String) Has(value string) bool {
if len(s) == 0 {
return false
}
for _, k := range s {
if k == value {
return true
}
}
return false
}

@ -24,7 +24,7 @@
package packs
import (
"activething.com/go/atila/core/slices"
"activething.com/go/gogo/core/slices"
"sync"
"sync/atomic"
)

Loading…
Cancel
Save

Powered by TurnKey Linux.