| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. All rights reserved. |
| 4 | // http://code.google.com/p/goprotobuf/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | /* |
| 33 | The code generator for the plugin for the Google protocol buffer compiler. |
| 34 | It generates Go code from the protocol buffer description files read by the |
| 35 | main routine. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 36 | */ |
| 37 | package generator |
| 38 | |
| 39 | import ( |
| 40 | "bytes" |
| 41 | "fmt" |
| David Symonds | b1d55a0 | 2011-04-08 09:55:06 +1000 | [diff] [blame] | 42 | "go/parser" |
| 43 | "go/printer" |
| 44 | "go/token" |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 45 | "log" |
| 46 | "os" |
| Rob Pike | 87af39e | 2010-07-19 10:48:02 -0700 | [diff] [blame] | 47 | "path" |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 48 | "strconv" |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 49 | "strings" |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 50 | |
| 51 | "goprotobuf.googlecode.com/hg/proto" |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 52 | plugin "goprotobuf.googlecode.com/hg/compiler/plugin" |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 53 | descriptor "goprotobuf.googlecode.com/hg/compiler/descriptor" |
| 54 | ) |
| 55 | |
| 56 | // A Plugin provides functionality to add to the output during Go code generation, |
| 57 | // such as to produce RPC stubs. |
| 58 | type Plugin interface { |
| 59 | // Name identifies the plugin. |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 60 | Name() string |
| 61 | // Init is called once after data structures are built but before |
| 62 | // code generation begins. |
| 63 | Init(g *Generator) |
| 64 | // Generate produces the code generated by the plugin for this file, |
| 65 | // except for the imports, by calling the generator's methods P, In, and Out. |
| 66 | Generate(file *FileDescriptor) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 67 | // GenerateImports produces the import declarations for this file. |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 68 | // It is called after Generate. |
| 69 | GenerateImports(file *FileDescriptor) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | var plugins []Plugin |
| 73 | |
| 74 | // RegisterPlugin installs a (second-order) plugin to be run when the Go output is generated. |
| 75 | // It is typically called during initialization. |
| 76 | func RegisterPlugin(p Plugin) { |
| David Symonds | cc7142e | 2010-11-06 14:37:15 +1100 | [diff] [blame] | 77 | plugins = append(plugins, p) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Each type we import as a protocol buffer (other than FileDescriptorProto) needs |
| 81 | // a pointer to the FileDescriptorProto that represents it. These types achieve that |
| 82 | // wrapping by placing each Proto inside a struct with the pointer to its File. The |
| 83 | // structs have the same names as their contents, with "Proto" removed. |
| 84 | // FileDescriptor is used to store the things that it points to. |
| 85 | |
| 86 | // The file and package name method are common to messages and enums. |
| 87 | type common struct { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 88 | file *descriptor.FileDescriptorProto // File this object comes from. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // PackageName is name in the package clause in the generated file. |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 92 | func (c *common) PackageName() string { return uniquePackageOf(c.file) } |
| 93 | |
| 94 | func (c *common) File() *descriptor.FileDescriptorProto { return c.file } |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 95 | |
| 96 | // Descriptor represents a protocol buffer message. |
| 97 | type Descriptor struct { |
| 98 | common |
| 99 | *descriptor.DescriptorProto |
| 100 | parent *Descriptor // The containing message, if any. |
| 101 | nested []*Descriptor // Inner messages, if any. |
| 102 | ext []*ExtensionDescriptor // Extensions, if any. |
| 103 | typename []string // Cached typename vector. |
| 104 | } |
| 105 | |
| 106 | // TypeName returns the elements of the dotted type name. |
| 107 | // The package name is not part of this name. |
| 108 | func (d *Descriptor) TypeName() []string { |
| 109 | if d.typename != nil { |
| 110 | return d.typename |
| 111 | } |
| 112 | n := 0 |
| 113 | for parent := d; parent != nil; parent = parent.parent { |
| 114 | n++ |
| 115 | } |
| 116 | s := make([]string, n, n) |
| 117 | for parent := d; parent != nil; parent = parent.parent { |
| 118 | n-- |
| 119 | s[n] = proto.GetString(parent.Name) |
| 120 | } |
| 121 | d.typename = s |
| 122 | return s |
| 123 | } |
| 124 | |
| 125 | // EnumDescriptor describes an enum. If it's at top level, its parent will be nil. |
| 126 | // Otherwise it will be the descriptor of the message in which it is defined. |
| 127 | type EnumDescriptor struct { |
| 128 | common |
| 129 | *descriptor.EnumDescriptorProto |
| 130 | parent *Descriptor // The containing message, if any. |
| 131 | typename []string // Cached typename vector. |
| 132 | } |
| 133 | |
| 134 | // TypeName returns the elements of the dotted type name. |
| 135 | // The package name is not part of this name. |
| 136 | func (e *EnumDescriptor) TypeName() (s []string) { |
| 137 | if e.typename != nil { |
| 138 | return e.typename |
| 139 | } |
| 140 | name := proto.GetString(e.Name) |
| 141 | if e.parent == nil { |
| 142 | s = make([]string, 1) |
| 143 | } else { |
| 144 | pname := e.parent.TypeName() |
| 145 | s = make([]string, len(pname)+1) |
| 146 | copy(s, pname) |
| 147 | } |
| 148 | s[len(s)-1] = name |
| 149 | e.typename = s |
| 150 | return s |
| 151 | } |
| 152 | |
| 153 | // Everything but the last element of the full type name, CamelCased. |
| 154 | // The values of type Foo.Bar are call Foo_value1... not Foo_Bar_value1... . |
| 155 | func (e *EnumDescriptor) prefix() string { |
| 156 | typeName := e.TypeName() |
| 157 | ccPrefix := CamelCaseSlice(typeName[0:len(typeName)-1]) + "_" |
| 158 | if e.parent == nil { |
| 159 | // If the enum is not part of a message, the prefix is just the type name. |
| 160 | ccPrefix = CamelCase(*e.Name) + "_" |
| 161 | } |
| 162 | return ccPrefix |
| 163 | } |
| 164 | |
| 165 | // The integer value of the named constant in this enumerated type. |
| 166 | func (e *EnumDescriptor) integerValueAsString(name string) string { |
| 167 | for _, c := range e.Value { |
| 168 | if proto.GetString(c.Name) == name { |
| 169 | return fmt.Sprint(proto.GetInt32(c.Number)) |
| 170 | } |
| 171 | } |
| David Symonds | 9d0000e | 2011-02-03 10:48:14 +1100 | [diff] [blame] | 172 | log.Fatal("cannot find value for enum constant") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 173 | return "" |
| 174 | } |
| 175 | |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 176 | // ExtensionDescriptor describes an extension. If it's at top level, its parent will be nil. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 177 | // Otherwise it will be the descriptor of the message in which it is defined. |
| 178 | type ExtensionDescriptor struct { |
| 179 | common |
| 180 | *descriptor.FieldDescriptorProto |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 181 | parent *Descriptor // The containing message, if any. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // TypeName returns the elements of the dotted type name. |
| 185 | // The package name is not part of this name. |
| 186 | func (e *ExtensionDescriptor) TypeName() (s []string) { |
| 187 | name := proto.GetString(e.Name) |
| 188 | if e.parent == nil { |
| 189 | // top-level extension |
| 190 | s = make([]string, 1) |
| 191 | } else { |
| 192 | pname := e.parent.TypeName() |
| 193 | s = make([]string, len(pname)+1) |
| 194 | copy(s, pname) |
| 195 | } |
| 196 | s[len(s)-1] = name |
| 197 | return s |
| 198 | } |
| 199 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 200 | // DescName returns the variable name used for the generated descriptor. |
| 201 | func (e *ExtensionDescriptor) DescName() string { |
| 202 | // The full type name. |
| 203 | typeName := e.TypeName() |
| 204 | // Each scope of the extension is individually CamelCased, and all are joined with "_" with an "E_" prefix. |
| 205 | for i, s := range typeName { |
| 206 | typeName[i] = CamelCase(s) |
| 207 | } |
| 208 | return "E_" + strings.Join(typeName, "_") |
| 209 | } |
| 210 | |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 211 | // ImportedDescriptor describes a type that has been publicly imported from another file. |
| 212 | type ImportedDescriptor struct { |
| 213 | common |
| 214 | o Object |
| 215 | } |
| 216 | |
| 217 | func (id *ImportedDescriptor) TypeName() []string { return id.o.TypeName() } |
| 218 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 219 | // FileDescriptor describes an protocol buffer descriptor file (.proto). |
| 220 | // It includes slices of all the messages and enums defined within it. |
| 221 | // Those slices are constructed by WrapTypes. |
| 222 | type FileDescriptor struct { |
| 223 | *descriptor.FileDescriptorProto |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 224 | desc []*Descriptor // All the messages defined in this file. |
| 225 | enum []*EnumDescriptor // All the enums defined in this file. |
| 226 | ext []*ExtensionDescriptor // All the top-level extensions defined in this file. |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 227 | imp []*ImportedDescriptor // All types defined in files publicly imported by this file. |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 228 | |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 229 | // The full list of symbols that are exported, |
| 230 | // as a map from the exported object to its symbols. |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 231 | // This is used for supporting public imports. |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 232 | exported map[Object][]Symbol |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // PackageName is the package name we'll use in the generated code to refer to this file. |
| 236 | func (d *FileDescriptor) PackageName() string { return uniquePackageOf(d.FileDescriptorProto) } |
| 237 | |
| 238 | // The package named defined in the input for this file, possibly dotted. |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 239 | // If the file does not define a package, use the base of the file name. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 240 | func (d *FileDescriptor) originalPackageName() string { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 241 | // Does the file have a package clause? |
| David Symonds | 7d5c824 | 2011-03-14 12:03:50 -0700 | [diff] [blame] | 242 | if pkg := proto.GetString(d.Package); pkg != "" { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 243 | return pkg |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 244 | } |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 245 | // Use the file base name. |
| 246 | return BaseName(proto.GetString(d.Name)) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 249 | func (d *FileDescriptor) addExport(obj Object, symbol Symbol) { |
| 250 | d.exported[obj] = append(d.exported[obj], symbol) |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // Symbol is an interface representing an exported Go symbol. |
| 254 | type Symbol interface { |
| 255 | // GenerateAlias should generate an appropriate alias |
| 256 | // for the symbol from the named package. |
| 257 | GenerateAlias(g *Generator, pkg string) |
| 258 | } |
| 259 | |
| 260 | type messageSymbol struct { |
| 261 | sym string |
| 262 | hasExtensions, isMessageSet bool |
| 263 | } |
| 264 | |
| 265 | func (ms messageSymbol) GenerateAlias(g *Generator, pkg string) { |
| 266 | remoteSym := pkg + "." + ms.sym |
| 267 | |
| 268 | g.P("type ", ms.sym, " ", remoteSym) |
| 269 | g.P("func (this *", ms.sym, ") Reset() { (*", remoteSym, ")(this).Reset() }") |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 270 | g.P("func (this *", ms.sym, ") String() string { return (*", remoteSym, ")(this).String() }") |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 271 | if ms.hasExtensions { |
| 272 | g.P("func (*", ms.sym, ") ExtensionRangeArray() []", g.ProtoPkg, ".ExtensionRange ", |
| 273 | "{ return (*", remoteSym, ")(nil).ExtensionRangeArray() }") |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 274 | g.P("func (this *", ms.sym, ") ExtensionMap() map[int32]", g.ProtoPkg, ".Extension ", |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 275 | "{ return (*", remoteSym, ")(this).ExtensionMap() }") |
| 276 | if ms.isMessageSet { |
| 277 | g.P("func (this *", ms.sym, ") Marshal() ([]byte, os.Error) ", |
| 278 | "{ return (*", remoteSym, ")(this).Marshal() }") |
| 279 | g.P("func (this *", ms.sym, ") Unmarshal(buf []byte) os.Error ", |
| 280 | "{ return (*", remoteSym, ")(this).Unmarshal(buf) }") |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | type enumSymbol string |
| 286 | |
| 287 | func (es enumSymbol) GenerateAlias(g *Generator, pkg string) { |
| 288 | s := string(es) |
| 289 | g.P("type ", s, " ", pkg, ".", s) |
| 290 | g.P("var ", s, "_name = ", pkg, ".", s, "_name") |
| 291 | g.P("var ", s, "_value = ", pkg, ".", s, "_value") |
| 292 | g.P("func New", s, "(x int32) *", s, " { e := ", s, "(x); return &e }") |
| 293 | } |
| 294 | |
| 295 | type constOrVarSymbol struct { |
| 296 | sym string |
| 297 | typ string // either "const" or "var" |
| 298 | } |
| 299 | |
| 300 | func (cs constOrVarSymbol) GenerateAlias(g *Generator, pkg string) { |
| 301 | g.P(cs.typ, " ", cs.sym, " = ", pkg, ".", cs.sym) |
| 302 | } |
| 303 | |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 304 | // Object is an interface abstracting the abilities shared by enums, messages, extensions and imported objects. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 305 | type Object interface { |
| 306 | PackageName() string // The name we use in our output (a_b_c), possibly renamed for uniqueness. |
| 307 | TypeName() []string |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 308 | File() *descriptor.FileDescriptorProto |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Each package name we generate must be unique. The package we're generating |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 312 | // gets its own name but every other package must have a unique name that does |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 313 | // not conflict in the code we generate. These names are chosen globally (although |
| 314 | // they don't have to be, it simplifies things to do them globally). |
| 315 | func uniquePackageOf(fd *descriptor.FileDescriptorProto) string { |
| 316 | s, ok := uniquePackageName[fd] |
| 317 | if !ok { |
| David Symonds | 9d0000e | 2011-02-03 10:48:14 +1100 | [diff] [blame] | 318 | log.Fatal("internal error: no package name defined for", proto.GetString(fd.Name)) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 319 | } |
| 320 | return s |
| 321 | } |
| 322 | |
| 323 | // Generator is the type whose methods generate the output, stored in the associated response structure. |
| 324 | type Generator struct { |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 325 | *bytes.Buffer |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 326 | |
| 327 | Request *plugin.CodeGeneratorRequest // The input. |
| 328 | Response *plugin.CodeGeneratorResponse // The output. |
| 329 | |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 330 | Param map[string]string // Command-line parameters. |
| 331 | ImportPrefix string // String to prefix to imported package file names. |
| 332 | ImportMap map[string]string // Mapping from import name to generated name |
| 333 | |
| 334 | ProtoPkg string // The name under which we import the library's package proto. |
| 335 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 336 | packageName string // What we're calling ourselves. |
| 337 | allFiles []*FileDescriptor // All files in the tree |
| 338 | genFiles []*FileDescriptor // Those files we will generate output for. |
| 339 | file *FileDescriptor // The file we are compiling now. |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 340 | usedPackages map[string]bool // Names of packages used in current file. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 341 | typeNameToObject map[string]Object // Key is a fully-qualified name in input syntax. |
| 342 | indent string |
| 343 | } |
| 344 | |
| 345 | // New creates a new generator and allocates the request and response protobufs. |
| 346 | func New() *Generator { |
| 347 | g := new(Generator) |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 348 | g.Buffer = new(bytes.Buffer) |
| David Symonds | b012753 | 2010-11-09 11:10:46 +1100 | [diff] [blame] | 349 | g.Request = new(plugin.CodeGeneratorRequest) |
| 350 | g.Response = new(plugin.CodeGeneratorResponse) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 351 | return g |
| 352 | } |
| 353 | |
| 354 | // Error reports a problem, including an os.Error, and exits the program. |
| 355 | func (g *Generator) Error(err os.Error, msgs ...string) { |
| 356 | s := strings.Join(msgs, " ") + ":" + err.String() |
| Rob Pike | 5194c51 | 2010-10-14 13:02:16 -0700 | [diff] [blame] | 357 | log.Println("protoc-gen-go: error:", s) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 358 | g.Response.Error = proto.String(s) |
| 359 | os.Exit(1) |
| 360 | } |
| 361 | |
| 362 | // Fail reports a problem and exits the program. |
| 363 | func (g *Generator) Fail(msgs ...string) { |
| 364 | s := strings.Join(msgs, " ") |
| Rob Pike | 5194c51 | 2010-10-14 13:02:16 -0700 | [diff] [blame] | 365 | log.Println("protoc-gen-go: error:", s) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 366 | g.Response.Error = proto.String(s) |
| 367 | os.Exit(1) |
| 368 | } |
| 369 | |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 370 | // CommandLineParameters breaks the comma-separated list of key=value pairs |
| 371 | // in the parameter (a member of the request protobuf) into a key/value map. |
| 372 | // It then sets file name mappings defined by those entries. |
| 373 | func (g *Generator) CommandLineParameters(parameter string) { |
| 374 | g.Param = make(map[string]string) |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 375 | for _, p := range strings.Split(parameter, ",") { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 376 | if i := strings.Index(p, "="); i < 0 { |
| 377 | g.Param[p] = "" |
| 378 | } else { |
| 379 | g.Param[p[0:i]] = p[i+1:] |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | g.ImportMap = make(map[string]string) |
| 384 | for k, v := range g.Param { |
| 385 | if k == "import_prefix" { |
| 386 | g.ImportPrefix = v |
| 387 | } else if len(k) > 0 && k[0] == 'M' { |
| 388 | g.ImportMap[k[1:]] = v |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 393 | // DefaultPackageName returns the package name printed for the object. |
| 394 | // If its file is in a different package, it returns the package name we're using for this file, plus ".". |
| 395 | // Otherwise it returns the empty string. |
| 396 | func (g *Generator) DefaultPackageName(obj Object) string { |
| 397 | pkg := obj.PackageName() |
| 398 | if pkg == g.packageName { |
| 399 | return "" |
| 400 | } |
| 401 | return pkg + "." |
| 402 | } |
| 403 | |
| 404 | // For each input file, the unique package name to use, underscored. |
| 405 | var uniquePackageName = make(map[*descriptor.FileDescriptorProto]string) |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 406 | // Package names already registered. Key is the name from the .proto file; |
| 407 | // value is the name that appears in the generated code. |
| 408 | var pkgNamesInUse = make(map[string]bool) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 409 | |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 410 | // Create and remember a guaranteed unique package name for this file descriptor. |
| 411 | // Pkg is the candidate name. If f is nil, it's a builtin package like "proto" and |
| 412 | // has no file descriptor. |
| 413 | func RegisterUniquePackageName(pkg string, f *FileDescriptor) string { |
| David Symonds | 4138981 | 2011-07-19 14:57:40 +1000 | [diff] [blame] | 414 | // Convert dots to underscores before finding a unique alias. |
| 415 | pkg = strings.Map(DotToUnderscore, pkg) |
| 416 | |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 417 | for i, orig := 1, pkg; pkgNamesInUse[pkg]; i++ { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 418 | // It's a duplicate; must rename. |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 419 | pkg = orig + strconv.Itoa(i) |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 420 | } |
| 421 | // Install it. |
| 422 | pkgNamesInUse[pkg] = true |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 423 | if f != nil { |
| 424 | uniquePackageName[f.FileDescriptorProto] = pkg |
| 425 | } |
| 426 | return pkg |
| 427 | } |
| 428 | |
| 429 | // SetPackageNames sets the package name for this run. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 430 | // The package name must agree across all files being generated. |
| 431 | // It also defines unique package names for all imported files. |
| 432 | func (g *Generator) SetPackageNames() { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 433 | // Register the name for this package. It will be the first name |
| 434 | // registered so is guaranteed to be unmodified. |
| 435 | pkg := g.genFiles[0].originalPackageName() |
| 436 | g.packageName = RegisterUniquePackageName(pkg, g.genFiles[0]) |
| 437 | // Register the proto package name. It might collide with the |
| 438 | // name of a package we import. |
| 439 | g.ProtoPkg = RegisterUniquePackageName("proto", nil) |
| David Symonds | 7d5c824 | 2011-03-14 12:03:50 -0700 | [diff] [blame] | 440 | // Verify that we are generating output for a single package. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 441 | for _, f := range g.genFiles { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 442 | thisPkg := f.originalPackageName() |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 443 | if thisPkg != pkg { |
| 444 | g.Fail("inconsistent package names:", thisPkg, pkg) |
| 445 | } |
| 446 | } |
| 447 | AllFiles: |
| 448 | for _, f := range g.allFiles { |
| 449 | for _, genf := range g.genFiles { |
| 450 | if f == genf { |
| 451 | // In this package already. |
| 452 | uniquePackageName[f.FileDescriptorProto] = g.packageName |
| 453 | continue AllFiles |
| 454 | } |
| 455 | } |
| David Symonds | 7d5c824 | 2011-03-14 12:03:50 -0700 | [diff] [blame] | 456 | // The file is a dependency, so we want to ignore its go_package option |
| 457 | // because that is only relevant for its specific generated output. |
| 458 | pkg := proto.GetString(f.Package) |
| 459 | if pkg == "" { |
| 460 | pkg = BaseName(*f.Name) |
| 461 | } |
| 462 | RegisterUniquePackageName(pkg, f) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | // WrapTypes walks the incoming data, wrapping DescriptorProtos, EnumDescriptorProtos |
| 467 | // and FileDescriptorProtos into file-referenced objects within the Generator. |
| 468 | // It also creates the list of files to generate and so should be called before GenerateAllFiles. |
| 469 | func (g *Generator) WrapTypes() { |
| 470 | g.allFiles = make([]*FileDescriptor, len(g.Request.ProtoFile)) |
| 471 | for i, f := range g.Request.ProtoFile { |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 472 | // We must wrap the descriptors before we wrap the enums |
| 473 | descs := wrapDescriptors(f) |
| 474 | g.buildNestedDescriptors(descs) |
| 475 | enums := wrapEnumDescriptors(f, descs) |
| 476 | exts := wrapExtensions(f) |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 477 | imps := wrapImported(f, g) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 478 | g.allFiles[i] = &FileDescriptor{ |
| 479 | FileDescriptorProto: f, |
| 480 | desc: descs, |
| 481 | enum: enums, |
| 482 | ext: exts, |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 483 | imp: imps, |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 484 | exported: make(map[Object][]Symbol), |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | g.genFiles = make([]*FileDescriptor, len(g.Request.FileToGenerate)) |
| 489 | FindFiles: |
| 490 | for i, fileName := range g.Request.FileToGenerate { |
| 491 | // Search the list. This algorithm is n^2 but n is tiny. |
| 492 | for _, file := range g.allFiles { |
| 493 | if fileName == proto.GetString(file.Name) { |
| 494 | g.genFiles[i] = file |
| 495 | continue FindFiles |
| 496 | } |
| 497 | } |
| 498 | g.Fail("could not find file named", fileName) |
| 499 | } |
| 500 | g.Response.File = make([]*plugin.CodeGeneratorResponse_File, len(g.genFiles)) |
| 501 | } |
| 502 | |
| 503 | // Scan the descriptors in this file. For each one, build the slice of nested descriptors |
| 504 | func (g *Generator) buildNestedDescriptors(descs []*Descriptor) { |
| 505 | for _, desc := range descs { |
| 506 | if len(desc.NestedType) != 0 { |
| 507 | desc.nested = make([]*Descriptor, len(desc.NestedType)) |
| 508 | n := 0 |
| 509 | for _, nest := range descs { |
| 510 | if nest.parent == desc { |
| 511 | desc.nested[n] = nest |
| 512 | n++ |
| 513 | } |
| 514 | } |
| 515 | if n != len(desc.NestedType) { |
| 516 | g.Fail("internal error: nesting failure for", proto.GetString(desc.Name)) |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // Construct the Descriptor and add it to the slice |
| 523 | func addDescriptor(sl []*Descriptor, desc *descriptor.DescriptorProto, parent *Descriptor, file *descriptor.FileDescriptorProto) []*Descriptor { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 524 | d := &Descriptor{common{file}, desc, parent, nil, nil, nil} |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 525 | |
| 526 | d.ext = make([]*ExtensionDescriptor, len(desc.Extension)) |
| 527 | for i, field := range desc.Extension { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 528 | d.ext[i] = &ExtensionDescriptor{common{file}, field, d} |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| David Symonds | cc7142e | 2010-11-06 14:37:15 +1100 | [diff] [blame] | 531 | return append(sl, d) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // Return a slice of all the Descriptors defined within this file |
| 535 | func wrapDescriptors(file *descriptor.FileDescriptorProto) []*Descriptor { |
| 536 | sl := make([]*Descriptor, 0, len(file.MessageType)+10) |
| 537 | for _, desc := range file.MessageType { |
| 538 | sl = wrapThisDescriptor(sl, desc, nil, file) |
| 539 | } |
| 540 | return sl |
| 541 | } |
| 542 | |
| 543 | // Wrap this Descriptor, recursively |
| 544 | func wrapThisDescriptor(sl []*Descriptor, desc *descriptor.DescriptorProto, parent *Descriptor, file *descriptor.FileDescriptorProto) []*Descriptor { |
| 545 | sl = addDescriptor(sl, desc, parent, file) |
| 546 | me := sl[len(sl)-1] |
| 547 | for _, nested := range desc.NestedType { |
| 548 | sl = wrapThisDescriptor(sl, nested, me, file) |
| 549 | } |
| 550 | return sl |
| 551 | } |
| 552 | |
| 553 | // Construct the EnumDescriptor and add it to the slice |
| 554 | func addEnumDescriptor(sl []*EnumDescriptor, desc *descriptor.EnumDescriptorProto, parent *Descriptor, file *descriptor.FileDescriptorProto) []*EnumDescriptor { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 555 | return append(sl, &EnumDescriptor{common{file}, desc, parent, nil}) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // Return a slice of all the EnumDescriptors defined within this file |
| 559 | func wrapEnumDescriptors(file *descriptor.FileDescriptorProto, descs []*Descriptor) []*EnumDescriptor { |
| 560 | sl := make([]*EnumDescriptor, 0, len(file.EnumType)+10) |
| David Symonds | 5256cf6 | 2010-06-27 10:33:42 +1000 | [diff] [blame] | 561 | // Top-level enums. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 562 | for _, enum := range file.EnumType { |
| 563 | sl = addEnumDescriptor(sl, enum, nil, file) |
| 564 | } |
| David Symonds | 5256cf6 | 2010-06-27 10:33:42 +1000 | [diff] [blame] | 565 | // Enums within messages. Enums within embedded messages appear in the outer-most message. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 566 | for _, nested := range descs { |
| David Symonds | 5256cf6 | 2010-06-27 10:33:42 +1000 | [diff] [blame] | 567 | for _, enum := range nested.EnumType { |
| 568 | sl = addEnumDescriptor(sl, enum, nested, file) |
| 569 | } |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 570 | } |
| 571 | return sl |
| 572 | } |
| 573 | |
| 574 | // Return a slice of all the top-level ExtensionDescriptors defined within this file. |
| 575 | func wrapExtensions(file *descriptor.FileDescriptorProto) []*ExtensionDescriptor { |
| 576 | sl := make([]*ExtensionDescriptor, len(file.Extension)) |
| 577 | for i, field := range file.Extension { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 578 | sl[i] = &ExtensionDescriptor{common{file}, field, nil} |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 579 | } |
| 580 | return sl |
| 581 | } |
| 582 | |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 583 | // Return a slice of all the types that are publicly imported into this file. |
| 584 | func wrapImported(file *descriptor.FileDescriptorProto, g *Generator) (sl []*ImportedDescriptor) { |
| 585 | for _, index := range file.PublicDependency { |
| 586 | df := g.fileByName(file.Dependency[index]) |
| 587 | for _, d := range df.desc { |
| 588 | sl = append(sl, &ImportedDescriptor{common{file}, d}) |
| 589 | } |
| 590 | for _, e := range df.enum { |
| 591 | sl = append(sl, &ImportedDescriptor{common{file}, e}) |
| 592 | } |
| 593 | for _, ext := range df.ext { |
| 594 | sl = append(sl, &ImportedDescriptor{common{file}, ext}) |
| 595 | } |
| 596 | } |
| 597 | return |
| 598 | } |
| 599 | |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 600 | // BuildTypeNameMap builds the map from fully qualified type names to objects. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 601 | // The key names for the map come from the input data, which puts a period at the beginning. |
| 602 | // It should be called after SetPackageNames and before GenerateAllFiles. |
| 603 | func (g *Generator) BuildTypeNameMap() { |
| 604 | g.typeNameToObject = make(map[string]Object) |
| 605 | for _, f := range g.allFiles { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 606 | // The names in this loop are defined by the proto world, not us, so the |
| 607 | // package name may be empty. If so, the dotted package name of X will |
| 608 | // be ".X"; otherwise it will be ".pkg.X". |
| 609 | dottedPkg := "." + proto.GetString(f.Package) |
| 610 | if dottedPkg != "." { |
| 611 | dottedPkg += "." |
| 612 | } |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 613 | for _, enum := range f.enum { |
| 614 | name := dottedPkg + dottedSlice(enum.TypeName()) |
| 615 | g.typeNameToObject[name] = enum |
| 616 | } |
| 617 | for _, desc := range f.desc { |
| 618 | name := dottedPkg + dottedSlice(desc.TypeName()) |
| 619 | g.typeNameToObject[name] = desc |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | // ObjectNamed, given a fully-qualified input type name as it appears in the input data, |
| 625 | // returns the descriptor for the message or enum with that name. |
| 626 | func (g *Generator) ObjectNamed(typeName string) Object { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 627 | o, ok := g.typeNameToObject[typeName] |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 628 | if !ok { |
| 629 | g.Fail("can't find object with type", typeName) |
| 630 | } |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 631 | |
| 632 | // If the file of this object isn't a direct dependency of the current file, |
| 633 | // or in the current file, then this object has been publicly imported into |
| 634 | // a dependency of the current file. |
| 635 | // We should return the ImportedDescriptor object for it instead. |
| 636 | direct := *o.File().Name == *g.file.Name |
| 637 | if !direct { |
| 638 | for _, dep := range g.file.Dependency { |
| 639 | if *g.fileByName(dep).Name == *o.File().Name { |
| 640 | direct = true |
| 641 | break |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | if !direct { |
| 646 | found := false |
| 647 | Loop: |
| 648 | for _, dep := range g.file.Dependency { |
| 649 | df := g.fileByName(*g.fileByName(dep).Name) |
| 650 | for _, td := range df.imp { |
| 651 | if td.o == o { |
| 652 | // Found it! |
| 653 | o = td |
| 654 | found = true |
| 655 | break Loop |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | if !found { |
| 660 | log.Printf("protoc-gen-go: WARNING: failed finding publicly imported dependency for %v, used in %v", typeName, *g.file.Name) |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return o |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // P prints the arguments to the generated output. It handles strings and int32s, plus |
| 668 | // handling indirections because they may be *string, etc. |
| 669 | func (g *Generator) P(str ...interface{}) { |
| 670 | g.WriteString(g.indent) |
| 671 | for _, v := range str { |
| 672 | switch s := v.(type) { |
| 673 | case string: |
| 674 | g.WriteString(s) |
| 675 | case *string: |
| 676 | g.WriteString(*s) |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 677 | case bool: |
| 678 | g.WriteString(fmt.Sprintf("%t", s)) |
| 679 | case *bool: |
| 680 | g.WriteString(fmt.Sprintf("%t", *s)) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 681 | case *int32: |
| 682 | g.WriteString(fmt.Sprintf("%d", *s)) |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 683 | case float64: |
| 684 | g.WriteString(fmt.Sprintf("%g", s)) |
| 685 | case *float64: |
| 686 | g.WriteString(fmt.Sprintf("%g", *s)) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 687 | default: |
| 688 | g.Fail(fmt.Sprintf("unknown type in printer: %T", v)) |
| 689 | } |
| 690 | } |
| 691 | g.WriteByte('\n') |
| 692 | } |
| 693 | |
| 694 | // In Indents the output one tab stop. |
| 695 | func (g *Generator) In() { g.indent += "\t" } |
| 696 | |
| 697 | // Out unindents the output one tab stop. |
| 698 | func (g *Generator) Out() { |
| 699 | if len(g.indent) > 0 { |
| 700 | g.indent = g.indent[1:] |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | // GenerateAllFiles generates the output for all the files we're outputting. |
| 705 | func (g *Generator) GenerateAllFiles() { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 706 | // Initialize the plugins |
| 707 | for _, p := range plugins { |
| 708 | p.Init(g) |
| 709 | } |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 710 | // Generate the output. The generator runs for every file, even the files |
| 711 | // that we don't generate output for, so that we can collate the full list |
| 712 | // of exported symbols to support public imports. |
| 713 | genFileMap := make(map[*FileDescriptor]bool, len(g.genFiles)) |
| 714 | for _, file := range g.genFiles { |
| 715 | genFileMap[file] = true |
| 716 | } |
| 717 | i := 0 |
| 718 | for _, file := range g.allFiles { |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 719 | g.Reset() |
| 720 | g.generate(file) |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 721 | if _, ok := genFileMap[file]; !ok { |
| 722 | continue |
| 723 | } |
| David Symonds | b012753 | 2010-11-09 11:10:46 +1100 | [diff] [blame] | 724 | g.Response.File[i] = new(plugin.CodeGeneratorResponse_File) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 725 | g.Response.File[i].Name = proto.String(goFileName(*file.Name)) |
| 726 | g.Response.File[i].Content = proto.String(g.String()) |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 727 | i++ |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
| 731 | // Run all the plugins associated with the file. |
| 732 | func (g *Generator) runPlugins(file *FileDescriptor) { |
| 733 | for _, p := range plugins { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 734 | p.Generate(file) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
| 738 | |
| 739 | // FileOf return the FileDescriptor for this FileDescriptorProto. |
| 740 | func (g *Generator) FileOf(fd *descriptor.FileDescriptorProto) *FileDescriptor { |
| 741 | for _, file := range g.allFiles { |
| 742 | if file.FileDescriptorProto == fd { |
| 743 | return file |
| 744 | } |
| 745 | } |
| 746 | g.Fail("could not find file in table:", proto.GetString(fd.Name)) |
| 747 | return nil |
| 748 | } |
| 749 | |
| 750 | // Fill the response protocol buffer with the generated output for all the files we're |
| 751 | // supposed to generate. |
| 752 | func (g *Generator) generate(file *FileDescriptor) { |
| 753 | g.file = g.FileOf(file.FileDescriptorProto) |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 754 | g.usedPackages = make(map[string]bool) |
| 755 | |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 756 | for _, td := range g.file.imp { |
| 757 | g.generateImported(td) |
| 758 | } |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 759 | for _, enum := range g.file.enum { |
| 760 | g.generateEnum(enum) |
| 761 | } |
| 762 | for _, desc := range g.file.desc { |
| 763 | g.generateMessage(desc) |
| 764 | } |
| 765 | for _, ext := range g.file.ext { |
| 766 | g.generateExtension(ext) |
| 767 | } |
| 768 | g.generateInitFunction() |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 769 | |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 770 | // Run the plugins before the imports so we know which imports are necessary. |
| 771 | g.runPlugins(file) |
| 772 | |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 773 | // Generate header and imports last, though they appear first in the output. |
| 774 | rem := g.Buffer |
| 775 | g.Buffer = new(bytes.Buffer) |
| 776 | g.generateHeader() |
| 777 | g.generateImports() |
| 778 | g.Write(rem.Bytes()) |
| David Symonds | b1d55a0 | 2011-04-08 09:55:06 +1000 | [diff] [blame] | 779 | |
| 780 | // Reformat generated code. |
| 781 | fset := token.NewFileSet() |
| 782 | ast, err := parser.ParseFile(fset, "", g, parser.ParseComments) |
| 783 | if err != nil { |
| 784 | g.Fail("bad Go source code was generated:", err.String()) |
| 785 | return |
| 786 | } |
| 787 | g.Reset() |
| David Symonds | 2ed81c4 | 2011-08-12 17:06:51 +1000 | [diff] [blame] | 788 | _, err = (&printer.Config{printer.TabIndent | printer.UseSpaces, 8}).Fprint(g, fset, ast) |
| David Symonds | b1d55a0 | 2011-04-08 09:55:06 +1000 | [diff] [blame] | 789 | if err != nil { |
| 790 | g.Fail("generated Go source code could not be reformatted:", err.String()) |
| 791 | } |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | // Generate the header, including package definition and imports |
| 795 | func (g *Generator) generateHeader() { |
| 796 | g.P("// Code generated by protoc-gen-go from ", Quote(*g.file.Name)) |
| 797 | g.P("// DO NOT EDIT!") |
| 798 | g.P() |
| 799 | g.P("package ", g.file.PackageName()) |
| 800 | g.P() |
| 801 | } |
| 802 | |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 803 | func (g *Generator) fileByName(filename string) *FileDescriptor { |
| 804 | for _, fd := range g.allFiles { |
| 805 | if proto.GetString(fd.Name) == filename { |
| 806 | return fd |
| 807 | } |
| 808 | } |
| 809 | return nil |
| 810 | } |
| 811 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 812 | // Generate the header, including package definition and imports |
| 813 | func (g *Generator) generateImports() { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 814 | // We almost always need a proto import. Rather than computing when we |
| 815 | // do, which is tricky when there's a plugin, just import it and |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 816 | // reference it later. The same argument applies to the os package. |
| Rob Pike | 809831a | 2010-06-16 10:10:58 -0700 | [diff] [blame] | 817 | g.P("import " + g.ProtoPkg + " " + Quote(g.ImportPrefix+"goprotobuf.googlecode.com/hg/proto")) |
| David Symonds | cea785b | 2011-01-07 11:02:30 +1100 | [diff] [blame] | 818 | g.P(`import "math"`) |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 819 | g.P(`import "os"`) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 820 | for _, s := range g.file.Dependency { |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 821 | fd := g.fileByName(s) |
| 822 | // Do not import our own package. |
| 823 | if fd.PackageName() == g.packageName { |
| 824 | continue |
| 825 | } |
| 826 | filename := goFileName(s) |
| 827 | if substitution, ok := g.ImportMap[s]; ok { |
| 828 | filename = substitution |
| 829 | } |
| 830 | filename = g.ImportPrefix + filename |
| 831 | if strings.HasSuffix(filename, ".go") { |
| 832 | filename = filename[0 : len(filename)-3] |
| 833 | } |
| 834 | if _, ok := g.usedPackages[fd.PackageName()]; ok { |
| 835 | g.P("import ", fd.PackageName(), " ", Quote(filename)) |
| 836 | } else { |
| David Symonds | 3fa055f | 2011-05-05 15:19:04 -0700 | [diff] [blame] | 837 | // TODO: Re-enable this when we are more feature-complete. |
| 838 | // For instance, some protos use foreign field extensions, which we don't support. |
| 839 | // Until then, this is just annoying spam. |
| 840 | //log.Printf("protoc-gen-go: discarding unused import from %v: %v", *g.file.Name, s) |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 841 | g.P("// discarding unused import ", fd.PackageName(), " ", Quote(filename)) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | g.P() |
| 845 | // TODO: may need to worry about uniqueness across plugins |
| 846 | for _, p := range plugins { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 847 | p.GenerateImports(g.file) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 848 | g.P() |
| 849 | } |
| David Symonds | cea785b | 2011-01-07 11:02:30 +1100 | [diff] [blame] | 850 | g.P("// Reference proto, math & os imports to suppress error if they are not otherwise used.") |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 851 | g.P("var _ = ", g.ProtoPkg, ".GetString") |
| David Symonds | cea785b | 2011-01-07 11:02:30 +1100 | [diff] [blame] | 852 | g.P("var _ = math.Inf") |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 853 | g.P("var _ os.Error") |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 854 | g.P() |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 857 | func (g *Generator) generateImported(id *ImportedDescriptor) { |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 858 | // Don't generate public import symbols for files that we are generating |
| 859 | // code for, since those symbols will already be in this package. |
| 860 | // We can't simply avoid creating the ImportedDescriptor objects, |
| 861 | // because g.genFiles isn't populated at that stage. |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 862 | tn := id.TypeName() |
| 863 | sn := tn[len(tn)-1] |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 864 | df := g.FileOf(id.o.File()) |
| 865 | filename := *df.Name |
| 866 | for _, fd := range g.genFiles { |
| 867 | if *fd.Name == filename { |
| 868 | g.P("// Ignoring public import of ", sn, " from ", filename) |
| 869 | g.P() |
| 870 | return |
| 871 | } |
| 872 | } |
| 873 | g.P("// ", sn, " from public import ", filename) |
| 874 | g.usedPackages[df.PackageName()] = true |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 875 | |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 876 | for _, sym := range df.exported[id.o] { |
| 877 | sym.GenerateAlias(g, df.PackageName()) |
| 878 | } |
| 879 | |
| 880 | g.P() |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 881 | } |
| 882 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 883 | // Generate the enum definitions for this EnumDescriptor. |
| 884 | func (g *Generator) generateEnum(enum *EnumDescriptor) { |
| 885 | // The full type name |
| 886 | typeName := enum.TypeName() |
| 887 | // The full type name, CamelCased. |
| 888 | ccTypeName := CamelCaseSlice(typeName) |
| 889 | ccPrefix := enum.prefix() |
| 890 | g.P("type ", ccTypeName, " int32") |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 891 | g.file.addExport(enum, enumSymbol(ccTypeName)) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 892 | g.P("const (") |
| 893 | g.In() |
| 894 | for _, e := range enum.Value { |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 895 | name := ccPrefix + *e.Name |
| 896 | g.P(name, " = ", e.Number) |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 897 | g.file.addExport(enum, constOrVarSymbol{name, "const"}) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 898 | } |
| 899 | g.Out() |
| 900 | g.P(")") |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 901 | g.P("var ", ccTypeName, "_name = map[int32]string{") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 902 | g.In() |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 903 | generated := make(map[int32]bool) // avoid duplicate values |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 904 | for _, e := range enum.Value { |
| 905 | duplicate := "" |
| 906 | if _, present := generated[*e.Number]; present { |
| 907 | duplicate = "// Duplicate value: " |
| 908 | } |
| 909 | g.P(duplicate, e.Number, ": ", Quote(*e.Name), ",") |
| 910 | generated[*e.Number] = true |
| 911 | } |
| 912 | g.Out() |
| 913 | g.P("}") |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 914 | g.P("var ", ccTypeName, "_value = map[string]int32{") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 915 | g.In() |
| 916 | for _, e := range enum.Value { |
| 917 | g.P(Quote(*e.Name), ": ", e.Number, ",") |
| 918 | } |
| 919 | g.Out() |
| 920 | g.P("}") |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 921 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 922 | g.P("func New", ccTypeName, "(x int32) *", ccTypeName, " {") |
| 923 | g.In() |
| 924 | g.P("e := ", ccTypeName, "(x)") |
| 925 | g.P("return &e") |
| 926 | g.Out() |
| 927 | g.P("}") |
| David Symonds | 940b961 | 2011-04-01 10:45:23 +1100 | [diff] [blame] | 928 | |
| 929 | g.P("func (x ", ccTypeName, ") String() string {") |
| 930 | g.In() |
| 931 | g.P("return ", g.ProtoPkg, ".EnumName(", ccTypeName, "_name, int32(x))") |
| 932 | g.Out() |
| 933 | g.P("}") |
| 934 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 935 | g.P() |
| 936 | } |
| 937 | |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 938 | // The tag is a string like "varint,2,opt,name=fieldname,def=7" that |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 939 | // identifies details of the field for the protocol buffer marshaling and unmarshaling |
| 940 | // code. The fields are: |
| 941 | // wire encoding |
| 942 | // protocol tag number |
| 943 | // opt,req,rep for optional, required, or repeated |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 944 | // packed whether the encoding is "packed" (optional; repeated primitives only) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 945 | // name= the original declared name |
| 946 | // enum= the name of the enum type if it is an enum-typed field. |
| 947 | // def= string representation of the default value, if any. |
| 948 | // The default value must be in a representation that can be used at run-time |
| 949 | // to generate the default value. Thus bools become 0 and 1, for instance. |
| 950 | func (g *Generator) goTag(field *descriptor.FieldDescriptorProto, wiretype string) string { |
| 951 | optrepreq := "" |
| 952 | switch { |
| 953 | case isOptional(field): |
| 954 | optrepreq = "opt" |
| 955 | case isRequired(field): |
| 956 | optrepreq = "req" |
| 957 | case isRepeated(field): |
| 958 | optrepreq = "rep" |
| 959 | } |
| 960 | defaultValue := proto.GetString(field.DefaultValue) |
| 961 | if defaultValue != "" { |
| 962 | switch *field.Type { |
| 963 | case descriptor.FieldDescriptorProto_TYPE_BOOL: |
| 964 | if defaultValue == "true" { |
| 965 | defaultValue = "1" |
| 966 | } else { |
| 967 | defaultValue = "0" |
| 968 | } |
| 969 | case descriptor.FieldDescriptorProto_TYPE_STRING, |
| 970 | descriptor.FieldDescriptorProto_TYPE_BYTES: |
| 971 | // Protect frogs. |
| 972 | defaultValue = Quote(defaultValue) |
| 973 | // Don't need the quotes |
| 974 | defaultValue = defaultValue[1 : len(defaultValue)-1] |
| 975 | case descriptor.FieldDescriptorProto_TYPE_ENUM: |
| 976 | // For enums we need to provide the integer constant. |
| 977 | obj := g.ObjectNamed(proto.GetString(field.TypeName)) |
| 978 | enum, ok := obj.(*EnumDescriptor) |
| 979 | if !ok { |
| 980 | g.Fail("enum type inconsistent for", CamelCaseSlice(obj.TypeName())) |
| 981 | } |
| 982 | defaultValue = enum.integerValueAsString(defaultValue) |
| 983 | } |
| 984 | defaultValue = ",def=" + defaultValue |
| 985 | } |
| 986 | enum := "" |
| 987 | if *field.Type == descriptor.FieldDescriptorProto_TYPE_ENUM { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 988 | // We avoid using obj.PackageName(), because we want to use the |
| 989 | // original (proto-world) package name. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 990 | obj := g.ObjectNamed(proto.GetString(field.TypeName)) |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 991 | enum = ",enum=" |
| 992 | if pkg := proto.GetString(obj.File().Package); pkg != "" { |
| 993 | enum += pkg + "." |
| 994 | } |
| 995 | enum += CamelCaseSlice(obj.TypeName()) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 996 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 997 | packed := "" |
| 998 | if field.Options != nil && proto.GetBool(field.Options.Packed) { |
| 999 | packed = ",packed" |
| 1000 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1001 | fieldName := proto.GetString(field.Name) |
| 1002 | name := fieldName |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 1003 | if *field.Type == descriptor.FieldDescriptorProto_TYPE_GROUP { |
| 1004 | // We must use the type name for groups instead of |
| 1005 | // the field name to preserve capitalization. |
| 1006 | // type_name in FieldDescriptorProto is fully-qualified, |
| 1007 | // but we only want the local part. |
| 1008 | name = *field.TypeName |
| 1009 | if i := strings.LastIndex(name, "."); i >= 0 { |
| 1010 | name = name[i+1:] |
| 1011 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1012 | } |
| 1013 | if name == CamelCase(fieldName) { |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1014 | name = "" |
| 1015 | } else { |
| 1016 | name = ",name=" + name |
| 1017 | } |
| David Symonds | 8935abf | 2011-07-04 15:53:16 +1000 | [diff] [blame] | 1018 | return Quote(fmt.Sprintf("%s,%d,%s%s%s%s%s", |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1019 | wiretype, |
| 1020 | proto.GetInt32(field.Number), |
| 1021 | optrepreq, |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1022 | packed, |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1023 | name, |
| 1024 | enum, |
| 1025 | defaultValue)) |
| 1026 | } |
| 1027 | |
| 1028 | func needsStar(typ descriptor.FieldDescriptorProto_Type) bool { |
| 1029 | switch typ { |
| 1030 | case descriptor.FieldDescriptorProto_TYPE_GROUP: |
| 1031 | return false |
| 1032 | case descriptor.FieldDescriptorProto_TYPE_MESSAGE: |
| 1033 | return false |
| 1034 | case descriptor.FieldDescriptorProto_TYPE_BYTES: |
| 1035 | return false |
| 1036 | } |
| 1037 | return true |
| 1038 | } |
| 1039 | |
| 1040 | // TypeName is the printed name appropriate for an item. If the object is in the current file, |
| 1041 | // TypeName drops the package name and underscores the rest. |
| David Symonds | 7d5c824 | 2011-03-14 12:03:50 -0700 | [diff] [blame] | 1042 | // Otherwise the object is from another package; and the result is the underscored |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1043 | // package name followed by the item name. |
| 1044 | // The result always has an initial capital. |
| 1045 | func (g *Generator) TypeName(obj Object) string { |
| 1046 | return g.DefaultPackageName(obj) + CamelCaseSlice(obj.TypeName()) |
| 1047 | } |
| 1048 | |
| 1049 | // TypeNameWithPackage is like TypeName, but always includes the package |
| 1050 | // name even if the object is in our own package. |
| 1051 | func (g *Generator) TypeNameWithPackage(obj Object) string { |
| 1052 | return obj.PackageName() + CamelCaseSlice(obj.TypeName()) |
| 1053 | } |
| 1054 | |
| 1055 | // GoType returns a string representing the type name, and the wire type |
| 1056 | func (g *Generator) GoType(message *Descriptor, field *descriptor.FieldDescriptorProto) (typ string, wire string) { |
| 1057 | // TODO: Options. |
| 1058 | switch *field.Type { |
| 1059 | case descriptor.FieldDescriptorProto_TYPE_DOUBLE: |
| 1060 | typ, wire = "float64", "fixed64" |
| 1061 | case descriptor.FieldDescriptorProto_TYPE_FLOAT: |
| 1062 | typ, wire = "float32", "fixed32" |
| 1063 | case descriptor.FieldDescriptorProto_TYPE_INT64: |
| 1064 | typ, wire = "int64", "varint" |
| 1065 | case descriptor.FieldDescriptorProto_TYPE_UINT64: |
| 1066 | typ, wire = "uint64", "varint" |
| 1067 | case descriptor.FieldDescriptorProto_TYPE_INT32: |
| 1068 | typ, wire = "int32", "varint" |
| 1069 | case descriptor.FieldDescriptorProto_TYPE_UINT32: |
| 1070 | typ, wire = "uint32", "varint" |
| 1071 | case descriptor.FieldDescriptorProto_TYPE_FIXED64: |
| 1072 | typ, wire = "uint64", "fixed64" |
| 1073 | case descriptor.FieldDescriptorProto_TYPE_FIXED32: |
| 1074 | typ, wire = "uint32", "fixed32" |
| 1075 | case descriptor.FieldDescriptorProto_TYPE_BOOL: |
| 1076 | typ, wire = "bool", "varint" |
| 1077 | case descriptor.FieldDescriptorProto_TYPE_STRING: |
| 1078 | typ, wire = "string", "bytes" |
| 1079 | case descriptor.FieldDescriptorProto_TYPE_GROUP: |
| 1080 | desc := g.ObjectNamed(proto.GetString(field.TypeName)) |
| 1081 | typ, wire = "*"+g.TypeName(desc), "group" |
| 1082 | case descriptor.FieldDescriptorProto_TYPE_MESSAGE: |
| 1083 | desc := g.ObjectNamed(proto.GetString(field.TypeName)) |
| 1084 | typ, wire = "*"+g.TypeName(desc), "bytes" |
| 1085 | case descriptor.FieldDescriptorProto_TYPE_BYTES: |
| 1086 | typ, wire = "[]byte", "bytes" |
| 1087 | case descriptor.FieldDescriptorProto_TYPE_ENUM: |
| 1088 | desc := g.ObjectNamed(proto.GetString(field.TypeName)) |
| 1089 | typ, wire = g.TypeName(desc), "varint" |
| 1090 | case descriptor.FieldDescriptorProto_TYPE_SFIXED32: |
| 1091 | typ, wire = "int32", "fixed32" |
| 1092 | case descriptor.FieldDescriptorProto_TYPE_SFIXED64: |
| 1093 | typ, wire = "int64", "fixed64" |
| 1094 | case descriptor.FieldDescriptorProto_TYPE_SINT32: |
| 1095 | typ, wire = "int32", "zigzag32" |
| 1096 | case descriptor.FieldDescriptorProto_TYPE_SINT64: |
| 1097 | typ, wire = "int64", "zigzag64" |
| 1098 | default: |
| 1099 | g.Fail("unknown type for", proto.GetString(field.Name)) |
| 1100 | } |
| 1101 | if isRepeated(field) { |
| 1102 | typ = "[]" + typ |
| 1103 | } else if needsStar(*field.Type) { |
| 1104 | typ = "*" + typ |
| 1105 | } |
| 1106 | return |
| 1107 | } |
| 1108 | |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 1109 | func (g *Generator) RecordTypeUse(t string) { |
| 1110 | if obj, ok := g.typeNameToObject[t]; ok { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 1111 | // Call ObjectNamed to get the true object to record the use. |
| 1112 | obj = g.ObjectNamed(t) |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 1113 | g.usedPackages[obj.PackageName()] = true |
| 1114 | } |
| 1115 | } |
| 1116 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1117 | // Generate the type and default constant definitions for this Descriptor. |
| 1118 | func (g *Generator) generateMessage(message *Descriptor) { |
| 1119 | // The full type name |
| 1120 | typeName := message.TypeName() |
| 1121 | // The full type name, CamelCased. |
| 1122 | ccTypeName := CamelCaseSlice(typeName) |
| 1123 | |
| 1124 | g.P("type ", ccTypeName, " struct {") |
| 1125 | g.In() |
| 1126 | for _, field := range message.Field { |
| 1127 | fieldname := CamelCase(*field.Name) |
| 1128 | typename, wiretype := g.GoType(message, field) |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 1129 | jsonName := *field.Name |
| 1130 | tag := fmt.Sprintf("`protobuf:%s json:%q`", g.goTag(field, wiretype), jsonName) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1131 | g.P(fieldname, "\t", typename, "\t", tag) |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 1132 | g.RecordTypeUse(proto.GetString(field.TypeName)) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1133 | } |
| 1134 | if len(message.ExtensionRange) > 0 { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 1135 | g.P("XXX_extensions\t\tmap[int32]", g.ProtoPkg, ".Extension") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1136 | } |
| 1137 | g.P("XXX_unrecognized\t[]byte") |
| 1138 | g.Out() |
| 1139 | g.P("}") |
| 1140 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1141 | // Reset and String functions |
| 1142 | g.P("func (this *", ccTypeName, ") Reset() { *this = ", ccTypeName, "{} }") |
| 1143 | g.P("func (this *", ccTypeName, ") String() string { return ", g.ProtoPkg, ".CompactTextString(this) }") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1144 | |
| 1145 | // Extension support methods |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 1146 | var hasExtensions, isMessageSet bool |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1147 | if len(message.ExtensionRange) > 0 { |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 1148 | hasExtensions = true |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 1149 | // message_set_wire_format only makes sense when extensions are defined. |
| 1150 | if opts := message.Options; opts != nil && proto.GetBool(opts.MessageSetWireFormat) { |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 1151 | isMessageSet = true |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 1152 | g.P() |
| 1153 | g.P("func (this *", ccTypeName, ") Marshal() ([]byte, os.Error) {") |
| 1154 | g.In() |
| 1155 | g.P("return ", g.ProtoPkg, ".MarshalMessageSet(this.ExtensionMap())") |
| 1156 | g.Out() |
| 1157 | g.P("}") |
| 1158 | g.P("func (this *", ccTypeName, ") Unmarshal(buf []byte) os.Error {") |
| 1159 | g.In() |
| 1160 | g.P("return ", g.ProtoPkg, ".UnmarshalMessageSet(buf, this.ExtensionMap())") |
| 1161 | g.Out() |
| 1162 | g.P("}") |
| 1163 | g.P("// ensure ", ccTypeName, " satisfies proto.Marshaler and proto.Unmarshaler") |
| 1164 | g.P("var _ ", g.ProtoPkg, ".Marshaler = (*", ccTypeName, ")(nil)") |
| 1165 | g.P("var _ ", g.ProtoPkg, ".Unmarshaler = (*", ccTypeName, ")(nil)") |
| 1166 | } |
| 1167 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1168 | g.P() |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 1169 | g.P("var extRange_", ccTypeName, " = []", g.ProtoPkg, ".ExtensionRange{") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1170 | g.In() |
| 1171 | for _, r := range message.ExtensionRange { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 1172 | end := fmt.Sprint(*r.End - 1) // make range inclusive on both ends |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 1173 | g.P("{", r.Start, ", ", end, "},") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1174 | } |
| 1175 | g.Out() |
| 1176 | g.P("}") |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 1177 | g.P("func (*", ccTypeName, ") ExtensionRangeArray() []", g.ProtoPkg, ".ExtensionRange {") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1178 | g.In() |
| 1179 | g.P("return extRange_", ccTypeName) |
| 1180 | g.Out() |
| 1181 | g.P("}") |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 1182 | g.P("func (this *", ccTypeName, ") ExtensionMap() map[int32]", g.ProtoPkg, ".Extension {") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1183 | g.In() |
| 1184 | g.P("if this.XXX_extensions == nil {") |
| 1185 | g.In() |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 1186 | g.P("this.XXX_extensions = make(map[int32]", g.ProtoPkg, ".Extension)") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1187 | g.Out() |
| 1188 | g.P("}") |
| 1189 | g.P("return this.XXX_extensions") |
| 1190 | g.Out() |
| 1191 | g.P("}") |
| 1192 | } |
| 1193 | |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 1194 | g.file.addExport(message, messageSymbol{ccTypeName, hasExtensions, isMessageSet}) |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 1195 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1196 | // Default constants |
| 1197 | for _, field := range message.Field { |
| 1198 | def := proto.GetString(field.DefaultValue) |
| 1199 | if def == "" { |
| 1200 | continue |
| 1201 | } |
| 1202 | fieldname := "Default_" + ccTypeName + "_" + CamelCase(*field.Name) |
| 1203 | typename, _ := g.GoType(message, field) |
| 1204 | if typename[0] == '*' { |
| 1205 | typename = typename[1:] |
| 1206 | } |
| 1207 | kind := "const " |
| 1208 | switch { |
| 1209 | case typename == "bool": |
| 1210 | case typename == "string": |
| 1211 | def = Quote(def) |
| 1212 | case typename == "[]byte": |
| 1213 | def = "[]byte(" + Quote(def) + ")" |
| 1214 | kind = "var " |
| David Symonds | cea785b | 2011-01-07 11:02:30 +1100 | [diff] [blame] | 1215 | case def == "inf", def == "-inf", def == "nan": |
| 1216 | // These names are known to, and defined by, the protocol language. |
| 1217 | switch def { |
| 1218 | case "inf": |
| 1219 | def = "math.Inf(1)" |
| 1220 | case "-inf": |
| 1221 | def = "math.Inf(-1)" |
| 1222 | case "nan": |
| 1223 | def = "math.NaN()" |
| 1224 | } |
| 1225 | if *field.Type == descriptor.FieldDescriptorProto_TYPE_FLOAT { |
| 1226 | def = "float32(" + def + ")" |
| 1227 | } |
| 1228 | kind = "var " |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1229 | case *field.Type == descriptor.FieldDescriptorProto_TYPE_ENUM: |
| 1230 | // Must be an enum. Need to construct the prefixed name. |
| 1231 | obj := g.ObjectNamed(proto.GetString(field.TypeName)) |
| 1232 | enum, ok := obj.(*EnumDescriptor) |
| 1233 | if !ok { |
| Rob Pike | 5194c51 | 2010-10-14 13:02:16 -0700 | [diff] [blame] | 1234 | log.Println("don't know how to generate constant for", fieldname) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1235 | continue |
| 1236 | } |
| Rob Pike | 87af39e | 2010-07-19 10:48:02 -0700 | [diff] [blame] | 1237 | def = g.DefaultPackageName(enum) + enum.prefix() + def |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1238 | } |
| 1239 | g.P(kind, fieldname, " ", typename, " = ", def) |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 1240 | g.file.addExport(message, constOrVarSymbol{fieldname, kind}) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1241 | } |
| 1242 | g.P() |
| 1243 | |
| 1244 | for _, ext := range message.ext { |
| 1245 | g.generateExtension(ext) |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | func (g *Generator) generateExtension(ext *ExtensionDescriptor) { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1250 | ccTypeName := ext.DescName() |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1251 | |
| 1252 | extendedType := "*" + g.TypeName(g.ObjectNamed(*ext.Extendee)) |
| 1253 | field := ext.FieldDescriptorProto |
| 1254 | fieldType, wireType := g.GoType(ext.parent, field) |
| 1255 | tag := g.goTag(field, wireType) |
| David Symonds | f90e338 | 2010-05-05 10:53:44 +1000 | [diff] [blame] | 1256 | g.RecordTypeUse(*ext.Extendee) |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 1257 | if n := ext.FieldDescriptorProto.TypeName; n != nil { |
| 1258 | // foreign extension type |
| 1259 | g.RecordTypeUse(*n) |
| 1260 | } |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1261 | |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 1262 | g.P("var ", ccTypeName, " = &", g.ProtoPkg, ".ExtensionDesc{") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1263 | g.In() |
| 1264 | g.P("ExtendedType: (", extendedType, ")(nil),") |
| 1265 | g.P("ExtensionType: (", fieldType, ")(nil),") |
| 1266 | g.P("Field: ", field.Number, ",") |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 1267 | g.P(`Name: "`, g.packageName, ".", strings.Join(ext.TypeName(), "."), `",`) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1268 | g.P("Tag: ", tag, ",") |
| 1269 | |
| 1270 | g.Out() |
| 1271 | g.P("}") |
| 1272 | g.P() |
| David Symonds | 31d58a2 | 2011-01-20 18:33:21 +1100 | [diff] [blame] | 1273 | |
| David Symonds | b2a00c8 | 2011-08-04 16:52:58 +1000 | [diff] [blame] | 1274 | g.file.addExport(ext, constOrVarSymbol{ccTypeName, "var"}) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | func (g *Generator) generateInitFunction() { |
| 1278 | g.P("func init() {") |
| 1279 | g.In() |
| 1280 | for _, enum := range g.file.enum { |
| 1281 | g.generateEnumRegistration(enum) |
| 1282 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1283 | for _, d := range g.file.desc { |
| 1284 | for _, ext := range d.ext { |
| 1285 | g.generateExtensionRegistration(ext) |
| 1286 | } |
| 1287 | } |
| 1288 | for _, ext := range g.file.ext { |
| 1289 | g.generateExtensionRegistration(ext) |
| 1290 | } |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1291 | g.Out() |
| 1292 | g.P("}") |
| 1293 | } |
| 1294 | |
| 1295 | func (g *Generator) generateEnumRegistration(enum *EnumDescriptor) { |
| David Symonds | 4decd80 | 2011-08-04 11:27:07 +1000 | [diff] [blame] | 1296 | // // We always print the full (proto-world) package name here. |
| 1297 | pkg := proto.GetString(enum.File().Package) |
| 1298 | if pkg != "" { |
| 1299 | pkg += "." |
| 1300 | } |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1301 | // The full type name |
| 1302 | typeName := enum.TypeName() |
| 1303 | // The full type name, CamelCased. |
| 1304 | ccTypeName := CamelCaseSlice(typeName) |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 1305 | g.P(g.ProtoPkg+".RegisterEnum(", Quote(pkg+ccTypeName), ", ", ccTypeName+"_name, ", ccTypeName+"_value)") |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1308 | func (g *Generator) generateExtensionRegistration(ext *ExtensionDescriptor) { |
| 1309 | g.P(g.ProtoPkg+".RegisterExtension(", ext.DescName(), ")") |
| 1310 | } |
| 1311 | |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1312 | // And now lots of helper functions. |
| 1313 | |
| Rob Pike | 2c7bafc | 2010-06-10 16:07:14 -0700 | [diff] [blame] | 1314 | // Is c an ASCII lower-case letter? |
| 1315 | func isASCIILower(c byte) bool { |
| 1316 | return 'a' <= c && c <= 'z' |
| 1317 | } |
| 1318 | |
| 1319 | // Is c an ASCII digit? |
| 1320 | func isASCIIDigit(c byte) bool { |
| 1321 | return '0' <= c && c <= '9' |
| 1322 | } |
| 1323 | |
| 1324 | // CamelCase returns the CamelCased name. |
| 1325 | // If there is an interior underscore followed by a lower case letter, |
| 1326 | // drop the underscore and convert the letter to upper case. |
| 1327 | // There is a remote possibility of this rewrite causing a name collision, |
| 1328 | // but it's so remote we're prepared to pretend it's nonexistent - since the |
| 1329 | // C++ generator lowercases names, it's extremely unlikely to have two fields |
| 1330 | // with different capitalizations. |
| 1331 | // In short, _my_field_name_2 becomes XMyFieldName2. |
| 1332 | func CamelCase(s string) string { |
| 1333 | if s == "" { |
| 1334 | return "" |
| 1335 | } |
| 1336 | t := make([]byte, 0, 32) |
| Rob Pike | 2c7bafc | 2010-06-10 16:07:14 -0700 | [diff] [blame] | 1337 | i := 0 |
| 1338 | if s[0] == '_' { |
| 1339 | // Need a capital letter; drop the '_'. |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 1340 | t = append(t, 'X') |
| Rob Pike | 2c7bafc | 2010-06-10 16:07:14 -0700 | [diff] [blame] | 1341 | i++ |
| 1342 | } |
| 1343 | // Invariant: if the next letter is lower case, it must be converted |
| 1344 | // to upper case. |
| 1345 | // That is, we process a word at a time, where words are marked by _ or |
| 1346 | // upper case letter. Digits are treated as words. |
| 1347 | for ; i < len(s); i++ { |
| 1348 | c := s[i] |
| Rob Pike | 2c7bafc | 2010-06-10 16:07:14 -0700 | [diff] [blame] | 1349 | if c == '_' && i+1 < len(s) && isASCIILower(s[i+1]) { |
| 1350 | continue // Skip the underscore in s. |
| 1351 | } |
| 1352 | if isASCIIDigit(c) { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 1353 | t = append(t, c) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1354 | continue |
| 1355 | } |
| Rob Pike | 2c7bafc | 2010-06-10 16:07:14 -0700 | [diff] [blame] | 1356 | // Assume we have a letter now - if not, it's a bogus identifier. |
| 1357 | // The next word is a sequence of characters that must start upper case. |
| 1358 | if isASCIILower(c) { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 1359 | c ^= ' ' // Make it a capital letter. |
| Rob Pike | 2c7bafc | 2010-06-10 16:07:14 -0700 | [diff] [blame] | 1360 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 1361 | t = append(t, c) // Guaranteed not lower case. |
| Rob Pike | 2c7bafc | 2010-06-10 16:07:14 -0700 | [diff] [blame] | 1362 | // Accept lower case sequence that follows. |
| 1363 | for i+1 < len(s) && isASCIILower(s[i+1]) { |
| 1364 | i++ |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 1365 | t = append(t, s[i]) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1366 | } |
| 1367 | } |
| Rob Pike | 2c7bafc | 2010-06-10 16:07:14 -0700 | [diff] [blame] | 1368 | return string(t) |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | // CamelCaseSlice is like CamelCase, but the argument is a slice of strings to |
| 1372 | // be joined with "_". |
| 1373 | func CamelCaseSlice(elem []string) string { return CamelCase(strings.Join(elem, "_")) } |
| 1374 | |
| 1375 | // dottedSlice turns a sliced name into a dotted name. |
| 1376 | func dottedSlice(elem []string) string { return strings.Join(elem, ".") } |
| 1377 | |
| 1378 | // Quote returns a Go-source quoted string representation of s. |
| 1379 | func Quote(s string) string { return fmt.Sprintf("%q", s) } |
| 1380 | |
| 1381 | // Given a .proto file name, return the output name for the generated Go program. |
| 1382 | func goFileName(name string) string { |
| Rob Pike | 87af39e | 2010-07-19 10:48:02 -0700 | [diff] [blame] | 1383 | ext := path.Ext(name) |
| 1384 | if ext == ".proto" || ext == ".protodevel" { |
| 1385 | name = name[0 : len(name)-len(ext)] |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1386 | } |
| 1387 | return name + ".pb.go" |
| 1388 | } |
| 1389 | |
| 1390 | // Is this field optional? |
| 1391 | func isOptional(field *descriptor.FieldDescriptorProto) bool { |
| 1392 | return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_OPTIONAL |
| 1393 | } |
| 1394 | |
| 1395 | // Is this field required? |
| 1396 | func isRequired(field *descriptor.FieldDescriptorProto) bool { |
| 1397 | return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_REQUIRED |
| 1398 | } |
| 1399 | |
| 1400 | // Is this field repeated? |
| 1401 | func isRepeated(field *descriptor.FieldDescriptorProto) bool { |
| 1402 | return field.Label != nil && *field.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED |
| 1403 | } |
| 1404 | |
| 1405 | // DotToUnderscore is the mapping function used to generate Go names from package names, |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 1406 | // which can be dotted in the input .proto file. It maps dots to underscores. |
| 1407 | // Because we also get here from package names generated from file names, it also maps |
| 1408 | // minus signs to underscores. |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1409 | func DotToUnderscore(rune int) int { |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 1410 | switch rune { |
| 1411 | case '.', '-': |
| Rob Pike | af82b4e | 2010-04-30 15:19:25 -0700 | [diff] [blame] | 1412 | return '_' |
| 1413 | } |
| 1414 | return rune |
| 1415 | } |
| Rob Pike | c9e7d97 | 2010-06-10 10:30:22 -0700 | [diff] [blame] | 1416 | |
| 1417 | // BaseName returns the last path element of the name, with the last dotted suffix removed. |
| 1418 | func BaseName(name string) string { |
| 1419 | // First, find the last element |
| 1420 | if i := strings.LastIndex(name, "/"); i >= 0 { |
| 1421 | name = name[i+1:] |
| 1422 | } |
| 1423 | // Now drop the suffix |
| 1424 | if i := strings.LastIndex(name, "."); i >= 0 { |
| 1425 | name = name[0:i] |
| 1426 | } |
| 1427 | return name |
| 1428 | } |