parent
265e455452
commit
80fc2ece02
@ -0,0 +1,66 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project Active8
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name actor_id.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 07/12/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 brbac
|
||||||
|
|
||||||
|
|
||||||
|
type (
|
||||||
|
|
||||||
|
|
||||||
|
ActorID uint64
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Type extrae los 6 bits más significativos para determinar el tipo de Actor.
|
||||||
|
func (a ActorID) Type() ActorType {
|
||||||
|
// Desplazar el ActorID 58 bits a la derecha para que los 6 bits más significativos estén al inicio,
|
||||||
|
// y luego convertirlos en un ActorType.
|
||||||
|
return ActorType(a >> 58) // Extraemos los 6 bits más significativos
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// SetType establece los 6 bits más significativos como el tipo de Actor.
|
||||||
|
func (a ActorID) SetType(actorType ActorType) ActorID {
|
||||||
|
// Verificar que el tipo está dentro del rango de 6 bits (0-63).
|
||||||
|
if actorType > 0x3F { // 0x3F es 63 en decimal, el rango de 6 bits
|
||||||
|
panic(G.ErrInvalidActor)
|
||||||
|
}
|
||||||
|
// Establecer el nuevo tipo en los 6 bits más significativos.
|
||||||
|
return ActorID((a &^ (ActorID(0x3F) << 58)) | (ActorID(actorType) << 58))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func HasActor (actors []ActorID, id ActorID) bool {
|
||||||
|
for _,ai := range actors {
|
||||||
|
if ai == id {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,102 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project Active8
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name actor_type.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 08/12/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 brbac
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Definición de los valores posibles para ActorType (64 tipos con 8 bits).
|
||||||
|
const (
|
||||||
|
// Tipos Generales
|
||||||
|
ActorTypeUser ActorType = 0x00 // Usuario
|
||||||
|
ActorTypeRole ActorType = 0x01 // Rol
|
||||||
|
ActorTypeService ActorType = 0x02 // Servicio
|
||||||
|
ActorTypeDevice ActorType = 0x03 // Dispositivo (IoT, hardware)
|
||||||
|
ActorTypeGroup ActorType = 0x04 // Grupo
|
||||||
|
ActorTypeOrganization ActorType = 0x05 // Organización
|
||||||
|
ActorTypeAPIKey ActorType = 0x06 // API Key
|
||||||
|
ActorTypeSystem ActorType = 0x07 // Sistema interno
|
||||||
|
ActorTypeAdmin ActorType = 0x08 // Administrador
|
||||||
|
ActorTypeVisitor ActorType = 0x09 // Visitante
|
||||||
|
ActorTypeAuditor ActorType = 0x0A // Auditor
|
||||||
|
ActorTypeExternalClient ActorType = 0x0B // Cliente Externo
|
||||||
|
ActorTypeProvider ActorType = 0x0C // Proveedor
|
||||||
|
ActorTypeTemporaryToken ActorType = 0x0D // Token Temporal
|
||||||
|
ActorTypeBot ActorType = 0x0E // Bot o script
|
||||||
|
ActorTypeReserved ActorType = 0x0F // Reservado para futuras expansiones
|
||||||
|
|
||||||
|
// Tipos Relacionados con Blockchain
|
||||||
|
ActorTypeBlockchainNode ActorType = 0x10 // Nodo de blockchain
|
||||||
|
ActorTypeBlockchainUser ActorType = 0x11 // Usuario de blockchain
|
||||||
|
ActorTypeSmartContract ActorType = 0x12 // Contrato inteligente (smart contract)
|
||||||
|
ActorTypeBlockchainAPI ActorType = 0x13 // API de blockchain
|
||||||
|
ActorTypeWallet ActorType = 0x14 // Billetera (wallet) de blockchain
|
||||||
|
ActorTypeMiner ActorType = 0x15 // Minero de blockchain
|
||||||
|
ActorTypeValidator ActorType = 0x16 // Validador de transacciones o bloques
|
||||||
|
ActorTypeDApp ActorType = 0x17 // Aplicación descentralizada (DApp)
|
||||||
|
ActorTypeToken ActorType = 0x18 // Token o criptomoneda
|
||||||
|
ActorTypeBlockchainOracle ActorType = 0x19 // Oráculo de blockchain (proveedor de datos externos)
|
||||||
|
ActorTypeBlockchainExplorer ActorType = 0x1A // Explorador de blockchain
|
||||||
|
ActorTypeDAO ActorType = 0x1B // Organización Autónoma Descentralizada (DAO)
|
||||||
|
ActorTypeNFT ActorType = 0x1C // Token No Fungible (NFT)
|
||||||
|
ActorTypeBlockchainBridge ActorType = 0x1D // Puente entre blockchains
|
||||||
|
ActorTypeStaking ActorType = 0x1E // Participante en staking
|
||||||
|
ActorTypeDefiPlatform ActorType = 0x1F // Plataforma de Finanzas Descentralizadas (DeFi)
|
||||||
|
ActorTypeBlockchainWallet ActorType = 0x20 // Billetera específica de blockchain
|
||||||
|
ActorTypeBlockchainExplorerAPI ActorType = 0x21 // API para exploradores de blockchain
|
||||||
|
ActorTypeBlockchainTransaction ActorType = 0x22 // Transacción de blockchain
|
||||||
|
ActorTypeBlockchainBackup ActorType = 0x23 // Copia de seguridad o servicio de respaldo de blockchain
|
||||||
|
ActorTypeSecurityModule ActorType = 0x24 // Módulo de seguridad de blockchain
|
||||||
|
ActorTypeQuantumComputer ActorType = 0x25 // Computadora cuántica (relacionada con blockchain)
|
||||||
|
ActorTypeQuantumWallet ActorType = 0x26 // Billetera cuántica (relacionada con blockchain)
|
||||||
|
|
||||||
|
// Otros Tipos
|
||||||
|
ActorTypeAI ActorType = 0x30 // Actor basado en inteligencia artificial
|
||||||
|
ActorTypeCloudService ActorType = 0x31 // Servicio en la nube
|
||||||
|
ActorTypeMicroservice ActorType = 0x32 // Microservicio
|
||||||
|
ActorTypeThirdParty ActorType = 0x33 // Actor externo o tercero
|
||||||
|
ActorTypeExternalSystem ActorType = 0x34 // Sistema Externo
|
||||||
|
ActorTypeMobileDevice ActorType = 0x35 // Dispositivo móvil
|
||||||
|
ActorTypeExternalAPI ActorType = 0x36 // API externa
|
||||||
|
ActorTypeServer ActorType = 0x37 // Servidor físico o virtual
|
||||||
|
ActorTypeUserAgent ActorType = 0x38 // Agente de usuario (navegador, app, etc.)
|
||||||
|
ActorTypeContainer ActorType = 0x39 // Contenedor (Docker, etc.)
|
||||||
|
ActorTypeNetwork ActorType = 0x3A // Red o infraestructura de red
|
||||||
|
ActorTypeEdgeDevice ActorType = 0x3B // Dispositivo en la periferia de la red
|
||||||
|
ActorTypeVirtualMachine ActorType = 0x3C // Máquina virtual
|
||||||
|
ActorTypeHypervisor ActorType = 0x3D // Hypervisor o administrador de máquinas virtuales
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type ActorType uint8
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func (a ActorType) Name () string {
|
||||||
|
i := int(a)
|
||||||
|
if i < len(g.actorNames) {
|
||||||
|
return g.actorNames[i]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
@ -0,0 +1,135 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project Active8
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name role.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 08/12/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 brbac
|
||||||
|
|
||||||
|
import "rsls"
|
||||||
|
|
||||||
|
type (
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Role struct {
|
||||||
|
roleID ActorID
|
||||||
|
State ActorState
|
||||||
|
Flags ActorFlags
|
||||||
|
name string
|
||||||
|
actors []ActorID
|
||||||
|
subRoles []ActorID
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func (r *Role) AddRole (roleID ...ActorID) error {
|
||||||
|
if len(roleID) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usamos un mapa persistente para verificar los ciclos
|
||||||
|
state := make(map[ActorID]byte)
|
||||||
|
|
||||||
|
for _,ri := range roleID {
|
||||||
|
if ri == r.roleID {
|
||||||
|
return G.ErrRoleCycle
|
||||||
|
}
|
||||||
|
// Check if is RoleID
|
||||||
|
if ri.Type() != ActorTypeRole {
|
||||||
|
return G.ErrInvalidIDRole
|
||||||
|
}
|
||||||
|
// Check if already exists
|
||||||
|
if HasActor(r.subRoles,ri) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// **Verificación de ciclo antes de agregar el rol**
|
||||||
|
cycleResult := detectRoleCycleInHierarchy(r.roleID, ri, G.RoleProvider, &state)
|
||||||
|
if !cycleResult.IsOk() {
|
||||||
|
return cycleResult.Fail()
|
||||||
|
} else if cycleResult.Result() {
|
||||||
|
return G.ErrRoleCycle // Retornar error si se detecta un ciclo
|
||||||
|
}
|
||||||
|
|
||||||
|
// Agregar el rol a los subroles
|
||||||
|
r.subRoles = append(r.subRoles, ri)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func DetectRoleCycleInHierarchy(roleID, newRoleID ActorID, provider RoleProvider) rsls.RBool {
|
||||||
|
state := make(map[ActorID]byte)
|
||||||
|
return detectRoleCycleInHierarchy(roleID, newRoleID, G.RoleProvider, &state)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// detectRoleCycleInHierarchy realiza la verificación recursiva de si se forma un ciclo
|
||||||
|
func detectRoleCycleInHierarchy(roleID, newRoleID ActorID, provider RoleProvider, state *map[ActorID]byte) rsls.RBool {
|
||||||
|
|
||||||
|
// Si el nuevo rol ya está en la jerarquía del rol actual, entonces hemos encontrado un ciclo
|
||||||
|
if (*state)[newRoleID] == 1 {
|
||||||
|
return rsls.BoolSuccessTrue // Ciclo detectado
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si el rol ha sido visitado completamente, entonces no es necesario revisarlo de nuevo
|
||||||
|
if (*state)[newRoleID] == 2 {
|
||||||
|
return rsls.BoolSuccessFalse
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marcar el rol como "en proceso"
|
||||||
|
(*state)[newRoleID] = 1
|
||||||
|
|
||||||
|
// Obtener los subroles del nuevo rol
|
||||||
|
subRoles, err := provider.GetSubRoles(newRoleID)
|
||||||
|
if err != nil {
|
||||||
|
return rsls.MakeBoolError(err) // Error al obtener los subroles
|
||||||
|
}
|
||||||
|
|
||||||
|
// Realizar DFS para cada subrol del nuevo rol
|
||||||
|
for _, subRoleID := range subRoles {
|
||||||
|
if subRoleID == roleID {
|
||||||
|
// Detectar referencia directa al rol original, indicando un ciclo
|
||||||
|
return rsls.BoolSuccessTrue
|
||||||
|
}
|
||||||
|
cycleCheck := detectRoleCycleInHierarchy(roleID, subRoleID, provider, state)
|
||||||
|
if !cycleCheck.IsOk() ||cycleCheck.Result() {
|
||||||
|
return cycleCheck
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marcar el rol como "visitado" después de procesar todos sus subroles
|
||||||
|
(*state)[newRoleID] = 2
|
||||||
|
return rsls.BoolSuccessFalse // No se ha detectado un ciclo
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,112 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project Active8
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name roles_test.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 09/12/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 brbac
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// MockRoleProvider simula un proveedor de roles que contiene roles predefinidos
|
||||||
|
type MockRoleProvider struct {
|
||||||
|
roles map[ActorID][]ActorID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockRoleProvider) GetSubRoles(roleID ActorID) ([]ActorID, error) {
|
||||||
|
if subRoles, exists := m.roles[roleID]; exists {
|
||||||
|
return subRoles, nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func TestAddRole(t *testing.T) {
|
||||||
|
// Crear un RoleProvider simulado con algunos roles y subroles
|
||||||
|
|
||||||
|
provider := &MockRoleProvider{
|
||||||
|
roles: map[ActorID][]ActorID{
|
||||||
|
0x10: {0x11, 0x12}, // El rol 0x10 tiene subroles 0x11 y 0x12
|
||||||
|
0x11: {0x12}, // El rol 0x11 tiene subrol 0x12
|
||||||
|
0x12: {}, // El rol 0x12 no tiene subroles
|
||||||
|
},
|
||||||
|
}
|
||||||
|
G.RoleProvider = provider
|
||||||
|
|
||||||
|
// Crear un rol inicial
|
||||||
|
role := &Role{
|
||||||
|
roleID: 0x10,
|
||||||
|
State: ActorStateActive,
|
||||||
|
name: "Role10",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 1: Añadir un rol que no crea ciclo
|
||||||
|
err := role.AddRole(0x11)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error inesperado al agregar un rol no cíclico: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 2: Intentar añadir un rol que ya existe
|
||||||
|
err = role.AddRole(0x11) // El rol 0x11 ya fue añadido
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error inesperado al agregar un rol ya existente: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 3: Intentar añadir un rol que causa un ciclo
|
||||||
|
provider.roles[0x12] = []ActorID{0x10} // Esto crea un ciclo: 0x10 -> 0x11 -> 0x12 -> 0x10
|
||||||
|
err = role.AddRole(0x12)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Se esperaba un error de ciclo al agregar el rol")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 4: Añadir múltiples roles
|
||||||
|
provider.roles[0x12] = []ActorID{} // Eliminar el ciclo previo
|
||||||
|
err = role.AddRole(0x12)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error inesperado al agregar múltiples roles: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDetectRoleCycleInHierarchy(t *testing.T) {
|
||||||
|
// Crear un RoleProvider simulado con algunos roles y subroles
|
||||||
|
provider := &MockRoleProvider{
|
||||||
|
roles: map[ActorID][]ActorID{
|
||||||
|
0x10: {0x11, 0x12}, // El rol 0x10 tiene subroles 0x11 y 0x12
|
||||||
|
0x11: {0x12}, // El rol 0x11 tiene subrol 0x12
|
||||||
|
0x12: {0x10}, // El rol 0x12 tiene subrol 0x10, lo que genera un ciclo
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 1: Detectar ciclo entre roles
|
||||||
|
state := make(map[ActorID]byte)
|
||||||
|
result := detectRoleCycleInHierarchy(0x10, 0x11, provider, &state)
|
||||||
|
if !result.Result() {
|
||||||
|
t.Errorf("Se esperaba detectar un ciclo entre los roles, pero no se detectó")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 2: No hay ciclo, rol 0x11 no tiene subroles
|
||||||
|
provider.roles[0x12] = []ActorID{} // Romper el ciclo eliminando subroles de 0x12
|
||||||
|
state = make(map[ActorID]byte) // Reiniciar el estado
|
||||||
|
result = detectRoleCycleInHierarchy(0x10, 0x11, provider, &state)
|
||||||
|
if result.Result() {
|
||||||
|
t.Errorf("No debería haber un ciclo cuando no hay subroles")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
module tests
|
|
||||||
|
|
||||||
go 1.22
|
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project Active8
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name err_code.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 08/12/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 errs
|
||||||
|
|
||||||
|
|
||||||
|
type (
|
||||||
|
|
||||||
|
ErrCode string
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func (e ErrCode) Error() string {
|
||||||
|
return string(e) }
|
||||||
|
|
||||||
|
func (e ErrCode) Code () string {
|
||||||
|
return string(e) }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
module errs
|
||||||
|
|
||||||
|
go 1.22
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
module core
|
||||||
|
|
||||||
|
go 1.22
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
module rsls
|
||||||
|
|
||||||
|
go 1.22
|
||||||
@ -0,0 +1,85 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project Active8
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name r_bool.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 08/12/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 rsls
|
||||||
|
|
||||||
|
|
||||||
|
const (
|
||||||
|
BoolSuccessTrue BoolSuccess = true
|
||||||
|
BoolSuccessFalse BoolSuccess = false
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type (
|
||||||
|
|
||||||
|
RBool interface {
|
||||||
|
IsOk () bool
|
||||||
|
Result() bool
|
||||||
|
Fail () error
|
||||||
|
}
|
||||||
|
|
||||||
|
BoolSuccess bool
|
||||||
|
|
||||||
|
BoolError struct {
|
||||||
|
error
|
||||||
|
}
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func (s BoolSuccess) IsOk () bool {
|
||||||
|
return true }
|
||||||
|
|
||||||
|
func (s BoolSuccess) Result() bool {
|
||||||
|
return bool(s) }
|
||||||
|
|
||||||
|
func (s BoolSuccess) Fail () error {
|
||||||
|
return nil }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func MakeBoolError (err error) BoolError {
|
||||||
|
return BoolError{ err }
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s BoolError) IsOk () bool {
|
||||||
|
return false }
|
||||||
|
|
||||||
|
func (s BoolError) Result() bool {
|
||||||
|
return false }
|
||||||
|
|
||||||
|
func (s BoolError) Fail () error {
|
||||||
|
return s.error }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,63 @@
|
|||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// 🚀 Project Active8
|
||||||
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||||
|
//
|
||||||
|
// File name rboolBench_test.go
|
||||||
|
// ✍️ Created by DEV
|
||||||
|
// 📅 Modified 08/12/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 rsls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"rsls"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// Método tradicional
|
||||||
|
func traditionalBoolError(input int) (bool, error) {
|
||||||
|
if input > 0 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, errors.New("input must be greater than zero")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Método con RBool
|
||||||
|
func rboolMethod(input int) rsls.RBool {
|
||||||
|
if input > 0 {
|
||||||
|
return rsls.BoolSuccessTrue
|
||||||
|
}
|
||||||
|
return rsls.MakeBoolError(errors.New("input must be greater than zero"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkTraditional(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, _ = traditionalBoolError(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkRBool(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = rboolMethod(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in new issue