Skip to content

Commit

Permalink
fix(gengapic): make field var name generic (#1537)
Browse files Browse the repository at this point in the history
This helps avoid clashes with keywords.
  • Loading branch information
codyoss authored Jun 20, 2024
1 parent 36e3032 commit bafae38
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (g *generator) generateQueryString(m *descriptorpb.MethodDescriptorProto) {
// Handle well known protobuf types with special JSON encodings.
if strContains(wellKnownTypeNames, field.GetTypeName()) {
b := strings.Builder{}
b.WriteString(fmt.Sprintf("%s, err := protojson.Marshal(req%s)\n", field.GetJsonName(), accessor))
b.WriteString(fmt.Sprintf("field, err := protojson.Marshal(req%s)\n", accessor))
b.WriteString("if err != nil {\n")
if m.GetOutputType() == emptyType {
b.WriteString(" return err\n")
Expand All @@ -429,9 +429,9 @@ func (g *generator) generateQueryString(m *descriptorpb.MethodDescriptorProto) {
b.WriteString("}\n")
// Only some of the well known types will be encoded as strings, remove the wrapping quotations for those.
if strContains(wellKnownStringTypes, field.GetTypeName()) {
b.WriteString(fmt.Sprintf("params.Add(%q, string(%s[1:len(%[2]s)-1]))", key, field.GetJsonName()))
b.WriteString(fmt.Sprintf("params.Add(%q, string(field[1:len(field)-1]))", key))
} else {
b.WriteString(fmt.Sprintf("params.Add(%q, string(%s))", key, field.GetJsonName()))
b.WriteString(fmt.Sprintf("params.Add(%q, string(field))", key))
}
paramAdd = b.String()
} else {
Expand Down
8 changes: 4 additions & 4 deletions internal/gengapic/testdata/rest_UpdateRPC.want
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ func (c *fooRESTClient) UpdateRPC(ctx context.Context, req *foopb.UpdateRequest,
params := url.Values{}
params.Add("$alt", "json;enum-encoding=int")
if req.GetNumericWrapper() != nil {
numericWrapper, err := protojson.Marshal(req.GetNumericWrapper())
field, err := protojson.Marshal(req.GetNumericWrapper())
if err != nil {
return nil, err
}
params.Add("numericWrapper", string(numericWrapper))
params.Add("numericWrapper", string(field))
}
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())
field, err := protojson.Marshal(req.GetUpdateMask())
if err != nil {
return nil, err
}
params.Add("updateMask", string(updateMask[1:len(updateMask)-1]))
params.Add("updateMask", string(field[1:len(field)-1]))
}

baseUrl.RawQuery = params.Encode()
Expand Down

0 comments on commit bafae38

Please sign in to comment.