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