Skip to content

Commit

Permalink
types/known/structpb: preallocate map in AsMap
Browse files Browse the repository at this point in the history
Change-Id: I4fe676418959c23055e91d99a07ee3be8d466e25
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/460215
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
  • Loading branch information
ash2k authored and stapelberg committed Jan 5, 2023
1 parent b2a7dfe commit a8d446d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
17 changes: 8 additions & 9 deletions cmd/protoc-gen-go/internal_gengo/well_known_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ func genPackageKnownComment(f *fileInfo) protogen.Comments {
"google.golang.org/protobuf/encoding/protojson" package
ensures that they will be serialized as their JSON equivalent.
Conversion to and from a Go interface
# Conversion to and from a Go interface
The standard Go "encoding/json" package has functionality to serialize
arbitrary types to a large degree. The Value.AsInterface, Struct.AsMap, and
Expand All @@ -222,8 +221,7 @@ func genPackageKnownComment(f *fileInfo) protogen.Comments {
forms back as Value, Struct, and ListValue messages, use the NewStruct,
NewList, and NewValue constructor functions.
Example usage
# Example usage
Consider the following example JSON object:
Expand Down Expand Up @@ -282,7 +280,6 @@ func genPackageKnownComment(f *fileInfo) protogen.Comments {
... // handle error
}
... // make use of m as a *structpb.Value
`
case genid.File_google_protobuf_field_mask_proto:
return ` Package fieldmaskpb contains generated types for ` + genid.File_google_protobuf_field_mask_proto + `.
Expand Down Expand Up @@ -656,8 +653,9 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P("// AsMap converts x to a general-purpose Go map.")
g.P("// The map values are converted by calling Value.AsInterface.")
g.P("func (x *Struct) AsMap() map[string]interface{} {")
g.P(" vs := make(map[string]interface{})")
g.P(" for k, v := range x.GetFields() {")
g.P(" f := x.GetFields()")
g.P(" vs := make(map[string]interface{}, len(f))")
g.P(" for k, v := range f {")
g.P(" vs[k] = v.AsInterface()")
g.P(" }")
g.P(" return vs")
Expand Down Expand Up @@ -693,8 +691,9 @@ func genMessageKnownFunctions(g *protogen.GeneratedFile, f *fileInfo, m *message
g.P("// AsSlice converts x to a general-purpose Go slice.")
g.P("// The slice elements are converted by calling Value.AsInterface.")
g.P("func (x *ListValue) AsSlice() []interface{} {")
g.P(" vs := make([]interface{}, len(x.GetValues()))")
g.P(" for i, v := range x.GetValues() {")
g.P(" vals := x.GetValues()")
g.P(" vs := make([]interface{}, len(vals))")
g.P(" for i, v := range vals {")
g.P(" vs[i] = v.AsInterface()")
g.P(" }")
g.P(" return vs")
Expand Down
17 changes: 8 additions & 9 deletions types/known/structpb/struct.pb.go

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

0 comments on commit a8d446d

Please sign in to comment.