bytes.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cc
  2. const bytesTpl = `
  3. {{ $f := .Field }}{{ $r := .Rules }}
  4. {{ if $r.GetIgnoreEmpty }}
  5. if ({{ accessor . }}.size() > 0) {
  6. {{ end }}
  7. {{ template "const" . }}
  8. {{ template "in" . }}
  9. {{ if or $r.Len (and $r.MinLen $r.MaxLen (eq $r.GetMinLen $r.GetMaxLen)) }}
  10. {
  11. const auto length = {{ accessor . }}.size();
  12. {{ if $r.Len }}
  13. if (length != {{ $r.GetLen }}) {
  14. {{ err . "value length must be " $r.GetLen " bytes" }}
  15. }
  16. {{ else }}
  17. if (length != {{ $r.GetMinLen }}) {
  18. {{ err . "value length must be " $r.GetMinLen " bytes" }}
  19. }
  20. {{ end }}
  21. }
  22. {{ else if $r.MinLen }}
  23. {
  24. const auto length = {{ accessor . }}.size();
  25. {{ if $r.MaxLen }}
  26. if (length < {{ $r.GetMinLen }} || length > {{ $r.GetMaxLen }}) {
  27. {{ err . "value length must be between " $r.GetMinLen " and " $r.GetMaxLen " bytes, inclusive" }}
  28. }
  29. {{ else }}
  30. if (length < {{ $r.GetMinLen }}) {
  31. {{ err . "value length must be at least " $r.GetMinLen " bytes" }}
  32. }
  33. {{ end }}
  34. }
  35. {{ else if $r.MaxLen }}
  36. if ({{ accessor . }}.size() > {{ $r.GetMaxLen }}) {
  37. {{ err . "value length must be at most " $r.GetMaxLen " bytes" }}
  38. }
  39. {{ end }}
  40. {{ if $r.Prefix }}
  41. {
  42. const std::string prefix = {{ lit $r.GetPrefix }};
  43. if (!pgv::IsPrefix(prefix, {{ accessor . }})) {
  44. {{ err . "value does not have prefix " (lit $r.GetPrefix) }}
  45. }
  46. }
  47. {{ end }}
  48. {{ if $r.Suffix }}
  49. {
  50. const std::string suffix = {{ lit $r.GetSuffix }};
  51. if (!pgv::IsSuffix(suffix, {{ accessor .}})) {
  52. {{ err . "value does not have suffix " (lit $r.GetSuffix) }}
  53. }
  54. }
  55. {{ end }}
  56. {{ if $r.Contains }}
  57. {
  58. if (!pgv::Contains({{ accessor . }}, {{ lit $r.GetContains }})) {
  59. {{ err . "value does not contain substring " (lit $r.GetContains) }}
  60. }
  61. }
  62. {{ end }}
  63. {{ if $r.Pattern }}
  64. {
  65. if (!RE2::FullMatch(re2::StringPiece({{ accessor . }}.c_str(), {{ accessor . }}.size()),
  66. {{ lookup $f "Pattern" }})) {
  67. {{ err . "value does not match regex pattern " (lit $r.GetPattern) }}
  68. }
  69. }
  70. {{ end }}
  71. {{ if $r.GetIp }}
  72. {{ unimplemented "C++ ip address validation is not implemented" }}
  73. {{/* TODO(akonradi) implement all of this
  74. if ip := net.IP({{ accessor . }}); ip.To16() == nil {
  75. return {{ err . "value must be a valid IP address" }}
  76. }
  77. */}}
  78. {{ else if $r.GetIpv4 }}
  79. {{ unimplemented "C++ ip address validation is not implemented" }}
  80. {{/* TODO(akonradi) implement all of this
  81. if ip := net.IP({{ accessor . }}); ip.To4() == nil {
  82. return {{ err . "value must be a valid IPv4 address" }}
  83. }
  84. */}}
  85. {{ else if $r.GetIpv6 }}
  86. {{ unimplemented "C++ ip address validation is not implemented" }}
  87. {{/* TODO(akonradi) implement all of this
  88. if ip := net.IP({{ accessor . }}); ip.To16() == nil || ip.To4() != nil {
  89. return {{ err . "value must be a valid IPv6 address" }}
  90. }
  91. */}}
  92. {{ end }}
  93. {{ if $r.GetIgnoreEmpty }}
  94. }
  95. {{ end }}
  96. `