doc.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  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. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. /*
  29. Package gogoproto provides extensions for protocol buffers to achieve:
  30. - fast marshalling and unmarshalling.
  31. - peace of mind by optionally generating test and benchmark code.
  32. - more canonical Go structures.
  33. - less typing by optionally generating extra helper code.
  34. - goprotobuf compatibility
  35. More Canonical Go Structures
  36. A lot of time working with a goprotobuf struct will lead you to a place where you create another struct that is easier to work with and then have a function to copy the values between the two structs.
  37. You might also find that basic structs that started their life as part of an API need to be sent over the wire. With gob, you could just send it. With goprotobuf, you need to make a parallel struct.
  38. Gogoprotobuf tries to fix these problems with the nullable, embed, customtype and customname field extensions.
  39. - nullable, if false, a field is generated without a pointer (see warning below).
  40. - embed, if true, the field is generated as an embedded field.
  41. - customtype, It works with the Marshal and Unmarshal methods, to allow you to have your own types in your struct, but marshal to bytes. For example, custom.Uuid or custom.Fixed128
  42. - customname (beta), Changes the generated fieldname. This is especially useful when generated methods conflict with fieldnames.
  43. - casttype (beta), Changes the generated fieldtype. All generated code assumes that this type is castable to the protocol buffer field type. It does not work for structs or enums.
  44. - castkey (beta), Changes the generated fieldtype for a map key. All generated code assumes that this type is castable to the protocol buffer field type. Only supported on maps.
  45. - castvalue (beta), Changes the generated fieldtype for a map value. All generated code assumes that this type is castable to the protocol buffer field type. Only supported on maps.
  46. Warning about nullable: According to the Protocol Buffer specification, you should be able to tell whether a field is set or unset. With the option nullable=false this feature is lost, since your non-nullable fields will always be set. It can be seen as a layer on top of Protocol Buffers, where before and after marshalling all non-nullable fields are set and they cannot be unset.
  47. Let us look at:
  48. github.com/gogo/protobuf/test/example/example.proto
  49. for a quicker overview.
  50. The following message:
  51. package test;
  52. import "github.com/gogo/protobuf/gogoproto/gogo.proto";
  53. message A {
  54. optional string Description = 1 [(gogoproto.nullable) = false];
  55. optional int64 Number = 2 [(gogoproto.nullable) = false];
  56. optional bytes Id = 3 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uuid", (gogoproto.nullable) = false];
  57. }
  58. Will generate a go struct which looks a lot like this:
  59. type A struct {
  60. Description string
  61. Number int64
  62. Id github_com_gogo_protobuf_test_custom.Uuid
  63. }
  64. You will see there are no pointers, since all fields are non-nullable.
  65. You will also see a custom type which marshals to a string.
  66. Be warned it is your responsibility to test your custom types thoroughly.
  67. You should think of every possible empty and nil case for your marshaling, unmarshaling and size methods.
  68. Next we will embed the message A in message B.
  69. message B {
  70. optional A A = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
  71. repeated bytes G = 2 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable) = false];
  72. }
  73. See below that A is embedded in B.
  74. type B struct {
  75. A
  76. G []github_com_gogo_protobuf_test_custom.Uint128
  77. }
  78. Also see the repeated custom type.
  79. type Uint128 [2]uint64
  80. Next we will create a custom name for one of our fields.
  81. message C {
  82. optional int64 size = 1 [(gogoproto.customname) = "MySize"];
  83. }
  84. See below that the field's name is MySize and not Size.
  85. type C struct {
  86. MySize *int64
  87. }
  88. The is useful when having a protocol buffer message with a field name which conflicts with a generated method.
  89. As an example, having a field name size and using the sizer plugin to generate a Size method will cause a go compiler error.
  90. Using customname you can fix this error without changing the field name.
  91. This is typically useful when working with a protocol buffer that was designed before these methods and/or the go language were avialable.
  92. Gogoprotobuf also has some more subtle changes, these could be changed back:
  93. - the generated package name for imports do not have the extra /filename.pb,
  94. but are actually the imports specified in the .proto file.
  95. Gogoprotobuf also has lost some features which should be brought back with time:
  96. - Marshalling and unmarshalling with reflect and without the unsafe package,
  97. this requires work in pointer_reflect.go
  98. Why does nullable break protocol buffer specifications:
  99. The protocol buffer specification states, somewhere, that you should be able to tell whether a
  100. field is set or unset. With the option nullable=false this feature is lost,
  101. since your non-nullable fields will always be set. It can be seen as a layer on top of
  102. protocol buffers, where before and after marshalling all non-nullable fields are set
  103. and they cannot be unset.
  104. Goprotobuf Compatibility:
  105. Gogoprotobuf is compatible with Goprotobuf, because it is compatible with protocol buffers.
  106. Gogoprotobuf generates the same code as goprotobuf if no extensions are used.
  107. The enumprefix, getters and stringer extensions can be used to remove some of the unnecessary code generated by goprotobuf:
  108. - gogoproto_import, if false, the generated code imports github.com/golang/protobuf/proto instead of github.com/gogo/protobuf/proto.
  109. - goproto_enum_prefix, if false, generates the enum constant names without the messagetype prefix
  110. - goproto_enum_stringer (experimental), if false, the enum is generated without the default string method, this is useful for rather using enum_stringer, or allowing you to write your own string method.
  111. - goproto_getters, if false, the message is generated without get methods, this is useful when you would rather want to use face
  112. - goproto_stringer, if false, the message is generated without the default string method, this is useful for rather using stringer, or allowing you to write your own string method.
  113. - goproto_extensions_map (beta), if false, the extensions field is generated as type []byte instead of type map[int32]proto.Extension
  114. - goproto_unrecognized (beta), if false, XXX_unrecognized field is not generated. This is useful in conjunction with gogoproto.nullable=false, to generate structures completely devoid of pointers and reduce GC pressure at the cost of losing information about unrecognized fields.
  115. - goproto_registration (beta), if true, the generated files will register all messages and types against both gogo/protobuf and golang/protobuf. This is necessary when using third-party packages which read registrations from golang/protobuf (such as the grpc-gateway).
  116. Less Typing and Peace of Mind is explained in their specific plugin folders godoc:
  117. - github.com/gogo/protobuf/plugin/<extension_name>
  118. If you do not use any of these extension the code that is generated
  119. will be the same as if goprotobuf has generated it.
  120. The most complete way to see examples is to look at
  121. github.com/gogo/protobuf/test/thetest.proto
  122. Gogoprototest is a seperate project,
  123. because we want to keep gogoprotobuf independent of goprotobuf,
  124. but we still want to test it thoroughly.
  125. */
  126. package gogoproto