First Commit

master
DEV 3 years ago
parent fa906bf014
commit 739cd70fde

@ -0,0 +1,3 @@
module app
go 1.22

@ -0,0 +1,35 @@
package main
import (
"errs"
"fmt"
)
func callerA () {
callerB()
}
func callerB () {
e := errs.NewErrInfo(errs.ErrCode("Un-Error"),
errs.ErrInfoWithDate(),
errs.ErrInfoWithCause(errs.ErrCode("UnMalError")),
errs.ErrInfoWithField("Campo1",33),
errs.ErrInfoWithMessage("Esto es el mensage"),
errs.ErrInfoWithStack(0),
)
fmt.Print(e.String())
}
func main() {
callerA()
}

@ -0,0 +1,58 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name active.go
// Created by DEV
// Modified 20/03/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 errs
const (
stackMaxFrames = 32
stackSkipFrames = 2
unknownInfo = "???"
)
const (
traceUnknown string = "??"
)
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"),
}
)

@ -0,0 +1,42 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name causer.go
// Created by DEV
// Modified 20/03/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 errs
type (
Causer interface {
Cause () error
}
CauseFnc func () error
)
func (f CauseFnc) Cause() error {
return f() }

@ -0,0 +1,63 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_cause.go
// Created by DEV
// Modified 20/03/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 errs
import "fmt"
type (
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) Cause () error {
return e.cause }
func (e ErrCause) Unwrap() error {
return e.cause }
func (e ErrCause) String() string {
return fmt.Sprintf("%s <- %s",e.Error(),e.cause.Error()) }
func (e ErrCause) ToErrInfo (options ...ErrInfoFnc) *ErrInfo {
return NewErrInfo(e,options...) }

@ -0,0 +1,78 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_code.go
// Created by DEV
// Modified 20/03/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 errs
type (
// ErrCode
// Representa una cadena identificando un error.
// Dicha cadena debe de estar formada por el dominio del paquete y un codigo alfa identificando el error, con la
// siguiente estructura
// dominio:paquete.codigo-del-error
//
// como por ejemplo:
// activething.com:http.404-not-found
// activething.com:byte.buffer-overflow
//
// Implementa: Error
ErrCode string
)
func (e ErrCode) Error() string {
return string(e) }
func (e ErrCode) Cause() error {
return nil }
func (e ErrCode) Unwrap() error {
return nil }
func (e ErrCode) Stack() *Stack {
return nil }
func (e ErrCode) ToErrCause(cause error) *ErrCause {
return NewErrCause(e, cause) }
func (e ErrCode) ToErrInfo (options ...ErrInfoFnc) *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,130 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_info.go
// Created by DEV
// Modified 21/03/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 errs
import (
"bytes"
"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
help string
message string
fields map[string]any
stack Stack
}
ErrInfoFnc func (info *ErrInfo)
)
func NewErrInfo (err error, options ...ErrInfoFnc) *ErrInfo {
if err == nil {
return nil
}
return applyErrInfoOptions(&ErrInfo{ error: err }, options ...)
}
func (e ErrInfo) Cause() error {
if cs,is := e.error.(Causer); is {
return cs.Cause()
}
return Unwrap(e.error)
}
func (e ErrInfo) Date () time.Time {
return e.date }
func (e ErrInfo) Message() string {
return e.message }
func (e ErrInfo) Help () string {
return e.help }
func (e ErrInfo) Stack () Stack {
return e.stack }
func (e ErrInfo) Fields () map[string]any {
return e.fields }
func (e ErrInfo) String () string {
bf := bytes.Buffer{}
defer bf.Reset()
bf.WriteString("Error **************************************************\n")
bf.WriteString("Source : ")
bf.WriteString(e.error.Error())
if e.Cause() != nil {
bf.WriteString("\nCause : ")
bf.WriteString(e.Cause().Error())
}
if len(e.message) > 0 {
bf.WriteString("\nMessage: ")
bf.WriteString(e.message)
}
if !e.date.IsZero() {
bf.WriteString("\nDate : ")
bf.WriteString(e.date.Format(time.RFC850))
}
if len(e.help) > 0 {
bf.WriteString("\nHelp : ")
bf.WriteString(e.message)
}
if len(e.fields) > 0 {
bf.WriteString(fmt.Sprintf("\nFields : (%d)",len(e.fields)))
for f, v := range e.fields {
bf.WriteString(fmt.Sprintf("\n - %s: %v",f,v))
}
}
if e.stack != nil {
bf.WriteString("\nStack --------------------------------------------------\n")
bf.WriteString(e.stack.String())
bf.WriteString("\n--------------------------------------------------------\n")
}
return bf.String()
}

