Skip to content

Commit

Permalink
protoc-gen-go: normalize floating-point default values (#737)
Browse files Browse the repository at this point in the history
Parse floating-point default values and format them with fmt.Sprint.
Eliminates a minor point of inconsistency with the v2 generator.

Change-Id: I153318f1760bac878cb0303301bed7e4bafe3431
Cherry-Pick: github.com/golang/protobuf@d3de96c4c28ef8af3aa1a892fc481e0f103c01ff
Original-Author: Damien Neil <neild@users.noreply.github.com>
Reviewed-on: https://go-review.googlesource.com/c/151429
Reviewed-by: Damien Neil <dneil@google.com>
  • Loading branch information
dsnet committed Nov 27, 2018
1 parent b4468de commit 4e2754b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 66 deletions.
20 changes: 20 additions & 0 deletions protoc-gen-go/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,18 @@ func (g *Generator) goTag(message *Descriptor, field *descriptor.FieldDescriptor
g.Fail("unknown enum type", CamelCaseSlice(obj.TypeName()))
}
defaultValue = enum.integerValueAsString(defaultValue)
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
if def := defaultValue; def != "inf" && def != "-inf" && def != "nan" {
if f, err := strconv.ParseFloat(defaultValue, 32); err == nil {
defaultValue = fmt.Sprint(float32(f))
}
}
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
if def := defaultValue; def != "inf" && def != "-inf" && def != "nan" {
if f, err := strconv.ParseFloat(defaultValue, 64); err == nil {
defaultValue = fmt.Sprint(f)
}
}
}
defaultValue = ",def=" + defaultValue
}
Expand Down Expand Up @@ -2236,6 +2248,14 @@ func (g *Generator) generateDefaultConstants(mc *msgCtx, topLevelFields []topLev
def = "float32(" + def + ")"
}
kind = "var "
case df.getProtoType() == descriptor.FieldDescriptorProto_TYPE_FLOAT:
if f, err := strconv.ParseFloat(def, 32); err == nil {
def = fmt.Sprint(float32(f))
}
case df.getProtoType() == descriptor.FieldDescriptorProto_TYPE_DOUBLE:
if f, err := strconv.ParseFloat(def, 64); err == nil {
def = fmt.Sprint(f)
}
case df.getProtoType() == descriptor.FieldDescriptorProto_TYPE_ENUM:
// Must be an enum. Need to construct the prefixed name.
obj := g.ObjectNamed(df.getProtoTypeName())
Expand Down
196 changes: 130 additions & 66 deletions protoc-gen-go/testdata/my_test/test.pb.go

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

7 changes: 7 additions & 0 deletions protoc-gen-go/testdata/my_test/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ message Request {
optional int32 reset = 12;
// This field should not conflict with any getters.
optional string get_key = 16;

optional float float_ninf = 20 [default=-inf];
optional float float_pinf = 21 [default=inf];
optional float float_exp = 22 [default=1e9];
optional double double_ninf = 23 [default=-inf];
optional double double_pinf = 24 [default=inf];
optional double double_exp = 25 [default=1e9];
}

message Reply {
Expand Down

0 comments on commit 4e2754b

Please sign in to comment.