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

feat: gin.Context GetXxx added support for more go native types #3633

Merged
merged 1 commit into from
Sep 15, 2024

Conversation

CC11001100
Copy link
Contributor

@CC11001100 CC11001100 commented Jun 5, 2023

Thanks for such a nice open-source web framework, when I was using gin middleware today I found that the support for go native types when getting the data from gin. I put in a uint8 type, for example I put in a uint8 type, which when I get it doesn't provide a quick method but requires me to type it myself. I added some get methods of go native type, please review and merge to help more people, thank you very much

gin.Context new add methods:

  • GetInt8
  • GetInt16
  • GetInt32
  • GetUint8
  • GetUint16
  • GetUint32
  • GetFloat32
  • GetIntSlice
  • GetInt8Slice
  • GetInt16Slice
  • GetInt32Slice
  • GetInt64Slice
  • GetUintSlice
  • GetUint8Slice
  • GetUint16Slice
  • GetUint32Slice
  • GetUint64Slice
  • GetFloat32Slice
  • GetFloat64Slice

@@ -297,7 +297,31 @@ func (c *Context) GetInt(key string) (i int) {
return
}

// GetInt64 returns the value associated with the key as an integer.
// GetInt8 returns the value associated with the key as an integer 8.
func (c *Context) GetInt8(key string) (i8 int8) {

Choose a reason for hiding this comment

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

Maybe we should keep the same logic as the original .Get and returns a (int8, bool) to avoid panicking in case the value if not of the asked type. This comment applies to other methods as well. What do you think?

Copy link
Contributor Author

@CC11001100 CC11001100 Jun 22, 2023

Choose a reason for hiding this comment

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

Yes, you are right, I just noticed this problem too, but there is a small compatibility problem that seems strange, the methods I added this time are styled by the functions that already existed before:

func (c *Context) GetInt(key string) (i int)

and

func (c *Context) GetInt64(key string) (i64 int64)

The style is kept uniform, and these two functions seem to start from this PR:

https://github.com/gin-gonic/gin/pull/2425

Then other GetXxx functions such as:

func (c *Context) GetString(key string) (s string)
func (c *Context) GetBool(key string) (b bool)

It seems that they all follow the rule of only one return value.

All in all, there seem to be two styles for the Get method on the Context. The first one is:

func (c *Context) Get(key string) (value any, exists bool)

Represented by two return values, the other is based on

func (c *Context) GetInt(key string) (i int)

represented by one return value

image

Choose a reason for hiding this comment

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

You're right, I didn't see that, so what you've done makes sense.

But for the record, I guess we could leverage Go generic with something like this:

func Get[T any](c *gin.Context, key string) (r T) {
	if val, ok := c.Get(key); ok && val != nil {
		r, _ = val.(T)
	}
	return
}

Sure we can't add this on the Context type because Go doesn't currently support method level generic types yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you have a point, the best way is to add a generic method to Context, although I don't think Golang will support this feature any time soon, but I believe it will be supported in the future, it is a mature language should have a feature. some proposal about it:
golang/go#49085

However, in the absence of generics, enumeration method can only be used. Although there is a lot of repetitive work, using a separate method can solve this problem, but it will be a problem to promote the use of this function. Most people use the method in Context and then view it with dot. So I think it is necessary to add these methods to Context for now, as a transition that will remove in the future when Golang supports Struct generic methods.

@CC11001100 CC11001100 requested a review from YuukanOO August 13, 2024 13:05
@CC11001100
Copy link
Contributor Author

Hello, is there anyone with merge permissions to merge this PR?

@appleboy appleboy added this to the v1.11 milestone Sep 15, 2024
@appleboy appleboy merged commit 9d7c0e9 into gin-gonic:master Sep 15, 2024
@appleboy
Copy link
Member

@CC11001100 see the PR #3329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants