master
DEV 2 years ago
parent 265e455452
commit 80fc2ece02

@ -24,11 +24,94 @@
package brbac package brbac
import "errs"
var ( var (
// G Public vars
G = struct {
ErrInvalidActor errs.ErrCode
ErrInvalidIDRole errs.ErrCode
ErrRoleCycle errs.ErrCode
RoleProvider RoleProvider
} {
ErrInvalidActor : errs.ErrCode("invalid-actor-type"),
ErrInvalidIDRole: errs.ErrCode("invalid-id-role"),
ErrRoleCycle : errs.ErrCode("role-cycle"),
}
// g Private vars
g = struct { g = struct {
agentAllFlags []AgentStateFlags actorFlags []ActorFlags
actorNames []string
}{ }{
agentAllFlags: []AgentStateFlags{AgentStateFlagVerified, AgentStateFlagRequired, AgentStateFlagRestricted, AgentStateFlagLimited}, actorFlags: []ActorFlags{
ActorFlagVerified,
ActorFlagRequired,
ActorFlagRestricted,
ActorFlagLimited},
actorNames: []string{
"User", // 0x00
"Role", // 0x01
"Service", // 0x02
"Device", // 0x03
"Group", // 0x04
"Organization", // 0x05
"APIKey", // 0x06
"System", // 0x07
"Admin", // 0x08
"Visitor", // 0x09
"Auditor", // 0x0A
"ExternalClient", // 0x0B
"Provider", // 0x0C
"TemporaryToken", // 0x0D
"Bot", // 0x0E
"Reserved", // 0x0F
"BlockchainNode", // 0x10
"BlockchainUser", // 0x11
"SmartContract", // 0x12
"BlockchainAPI", // 0x13
"Wallet", // 0x14
"Miner", // 0x15
"Validator", // 0x16
"DApp", // 0x17
"Token", // 0x18
"BlockchainOracle", // 0x19
"BlockchainExplorer", // 0x1A
"DAO", // 0x1B
"NFT", // 0x1C
"BlockchainBridge", // 0x1D
"Staking", // 0x1E
"DefiPlatform", // 0x1F
"BlockchainWallet", // 0x20
"BlockchainExplorerAPI", // 0x21
"BlockchainTransaction", // 0x22
"BlockchainBackup", // 0x23
"SecurityModule", // 0x24
"QuantumComputer", // 0x25
"QuantumWallet", // 0x26
"AI", // 0x30
"CloudService", // 0x31
"Microservice", // 0x32
"ThirdParty", // 0x33
"ExternalSystem", // 0x34
"MobileDevice", // 0x35
"ExternalAPI", // 0x36
"Server", // 0x37
"UserAgent", // 0x38
"Container", // 0x39
"Network", // 0x3A
"EdgeDevice", // 0x3B
"VirtualMachine", // 0x3C
"Hypervisor", // 0x3D
},
} }
) )

