Skip to content

Commit

Permalink
🎉 Create Go port
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarbar338 committed Jun 23, 2022
0 parents commit 20905ee
Show file tree
Hide file tree
Showing 7 changed files with 941 additions and 0 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[![stars](https://img.shields.io/github/stars/barbarbar338/perman?color=yellow&logo=github&style=for-the-badge)](https://github.com/barbarbar338/perman)
[![license](https://img.shields.io/github/license/barbarbar338/perman?logo=github&style=for-the-badge)](https://github.com/barbarbar338/perman)
[![supportServer](https://img.shields.io/discord/711995199945179187?color=7289DA&label=Support&logo=discord&style=for-the-badge)](https://discord.gg/BjEJFwh)
[![forks](https://img.shields.io/github/forks/barbarbar338/perman?color=green&logo=github&style=for-the-badge)](https://github.com/barbarbar338/perman)
[![issues](https://img.shields.io/github/issues/barbarbar338/perman?color=red&logo=github&style=for-the-badge)](https://github.com/barbarbar338/perman)

<p align="center">
<img src="https://raw.githubusercontent.com/barbarbar338/readme-template/main/icon.png" alt="Logo" width="160" height="160" />
<h3 align="center">Perman.GO</h3>

<p align="center">
🔑 Permission management made easy
<br />
<a href="https://discord.gg/BjEJFwh"><strong>Get support »</strong></a>
<br />
<br />
<a href="https://github.com/barbarbar338/perman/issues">Report Bug</a>
·
<a href="https://github.com/barbarbar338/perman/issues">Request Feature</a>
·
<a href="https://338.rocks">Webpage</a>
</p>
</p>

# 🔑 Perman

Permission management made easy

# 📦 Installation

- Run `go get github.com/barbarbar338/perman`

# 🤓 Usage

```go
package main

import (
"github.com/barbarbar338/perman"
)

func main() {

}
```

# 📄 License

Copyright © 2022 [Barış DEMİRCİ](https://github.com/barbarbar338).

Distributed under the [GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html) License. See `LICENSE` for more information.

# 🧦 Contributing

Fell free to use GitHub's features.

1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/my-feature`)
3. Commit your Changes (`git commit -m 'my awesome feature my-feature'`)
4. Push to the Branch (`git push origin feature/my-feature`)
5. Open a Pull Request

# 🔥 Show your support

Give a ⭐️ if this project helped you!

# 📞 Contact

- Mail: [email protected]
- Discord: https://discord.gg/BjEJFwh
- Instagram: https://www.instagram.com/ben_baris.d/
- Webpage: https://338.rocks
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/barbarbar338/perman

go 1.18
28 changes: 28 additions & 0 deletions management.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package perman

func (p Perman) Serialize(flags []string) int64 {
if len(flags) < 1 {
return 0
}

res := int64(0)
for _, flag := range flags {
res |= p.Get(flag)
}

return res
}

func (p Perman) Deserialize(permission int64) []string {
if permission == 0 {
return []string{}
}

res := make([]string, 0)
for key, value := range p.flags {
if permission&value != 0 {
res = append(res, key)
}
}
return res
}
17 changes: 17 additions & 0 deletions struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package perman

import "math"

type Perman struct {
flags map[string]int64
}

func Factory(flags []string) Perman {
p := Perman{
flags: make(map[string]int64),
}
for i, flag := range flags {
p.flags[flag] = int64(math.Pow(2, float64(i)))
}
return p
}
100 changes: 100 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package perman

func (p Perman) Match(permission int64, flags []string) bool {
if len(flags) < 1 {
return true
}

for _, flag := range flags {
if permission&p.Get(flag) != p.Get(flag) {
return false
}
}
return true
}

func (p Perman) MatchAll(permission int64, flags []string) bool {
if len(flags) < 1 {
return true
}

for _, flag := range flags {
if permission&p.Get(flag) != p.Get(flag) {
return false
}
}
return true
}

func (p Perman) HasAll(permission int64, flags []string) bool {
if len(flags) < 1 {
return true
}

for _, flag := range flags {
if permission&p.Get(flag) != p.Get(flag) {
return false
}
}
return true
}

func (p Perman) Some(permission int64, flags []string) bool {
if len(flags) < 1 {
return true
}

for _, flag := range flags {
if permission&p.Get(flag) == p.Get(flag) {
return true
}
}
return false
}

func (p Perman) HasSome(permission int64, flags []string) bool {
if len(flags) < 1 {
return true
}

for _, flag := range flags {
if permission&p.Get(flag) == p.Get(flag) {
return true
}
}
return false
}

func (p Perman) HasNone(permission int64, flags []string) bool {
if len(flags) < 1 {
return true
}

for _, flag := range flags {
if permission&p.Get(flag) == p.Get(flag) {
return false
}
}
return true
}

func (p Perman) None(permission int64, flags []string) bool {
if len(flags) < 1 {
return true
}

for _, flag := range flags {
if permission&p.Get(flag) == p.Get(flag) {
return false
}
}
return true
}

func (p Perman) Has(permission int64, flag string) bool {
return permission&p.Get(flag) == p.Get(flag)
}

func (p Perman) Test(permission int64, flag string) bool {
return permission&p.Get(flag) == p.Get(flag)
}
47 changes: 47 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package perman

func (p Perman) Keys() []string {
keys := make([]string, 0)
for key := range p.flags {
keys = append(keys, key)
}
return keys
}

func (p Perman) Values() []int64 {
values := make([]int64, 0)
for flag := range p.flags {
values = append(values, p.flags[flag])
}
return values
}

func (p Perman) Get(flag string) int64 {
value, ok := p.flags[flag]
if ok {
return value
} else {
return int64(0)
}
}

func (p Perman) Add(permission int64, key string) int64 {
oldValues := p.Deserialize(permission)
newValues := append(oldValues, key)
return p.Serialize(newValues)
}

func (p Perman) Remove(permission int64, key string) int64 {
oldValues := p.Deserialize(permission)
newValues := make([]string, 0)
for _, value := range oldValues {
if value != key {
newValues = append(newValues, value)
}
}
return p.Serialize(newValues)
}

func (p Perman) Full() int64 {
return p.Serialize(p.Keys())
}

0 comments on commit 20905ee

Please sign in to comment.