@ -0,0 +1,79 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_info_ops.go
// Created by DEV
// Modified 21/03/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 errs
import "time"
func ErrInfoWithCause (cause error) ErrInfoFnc {
return func(e *ErrInfo) {
if cause == nil {
return
}
e.error = *NewErrCause(e.error,cause)
}
}
func ErrInfoWithDate () ErrInfoFnc {
return func(e *ErrInfo) {
e.date = time.Now() }
}
func ErrInfoWithStack (offset int) ErrInfoFnc {
return func (e *ErrInfo) {
e.stack = MakeStack(offset) }
}
func ErrInfoWithMessage(message string) ErrInfoFnc {
return func (e *ErrInfo) {
e.message = message }
}
func ErrInfoWithHelp (help string) ErrInfoFnc {
return func (e *ErrInfo) {
e.help = help }
}
func ErrInfoWithField(name string, value any) ErrInfoFnc {
return func (e *ErrInfo) {
if e.fields == nil {
e.fields = map[string]any{name: value}
return
}
e.fields[name] = value
}
}
func applyErrInfoOptions (errInfo *ErrInfo, options ...ErrInfoFnc) *ErrInfo {
for _,o := range options {
o(errInfo)
}
return errInfo
}

@ -0,0 +1,83 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name stack_trace.go
// Created by DEV
// Modified 20/03/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 errs
import (
"bytes"
"runtime"
)
type (
Stack []uintptr
)
func MakeStack(offset int) Stack {
ls := make([]uintptr,stackMaxFrames)
if offset < 0 {
offset = 0
}
cn := runtime.Callers(offset+stackSkipFrames,ls)
return ls[:cn]
}
func (s Stack) Frames() []StackFrame {
f := make([]StackFrame,len(s))
for i,p := range s {
f[i]=*NewStackFrame(p)
}
return f
}
func (s Stack) String () (res string) {
bf := bytes.Buffer{}
defer bf.Reset()
for _,f := range s.Frames() {
bf.WriteString(f.String())
}
res = bf.String()
return
}
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
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,111 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name stack_frame.go
// Created by DEV
// Modified 20/03/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 errs
import (
"bufio"
"bytes"
"fmt"
"os"
"runtime"
"strings"
)
type (
StackFrame struct {
LineNumber int // Number line
File string // File FuncName
FuncName string // Function name
Package string // Package name
Counter uintptr
}
)
func NewStackFrame (counter uintptr) *StackFrame {
if counter == 0 {
return nil
}
sf := &StackFrame{
Counter: counter,
}
fn := runtime.FuncForPC(counter)
sf.Package, sf.FuncName = packageAndName(fn)
sf.File, sf.LineNumber = fn.FileLine(counter-1)
return sf
}
func (f StackFrame) SourceLine() (string, error) {
if f.LineNumber <= 0 {
return unknownInfo, nil
}
file, err := os.Open(f.File)
if err != nil {
return "", err
}
scanner := bufio.NewScanner(file)
currentLine := 1
for scanner.Scan() {
if currentLine == f.LineNumber {
return string(bytes.Trim(scanner.Bytes(), " \t")), nil
}
currentLine++
}
_ = file.Close()
if err := scanner.Err(); err != nil {
return "", err
}
return unknownInfo, nil
}
func (f StackFrame) String () string {
source, err := f.SourceLine()
if err != nil {
return fmt.Sprintf("%s:%d (0x%x)\n\t%s:%s\n", f.File, f.LineNumber, f.Counter, f.Package, f.FuncName)
}
return fmt.Sprintf("%s:%d (0x%x)\n\t%s:%s -> %s\n", f.File, f.LineNumber, f.Counter, f.Package, f.FuncName, source)
}
func packageAndName (fn *runtime.Func) (string,string) {
nm := fn.Name()
pg := ""
if ls := strings.LastIndex(nm,"/"); ls >= 0 {
pg = nm[:ls] + "/"
nm = nm[ls+1:]
}
if pd := strings.Index(nm,"."); pd >= 0 {
pg += nm[:pd]
nm = nm[pd+1:]
}
return pg,strings.Replace(nm,"·", ".",-1)
}

