protoc_plugin.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (C) 2020 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //+build ignore
  7. package main
  8. import (
  9. "fmt"
  10. "path/filepath"
  11. "strings"
  12. "unicode"
  13. "github.com/syncthing/syncthing/proto/ext"
  14. "github.com/gogo/protobuf/gogoproto"
  15. "github.com/gogo/protobuf/proto"
  16. "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
  17. "github.com/gogo/protobuf/vanity"
  18. "github.com/gogo/protobuf/vanity/command"
  19. )
  20. func main() {
  21. req := command.Read()
  22. files := req.GetProtoFile()
  23. files = vanity.FilterFiles(files, vanity.NotGoogleProtobufDescriptorProto)
  24. vanity.ForEachFile(files, vanity.TurnOffGoGettersAll)
  25. vanity.ForEachFile(files, TurnOnProtoSizerAll)
  26. vanity.ForEachFile(files, vanity.TurnOffGoEnumPrefixAll)
  27. vanity.ForEachFile(files, vanity.TurnOffGoUnrecognizedAll)
  28. vanity.ForEachFile(files, vanity.TurnOffGoUnkeyedAll)
  29. vanity.ForEachFile(files, vanity.TurnOffGoSizecacheAll)
  30. vanity.ForEachFile(files, vanity.TurnOffGoEnumStringerAll)
  31. vanity.ForEachEnumInFiles(files, HandleCustomEnumExtensions)
  32. vanity.ForEachFile(files, SetPackagePrefix("github.com/syncthing/syncthing"))
  33. vanity.ForEachMessageInFiles(files, HandleCustomExtensions)
  34. vanity.ForEachFieldInFilesExcludingExtensions(files, TurnOffNullableForMessages)
  35. resp := command.Generate(req)
  36. command.Write(resp)
  37. }
  38. func TurnOnProtoSizerAll(file *descriptor.FileDescriptorProto) {
  39. vanity.SetBoolFileOption(gogoproto.E_ProtosizerAll, true)(file)
  40. }
  41. func TurnOffNullableForMessages(field *descriptor.FieldDescriptorProto) {
  42. if !vanity.FieldHasBoolExtension(field, gogoproto.E_Nullable) {
  43. _, hasCustomType := GetFieldStringExtension(field, gogoproto.E_Customtype)
  44. if field.IsMessage() || hasCustomType {
  45. vanity.SetBoolFieldOption(gogoproto.E_Nullable, false)(field)
  46. }
  47. }
  48. }
  49. func HandleCustomEnumExtensions(enum *descriptor.EnumDescriptorProto) {
  50. for _, field := range enum.Value {
  51. if field == nil {
  52. continue
  53. }
  54. if field.Options == nil {
  55. field.Options = &descriptor.EnumValueOptions{}
  56. }
  57. customName := gogoproto.GetEnumValueCustomName(field)
  58. if customName != "" {
  59. continue
  60. }
  61. if v, ok := GetEnumValueStringExtension(field, ext.E_Enumgoname); ok {
  62. SetEnumValueStringFieldOption(field, gogoproto.E_EnumvalueCustomname, v)
  63. } else {
  64. SetEnumValueStringFieldOption(field, gogoproto.E_EnumvalueCustomname, toCamelCase(*field.Name, true))
  65. }
  66. }
  67. }
  68. func SetPackagePrefix(prefix string) func(file *descriptor.FileDescriptorProto) {
  69. return func(file *descriptor.FileDescriptorProto) {
  70. if file.Options.GoPackage == nil {
  71. pkg, _ := filepath.Split(file.GetName())
  72. fullPkg := prefix + "/" + strings.TrimSuffix(pkg, "/")
  73. file.Options.GoPackage = &fullPkg
  74. }
  75. }
  76. }
  77. func toCamelCase(input string, firstUpper bool) string {
  78. runes := []rune(strings.ToLower(input))
  79. outputRunes := make([]rune, 0, len(runes))
  80. nextUpper := false
  81. for i, rune := range runes {
  82. if rune == '_' {
  83. nextUpper = true
  84. continue
  85. }
  86. if (firstUpper && i == 0) || nextUpper {
  87. rune = unicode.ToUpper(rune)
  88. nextUpper = false
  89. }
  90. outputRunes = append(outputRunes, rune)
  91. }
  92. return string(outputRunes)
  93. }
  94. func SetStringFieldOption(field *descriptor.FieldDescriptorProto, extension *proto.ExtensionDesc, value string) {
  95. if _, ok := GetFieldStringExtension(field, extension); ok {
  96. return
  97. }
  98. if field.Options == nil {
  99. field.Options = &descriptor.FieldOptions{}
  100. }
  101. if err := proto.SetExtension(field.Options, extension, &value); err != nil {
  102. panic(err)
  103. }
  104. }
  105. func SetEnumValueStringFieldOption(field *descriptor.EnumValueDescriptorProto, extension *proto.ExtensionDesc, value string) {
  106. if _, ok := GetEnumValueStringExtension(field, extension); ok {
  107. return
  108. }
  109. if field.Options == nil {
  110. field.Options = &descriptor.EnumValueOptions{}
  111. }
  112. if err := proto.SetExtension(field.Options, extension, &value); err != nil {
  113. panic(err)
  114. }
  115. }
  116. func GetEnumValueStringExtension(enumValue *descriptor.EnumValueDescriptorProto, extension *proto.ExtensionDesc) (string, bool) {
  117. if enumValue.Options == nil {
  118. return "", false
  119. }
  120. value, err := proto.GetExtension(enumValue.Options, extension)
  121. if err != nil {
  122. return "", false
  123. }
  124. if value == nil {
  125. return "", false
  126. }
  127. if v, ok := value.(*string); !ok || v == nil {
  128. return "", false
  129. } else {
  130. return *v, true
  131. }
  132. }
  133. func GetFieldStringExtension(field *descriptor.FieldDescriptorProto, extension *proto.ExtensionDesc) (string, bool) {
  134. if field.Options == nil {
  135. return "", false
  136. }
  137. value, err := proto.GetExtension(field.Options, extension)
  138. if err != nil {
  139. return "", false
  140. }
  141. if value == nil {
  142. return "", false
  143. }
  144. if v, ok := value.(*string); !ok || v == nil {
  145. return "", false
  146. } else {
  147. return *v, true
  148. }
  149. }
  150. func GetFieldBooleanExtension(field *descriptor.FieldDescriptorProto, extension *proto.ExtensionDesc) (bool, bool) {
  151. if field.Options == nil {
  152. return false, false
  153. }
  154. value, err := proto.GetExtension(field.Options, extension)
  155. if err != nil {
  156. return false, false
  157. }
  158. if value == nil {
  159. return false, false
  160. }
  161. if v, ok := value.(*bool); !ok || v == nil {
  162. return false, false
  163. } else {
  164. return *v, true
  165. }
  166. }
  167. func GetMessageBoolExtension(msg *descriptor.DescriptorProto, extension *proto.ExtensionDesc) (bool, bool) {
  168. if msg.Options == nil {
  169. return false, false
  170. }
  171. value, err := proto.GetExtension(msg.Options, extension)
  172. if err != nil {
  173. return false, false
  174. }
  175. if value == nil {
  176. return false, false
  177. }
  178. val, ok := value.(*bool)
  179. if !ok || val == nil {
  180. return false, false
  181. }
  182. return *val, true
  183. }
  184. func HandleCustomExtensions(msg *descriptor.DescriptorProto) {
  185. generateXmlTags := true
  186. if generate, ok := GetMessageBoolExtension(msg, ext.E_XmlTags); ok {
  187. generateXmlTags = generate
  188. }
  189. vanity.ForEachField([]*descriptor.DescriptorProto{msg}, func(field *descriptor.FieldDescriptorProto) {
  190. if field.Options == nil {
  191. field.Options = &descriptor.FieldOptions{}
  192. }
  193. deprecated := field.Options.Deprecated != nil && *field.Options.Deprecated == true
  194. if field.Type != nil && *field.Type == descriptor.FieldDescriptorProto_TYPE_INT32 {
  195. SetStringFieldOption(field, gogoproto.E_Casttype, "int")
  196. }
  197. if field.TypeName != nil && *field.TypeName == ".google.protobuf.Timestamp" {
  198. vanity.SetBoolFieldOption(gogoproto.E_Stdtime, true)(field)
  199. }
  200. if goName, ok := GetFieldStringExtension(field, ext.E_Goname); ok {
  201. SetStringFieldOption(field, gogoproto.E_Customname, goName)
  202. } else if deprecated {
  203. SetStringFieldOption(field, gogoproto.E_Customname, "Deprecated"+toCamelCase(*field.Name, true))
  204. }
  205. if val, ok := GetFieldBooleanExtension(field, ext.E_DeviceId); ok && val {
  206. SetStringFieldOption(field, gogoproto.E_Customtype, "github.com/syncthing/syncthing/lib/protocol.DeviceID")
  207. }
  208. if jsonValue, ok := GetFieldStringExtension(field, ext.E_Json); ok {
  209. SetStringFieldOption(field, gogoproto.E_Jsontag, jsonValue)
  210. } else if deprecated {
  211. SetStringFieldOption(field, gogoproto.E_Jsontag, "-")
  212. } else {
  213. SetStringFieldOption(field, gogoproto.E_Jsontag, toCamelCase(*field.Name, false))
  214. }
  215. current := ""
  216. if v, ok := GetFieldStringExtension(field, gogoproto.E_Moretags); ok {
  217. current = v
  218. }
  219. if generateXmlTags {
  220. if len(current) > 0 {
  221. current += " "
  222. }
  223. if xmlValue, ok := GetFieldStringExtension(field, ext.E_Xml); ok {
  224. current += fmt.Sprintf(`xml:"%s"`, xmlValue)
  225. } else {
  226. xmlValue = toCamelCase(*field.Name, false)
  227. // XML dictates element name within the collection, not collection name, so trim plural suffix.
  228. if field.IsRepeated() {
  229. if strings.HasSuffix(xmlValue, "ses") {
  230. // addresses -> address
  231. xmlValue = strings.TrimSuffix(xmlValue, "es")
  232. } else {
  233. // devices -> device
  234. xmlValue = strings.TrimSuffix(xmlValue, "s")
  235. }
  236. }
  237. if deprecated {
  238. xmlValue += ",omitempty"
  239. }
  240. current += fmt.Sprintf(`xml:"%s"`, xmlValue)
  241. }
  242. }
  243. if defaultValue, ok := GetFieldStringExtension(field, ext.E_Default); ok {
  244. if len(current) > 0 {
  245. current += " "
  246. }
  247. current += fmt.Sprintf(`default:"%s"`, defaultValue)
  248. }
  249. if restartValue, ok := GetFieldBooleanExtension(field, ext.E_Restart); ok {
  250. if len(current) > 0 {
  251. current += " "
  252. }
  253. current += fmt.Sprintf(`restart:"%t"`, restartValue)
  254. }
  255. SetStringFieldOption(field, gogoproto.E_Moretags, current)
  256. })
  257. }