@ -2,7 +2,7 @@
// 🚀 Project Active8 // 🚀 Project Active8
// 🔗 Active Thing (activething.com) git.activething.com/go // 🔗 Active Thing (activething.com) git.activething.com/go
// //
// File name agent_state_flags.go // File name actor_flags.go
// ✍️ Created by DEV // ✍️ Created by DEV
// 📅 Modified 07/12/2024 // 📅 Modified 07/12/2024
// //
@ -27,47 +27,51 @@ package brbac
import "math/bits" import "math/bits"
const ( const (
AgentStateFlagVerified AgentStateFlags = 0b00000001 ActorFlagVerified ActorFlags = 0b00000001
AgentStateFlagRequired AgentStateFlags = 0b00000010 ActorFlagRequired ActorFlags = 0b00000010
AgentStateFlagRestricted AgentStateFlags = 0b00000011 ActorFlagRestricted ActorFlags = 0b00000011
AgentStateFlagLimited AgentStateFlags = 0b00000100 ActorFlagLimited ActorFlags = 0b00000100
) )
type ( type (
AgentStateFlags uint8
ActorFlags uint8
) )
func (a AgentStateFlags) Has(flag AgentStateFlags) bool {
func (a ActorFlags) Has(flag ActorFlags) bool {
return a&flag != 0 return a&flag != 0
} }
func (a AgentStateFlags) HasVerified() bool { func (a ActorFlags) HasVerified() bool {
return a&AgentStateFlagVerified != 0 return a&ActorFlagVerified != 0
} }
func (a AgentStateFlags) HasRequired() bool { func (a ActorFlags) HasRequired() bool {
return a&AgentStateFlagRequired != 0 return a&ActorFlagRequired != 0
} }
func (a AgentStateFlags) HasRestricted() bool { func (a ActorFlags) HasRestricted() bool {
return a&AgentStateFlagRestricted != 0 return a&ActorFlagRestricted != 0
} }
func (a AgentStateFlags) HasLimited() bool { func (a ActorFlags) HasLimited() bool {
return a&AgentStateFlagLimited != 0 return a&ActorFlagLimited != 0
} }
func (a AgentStateFlags) Count() int { func (a ActorFlags) Count() int {
if a == 0 { if a == 0 {
return 0 return 0
} }
return bits.OnesCount8(uint8(a)) return bits.OnesCount8(uint8(a))
} }
func (a AgentStateFlags) Actives() []AgentStateFlags { func (a ActorFlags) Actives() []ActorFlags {
var l []AgentStateFlags var l []ActorFlags
if a > 0 { if a > 0 {
for _, f := range g.agentAllFlags { for _, f := range g.actorFlags {
if a&f != 0 { if a&f != 0 {
l = append(l, f) l = append(l, f)
} }
@ -76,14 +80,14 @@ func (a AgentStateFlags) Actives() []AgentStateFlags {
return l return l
} }
func (a AgentStateFlags) Set(flag AgentStateFlags) AgentStateFlags { func (a ActorFlags) Set(flag ActorFlags) ActorFlags {
return a | flag return a | flag
} }
func (a AgentStateFlags) Del(flag AgentStateFlags) AgentStateFlags { func (a ActorFlags) Del(flag ActorFlags) ActorFlags {
return a &^ flag return a &^ flag
} }
func (a AgentStateFlags) Clear() AgentStateFlags { func (a ActorFlags) Clear() ActorFlags {
return 0 return 0
} }

@ -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
}

@ -2,7 +2,7 @@
// 🚀 Project Active8 // 🚀 Project Active8
// 🔗 Active Thing (activething.com) git.activething.com/go // 🔗 Active Thing (activething.com) git.activething.com/go
// //
// File name agent_state.go // File name actor_state.go
// ✍️ Created by DEV // ✍️ Created by DEV
// 📅 Modified 07/12/2024 // 📅 Modified 07/12/2024
// //
@ -25,17 +25,17 @@
package brbac package brbac
const ( const (
AgentStateActive AgentState = 0b000 // El usuario está activo y no tiene restricciones ActorStateActive ActorState = 0b000 // El usuario está activo y no tiene restricciones
AgentStateInactive AgentState = 0b001 // El usuario está inactivo y temporalmente deshabilitado ActorStateInactive ActorState = 0b001 // El usuario está inactivo y temporalmente deshabilitado
AgentStatePending AgentState = 0b010 // El usuario está pendiente de activación (por ejemplo, verificación de correo electrónico) ActorStatePending ActorState = 0b010 // El usuario está pendiente de activación (por ejemplo, verificación de correo electrónico)
AgentStateSuspended AgentState = 0b011 // El usuario está suspendido debido a problemas de seguridad o políticas ActorStateSuspended ActorState = 0b011 // El usuario está suspendido debido a problemas de seguridad o políticas
AgentStateRevoked AgentState = 0b100 // El acceso del usuario ha sido revocado permanentemente ActorStateRevoked ActorState = 0b100 // El acceso del usuario ha sido revocado permanentemente
AgentStatePaused AgentState = 0b101 // El usuario ha pausado su cuenta (puede reactivarse) ActorStatePaused ActorState = 0b101 // El usuario ha pausado su cuenta (puede reactivarse)
AgentStateExpired AgentState = 0b110 // La cuenta del usuario ha expirado (por ejemplo, acceso temporal) ActorStateExpired ActorState = 0b110 // La cuenta del usuario ha expirado (por ejemplo, acceso temporal)
AgentStateDeleted AgentState = 0b111 // La cuenta del usuario está eliminada y ya no existe ActorStateDeleted ActorState = 0b111 // La cuenta del usuario está eliminada y ya no existe
) )
type ( type (
AgentState uint8 ActorState uint8
) )

@ -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
}

@ -1,10 +1,10 @@
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// 🚀 Project Active8 // 🚀 Project Active8
// 🔗 UserStateActive Thing (activething.com) git.activething.com/go // 🔗 Active Thing (activething.com) git.activething.com/go
// //
// File name user_test.go // File name role_provider.go
// ✍️ Created by DEV // ✍️ Created by DEV
// 📅 Modified 04/12/2024 // 📅 Modified 08/12/2024
// //
// 🔒 Copyright 2024 activething.com // 🔒 Copyright 2024 activething.com
// //
@ -22,4 +22,16 @@
// limitations under the License. // limitations under the License.
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
package tests package brbac
type (
RoleProvider interface {
//GetRole (roleID ActorID) (*Role,error)
GetSubRoles(roleID ActorID) ([]ActorID,error)
}
)

@ -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)
}
}

@ -2,6 +2,9 @@ go 1.22
use ( use (
. .
core
core/errs
core/rsls
brbac brbac
brbac/tests
) )
Loading…
Cancel
Save

Powered by TurnKey Linux.