Schema.hh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_Schema_hh__
  19. #define avro_Schema_hh__
  20. #include "Config.hh"
  21. #include "NodeImpl.hh"
  22. #include "CustomAttributes.hh"
  23. #include <string>
  24. /// \file
  25. ///
  26. /// Schemas for representing all the avro types. The compound schema objects
  27. /// allow composition from other schemas.
  28. ///
  29. namespace avro {
  30. /// The root Schema object is a base class. Nobody constructs this class directly.
  31. class AVRO_DECL Schema {
  32. public:
  33. virtual ~Schema() = default;
  34. Type type() const {
  35. return node_->type();
  36. }
  37. const NodePtr &root() const {
  38. return node_;
  39. }
  40. NodePtr &root() {
  41. return node_;
  42. }
  43. protected:
  44. explicit Schema(NodePtr node) : node_(std::move(node)) {}
  45. explicit Schema(Node *node) : node_(node) {}
  46. NodePtr node_;
  47. };
  48. class AVRO_DECL NullSchema : public Schema {
  49. public:
  50. NullSchema() : Schema(new NodePrimitive(AVRO_NULL)) {}
  51. };
  52. class AVRO_DECL BoolSchema : public Schema {
  53. public:
  54. BoolSchema() : Schema(new NodePrimitive(AVRO_BOOL)) {}
  55. };
  56. class AVRO_DECL IntSchema : public Schema {
  57. public:
  58. IntSchema() : Schema(new NodePrimitive(AVRO_INT)) {}
  59. };
  60. class AVRO_DECL LongSchema : public Schema {
  61. public:
  62. LongSchema() : Schema(new NodePrimitive(AVRO_LONG)) {}
  63. };
  64. class AVRO_DECL FloatSchema : public Schema {
  65. public:
  66. FloatSchema() : Schema(new NodePrimitive(AVRO_FLOAT)) {}
  67. };
  68. class AVRO_DECL DoubleSchema : public Schema {
  69. public:
  70. DoubleSchema() : Schema(new NodePrimitive(AVRO_DOUBLE)) {}
  71. };
  72. class AVRO_DECL StringSchema : public Schema {
  73. public:
  74. StringSchema() : Schema(new NodePrimitive(AVRO_STRING)) {}
  75. };
  76. class AVRO_DECL BytesSchema : public Schema {
  77. public:
  78. BytesSchema() : Schema(new NodePrimitive(AVRO_BYTES)) {}
  79. };
  80. class AVRO_DECL RecordSchema : public Schema {
  81. public:
  82. explicit RecordSchema(const std::string &name);
  83. void addField(const std::string &name, const Schema &fieldSchema);
  84. // Add a field with custom attributes
  85. void addField(const std::string &name, const Schema &fieldSchema,
  86. const CustomAttributes &customAttributes);
  87. std::string getDoc() const;
  88. void setDoc(const std::string &);
  89. };
  90. class AVRO_DECL EnumSchema : public Schema {
  91. public:
  92. explicit EnumSchema(const std::string &name);
  93. void addSymbol(const std::string &symbol);
  94. };
  95. class AVRO_DECL ArraySchema : public Schema {
  96. public:
  97. explicit ArraySchema(const Schema &itemsSchema);
  98. ArraySchema(const ArraySchema &itemsSchema);
  99. };
  100. class AVRO_DECL MapSchema : public Schema {
  101. public:
  102. explicit MapSchema(const Schema &valuesSchema);
  103. MapSchema(const MapSchema &itemsSchema);
  104. };
  105. class AVRO_DECL UnionSchema : public Schema {
  106. public:
  107. UnionSchema();
  108. void addType(const Schema &typeSchema);
  109. };
  110. class AVRO_DECL FixedSchema : public Schema {
  111. public:
  112. FixedSchema(int size, const std::string &name);
  113. };
  114. class AVRO_DECL SymbolicSchema : public Schema {
  115. public:
  116. SymbolicSchema(const Name &name, const NodePtr &link);
  117. };
  118. } // namespace avro
  119. #endif