Support for Inf/-Inf/NaN default values.
R=r
CC=golang-dev
http://codereview.appspot.com/3887041
diff --git a/compiler/generator/generator.go b/compiler/generator/generator.go
index f6b6c25..fa67d86 100644
--- a/compiler/generator/generator.go
+++ b/compiler/generator/generator.go
@@ -632,6 +632,7 @@
// do, which is tricky when there's a plugin, just import it and
// reference it later. The same argument applies to the os package.
g.P("import " + g.ProtoPkg + " " + Quote(g.ImportPrefix+"goprotobuf.googlecode.com/hg/proto"))
+ g.P(`import "math"`)
g.P(`import "os"`)
for _, s := range g.file.Dependency {
// Need to find the descriptor for this file
@@ -664,8 +665,9 @@
p.GenerateImports(g.file)
g.P()
}
- g.P("// Reference proto & os imports to suppress error if it's not otherwise used.")
+ g.P("// Reference proto, math & os imports to suppress error if they are not otherwise used.")
g.P("var _ = ", g.ProtoPkg, ".GetString")
+ g.P("var _ = math.Inf")
g.P("var _ os.Error")
g.P()
}
@@ -969,6 +971,20 @@
case typename == "[]byte":
def = "[]byte(" + Quote(def) + ")"
kind = "var "
+ case def == "inf", def == "-inf", def == "nan":
+ // These names are known to, and defined by, the protocol language.
+ switch def {
+ case "inf":
+ def = "math.Inf(1)"
+ case "-inf":
+ def = "math.Inf(-1)"
+ case "nan":
+ def = "math.NaN()"
+ }
+ if *field.Type == descriptor.FieldDescriptorProto_TYPE_FLOAT {
+ def = "float32(" + def + ")"
+ }
+ kind = "var "
case *field.Type == descriptor.FieldDescriptorProto_TYPE_ENUM:
// Must be an enum. Need to construct the prefixed name.
obj := g.ObjectNamed(proto.GetString(field.TypeName))
diff --git a/compiler/testdata/test.proto b/compiler/testdata/test.proto
index b63658b..7c7af50 100644
--- a/compiler/testdata/test.proto
+++ b/compiler/testdata/test.proto
@@ -57,6 +57,7 @@
optional Color hue = 3;
optional HatType hat = 4 [default=FEDORA];
optional imp.ImportedMessage.Owner owner = 6;
+ optional float deadline = 7 [default=inf];
}
message Reply {