Avro C++
Validator.hh
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 
19 #ifndef avro_Validating_hh__
20 #define avro_Validating_hh__
21 
22 #include <boost/noncopyable.hpp>
23 #include <cstdint>
24 #include <utility>
25 #include <vector>
26 
27 #include "Config.hh"
28 #include "Types.hh"
29 #include "ValidSchema.hh"
30 
31 namespace avro {
32 
33 class AVRO_DECL NullValidator : private boost::noncopyable {
34 public:
35  explicit NullValidator(const ValidSchema &schema) {}
36  NullValidator() = default;
37 
38  void setCount(int64_t) {}
39 
40  static bool typeIsExpected(Type) {
41  return true;
42  }
43 
44  static Type nextTypeExpected() {
45  return AVRO_UNKNOWN;
46  }
47 
48  static int nextSizeExpected() {
49  return 0;
50  }
51 
52  static bool getCurrentRecordName(std::string &name) {
53  return true;
54  }
55 
56  static bool getNextFieldName(std::string &name) {
57  return true;
58  }
59 
60  void checkTypeExpected(Type) {}
61  void checkFixedSizeExpected(int) {}
62 };
63 
69 
70 class AVRO_DECL Validator : private boost::noncopyable {
71 public:
72  explicit Validator(ValidSchema schema);
73 
74  void setCount(int64_t val);
75 
76  bool typeIsExpected(Type type) const {
77  return (expectedTypesFlag_ & typeToFlag(type)) != 0;
78  }
79 
80  Type nextTypeExpected() const {
81  return nextType_;
82  }
83 
84  int nextSizeExpected() const;
85 
86  bool getCurrentRecordName(std::string &name) const;
87  bool getNextFieldName(std::string &name) const;
88 
89  void checkTypeExpected(Type type) {
90  if (!typeIsExpected(type)) {
91  throw Exception(
92  boost::format("Type %1% does not match schema %2%")
93  % type % nextType_);
94  }
95  advance();
96  }
97 
98  void checkFixedSizeExpected(int size) {
99  if (nextSizeExpected() != size) {
100  throw Exception(
101  boost::format("Wrong size for fixed, got %1%, expected %2%")
102  % size % nextSizeExpected());
103  }
104  checkTypeExpected(AVRO_FIXED);
105  }
106 
107 private:
108  using flag_t = uint32_t;
109 
110  static flag_t typeToFlag(Type type) {
111  flag_t flag = (1L << type);
112  return flag;
113  }
114 
115  void setupOperation(const NodePtr &node);
116 
117  void setWaitingForCount();
118 
119  void advance();
120  void doAdvance();
121 
122  void enumAdvance();
123  bool countingSetup();
124  void countingAdvance();
125  void unionAdvance();
126  void fixedAdvance();
127 
128  void setupFlag(Type type);
129 
130  const ValidSchema schema_;
131 
132  Type nextType_;
133  flag_t expectedTypesFlag_;
134  bool compoundStarted_;
135  bool waitingForCount_;
136  int64_t count_;
137 
138  struct CompoundType {
139  explicit CompoundType(NodePtr n) : node(std::move(n)), pos(0) {}
140  NodePtr node;
141  size_t pos;
142  };
143 
144  std::vector<CompoundType> compoundStack_;
145  std::vector<size_t> counters_;
146 };
147 
148 } // namespace avro
149 
150 #endif
avro::NullValidator
Definition: Validator.hh:33
avro::Validator
This class is used by both the ValidatingSerializer and ValidationParser objects.
Definition: Validator.hh:70
avro
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
avro::ValidSchema
A ValidSchema is basically a non-mutable Schema that has passed some minimum of sanity checks.
Definition: ValidSchema.hh:40
avro::AVRO_FIXED
@ AVRO_FIXED
Definition: Types.hh:47
avro::AVRO_UNKNOWN
@ AVRO_UNKNOWN
Definition: Types.hh:54
avro::Type
Type
The "type" for the schema.
Definition: Types.hh:31
avro::Exception
Wrapper for std::runtime_error that provides convenience constructor for boost::format objects.
Definition: Exception.hh:31