这是indexloc提供的服务,不要输入任何密码
Skip to content

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

@sinloss

Description

@sinloss

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:

// 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:

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions