Skip to content

Commit

Permalink
Configurable query parameters parser (grpc-ecosystem#1138)
Browse files Browse the repository at this point in the history
* grpc-ecosystem#1128 make it possible to configure query param parser implementation

* Apply suggestions from code review

Makes names more consistent and improves docs

Co-Authored-By: Johan Brandhorst <johan.brandhorst@gmail.com>

* grpc-ecosystem#1128 make configured parser a global parser

* update docs for configuring query parser

Co-Authored-By: Johan Brandhorst <johan.brandhorst@gmail.com>

* grpc-ecosystem#1128 unexport default query parameter parser and rename mux option

Co-authored-by: Johan Brandhorst <johan.brandhorst@gmail.com>

Fixes grpc-ecosystem#1128
  • Loading branch information
wp0pw authored Feb 25, 2020
1 parent 61d4c1d commit 000dde5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 9 additions & 0 deletions runtime/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func WithForwardResponseOption(forwardResponseOption func(context.Context, http.
}
}

// SetQueryParameterParser sets the query parameter parser, used to populate message from query parameters.
// Configuring this will mean the generated swagger output is no longer correct, and it should be
// done with careful consideration.
func SetQueryParameterParser(queryParameterParser QueryParameterParser) ServeMuxOption {
return func(serveMux *ServeMux) {
currentQueryParser = queryParameterParser
}
}

// HeaderMatcherFunc checks whether a header key should be forwarded to/from gRPC context.
type HeaderMatcherFunc func(string) (string, bool)

Expand Down
19 changes: 17 additions & 2 deletions runtime/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,24 @@ import (

var valuesKeyRegexp = regexp.MustCompile("^(.*)\\[(.*)\\]$")

// PopulateQueryParameters populates "values" into "msg".
// A value is ignored if its key starts with one of the elements in "filter".
var currentQueryParser QueryParameterParser = &defaultQueryParser{}

// QueryParameterParser defines interface for all query parameter parsers
type QueryParameterParser interface {
Parse(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error
}

// PopulateQueryParameters parses query parameters
// into "msg" using current query parser
func PopulateQueryParameters(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error {
return currentQueryParser.Parse(msg, values, filter)
}

type defaultQueryParser struct{}

// Parse populates "values" into "msg".
// A value is ignored if its key starts with one of the elements in "filter".
func (*defaultQueryParser) Parse(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error {
for key, values := range values {
match := valuesKeyRegexp.FindStringSubmatch(key)
if len(match) == 3 {
Expand Down

0 comments on commit 000dde5

Please sign in to comment.