Skip to content

Commit

Permalink
feat: simple builder
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Aug 1, 2024
1 parent efe2edf commit cd94e62
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions authz/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package authz

import (
"context"
"strings"

"github.com/openfga/go-sdk/client"
"github.com/zeiss/pkg/cast"
Expand All @@ -17,6 +18,18 @@ type Object string
// Relation is the relation between the user and the object.
type Relation string

// NoopUser is a user that represents no user.
const NoopUser User = ""

// NoopRelation is a relation that represents no relation.
const NoopRelation Relation = ""

// NoopObject is an object that represents no object.
const NoopObject Object = ""

// Stringer create a string an adds it to the representation.
type Stringer func() string

// Store is an interface that provides methods for transactional operations on the authz database.
type Store[Tx any] interface {
// Allowed checks if the user is allowed to perform the operation on the object.
Expand Down Expand Up @@ -144,3 +157,69 @@ func (s *storeImpl[Tx]) DeleteTuple(ctx context.Context, user User, object Objec

return nil
}

// DefaultSeparator is the default separator for entities.
const DefaultSeparator = "/"

// DefaultNamespaceSeparator is the default separator for namespaces.
const DefaultNamespaceSeparator = ":"

// EntitiesString is a type that represents a list of entities.
func EntityString[E Entities](e E) string {
return conv.String(e)
}

// Entities is a type that represents a list of entities.
type Entities interface {
User | Relation | Object
}

// NewEntity returns a new User.
func NewEntity[E Entities](s ...Stringer) E {
u := ""

for _, v := range s {
u += v()
}

return E(u)
}

// NewUser returns a new User.
func NewUser(s ...Stringer) User {
return NewEntity[User](s...)
}

// NewRelation returns a new Relation.
func NewRelation(s ...Stringer) Relation {
return NewEntity[Relation](s...)
}

// NewObject returns a new Object.
func NewObject(s ...Stringer) Object {
return NewEntity[Object](s...)
}

// Namespace adds a namespace to the entity.
func Namespace(namespace string, sep ...string) Stringer {
return func() string {
s := DefaultNamespaceSeparator

if len(sep) > 0 {
s = sep[0]
}

if namespace == "" {
return ""
}

return namespace + s
}
}

// Join joins the entities with the separator.
func Join(sep string, entities ...string) Stringer {
return func() string {
return strings.Join(entities, sep)
}
}

0 comments on commit cd94e62

Please sign in to comment.