GenericDatum.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "GenericDatum.hh"
  19. #include "NodeImpl.hh"
  20. using std::string;
  21. using std::vector;
  22. namespace avro {
  23. GenericDatum::GenericDatum(const ValidSchema &schema) : type_(schema.root()->type()),
  24. logicalType_(schema.root()->logicalType()) {
  25. init(schema.root());
  26. }
  27. GenericDatum::GenericDatum(const NodePtr &schema) : type_(schema->type()),
  28. logicalType_(schema->logicalType()) {
  29. init(schema);
  30. }
  31. void GenericDatum::init(const NodePtr &schema) {
  32. NodePtr sc = schema;
  33. if (type_ == AVRO_SYMBOLIC) {
  34. sc = resolveSymbol(schema);
  35. type_ = sc->type();
  36. logicalType_ = sc->logicalType();
  37. }
  38. switch (type_) {
  39. case AVRO_NULL: break;
  40. case AVRO_BOOL:
  41. value_ = bool();
  42. break;
  43. case AVRO_INT:
  44. value_ = int32_t();
  45. break;
  46. case AVRO_LONG:
  47. value_ = int64_t();
  48. break;
  49. case AVRO_FLOAT:
  50. value_ = float();
  51. break;
  52. case AVRO_DOUBLE:
  53. value_ = double();
  54. break;
  55. case AVRO_STRING:
  56. value_ = string();
  57. break;
  58. case AVRO_BYTES:
  59. value_ = vector<uint8_t>();
  60. break;
  61. case AVRO_FIXED:
  62. value_ = GenericFixed(sc);
  63. break;
  64. case AVRO_RECORD:
  65. value_ = GenericRecord(sc);
  66. break;
  67. case AVRO_ENUM:
  68. value_ = GenericEnum(sc);
  69. break;
  70. case AVRO_ARRAY:
  71. value_ = GenericArray(sc);
  72. break;
  73. case AVRO_MAP:
  74. value_ = GenericMap(sc);
  75. break;
  76. case AVRO_UNION:
  77. value_ = GenericUnion(sc);
  78. break;
  79. default:
  80. throw Exception(boost::format("Unknown schema type %1%") % toString(type_));
  81. }
  82. }
  83. GenericRecord::GenericRecord(const NodePtr &schema) : GenericContainer(AVRO_RECORD, schema) {
  84. fields_.resize(schema->leaves());
  85. for (size_t i = 0; i < schema->leaves(); ++i) {
  86. fields_[i] = GenericDatum(schema->leafAt(i));
  87. }
  88. }
  89. GenericFixed::GenericFixed(const NodePtr &schema, const vector<uint8_t> &v) : GenericContainer(AVRO_FIXED, schema), value_(v) {}
  90. } // namespace avro