-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Go source representation of DoubleArray stable
- Loading branch information
Showing
2 changed files
with
19 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package gengateway | |
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"text/template" | ||
|
||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
yugui
via email
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) { | ||
|
@@ -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" .}} { | ||
|
Are the values of
internal.DoubleArray.Encoding
map, which are of typeint
, guaranteed to be unique and in the [0, len(Encoding) - 1] range?