@ -0,0 +1,50 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name unwraper.go
// Created by DEV
// Modified 21/03/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 errs
type (
Unwrapper interface {
Unwrap () error
}
UnwrapperFnc func () error
)
func (f UnwrapperFnc) Unwrap () error {
return f() }
func Unwrap (err error) error {
if cs,is := err.(Unwrapper); is {
return cs.Unwrap()
}
return nil
}

@ -0,0 +1,84 @@
// ------------------------------------------------------------------------
// Project active
// Active Thing (activething.com) git.activething.com/go
//
// File name bytes.go
// Created by DEV
// Modified 07/03/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 rets
type (
BytesResult interface {
Result
Value() []byte // GResult implementation
}
BytesSuccess []byte
BytesError struct{
error
}
)
func TupleBytesSuccess(bytes []byte) ([]byte, error) {
return bytes, nil }
func TupleBytesError(err error) ([]byte, error) {
return nil, err }
// BytesSuccess -> BytesResult Implementation
func MakeBytesSuccess(success []byte) BytesSuccess {
return success }
func (_ BytesSuccess) Ok() bool {
return true }
func (_ BytesSuccess) Fail() error {
return nil }
func (s BytesSuccess) Value() []byte {
return s }
func (s BytesSuccess) Success() any {
return s }
// BytesError -> BytesResult Implementation
func MakeBytesError(cause error) BytesError {
return BytesError{cause} }
func (e BytesError) Ok() bool {
return false }
func (e BytesError) Fail() error {
return e.error }
func (_ BytesError) Value() []byte {
return nil }
func (_ BytesError) Success() any {
return nil }

@ -0,0 +1,86 @@
// ------------------------------------------------------------------------
// Project active
// Active Thing (activething.com) git.activething.com/go
//
// File name result.go
// Created by DEV
// Modified 07/03/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 rets
type (
GResult [T any] interface {
Result
Value() T
}
ResultGSuccess [T any] struct {
Result T
}
ResultGError [T any] struct {
error
}
)
func TupleGSuccess[T any](res T) (T, error) {
return res, nil }
func TupleGError[T any](err error) (T, error) {
var r T
return r, err
}
func MakeGSuccess[T any](res T) ResultGSuccess[T] {
return ResultGSuccess[T]{res} }
func (r ResultGSuccess[T]) Value() T {
return r.Result }
func (r ResultGSuccess[T]) Success() any {
return r.Result }
func (_ ResultGSuccess[T]) Ok() bool {
return true }
func (_ ResultGSuccess[T]) Fail() error {
return nil }
func MakeGError[T any](err error) ResultGError[T] {
return ResultGError[T]{error: err} }
func (r ResultGError[T]) Value() T {
var zero T
return zero
}
func (_ ResultGError[T]) Ok() bool {
return false }
func (r ResultGError[T]) Success() any {
return nil }
func (r ResultGError[T]) Fail() error {
return r.error }

