policy/acl/aclbit.go

131 lines
1.9 KiB
Go
Raw Normal View History

package acl
import (
"fmt"
"strconv"
)
type NumBit int64
type AclBit int64
2023-03-11 08:27:53 +02:00
const (
AllBit AclBit = 9223372036854775807
)
// New AclBit
func New(n ...NumBit) AclBit {
var i64 AclBit
2023-03-05 10:40:21 +02:00
i64.SetTrue(n...)
return i64
}
// unite slice int to int64
2022-08-13 09:40:13 +03:00
func Unite(a ...AclBit) AclBit {
var endacl AclBit
for _, a0 := range a {
endacl = endacl | a0
}
return endacl
}
// set acl bit in true
2023-03-05 10:40:21 +02:00
func (a *AclBit) SetTrue(n ...NumBit) {
for _, n0 := range n {
if veryNumBit(n0) {
*a = *a | (1 << n0)
}
}
}
// set acl bit in false
2023-03-05 10:40:21 +02:00
func (a *AclBit) SetFalse(n ...NumBit) {
for _, n0 := range n {
if veryNumBit(n0) {
*a = *a &^ (1 << n0)
}
}
}
// verify bit return true or false
2023-03-06 13:38:53 +02:00
func (a AclBit) Verify(n NumBit) bool {
if veryNumBit(n) {
var msk AclBit = 1 << n
if (a & msk) == msk {
return true
}
}
return false
}
// verify or bit return true or false
func (a AclBit) VerifyOr(n ...NumBit) bool {
2023-03-05 10:40:21 +02:00
for _, n0 := range n {
2023-03-06 13:38:53 +02:00
if a.Verify(n0) {
return true
}
}
2023-03-06 13:38:53 +02:00
return false
}
2023-03-06 13:38:53 +02:00
// verify and bit return true or false
func (a AclBit) VerifyAnd(n ...NumBit) bool {
for _, n0 := range n {
if !a.Verify(n0) {
return false
}
}
return true
}
// converting acl bits to string
2023-03-04 08:34:57 +02:00
func (a AclBit) String() string {
return strconv.FormatInt(int64(a), 2)
}
2023-03-04 08:39:32 +02:00
// converting string bits to int64
func (a AclBit) Int64() int64 {
2023-03-04 08:58:02 +02:00
return int64(a)
2023-03-04 08:39:32 +02:00
}
2022-08-04 11:49:45 +03:00
// converting string bits to int64
func Int64(i any) int64 {
var i64 int64
var err error
switch i.(type) {
case string:
str := fmt.Sprintf("%s", i)
i64, err = strconv.ParseInt(str, 2, 64)
if err != nil {
return -1
}
return i64
case AclBit:
i64 = int64(i.(AclBit))
return i64
case int64:
i64 = i.(int64)
return i64
case uint64:
i64 = int64(i.(uint64))
return i64
default:
i64 = -1
return i64
}
}
2023-03-04 09:30:21 +02:00
// verytify Num bit (max range 0 - 62 , summary 63 bit)
func veryNumBit(n NumBit) bool {
if n >= 0 && n < 63 {
return true
}
return false
}