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

fix(gengapic): separate repeated prim qp values #1161

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ func (g *generator) generateQueryString(m *descriptor.MethodDescriptorProto) {
singularPrimitive := field.GetType() != fieldTypeMessage &&
field.GetType() != fieldTypeBytes &&
field.GetLabel() != fieldLabelRepeated
key := lowerFirst(snakeToCamel(path))

var paramAdd string
// Handle well known protobuf types with special JSON encodings.
Expand All @@ -435,10 +436,10 @@ func (g *generator) generateQueryString(m *descriptor.MethodDescriptorProto) {
b.WriteString(" return nil, err\n")
}
b.WriteString("}\n")
b.WriteString(fmt.Sprintf("params.Add(%q, string(%s))", lowerFirst(snakeToCamel(path)), field.GetJsonName()))
b.WriteString(fmt.Sprintf("params.Add(%q, string(%s))", key, field.GetJsonName()))
paramAdd = b.String()
} else {
paramAdd = fmt.Sprintf("params.Add(%q, fmt.Sprintf(%q, req%s))", lowerFirst(snakeToCamel(path)), "%v", accessor)
paramAdd = fmt.Sprintf("params.Add(%q, fmt.Sprintf(%q, req%s))", key, "%v", accessor)
g.imports[pbinfo.ImportSpec{Path: "fmt"}] = true
}

Expand All @@ -450,8 +451,14 @@ func (g *generator) generateQueryString(m *descriptor.MethodDescriptorProto) {
}

if field.GetLabel() == fieldLabelRepeated {
// It's a slice, so check for nil
p("if req%s != nil {", accessor)
// It's a slice, so check for len > 0, nil slice returns 0.
p("if items := req%s; len(items) > 0 {", accessor)
b := strings.Builder{}
b.WriteString("for _, item := range items {\n")
b.WriteString(fmt.Sprintf(" params.Add(%q, fmt.Sprintf(%q, item))\n", key, "%v"))
b.WriteString("}")
paramAdd = b.String()

} else if field.GetProto3Optional() {
// Split right before the raw access
toks := strings.Split(path, ".")
Expand Down
9 changes: 8 additions & 1 deletion internal/gengapic/genrest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,16 @@ func TestGenRestMethod(t *testing.T) {
TypeName: proto.String(".google.protobuf.FieldMask"),
}

repeatedPrimField := &descriptor.FieldDescriptorProto{
Name: proto.String("primitives"),
Type: descriptor.FieldDescriptorProto_TYPE_STRING.Enum().Enum(),
TypeName: proto.String(foofqn),
Label: descriptor.FieldDescriptorProto_LABEL_REPEATED.Enum(),
}

updateReq := &descriptor.DescriptorProto{
Name: proto.String("UpdateRequest"),
Field: []*descriptor.FieldDescriptorProto{fooField, maskField},
Field: []*descriptor.FieldDescriptorProto{fooField, maskField, repeatedPrimField},
}
updateReqFqn := fmt.Sprintf(".%s.UpdateRequest", pkg)

Expand Down
5 changes: 5 additions & 0 deletions internal/gengapic/testdata/rest_UpdateRPC.want
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func (c *fooRESTClient) UpdateRPC(ctx context.Context, req *foopb.UpdateRequest,

params := url.Values{}
params.Add("$alt", "json;enum-encoding=int")
if items := req.GetPrimitives(); len(items) > 0 {
for _, item := range items {
params.Add("primitives", fmt.Sprintf("%v", item))
}
}
if req.GetUpdateMask() != nil {
updateMask, err := protojson.Marshal(req.GetUpdateMask())
if err != nil {
Expand Down