Validator.hh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * https://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef avro_Validating_hh__
  19. #define avro_Validating_hh__
  20. #include <boost/noncopyable.hpp>
  21. #include <cstdint>
  22. #include <utility>
  23. #include <vector>
  24. #include "Config.hh"
  25. #include "Types.hh"
  26. #include "ValidSchema.hh"
  27. namespace avro {
  28. class AVRO_DECL NullValidator : private boost::noncopyable {
  29. public:
  30. explicit NullValidator(const ValidSchema &schema) {}
  31. NullValidator() = default;
  32. void setCount(int64_t) {}
  33. static bool typeIsExpected(Type) {
  34. return true;
  35. }
  36. static Type nextTypeExpected() {
  37. return AVRO_UNKNOWN;
  38. }
  39. static int nextSizeExpected() {
  40. return 0;
  41. }
  42. static bool getCurrentRecordName(std::string &name) {
  43. return true;
  44. }
  45. static bool getNextFieldName(std::string &name) {
  46. return true;
  47. }
  48. void checkTypeExpected(Type) {}
  49. void checkFixedSizeExpected(int) {}
  50. };
  51. /// This class is used by both the ValidatingSerializer and ValidationParser
  52. /// objects. It advances the parse tree (containing logic how to advance
  53. /// through the various compound types, for example a record must advance
  54. /// through all leaf nodes but a union only skips to one), and reports which
  55. /// type is next.
  56. class AVRO_DECL Validator : private boost::noncopyable {
  57. public:
  58. explicit Validator(ValidSchema schema);
  59. void setCount(int64_t val);
  60. bool typeIsExpected(Type type) const {
  61. return (expectedTypesFlag_ & typeToFlag(type)) != 0;
  62. }
  63. Type nextTypeExpected() const {
  64. return nextType_;
  65. }
  66. int nextSizeExpected() const;
  67. bool getCurrentRecordName(std::string &name) const;
  68. bool getNextFieldName(std::string &name) const;
  69. void checkTypeExpected(Type type) {
  70. if (!typeIsExpected(type)) {
  71. throw Exception(
  72. boost::format("Type %1% does not match schema %2%")
  73. % type % nextType_);
  74. }
  75. advance();
  76. }
  77. void checkFixedSizeExpected(int size) {
  78. if (nextSizeExpected() != size) {
  79. throw Exception(
  80. boost::format("Wrong size for fixed, got %1%, expected %2%")
  81. % size % nextSizeExpected());
  82. }
  83. checkTypeExpected(AVRO_FIXED);
  84. }
  85. private:
  86. using flag_t = uint32_t;
  87. static flag_t typeToFlag(Type type) {
  88. flag_t flag = (1L << type);
  89. return flag;
  90. }
  91. void setupOperation(const NodePtr &node);
  92. void setWaitingForCount();
  93. void advance();
  94. void doAdvance();
  95. void enumAdvance();
  96. bool countingSetup();
  97. void countingAdvance();
  98. void unionAdvance();
  99. void fixedAdvance();
  100. void setupFlag(Type type);
  101. const ValidSchema schema_;
  102. Type nextType_;
  103. flag_t expectedTypesFlag_;
  104. bool compoundStarted_;
  105. bool waitingForCount_;
  106. int64_t count_;
  107. struct CompoundType {
  108. explicit CompoundType(NodePtr n) : node(std::move(n)), pos(0) {}
  109. NodePtr node; ///< save the node
  110. size_t pos; ///< track the leaf position to visit
  111. };
  112. std::vector<CompoundType> compoundStack_;
  113. std::vector<size_t> counters_;
  114. };
  115. } // namespace avro
  116. #endif