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 package claims
import ( import (
"bufio"
"encoding/json" "encoding/json"
"github.com/becheran/wildmatch-go" "github.com/becheran/wildmatch-go"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"sort" "sort"
"strings"
) )
type PermStorage struct { type PermStorage struct {
@ -15,6 +17,16 @@ func NewPermStorage() *PermStorage {
return new(PermStorage).setup() 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 { func (p *PermStorage) setup() *PermStorage {
if p.values == nil { if p.values == nil {
p.values = make(map[string]struct{}) p.values = make(map[string]struct{})

View File

@ -6,6 +6,14 @@ import (
"testing" "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) { func TestPermStorage_Set(t *testing.T) {
t.Parallel() t.Parallel()
ps := NewPermStorage() ps := NewPermStorage()