blob: f48cd33a8f3583ff9642e656751854ea781c5c3a [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Exception.hh"
#include "LogicalType.hh"
namespace avro {
LogicalType::LogicalType(Type type)
: type_(type), precision_(0), scale_(0) {}
LogicalType::Type LogicalType::type() const {
return type_;
}
void LogicalType::setPrecision(int precision) {
if (type_ != DECIMAL) {
throw Exception("Only logical type DECIMAL can have precision");
}
if (precision <= 0) {
throw Exception(boost::format("Precision cannot be: %1%") % precision);
}
precision_ = precision;
}
void LogicalType::setScale(int scale) {
if (type_ != DECIMAL) {
throw Exception("Only logical type DECIMAL can have scale");
}
if (scale < 0) {
throw Exception(boost::format("Scale cannot be: %1%") % scale);
}
scale_ = scale;
}
void LogicalType::printJson(std::ostream& os) const {
switch (type_) {
case LogicalType::NONE:
break;
case LogicalType::DECIMAL:
os << "\"logicalType\": \"decimal\"";
os << ", \"precision\": " << precision_;
os << ", \"scale\": " << scale_;
break;
case DATE:
os << "\"logicalType\": \"date\"";
break;
case TIME_MILLIS:
os << "\"logicalType\": \"time-millis\"";
break;
case TIME_MICROS:
os << "\"logicalType\": \"time-micros\"";
break;
case TIMESTAMP_MILLIS:
os << "\"logicalType\": \"timestamp-millis\"";
break;
case TIMESTAMP_MICROS:
os << "\"logicalType\": \"timestamp-micros\"";
break;
case DURATION:
os << "\"logicalType\": \"duration\"";
break;
}
}
} // namespace avro