@ -0,0 +1,3 @@
module rets
go 1.22

@ -0,0 +1,80 @@
// ------------------------------------------------------------------------
// Project active
// Active Thing (activething.com) git.activething.com/go
//
// File name int.go
// Created by DEV
// Modified 07/03/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 rets
type (
IntResult interface {
Result
Value() int // GResult implementation
}
IntSuccess int
IntError struct{ error }
)
func TupleIntSuccess(result int) (int, error) {
return result, nil }
func TupleIntError(err error) ([]byte, error) {
return nil, err }
// IntSuccess -> Result, G.Result Implementation
func MakeIntSuccess(success int) IntSuccess {
return IntSuccess(success) }
func (_ IntSuccess) Ok() bool {
return true }
func (_ IntSuccess) Fail() error {
return nil }
func (s IntSuccess) Success() any {
return s }
func (s IntSuccess) Value() int {
return int(s) }
// IntError -> Result, G.Result Implementation
func MakeIntError(cause error) IntError {
return IntError{cause} }
func (e IntError) Ok() bool {
return false }
func (e IntError) Fail() error {
return e.error }
func (_ IntError) Success() any {
return nil }
func (_ IntError) Value() int {
return 0 }

@ -0,0 +1,44 @@
// ------------------------------------------------------------------------
// Project active
// Active Thing (activething.com) git.activething.com/go
//
// File name result.go
// Created by DEV
// Modified 08/03/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 rets
type (
Result interface {
// Ok ; con un valor de true indica que el resultado
// se ha producido sin errores
Ok () bool
// Success Value; contiene el valor del resultado en caso
// de que no se haya producido un error, en ese caso
// devolvera nil.
Success() any
// Fail Error; contiene el error del resultado
Fail() error
}
)

@ -0,0 +1,80 @@
// ------------------------------------------------------------------------
// Project active
// Active Thing (activething.com) git.activething.com/go
//
// File name string.go
// Created by DEV
// Modified 07/03/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 rets
type (
StringResult interface {
Result
Value() string // GResult implementation
}
StringSuccess string
StringError struct{ error }
)
func TupleStringSuccess(result string) (string, error) {
return result, nil }
func TupleStringError(err error) (string, error) {
return "", err }
// StringSuccess -> Result, G.Result Implementation
func (_ StringSuccess) Ok() bool {
return true }
func (s StringSuccess) Success() any {
return s }
func (_ StringSuccess) Fail() error {
return nil }
func (s StringSuccess) Value() string {
return string(s) }
// StringError -> Result, G.Result Implementation
func MakeStringError(cause error) IntError {
return IntError{cause} }
func (e StringError) Ok() bool {
return false }
func (_ StringError) Success() any {
return nil }
func (e StringError) Fail() error {
return e.error }
func (_ StringError) Value() string {
return "" }

@ -24,8 +24,10 @@
package vals package vals
type ( type (
GNameValuer[T any] interface { GNameValuer[T any] interface {
Namer Namer
GValuer[T] GValuer[T]
} }
) )

@ -24,13 +24,17 @@
package vals package vals
type ( type (
GValuer[T any] interface { GValuer[T any] interface {
Value() T Value() T
} }
GValuerFnc[T any] func() T GValuerFnc[T any] func() T
) )
func (f GValuerFnc[T]) Value() T { func (f GValuerFnc[T]) Value() T {
return f() return f() }
}

@ -24,13 +24,27 @@
package vals package vals
type ( type (
KeyValueGetter interface { KeyValueGetter interface {
GetValue(key any) (any, bool)
GetKeyValue(key any) (any, bool)
} }
GetKeyValueFnc func(key any) (any, bool) GetKeyValueFnc func(key any) (any, bool)
) )
func (f GetKeyValueFnc) GetKeyValue(key any) (any, bool) { func (f GetKeyValueFnc) GetKeyValue(key any) (any, bool) {
return f(key) return f(key) }
}
func nopGetKeyValue (_ any) (any,bool) {
return nil,false }

