msg.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package cc
  2. const declTpl = `
  3. {{ if not (ignored .) -}}
  4. extern bool Validate(const {{ class . }}& m, pgv::ValidationMsg* err);
  5. {{- end -}}
  6. `
  7. const msgTpl = `
  8. {{ if not (ignored .) -}}
  9. {{ if disabled . -}}
  10. {{ cmt "Validate is disabled for " (class .) ". This method will always return true." }}
  11. {{- else -}}
  12. {{ cmt "Validate checks the field values on " (class .) " with the rules defined in the proto definition for this message. If any rules are violated, the return value is false and an error message is written to the input string argument." }}
  13. {{- end -}}
  14. {{ range .Fields }}{{ with (context .) }}{{ $f := .Field }}
  15. {{ if has .Rules "In" }}{{ if .Rules.In }}
  16. const std::set<{{ inType .Field .Rules.In }}> {{ lookup .Field "InLookup" }} = {
  17. {{- range .Rules.In }}
  18. {{ inKey $f . }},
  19. {{- end }}
  20. };
  21. {{ end }}{{ end }}
  22. {{ if has .Rules "NotIn" }}{{ if .Rules.NotIn }}
  23. const std::set<{{ inType .Field .Rules.NotIn }}> {{ lookup .Field "NotInLookup" }} = {
  24. {{- range .Rules.NotIn }}
  25. {{ inKey $f . }},
  26. {{- end }}
  27. };
  28. {{ end }}{{ end }}
  29. {{ if has .Rules "Items"}}{{ if .Rules.Items }}
  30. {{ if has .Rules.Items.GetString_ "In" }} {{ if .Rules.Items.GetString_.In }}
  31. const std::set<string> {{ lookup .Field "InLookup" }} = {
  32. {{- range .Rules.Items.GetString_.In }}
  33. {{ inKey $f . }},
  34. {{- end }}
  35. };
  36. {{ end }}{{ end }}
  37. {{ if has .Rules.Items.GetEnum "In" }} {{ if .Rules.Items.GetEnum.In }}
  38. const std::set<{{ inType .Field .Rules.Items.GetEnum.In }}> {{ lookup .Field "InLookup" }} = {
  39. {{- range .Rules.Items.GetEnum.In }}
  40. {{ inKey $f . }},
  41. {{- end }}
  42. };
  43. {{ end }}{{ end }}
  44. {{ end }}{{ end }}
  45. {{ if has .Rules "Items"}}{{ if .Rules.Items }}
  46. {{ if has .Rules.Items.GetString_ "NotIn" }} {{ if .Rules.Items.GetString_.NotIn }}
  47. const std::set<string> {{ lookup .Field "NotInLookup" }} = {
  48. {{- range .Rules.Items.GetString_.NotIn }}
  49. {{ inKey $f . }},
  50. {{- end }}
  51. };
  52. {{ end }}{{ end }}
  53. {{ if has .Rules.Items.GetEnum "NotIn" }} {{ if .Rules.Items.GetEnum.NotIn }}
  54. const std::set<{{ inType .Field .Rules.Items.GetEnum.NotIn }}> {{ lookup .Field "NotInLookup" }} = {
  55. {{- range .Rules.Items.GetEnum.NotIn }}
  56. {{ inKey $f . }},
  57. {{- end }}
  58. };
  59. {{ end }}{{ end }}
  60. {{ end }}{{ end }}
  61. {{ if has .Rules "Pattern"}}{{ if .Rules.Pattern }}
  62. const re2::RE2 {{ lookup .Field "Pattern" }}(re2::StringPiece({{ lit .Rules.GetPattern }},
  63. sizeof({{ lit .Rules.GetPattern }}) - 1));
  64. {{ end }}{{ end }}
  65. {{ if has .Rules "Items"}}{{ if .Rules.Items }}
  66. {{ if has .Rules.Items.GetString_ "Pattern" }} {{ if .Rules.Items.GetString_.Pattern }}
  67. const re2::RE2 {{ lookup .Field "Pattern" }}(re2::StringPiece({{ lit .Rules.Items.GetString_.GetPattern }},
  68. sizeof({{ lit .Rules.Items.GetString_.GetPattern }}) - 1));
  69. {{ end }}{{ end }}
  70. {{ end }}{{ end }}
  71. {{ if has .Rules "Keys"}}{{ if .Rules.Keys }}
  72. {{ if has .Rules.Keys.GetString_ "Pattern" }} {{ if .Rules.Keys.GetString_.Pattern }}
  73. const re2::RE2 {{ lookup .Field "Pattern" }}(re2::StringPiece({{ lit .Rules.Keys.GetString_.GetPattern }},
  74. sizeof({{ lit .Rules.Keys.GetString_.GetPattern }}) - 1));
  75. {{ end }}{{ end }}
  76. {{ end }}{{ end }}
  77. {{ if has .Rules "Values"}}{{ if .Rules.Values }}
  78. {{ if has .Rules.Values.GetString_ "Pattern" }} {{ if .Rules.Values.GetString_.Pattern }}
  79. const re2::RE2 {{ lookup .Field "Pattern" }}(re2::StringPiece({{ lit .Rules.Values.GetString_.GetPattern }},
  80. sizeof({{ lit .Rules.Values.GetString_.GetPattern }}) - 1));
  81. {{ end }}{{ end }}
  82. {{ end }}{{ end }}
  83. {{ end }}{{ end }}
  84. bool Validate(const {{ class . }}& m, pgv::ValidationMsg* err) {
  85. (void)m;
  86. (void)err;
  87. {{- if disabled . }}
  88. return true;
  89. {{ else -}}
  90. {{ range .NonOneOfFields }}
  91. {{- render (context .) -}}
  92. {{ end -}}
  93. {{ range .OneOfs }}
  94. switch (m.{{ .Name }}_case()) {
  95. {{ range .Fields -}}
  96. case {{ oneof . }}:
  97. {{ render (context .) }}
  98. break;
  99. {{ end -}}
  100. default:
  101. {{- if required . }}
  102. *err = "field: " {{ .Name | quote | lit }} ", reason: is required";
  103. return false;
  104. {{ end }}
  105. break;
  106. }
  107. {{ end }}
  108. return true;
  109. {{ end -}}
  110. }
  111. {{- end -}}
  112. `