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

helpers: retrieve fields that are used as path variables #171

Open
mgenov opened this issue Jan 4, 2021 · 1 comment
Open

helpers: retrieve fields that are used as path variables #171

mgenov opened this issue Jan 4, 2021 · 1 comment

Comments

@mgenov
Copy link
Contributor

mgenov commented Jan 4, 2021

Is your feature request related to a problem? Please describe.
It would be really use-full if templates are able to iterate over fields used as path variables.

Describe the solution you'd like
Here is a sample snippet that better describes the intention. By adding this function as helper the template
will be able to list only fields used as variables and may reduce the generated code.

func urlVariableFields(path string, d *ggdescriptor.Message) []*descriptor.FieldDescriptorProto {
	vars := []*descriptor.FieldDescriptorProto{}
	for _, field := range d.Field {
		if !isFieldMessage(field) {
			if strings.Contains(path, fmt.Sprintf("{%s}", *field.Name)) {
				vars = append(vars, field)
				continue
			}
			// JSON name field is checked as fallback. The value set by the protocol compiler.
			// If the user has set a "json_name" option on a field, that option's value
			// will be used in this check. By default value in this property will be field name in
			// camelCase format.
			if strings.Contains(path, fmt.Sprintf("{%s}", *field.JsonName)) {
				vars = append(vars, field)
			}
		}
	}
	return vars
}
@vtolstov
Copy link

vtolstov commented Jan 4, 2021

very needed feature

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

No branches or pull requests

2 participants