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

Addition of Locals Function with Go Generics as an Alternative to c.Locals #2813

Merged
merged 7 commits into from
Jan 29, 2024

Conversation

ryanbekhen
Copy link
Member

@ryanbekhen ryanbekhen commented Jan 26, 2024

Introduced a new function, Locals, that utilizes Go's generics to handle and retrieve type-specific local values within a request context. This enhancement provides more accurate data type control within the context. Included are tests for generic and custom struct use-cases to ensure the function performs as expected.

Description

This pull request presents an alternative version of the Locals function using Go's generics. This new implementation of Locals function provides enhanced type safety and allows for more specific data type handling within the request context.

Key features of this implementation:

  • It utilizes the Go's generics feature allowing it to handle varying data types dynamically. It takes a generic parameter [V any].
  • The function tries to convert the input context to a DefaultCtx pointer and will throw an error if this conversion fails.
  • The implementation uses the generic V for defining the type of value being retrieved from the context.
  • It ensures that the returned value matches the type defined by the generic parameter.

This alternative Locals function provides an optional method for type-specific context manipulation. It brings enhanced type safety and data type specificity to the table which results in more resilient code.

Noteworthy: This code requires at least Go 1.18 for its use of generics.

Changes Introduced

List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.

  • Benchmarks: Describe any performance benchmarks and improvements related to the changes.
  • Documentation Update: Detail the updates made to the documentation and links to the changed files.
  • Changelog/What's New: Include a summary of the additions for the upcoming release notes.
  • Migration Guide: If necessary, provide a guide or steps for users to migrate their existing code to accommodate these changes.
  • API Alignment with Express: Explain how the changes align with the Express API.
  • API Longevity: Discuss the steps taken to ensure that the new or updated APIs are consistent and not prone to breaking changes.
  • Examples: Provide examples demonstrating the new features or changes in action.

Type of Change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Enhancement (improvement to existing features and functionality)
  • Documentation update (changes to documentation)
  • Performance improvement (non-breaking change which improves efficiency)
  • Code consistency (non-breaking change which improves code reliability and robustness)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

Commit Formatting

Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md

Introduced a new function, Locals, that utilizes Go's generics to handle and retrieve type-specific local values within a request context. This enhancement provides more accurate data type control within the context. Included are tests for generic and custom struct use-cases to ensure the function performs as expected.
@ryanbekhen ryanbekhen self-assigned this Jan 26, 2024
Added documentation to explain the new version of the Locals method that uses Go's generics feature. This version allows for better control of data types when manipulating and retrieving local values within a request's context. Examples are provided, along with a caution on using correct data types to prevent a runtime panic.
docs/api/ctx.md Outdated Show resolved Hide resolved
@efectn efectn added this to the v3 milestone Jan 26, 2024
ctx.go Outdated Show resolved Hide resolved
ctx.go Outdated Show resolved Hide resolved
ctx.go Outdated Show resolved Hide resolved
docs/api/ctx.md Outdated Show resolved Hide resolved
Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some little adjustements needed

@ryanbekhen
Copy link
Member Author

ryanbekhen commented Jan 29, 2024

@ReneWerner87 I've done the push, pls check.

@ryanbekhen
Copy link
Member Author

@ReneWerner87 I've done the push, pls check again.

@ReneWerner87 ReneWerner87 merged commit 738e062 into gofiber:main Jan 29, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants