avsc-json-sample
Version:
A librairy to generate predicable JSON sample from an avsc type
80 lines • 2.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.typeToJsonSample = void 0;
const avsc_1 = require("avsc");
var RecordType = avsc_1.types.RecordType;
var StringType = avsc_1.types.StringType;
var IntType = avsc_1.types.IntType;
var LongType = avsc_1.types.LongType;
var FloatType = avsc_1.types.FloatType;
var DoubleType = avsc_1.types.DoubleType;
var BooleanType = avsc_1.types.BooleanType;
var NullType = avsc_1.types.NullType;
var BytesType = avsc_1.types.BytesType;
var UnwrappedUnionType = avsc_1.types.UnwrappedUnionType;
var ArrayType = avsc_1.types.ArrayType;
var WrappedUnionType = avsc_1.types.WrappedUnionType;
var EnumType = avsc_1.types.EnumType;
var FixedType = avsc_1.types.FixedType;
var MapType = avsc_1.types.MapType;
function typeToJsonSample(type) {
if (type instanceof RecordType) {
return type.fields
.map((field) => ({
key: field.name,
value: typeToJsonSample(field.type),
}))
.reduce((allRecordInJson, { key, value }) => {
return Object.assign(Object.assign({}, allRecordInJson), { [key]: value });
}, {});
}
if (type instanceof StringType) {
return "string";
}
if (type instanceof IntType || type instanceof LongType) {
return 0;
}
if (type instanceof FloatType || type instanceof DoubleType) {
return 0.1;
}
if (type instanceof BooleanType) {
return true;
}
if (type instanceof NullType) {
return null;
}
if (type instanceof BytesType) {
return "\u00FF";
}
if (type instanceof UnwrappedUnionType) {
const canExcludeNullInUnionToHaveAMoreDescriptiveSample = type.types[0] instanceof NullType && type.types[1];
if (canExcludeNullInUnionToHaveAMoreDescriptiveSample) {
return typeToJsonSample(type.types[1]);
}
return typeToJsonSample(type.types[0]);
}
if (type instanceof WrappedUnionType) {
return type.types.reduce((allUnionSample, currentUnionType) => {
if (!currentUnionType.branchName) {
return allUnionSample;
}
return Object.assign(Object.assign({}, allUnionSample), { [currentUnionType.branchName]: typeToJsonSample(currentUnionType) });
}, {});
}
if (type instanceof ArrayType) {
return [typeToJsonSample(type.itemsType)];
}
if (type instanceof EnumType) {
return type.symbols[0];
}
if (type instanceof FixedType) {
return "".padStart(type.size, "a");
}
if (type instanceof MapType) {
return {
key: typeToJsonSample(type.valuesType),
};
}
}
exports.typeToJsonSample = typeToJsonSample;
//# sourceMappingURL=avsc-type-to-json-sample.js.map
;