Skip to content

Commit

Permalink
Make Go source representation of DoubleArray stable
Browse files Browse the repository at this point in the history
  • Loading branch information
yugui committed Jul 17, 2015
1 parent 7105678 commit edd2e79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/a_bit_of_everything.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gengateway

import (
"bytes"
"fmt"
"strings"
"text/template"

Expand Down Expand Up @@ -40,15 +41,29 @@ func (b binding) HasQueryParam() bool {
return len(fields) > 0
}

func (b binding) QueryParamFilter() *internal.DoubleArray {
func (b binding) QueryParamFilter() queryParamFilter {
var seqs [][]string
if b.Body != nil {
seqs = append(seqs, strings.Split(b.Body.FieldPath.String(), "."))
}
for _, p := range b.PathParams {
seqs = append(seqs, strings.Split(p.FieldPath.String(), "."))
}
return internal.NewDoubleArray(seqs)
return queryParamFilter{internal.NewDoubleArray(seqs)}
}

// queryParamFilter is a wrapper of internal.DoubleArray which provides String() to output DoubleArray.Encoding in a stable and predictable format.
type queryParamFilter struct {
*internal.DoubleArray
}

func (f queryParamFilter) String() string {
encodings := make([]string, len(f.Encoding))
for str, enc := range f.Encoding {
encodings[enc] = fmt.Sprintf("%q: %d", str, enc)

This comment has been minimized.

Copy link
@dmitshur

dmitshur Jul 17, 2015

Contributor

Are the values of internal.DoubleArray.Encoding map, which are of type int, guaranteed to be unique and in the [0, len(Encoding) - 1] range?

This comment has been minimized.

Copy link
@yugui

yugui via email Jul 18, 2015

Author Member
}
e := strings.Join(encodings, ", ")
return fmt.Sprintf("&internal.DoubleArray{Encoding: map[string]int{%s}, Base: %#v, Check: %#v}", e, f.Base, f.Check)
}

func applyTemplate(p param) (string, error) {
Expand Down Expand Up @@ -155,7 +170,7 @@ func request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx cont
_ = template.Must(handlerTemplate.New("client-rpc-request-func").Parse(`
{{if .HasQueryParam}}
var (
filter_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}} = {{.QueryParamFilter | printf "%#v"}}
filter_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}} = {{.QueryParamFilter}}
)
{{end}}
{{template "request-func-signature" .}} {
Expand Down

0 comments on commit edd2e79

Please sign in to comment.