enumstringer.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
  2. // http://github.com/gogo/protobuf
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. /*
  27. The enumstringer (experimental) plugin generates a String method for each enum.
  28. It is enabled by the following extensions:
  29. - enum_stringer
  30. - enum_stringer_all
  31. This package is subject to change.
  32. */
  33. package enumstringer
  34. import (
  35. "github.com/gogo/protobuf/gogoproto"
  36. "github.com/gogo/protobuf/protoc-gen-gogo/generator"
  37. )
  38. type enumstringer struct {
  39. *generator.Generator
  40. generator.PluginImports
  41. atleastOne bool
  42. localName string
  43. }
  44. func NewEnumStringer() *enumstringer {
  45. return &enumstringer{}
  46. }
  47. func (p *enumstringer) Name() string {
  48. return "enumstringer"
  49. }
  50. func (p *enumstringer) Init(g *generator.Generator) {
  51. p.Generator = g
  52. }
  53. func (p *enumstringer) Generate(file *generator.FileDescriptor) {
  54. p.PluginImports = generator.NewPluginImports(p.Generator)
  55. p.atleastOne = false
  56. p.localName = generator.FileName(file)
  57. strconvPkg := p.NewImport("strconv")
  58. for _, enum := range file.Enums() {
  59. if !gogoproto.IsEnumStringer(file.FileDescriptorProto, enum.EnumDescriptorProto) {
  60. continue
  61. }
  62. if gogoproto.IsGoEnumStringer(file.FileDescriptorProto, enum.EnumDescriptorProto) {
  63. panic("old enum string method needs to be disabled, please use gogoproto.old_enum_stringer or gogoproto.old_enum_string_all and set it to false")
  64. }
  65. p.atleastOne = true
  66. ccTypeName := generator.CamelCaseSlice(enum.TypeName())
  67. p.P("func (x ", ccTypeName, ") String() string {")
  68. p.In()
  69. p.P(`s, ok := `, ccTypeName, `_name[int32(x)]`)
  70. p.P(`if ok {`)
  71. p.In()
  72. p.P(`return s`)
  73. p.Out()
  74. p.P(`}`)
  75. p.P(`return `, strconvPkg.Use(), `.Itoa(int(x))`)
  76. p.Out()
  77. p.P(`}`)
  78. }
  79. if !p.atleastOne {
  80. return
  81. }
  82. }
  83. func init() {
  84. generator.RegisterPlugin(NewEnumStringer())
  85. }