generated from ZEISS/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
313a7c5
commit e4d1c13
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package authz | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/openfga/go-sdk/client" | ||
) | ||
|
||
// Store is an interface that provides methods for transactional operations on the authz database. | ||
type Store[Tx any] interface { | ||
// WriteTx starts a read write transaction. | ||
WriteTx(context.Context, func(context.Context, Tx) error) error | ||
} | ||
|
||
// AuthzError is an error that occurred while executing a query. | ||
type AuthzError struct { | ||
// Op is the operation that caused the error. | ||
Op string | ||
// Err is the error that occurred. | ||
Err error | ||
} | ||
|
||
// Error implements the error interface. | ||
func (e *AuthzError) Error() string { return e.Op + ": " + e.Err.Error() } | ||
|
||
// Unwrap implements the errors.Wrapper interface. | ||
func (e *AuthzError) Unwrap() error { return e.Err } | ||
|
||
// NewQueryError returns a new QueryError. | ||
func NewQueryError(op string, err error) *AuthzError { | ||
return &AuthzError{ | ||
Op: op, | ||
Err: err, | ||
} | ||
} | ||
|
||
type storeImpl[W any] struct { | ||
tx StoreTxFactory[W] | ||
client *client.OpenFgaClient | ||
} | ||
|
||
// StoreTxFactory is a function that creates a new instance of authz store. | ||
type StoreTxFactory[Tx any] func(*client.OpenFgaClient) (Tx, error) | ||
|
||
// NewStore returns a new instance of authz store. | ||
func NewStore[Tx any](client *client.OpenFgaClient, tx StoreTxFactory[Tx]) (Store[Tx], error) { | ||
return &storeImpl[Tx]{tx, client}, nil | ||
} | ||
|
||
// ReadWriteTx starts a read only transaction. | ||
func (s *storeImpl[Tx]) WriteTx(ctx context.Context, fn func(context.Context, Tx) error) error { | ||
t, err := s.tx(s.client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := fn(ctx, t); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type defaultStoreTxImpl struct { | ||
client *client.OpenFgaClient | ||
} | ||
|
||
// DefaultStoreTx is a default authz store transaction. | ||
type DefaultStoreTx interface{} | ||
|
||
// NewDefaultStoreTx returns a new instance of default authz store transaction. | ||
func NewDefaultStoreTx() StoreTxFactory[DefaultStoreTx] { | ||
return func(fga *client.OpenFgaClient) (DefaultStoreTx, error) { | ||
return &defaultStoreTxImpl{client: fga}, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters