danielgtaylor / danielgtaylor/huma

Unable to create a wrapper func that also requires the *http.Request object

Open
#811 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
4.4k
Forks
285
Avg merge
40m
Merged PRs (30d)
1

Description

Hi there, I'm trying to create a resuable wrapper function that can do some common logic against the `*http.Request`. Below is my attempt:
```go
// RequestAware will try to capture the http.Request from the huma.Context by implementing the huma.Resolver interface
type RequestAware struct {
*http.Request
}

func (i *RequestAware) Resolve(ctx huma.Context) []error {
i.Request, _ = humago.Unwrap(ctx)
return nil
}

// Here I'm struggling to get the Input value
type Blended[I any] struct {
RequestAware
Input I
}

// The wrapper function
func Wrapper[I, O any](
handler func(context.Context, *I) (*O, error),
) func(context.Context, *Blended[I]) (*O, error) {
return func(ctx context.Context, t *Blended[I]) (*O, error) {
doSomethingWithRequest(t.Request)
return handler(ctx, &t.Input)
}
}
```

In the above example, I'm struggling to get the Input value. The type `[I]` in my first registered handler, is like this:
```go
struct {
Body struct {
V int `json:"v"`
}
}
```

I can however successfully capture the `*http.Request`. Is it because that the `I` is not embedded in the `Blended` struct ? But there's no way to embed a generic type parameter currently AFAIK.
Can someone please help me, guide me out of this storm, please! Thank you beforehand!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.