@ -23,4 +23,20 @@
package vals package vals
type () type (
KeyValueSetter interface {
SetKeyValue(key, value any) error
}
SetKeyValueFnc func(key, value any) error
)
func (f SetKeyValueFnc) Key(key, value any) error {
return f(key,value) }

@ -24,8 +24,12 @@
package vals package vals
type ( type (
NameValuer interface { NameValuer interface {
Namer
Valuer Name () string // Namer
Value() any // Valuer
} }
) )

@ -24,6 +24,8 @@
package vals package vals
type ( type (
Namer interface { Namer interface {
Name() string Name() string
} }
@ -31,6 +33,6 @@ type (
NameFnc func() string NameFnc func() string
) )
func (f NameFnc) Name() string { func (f NameFnc) Name() string {
return f() return f() }
}

@ -26,26 +26,40 @@ package vals
import "sync" import "sync"
type ( type (
Scope struct {
ScopeAtt Scope struct {
parent KeyValueGetter atts ScopeAtt
values sync.Map parent KeyValueGetter
values sync.Map
} }
ScopeFnc func(*Scope) ScopeFnc func(*Scope)
) )
func NewScope(options ...ScopeFnc) *Scope { func NewScope(options ...ScopeFnc) *Scope {
return applyScopeOptions(&Scope{}, options...) return applyScopeOptions(&Scope{ parent: GetKeyValueFnc(nopGetKeyValue)}, options...) }
}
func (s *Scope) NewChildScope(options ...ScopeFnc) *Scope {
return applyScopeOptions(&Scope{parent: s },options ...) }
func (s *Scope) Atts () ScopeAtt {
return s.atts }
func (s *Scope) GetValue(key any) (any, bool) {
func (s *Scope) GetKeyValue(key any) (any, bool) {
if key == nil { if key == nil {
return nil, false return nil, false
} }
v, h := s.values.Load(key) v, h := s.values.Load(key)
if !h { if !h {
v, h = s.parent.GetValue(key) v, h = s.parent.GetKeyValue(key)
} }
if h { if h {
return AsValue(v), true return AsValue(v), true
@ -53,13 +67,32 @@ func (s *Scope) GetValue(key any) (any, bool) {
return nil, false return nil, false
} }
func (s *Scope) SetValue(key any, value any) error {
func (s *Scope) SetKeyValue(key any, value any) error {
if key == nil { if key == nil {
//todo error //todo error
} }
if s.IsReadOnly() { if s.atts.IsReadOnly() {
//todo error //todo error
} }
s.values.Store(key, AsValuer(value)) s.values.Store(key, AsValuer(value))
return nil return nil
} }
func (s *Scope) DelKeyValue (key any) {
s.values.Delete(key) }
func (s *Scope) Range (f func(key, value any) bool) {
s.values.Range(f) }
func (s *Scope) Reset () {
s.values.Range(func(k,v any) bool {
s.values.Delete(k)
return true
})
}

@ -23,6 +23,8 @@
package vals package vals
func applyScopeOptions(scope *Scope, options ...ScopeFnc) *Scope { func applyScopeOptions(scope *Scope, options ...ScopeFnc) *Scope {
for _, o := range options { for _, o := range options {
o(scope) o(scope)

@ -24,6 +24,7 @@
package vals package vals
type ( type (
Valuer interface { Valuer interface {
Value() any Value() any
} }
@ -31,6 +32,7 @@ type (
ValueFnc func() any ValueFnc func() any
) )
func (f ValueFnc) Value() any { func (f ValueFnc) Value() any {
return f() return f()
} }

@ -3,6 +3,9 @@ go 1.22
use ( use (
./core/errs ./core/errs
./core/rets
./core/vals ./core/vals
./app
) )
Loading…
Cancel
Save

Powered by TurnKey Linux.