Skip to content

Commit

Permalink
drafted new generic functions leveraging contextx (#95)
Browse files Browse the repository at this point in the history
* drafted new generic functions leveraging contextx

* task generate output
  • Loading branch information
matoszz authored Dec 9, 2024
1 parent 558d903 commit 53d9f3c
Show file tree
Hide file tree
Showing 25 changed files with 223 additions and 111 deletions.
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

0 comments on commit 53d9f3c

Please sign in to comment.