Skip to content

Commit

Permalink
progress till now
Browse files Browse the repository at this point in the history
  • Loading branch information
KavithaSiva committed Jan 14, 2025
1 parent f0a657d commit e34eee9
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 49 deletions.
2 changes: 0 additions & 2 deletions packages/generator/src/edmx-to-vdm/common/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ function properties(
isEnum
);

console.log("properties: " + p.Precision);

return {
originalName: p.Name,
instancePropertyName,
Expand Down
4 changes: 3 additions & 1 deletion packages/generator/src/entity/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ export function createPropertyFieldInitializerForEntity(

return `${fieldBuilderName}.buildEdmTypeField('${prop.originalName}', '${
prop.edmType
}', ${prop.nullable}${prop.precision ? `, ${prop.precision}` :''})`;
}', ${prop.nullable}${
prop.precision !== undefined ? `, ${prop.precision}` : ''
})`;
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/generator/src/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ function addConstraints(
description += `${unixEOL}@nullable`;
}
if (constraints.precision !== undefined) {
console.log(constraints.precision);
description += `${unixEOL}Precision: ${constraints.precision}.`;
}

Expand Down
8 changes: 6 additions & 2 deletions packages/odata-common/src/de-serializers/de-serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,13 @@ export function createValueDeserializer<DeSerializersT extends DeSerializers>(
*/
export function createValueSerializer<DeSerializersT extends DeSerializers>(
deSerializers: DeSerializersT
): <EdmT>(value: any, edmType: EdmT) => any {
return (value, edmType) => {
): <EdmT>(value: any, edmType: EdmT, precision?: number) => any {
return (value, edmType, precision) => {
const serialize = deSerializers[edmType as any]?.serialize;
// Check if the edmType is specifically Edm.DateTimeOffset as we can have precision, scale property defined
if (edmType === 'Edm.DateTimeOffset' && precision) {
return serialize(value, precision);
}
return serialize ? serialize(value) : value;
};
}
5 changes: 3 additions & 2 deletions packages/odata-common/src/entity-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
Link,
ComplexTypeField,
CollectionField,
EnumField
EnumField,
getFieldOptions
} from './selectable';
import { isEdmType } from './edm-types';
import { createValueSerializer } from './de-serializers';
Expand Down Expand Up @@ -84,7 +85,7 @@ export function entitySerializer(
return null;
}
if (field instanceof EdmTypeField) {
return tsToEdm(fieldValue, field.edmType);
return tsToEdm(fieldValue, field.edmType, getFieldOptions().precision);
}
if (field instanceof OneToOneLink) {
return serializeEntity(fieldValue, field._linkedEntityApi);
Expand Down
7 changes: 4 additions & 3 deletions packages/odata-common/src/selectable/edm-type-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ export class EdmTypeField<
DeSerializersT extends DeSerializers,
EdmT extends EdmTypeShared<'any'>,
NullableT extends boolean = false,
SelectableT extends boolean = false
SelectableT extends boolean = false,
PrecisionT extends number = number
>
extends Field<EntityT, NullableT, SelectableT>
extends Field<EntityT, NullableT, SelectableT, PrecisionT>
implements EntityIdentifiable<EntityT, DeSerializersT>
{
readonly _entity: EntityT;
Expand All @@ -70,7 +71,7 @@ export class EdmTypeField<
readonly _fieldOf: ConstructorOrField<EntityT>,
readonly edmType: EdmT,
public _deSerializers: DeSerializersT, // Only necessary for the type, unused otherwise
fieldOptions?: FieldOptions<NullableT, SelectableT>
fieldOptions?: FieldOptions<NullableT, SelectableT, PrecisionT>
) {
super(fieldName, getEntityConstructor(_fieldOf), fieldOptions);
this.precision = fieldOptions?.precision;
Expand Down
30 changes: 20 additions & 10 deletions packages/odata-common/src/selectable/field-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,39 @@ export class FieldBuilder<
private deSerializers: DeSerializersT
) {}

buildEdmTypeField<EdmT extends OrderableEdmType, NullableT extends boolean>(
buildEdmTypeField<
EdmT extends OrderableEdmType,
NullableT extends boolean,
PrecisionT extends number = number
>(
fieldName: string,
edmType: EdmT,
isNullable: NullableT,
precision?: number
precision?: PrecisionT
): OrderableEdmTypeField<
EntityTypeFromFieldOf<FieldOfT>,
DeSerializersT,
EdmT,
NullableT,
IsSelectableField<FieldOfT>
IsSelectableField<FieldOfT>,
PrecisionT
>;
buildEdmTypeField<
EdmT extends Exclude<EdmTypeShared<'any'>, OrderableEdmType>,
NullableT extends boolean
NullableT extends boolean,
PrecisionT extends number = number
>(
fieldName: string,
edmType: EdmT,
isNullable: NullableT,
precision?: number
precision?: PrecisionT
): EdmTypeField<
EntityTypeFromFieldOf<FieldOfT>,
DeSerializersT,
EdmT,
NullableT,
IsSelectableField<FieldOfT>
IsSelectableField<FieldOfT>,
PrecisionT
>;
/**
* Build a field for a property with an EDM type.
Expand All @@ -115,26 +122,29 @@ export class FieldBuilder<
*/
buildEdmTypeField<
EdmT extends EdmTypeShared<'any'>,
NullableT extends boolean
NullableT extends boolean,
PrecisionT extends number = number
>(
fieldName: string,
edmType: EdmT,
isNullable: NullableT,
precision?: number
precision?: PrecisionT
):
| OrderableEdmTypeField<
EntityTypeFromFieldOf<FieldOfT>,
DeSerializersT,
EdmT,
NullableT,
IsSelectableField<FieldOfT>
IsSelectableField<FieldOfT>,
PrecisionT
>
| EdmTypeField<
EntityTypeFromFieldOf<FieldOfT>,
DeSerializersT,
EdmT,
NullableT,
IsSelectableField<FieldOfT>
IsSelectableField<FieldOfT>,
PrecisionT
> {
const isSelectable = (this.fieldOf instanceof
ComplexTypeField) as IsSelectableField<FieldOfT>;
Expand Down
25 changes: 15 additions & 10 deletions packages/odata-common/src/selectable/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { EntityBase, Constructable } from '../entity-base';
*/
export interface FieldOptions<
NullableT extends boolean = false,
SelectableT extends boolean = false
SelectableT extends boolean = false,
PrecisionT extends number = number
> {
/**
* Whether the value of the field can be `null`.
Expand All @@ -18,7 +19,7 @@ export interface FieldOptions<
/**
* Precision associated with field.
*/
precision?: number;
precision?: PrecisionT;
}

/**
Expand All @@ -30,19 +31,20 @@ export interface FieldOptions<
*/
export function getFieldOptions<
NullableT extends boolean = false,
SelectableT extends boolean = false
SelectableT extends boolean = false,
PrecisionT extends number = number
>(
fieldOptions?: FieldOptions<NullableT, SelectableT>
): Required<FieldOptions<NullableT, SelectableT>> {
fieldOptions?: FieldOptions<NullableT, SelectableT, PrecisionT>
): Required<FieldOptions<NullableT, SelectableT, PrecisionT>> {
return { ...defaultFieldOptions, ...fieldOptions } as Required<
FieldOptions<NullableT, SelectableT>
FieldOptions<NullableT, SelectableT, PrecisionT>
>;
}

const defaultFieldOptions: Required<FieldOptions> = {
isNullable: false,
isSelectable: false,
precision : 0
precision: 0
};

/**
Expand All @@ -60,9 +62,12 @@ const defaultFieldOptions: Required<FieldOptions> = {
export class Field<
EntityT extends EntityBase,
NullableT extends boolean = false,
SelectableT extends boolean = false
SelectableT extends boolean = false,
PrecisionT extends number = number
> {
readonly _fieldOptions: Required<FieldOptions<NullableT, SelectableT>>;
readonly _fieldOptions: Required<
FieldOptions<NullableT, SelectableT, PrecisionT>
>;
/**
* Creates an instance of Field.
* @param _fieldName - Actual name of the field used in the OData request.
Expand All @@ -72,7 +77,7 @@ export class Field<
constructor(
readonly _fieldName: string,
readonly _entityConstructor: Constructable<EntityT>,
fieldOptions?: FieldOptions<NullableT, SelectableT>
fieldOptions?: FieldOptions<NullableT, SelectableT, PrecisionT>
) {
this._fieldOptions = getFieldOptions(fieldOptions);
}
Expand Down
12 changes: 10 additions & 2 deletions packages/odata-common/src/selectable/orderable-edm-type-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ export class OrderableEdmTypeField<
DeSerializersT extends DeSerializers,
EdmT extends EdmTypeShared<'any'>,
NullableT extends boolean = false,
SelectableT extends boolean = false
> extends EdmTypeField<EntityT, DeSerializersT, EdmT, NullableT, SelectableT> {
SelectableT extends boolean = false,
PrecisionT extends number = number
> extends EdmTypeField<
EntityT,
DeSerializersT,
EdmT,
NullableT,
SelectableT,
PrecisionT
> {
/**
* Creates an instance of Filter for this field and the given value using the operator 'gt', i.e. `>`.
* @param value - Value to be used in the filter.
Expand Down
13 changes: 11 additions & 2 deletions packages/odata-v4/src/de-serializers/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,17 @@ export function deserializeDateTimeOffsetToMoment(
/**
* @internal
*/
export function serializeToDateTimeOffset(value: moment.Moment): string {
return value.utc().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z';
export function serializeToDateTimeOffset(
value: moment.Moment,
precision?: number
): string {
// If precision is defined, create a format with decimal places
if (precision !== undefined && precision !== null) {
const precisionFormat = new Array(precision).fill('S').join('');
return value.utc().format(`YYYY-MM-DDTHH:mm:ss.${precisionFormat}`) + 'Z';
}
// If no precision, return without decimal places
return value.utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
serializeToTime,
deserializeDateToMoment
} from './converters';
import type { Moment } from 'moment';
import type BigNumber from 'bignumber.js';
import type { Time } from '@sap-cloud-sdk/odata-common/internal';
import type { DeSerializers } from './de-serializers';
Expand Down Expand Up @@ -55,7 +56,8 @@ const defaultDeSerializersRaw: DefaultDeSerializers = {
},
'Edm.DateTimeOffset': {
deserialize: deserializeDateTimeOffsetToMoment,
serialize: serializeToDateTimeOffset,
serialize: (value: Moment, precision?: number) =>
serializeToDateTimeOffset(value, precision),
serializeToUri: (value, serialize) => serialize(value)
},
'Edm.Duration': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function createTestEntity() {
.keyPropertyGuid(keyPropGuid)
.keyPropertyString(keyPropString)
.keyDateProperty(keyPropDate)
.dateTimeOffSetProperty(keyPropDate)
.dateTimeOffSetProperty(moment())
.int32Property(int32Prop)
.build();
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e34eee9

Please sign in to comment.