Schema.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include <utility>
  19. #include "Schema.hh"
  20. #include "CustomAttributes.hh"
  21. namespace avro {
  22. RecordSchema::RecordSchema(const std::string &name) : Schema(new NodeRecord) {
  23. node_->setName(Name(name));
  24. }
  25. void RecordSchema::addField(const std::string &name, const Schema &fieldSchema) {
  26. const CustomAttributes emptyCustomAttribute;
  27. addField(name, fieldSchema, emptyCustomAttribute);
  28. }
  29. void RecordSchema::addField(const std::string &name, const Schema &fieldSchema, const CustomAttributes &customFields) {
  30. // add the name first. it will throw if the name is a duplicate, preventing
  31. // the leaf from being added
  32. node_->addName(name);
  33. node_->addLeaf(fieldSchema.root());
  34. node_->addCustomAttributesForField(customFields);
  35. }
  36. std::string RecordSchema::getDoc() const {
  37. return node_->getDoc();
  38. }
  39. void RecordSchema::setDoc(const std::string &doc) {
  40. node_->setDoc(doc);
  41. }
  42. EnumSchema::EnumSchema(const std::string &name) : Schema(new NodeEnum) {
  43. node_->setName(Name(name));
  44. }
  45. void EnumSchema::addSymbol(const std::string &symbol) {
  46. node_->addName(symbol);
  47. }
  48. ArraySchema::ArraySchema(const Schema &itemsSchema) : Schema(new NodeArray) {
  49. node_->addLeaf(itemsSchema.root());
  50. }
  51. ArraySchema::ArraySchema(const ArraySchema &itemsSchema) : Schema(new NodeArray) {
  52. node_->addLeaf(itemsSchema.root());
  53. }
  54. MapSchema::MapSchema(const Schema &valuesSchema) : Schema(new NodeMap) {
  55. node_->addLeaf(valuesSchema.root());
  56. }
  57. MapSchema::MapSchema(const MapSchema &valuesSchema) : Schema(new NodeMap) {
  58. node_->addLeaf(valuesSchema.root());
  59. }
  60. UnionSchema::UnionSchema() : Schema(new NodeUnion) {}
  61. void UnionSchema::addType(const Schema &typeSchema) {
  62. if (typeSchema.type() == AVRO_UNION) {
  63. throw Exception("Cannot add unions to unions");
  64. }
  65. if (typeSchema.type() == AVRO_RECORD) {
  66. // check for duplicate records
  67. size_t types = node_->leaves();
  68. for (size_t i = 0; i < types; ++i) {
  69. const NodePtr &leaf = node_->leafAt(i);
  70. // TODO, more checks?
  71. if (leaf->type() == AVRO_RECORD && leaf->name() == typeSchema.root()->name()) {
  72. throw Exception("Records in unions cannot have duplicate names");
  73. }
  74. }
  75. }
  76. node_->addLeaf(typeSchema.root());
  77. }
  78. FixedSchema::FixedSchema(int size, const std::string &name) : Schema(new NodeFixed) {
  79. node_->setFixedSize(size);
  80. node_->setName(Name(name));
  81. }
  82. SymbolicSchema::SymbolicSchema(const Name &name, const NodePtr &link) : Schema(new NodeSymbolic(HasName(name), link)) {
  83. }
  84. } // namespace avro