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

Strongly typed params - declaration #357

Open
EwenQuim opened this issue Jan 20, 2025 · 5 comments
Open

Strongly typed params - declaration #357

EwenQuim opened this issue Jan 20, 2025 · 5 comments
Assignees
Labels
hard For experienced gophers OD Boost Issues for the OnlyDust event
Milestone

Comments

@EwenQuim
Copy link
Member

Additional context

Linked to #356. It's the "declaration/registration" part of the same issue.

Is your feature request related to a problem? Please describe.

Create a function that registers parameters in the openapi3.Operation and BaseRoute.OpenAPIParams.

Cf spec in #119

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

@EwenQuim EwenQuim added hard For experienced gophers OD Boost Issues for the OnlyDust event labels Jan 20, 2025
@EwenQuim EwenQuim added this to the v0.19 milestone Jan 21, 2025
@TheRanomial
Copy link

Could I take on this issue?I am a web3 developer and also an open source contributor.

@EwenQuim
Copy link
Member Author

OK no problem! Just know that it is a hard issue 😉

@TheRanomial
Copy link

I am currently working on this issue but having a hard time knowing what exactly is the requirement.Can you please elaborate and guide me.

@TheRanomial
Copy link

TheRanomial commented Jan 22, 2025

`
func (c netHttpContextWithParams[T]) Params() (T, error) {
var t T
typeOfT := reflect.TypeOf(t)
valueOfT := reflect.New(typeOfT).Elem()

for i := 0; i < typeOfT.NumField(); i++ {
	field := typeOfT.Field(i)
	fieldValue := valueOfT.Field(i)

	if headerKey, ok := field.Tag.Lookup("header"); ok {
		if headerValue := c.Request().Header.Get(headerKey); headerValue != "" {
			fieldValue.SetString(headerValue)
		}
	}

	if queryKey, ok := field.Tag.Lookup("query"); ok {
		if queryValue := c.Request().URL.Query().Get(queryKey); queryValue != "" {
			if fieldValue.Kind() == reflect.Ptr {
				if fieldValue.Type().Elem().Kind() == reflect.Int {
					intVal, err := strconv.Atoi(queryValue)
					if err != nil {
						return t, err
					}
					fieldValue.Set(reflect.ValueOf(&intVal))
				} else {
					fieldValue.Set(reflect.ValueOf(&queryValue))
				}
			} else {
				fieldValue.SetString(queryValue)
			}
		}
	}

	if cookieKey, ok := field.Tag.Lookup("cookie"); ok {
		if cookie, err := c.Request().Cookie(cookieKey); err == nil {
			fieldValue.SetString(cookie.Value)
		}
	}
}

return valueOfT.Interface().(T), nil

}`

@EwenQuim
Copy link
Member Author

Must look like this but please add it to openapi3.Operation, like in the function OptionParam!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hard For experienced gophers OD Boost Issues for the OnlyDust event
Projects
None yet
Development

No branches or pull requests

2 participants