From d85c0475d5d0b0390a618614a334b98627d738fd Mon Sep 17 00:00:00 2001 From: Babatunde Lawrence Afolabi <88771845+Mclawrenceco@users.noreply.github.com> Date: Thu, 20 Jun 2024 08:50:15 +0100 Subject: [PATCH] Update view.go Refactor view helper functions to improve code readability and maintainability - Introduced a generic getContextValue helper function to reduce redundancy in context value retrieval. - Enhanced documentation and comments for better understanding and maintainability. - Simplified error handling with default values in context retrieval. --- view/view.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/view/view.go b/view/view.go index e18faa1..fa202f8 100644 --- a/view/view.go +++ b/view/view.go @@ -9,7 +9,7 @@ import ( "github.com/anthdm/superkit/kit/middleware" ) -// Asset is view helper that returns the full asset path as a +// Asset is a view helper that returns the full asset path as a // string based on the given asset name. // // view.Asset("styles.css") // => /public/assets/styles.css. @@ -17,27 +17,28 @@ func Asset(name string) string { return fmt.Sprintf("/public/assets/%s", name) } +// getContextValue is a helper function to retrieve a value from the context. +// It returns the value if present, otherwise returns the provided default value. +func getContextValue[T any](ctx context.Context, key interface{}, defaultValue T) T { + value, ok := ctx.Value(key).(T) + if !ok { + return defaultValue + } + return value +} + // Auth is a view helper function that returns the current Auth. -// If Auth is not set a default auth will be returned +// If Auth is not set, a default Auth will be returned. // // view.Auth(ctx) func Auth(ctx context.Context) kit.Auth { - value, ok := ctx.Value(kit.AuthKey{}).(kit.Auth) - if !ok { - return kit.DefaultAuth{} - } - return value + return getContextValue(ctx, kit.AuthKey{}, kit.DefaultAuth{}) } // URL is a view helper that returns the current URL. -// The request path can be acccessed with: +// The request path can be accessed with: // // view.URL(ctx).Path // => ex. /login func URL(ctx context.Context) *url.URL { - value, ok := ctx.Value(middleware.RequestURLKey{}).(*url.URL) - if !ok { - return &url.URL{} - } - return value - + return getContextValue(ctx, middleware.RequestURLKey{}, &url.URL{}) }