utils.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2020 Google LLC. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package generator
  16. import (
  17. "strings"
  18. "google.golang.org/protobuf/reflect/protoreflect"
  19. )
  20. // contains returns true if an array contains a specified string.
  21. func contains(s []string, e string) bool {
  22. for _, a := range s {
  23. if a == e {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. // appendUnique appends a string, to a string slice, if the string is not already in the slice
  30. func appendUnique(s []string, e string) []string {
  31. if !contains(s, e) {
  32. return append(s, e)
  33. }
  34. return s
  35. }
  36. // singular produces the singular form of a collection name.
  37. func singular(plural string) string {
  38. if strings.HasSuffix(plural, "ves") {
  39. return strings.TrimSuffix(plural, "ves") + "f"
  40. }
  41. if strings.HasSuffix(plural, "ies") {
  42. return strings.TrimSuffix(plural, "ies") + "y"
  43. }
  44. if strings.HasSuffix(plural, "s") {
  45. return strings.TrimSuffix(plural, "s")
  46. }
  47. return plural
  48. }
  49. func getValueKind(message protoreflect.MessageDescriptor) string {
  50. valueField := getValueField(message)
  51. return valueField.Kind().String()
  52. }
  53. func getValueField(message protoreflect.MessageDescriptor) protoreflect.FieldDescriptor {
  54. fields := message.Fields()
  55. return fields.ByName("value")
  56. }