ParsePermStorage from string

This commit is contained in:
Melon 2024-02-14 19:55:19 +00:00
parent 2ecf871565
commit 82d4a4a414
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 20 additions and 0 deletions

View File

@ -1,10 +1,12 @@
package claims
import (
"bufio"
"encoding/json"
"github.com/becheran/wildmatch-go"
"gopkg.in/yaml.v3"
"sort"
"strings"
)
type PermStorage struct {
@ -15,6 +17,16 @@ func NewPermStorage() *PermStorage {
return new(PermStorage).setup()
}
func ParsePermStorage(perms string) *PermStorage {
ps := NewPermStorage()
sc := bufio.NewScanner(strings.NewReader(perms))
sc.Split(bufio.ScanWords)
for sc.Scan() {
ps.Set(sc.Text())
}
return ps
}
func (p *PermStorage) setup() *PermStorage {
if p.values == nil {
p.values = make(map[string]struct{})

View File

@ -6,6 +6,14 @@ import (
"testing"
)
func TestParsePermStorage(t *testing.T) {
t.Parallel()
ps := ParsePermStorage("mjwt:test mjwt:test2")
if _, ok := ps.values["mjwt:test"]; !ok {
assert.Fail(t, "perm not set")
}
}
func TestPermStorage_Set(t *testing.T) {
t.Parallel()
ps := NewPermStorage()