Skip to content

Commit

Permalink
chore: use map to store source version mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
anantjain45823 committed Nov 7, 2023
1 parent 34bfbf1 commit 9e03d53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
49 changes: 25 additions & 24 deletions src/controllers/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import {
RudderMessage,
SourceInput,
} from '../../types';
import { getValueFromMessage, isDefinedAndNotNull } from '../../v0/util';
import { getValueFromMessage } from '../../v0/util';
import genericFieldMap from '../../v0/util/data/GenericFieldMapping.json';
import { EventType, MappedToDestinationKey } from '../../constants';

export default class ControllerUtility {

private static sourceVersionMap: object;
private static sourceVersionMap: Map<string, string> = new Map();

public static timestampValsMap: Record<string, string[]> = {
[EventType.IDENTIFY]: [
Expand All @@ -29,42 +28,44 @@ export default class ControllerUtility {
[EventType.TRACK]: [`properties.${RETL_TIMESTAMP}`, ...genericFieldMap.timestamp],
};




private static getSourceVersionsMap(): object {
if (isDefinedAndNotNull(this.sourceVersionMap)) {
private static getSourceVersionsMap(): Map<string, any> {
if (this.sourceVersionMap.size > 0) {
return this.sourceVersionMap;
}
const versions = ["v0", "v1"];
const sourceToVersionMap = {};
// TODO: USE MAP instead object
versions.forEach(version => {
const files = fs.readdirSync(path.resolve(__dirname, `../../${version}/sources`), { withFileTypes: true });
const sources = files.filter(file => file.isDirectory()).map(folder => folder.name)
sources.forEach(source => {
sourceToVersionMap[source] = version
const versions = ['v0', 'v1'];
versions.forEach((version) => {
const files = fs.readdirSync(path.resolve(__dirname, `../../${version}/sources`), {
withFileTypes: true,
});
const sources = files.filter((file) => file.isDirectory()).map((folder) => folder.name);
sources.forEach((source) => {
this.sourceVersionMap.set(source, version);
});
});
this.sourceVersionMap = sourceToVersionMap;
return this.sourceVersionMap
return this.sourceVersionMap;
}

private static convertSourceInputv1Tov0(sourceEvents: SourceInput[]): NonNullable<unknown>[] {
return sourceEvents.map(sourceEvent => sourceEvent.event);
return sourceEvents.map((sourceEvent) => sourceEvent.event);
}

private static convertSourceInputv0Tov1(sourceEvents: unknown[]): SourceInput[] {
return sourceEvents.map(sourceEvent => ({ event: sourceEvent, source: undefined } as SourceInput));
return sourceEvents.map(
(sourceEvent) => ({ event: sourceEvent, source: undefined } as SourceInput),
);
}

public static adaptInputToVersion(sourceType: string, requestVersion: string, input: NonNullable<unknown>[]): { implementationVersion: string, input: NonNullable<unknown>[] } {
public static adaptInputToVersion(
sourceType: string,
requestVersion: string,
input: NonNullable<unknown>[],
): { implementationVersion: string; input: NonNullable<unknown>[] } {
const sourceToVersionMap = this.getSourceVersionsMap();
const implementationVersion = sourceToVersionMap[sourceType];
const implementationVersion = sourceToVersionMap.get(sourceType);
let updatedInput: NonNullable<unknown>[] = input;
if (requestVersion === "v0" && implementationVersion === "v1") {
if (requestVersion === 'v0' && implementationVersion === 'v1') {
updatedInput = this.convertSourceInputv0Tov1(input);
} else if (requestVersion === "v1" && implementationVersion === "v0") {
} else if (requestVersion === 'v1' && implementationVersion === 'v0') {
updatedInput = this.convertSourceInputv1Tov0(input as SourceInput[]);
}
return { implementationVersion, input: updatedInput };
Expand Down
2 changes: 1 addition & 1 deletion src/services/source/nativeIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class NativeIntegrationSourceService implements IntegrationSource


public async sourceTransformRoutine(
sourceEvents: unknown[],
sourceEvents: NonNullable<unknown>[],
sourceType: string,
version: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ type Source = {
Enabled: boolean;
WorkspaceID: string;
WriteKey: string
Transformations: UserTransformationInput[];
Transformations?: UserTransformationInput[];
RevisionID?: string;
Destinations: Destination[]
Destinations?: Destination[]
Transient: boolean
EventSchemasEnabled: boolean
DgSourceTrackingPlanConfig: DgSourceTrackingPlanConfigT
Expand Down

0 comments on commit 9e03d53

Please sign in to comment.