@sap-cloud-sdk/odata-common
Version:
SAP Cloud SDK for JavaScript common functions of OData client generator and OpenAPI clint generator.
98 lines • 4.56 kB
JavaScript
;
/* eslint-disable max-classes-per-file */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldBuilder = void 0;
const edm_types_1 = require("../edm-types");
const complex_type_field_1 = require("./complex-type-field");
const edm_type_field_1 = require("./edm-type-field");
const orderable_edm_type_field_1 = require("./orderable-edm-type-field");
const collection_field_1 = require("./collection-field");
const enum_field_1 = require("./enum-field");
/**
* Field builder to orchestrate the creation of the different kinds of fields.
* @typeParam FieldOfT - Type of the entity or complex type field this field belongs to.
* @typeParam DeSerializersT - Type of the (de-)serializers.
*/
class FieldBuilder {
/**
* Creates an instance of `FieldBuilder`.
* @param fieldOf - Entity or complex type field, for which the field builder shall create fields.
* @param deSerializers - (De-)serializers used for transformation.
*/
constructor(fieldOf, deSerializers) {
this.fieldOf = fieldOf;
this.deSerializers = deSerializers;
}
/**
* Build a field for a property with an EDM type.
* For `{@link OrderableEdmType}` fields, the returned fields are of type `OrderableEdmTypeField`.
* All other EDM types yield `EdmTypeField`s.
* Fields of entities are selectable; fields of complex types are not selectable.
* @param fieldName - Name of the field.
* @param edmType - EDM type of the field.
* @param isNullable - Whether the field is nullable.
* @param precision - Precision associated with the field.
* @returns An EDM type field.
*/
buildEdmTypeField(fieldName, edmType, isNullable, precision) {
const isSelectable = (this.fieldOf instanceof
complex_type_field_1.ComplexTypeField);
// The type assertion is necessary because the signatures of the two constructors differ (TS design limitation)
const ctor = ((0, edm_types_1.isOrderableEdmType)(edmType) ? orderable_edm_type_field_1.OrderableEdmTypeField : edm_type_field_1.EdmTypeField);
return new ctor(fieldName, this.fieldOf, edmType, this.deSerializers, {
isNullable,
isSelectable,
precision
});
}
/**
* Build a field for a property with a complex type.
* Fields of entities are selectable; fields of complex types are not selectable.
* @param fieldName - Name of the field.
* @param complexTypeFieldCtor - Constructor of the complex type field.
* @param isNullable - Whether the field is nullable.
* @returns A complex type field of the given type.
*/
buildComplexTypeField(fieldName, complexTypeFieldCtor, isNullable) {
const isSelectable = (this.fieldOf instanceof
complex_type_field_1.ComplexTypeField);
return new complexTypeFieldCtor(fieldName, this.fieldOf, this.deSerializers, {
isNullable,
isSelectable
});
}
/**
* Build a field for a property with a collection type.
* The type of the field can either be an EDM type or a complex type.
* Fields of entities are selectable; fields of complex types are not selectable.
* @param fieldName - Name of the field.
* @param collectionFieldType - Type of the collection. Can either be an EDM type or complex type (not complex type field).
* @param isNullable - Whether the field is nullable.
* @returns A collection field with the given collection type.
*/
buildCollectionField(fieldName, collectionFieldType, isNullable) {
const isSelectable = (this.fieldOf instanceof
complex_type_field_1.ComplexTypeField);
return new collection_field_1.CollectionField(fieldName, this.fieldOf, collectionFieldType, {
isNullable,
isSelectable
});
}
/**
* Build a field for a property with a enum type.
* @param fieldName - Name of the field.
* @param enumType - Enum type of this field.
* @param isNullable - Whether the field is nullable.
* @returns A collection field with the given collection type.
*/
buildEnumField(fieldName, enumType, isNullable) {
const isSelectable = (this.fieldOf instanceof
complex_type_field_1.ComplexTypeField);
return new enum_field_1.EnumField(fieldName, this.fieldOf, enumType, {
isNullable,
isSelectable
});
}
}
exports.FieldBuilder = FieldBuilder;
//# sourceMappingURL=field-builder.js.map