Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drafted new generic functions leveraging contextx #95

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions auth/newcontext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package auth

import (
"context"

echo "github.com/theopenlane/echox"
"github.com/theopenlane/utils/contextx"
)

// WithAuthenticatedUser sets the authenticated user in the context
func WithAuthenticatedUser(ctx context.Context, user *AuthenticatedUser) context.Context {
return contextx.With(ctx, user)
}

// AuthenticatedUserFromContext retrieves the authenticated user from the context
func AuthenticatedUserFromContext(ctx context.Context) (*AuthenticatedUser, bool) {
return contextx.From[*AuthenticatedUser](ctx)
}

// MustAuthenticatedUserFromContext retrieves the authenticated user from the context or panics if not found
func MustAuthenticatedUserFromContext(ctx context.Context) *AuthenticatedUser {
return contextx.MustFrom[*AuthenticatedUser](ctx)
}

// AuthenticatedUserFromContextOr retrieves the authenticated user from the context or returns the provided default value if not found
func AuthenticatedUserFromContextOr(ctx context.Context, def *AuthenticatedUser) *AuthenticatedUser {
return contextx.FromOr[*AuthenticatedUser](ctx, def)
}

// AuthenticatedUserFromContextOrFunc retrieves the authenticated user from the context or returns the result of the provided function if not found
func AuthenticatedUserFromContextOrFunc(ctx context.Context, f func() *AuthenticatedUser) *AuthenticatedUser {
return contextx.FromOrFunc[*AuthenticatedUser](ctx, f)
}

// WithAccessToken sets the access token in the context
func WithAccessToken(ctx context.Context, token string) context.Context {
return contextx.With(ctx, token)
}

// AccessTokenFromContext retrieves the access token from the context
func AccessTokenFromContext(ctx context.Context) (string, bool) {
return contextx.From[string](ctx)
}

// MustAccessTokenFromContext retrieves the access token from the context or panics if not found
func MustAccessTokenFromContext(ctx context.Context) string {
return contextx.MustFrom[string](ctx)
}

// AccessTokenFromContextOr retrieves the access token from the context or returns the provided default value if not found
func AccessTokenFromContextOr(ctx context.Context, def string) string {
return contextx.FromOr[string](ctx, def)
}

// AccessTokenFromContextOrFunc retrieves the access token from the context or returns the result of the provided function if not found
func AccessTokenFromContextOrFunc(ctx context.Context, f func() string) string {
return contextx.FromOrFunc[string](ctx, f)
}

// WithRequestID sets the request ID in the context
func WithRequestID(ctx context.Context, requestID string) context.Context {
return contextx.With(ctx, requestID)
}

// RequestIDFromContext retrieves the request ID from the context
func RequestIDFromContext(ctx context.Context) (string, bool) {
return contextx.From[string](ctx)
}

// MustRequestIDFromContext retrieves the request ID from the context or panics if not found
func MustRequestIDFromContext(ctx context.Context) string {
return contextx.MustFrom[string](ctx)
}

// RequestIDFromContextOr retrieves the request ID from the context or returns the provided default value if not found
func RequestIDFromContextOr(ctx context.Context, def string) string {
return contextx.FromOr[string](ctx, def)
}

// RequestIDFromContextOrFunc retrieves the request ID from the context or returns the result of the provided function if not found
func RequestIDFromContextOrFunc(ctx context.Context, f func() string) string {
return contextx.FromOrFunc[string](ctx, f)
}

// NewSetAuthenticatedUserContext sets the authenticated user context in the echo context
func NewSetAuthenticatedUserContext(c echo.Context, user *AuthenticatedUser) {
ctx := c.Request().Context()
ctx = WithAuthenticatedUser(ctx, user)
c.SetRequest(c.Request().WithContext(ctx))
}

// NewGetAuthenticatedUserContext retrieves the authenticated user from the echo context
func NewGetAuthenticatedUserContext(c echo.Context) (*AuthenticatedUser, bool) {
return AuthenticatedUserFromContext(c.Request().Context())
}

// MustGetAuthenticatedUserContext retrieves the authenticated user from the echo context or panics if not found
func MustGetAuthenticatedUserContext(c echo.Context) *AuthenticatedUser {
return MustAuthenticatedUserFromContext(c.Request().Context())
}

// GetAuthenticatedUserContextOr retrieves the authenticated user from the echo context or returns the provided default value if not found
func GetAuthenticatedUserContextOr(c echo.Context, def *AuthenticatedUser) *AuthenticatedUser {
return AuthenticatedUserFromContextOr(c.Request().Context(), def)
}

// GetAuthenticatedUserContextOrFunc retrieves the authenticated user from the echo context or returns the result of the provided function if not found
func GetAuthenticatedUserContextOrFunc(c echo.Context, f func() *AuthenticatedUser) *AuthenticatedUser {
return AuthenticatedUserFromContextOrFunc(c.Request().Context(), f)
}
2 changes: 2 additions & 0 deletions fgax/mockery/.mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ all: true
disable-version-string: true
packages:
github.com/openfga/go-sdk/client:
resolve-type-alias: false
issue-845-fix: true
12 changes: 6 additions & 6 deletions fgax/mockery/mock_SdkClientBatchCheckRequestInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions fgax/mockery/mock_SdkClientCheckRequestInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions fgax/mockery/mock_SdkClientCreateStoreRequestInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions fgax/mockery/mock_SdkClientDeleteStoreRequestInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions fgax/mockery/mock_SdkClientDeleteTuplesRequestInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions fgax/mockery/mock_SdkClientExpandRequestInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading