Skip to content

Commit

Permalink
consolidate methods splitTopicExcludingSeparator
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Jan 16, 2025
1 parent 2d849b0 commit 9ed5663
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,37 +250,35 @@ public static List<String> splitTopicIncludingSeparatorAsList(String topic) {
Arrays.asList(Mapping.splitTopicIncludingSeparatorAsArray(topic)));
}

public static String[] splitTopicExcludingSeparatorAsArray(String topic) {
topic = topic.trim().replaceAll("(\\/{1,}$)|(^\\/{1,})", "");
return topic.split("\\/");
}

public static String[] splitTopicExcludingSeparatorIncludingLeagingSlashAsArray(String topic) {
String topix = topic.trim().replaceAll("\\/{1,}$", ""); // Remove trailing slashes only
if (topix.startsWith("//")) { // If multiple leading slashes
topix = "/" + topix.replaceAll("^/+", ""); // Replace with single slash
}
public static String[] splitTopicExcludingSeparatorAsArray(String topic, boolean cutOffLeadingSlash) {
String topix = topic.trim();

// Special handling for the first slash
if (topix.startsWith("/")) {
String[] parts = topix.substring(1).split("\\/");
String[] result = new String[parts.length + 1];
result[0] = "/";
System.arraycopy(parts, 0, result, 1, parts.length);
return result;
if (cutOffLeadingSlash) {
// Original behavior: remove both leading and trailing slashes
topix = topix.replaceAll("(\\/{1,}$)|(^\\/{1,})", "");
return topix.split("\\/");
} else {
// New behavior: keep leading slash, remove only trailing slashes
topix = topix.replaceAll("\\/{1,}$", "");
if (topix.startsWith("//")) {
topix = "/" + topix.replaceAll("^/+", "");
}

if (topix.startsWith("/")) {
String[] parts = topix.substring(1).split("\\/");
String[] result = new String[parts.length + 1];
result[0] = "/";
System.arraycopy(parts, 0, result, 1, parts.length);
return result;
}

return topix.split("\\/");
}

return topix.split("\\/");
}

public static List<String> splitTopicExcludingSeparatorAsList(String topic) {
return new ArrayList<String>(
Arrays.asList(Mapping.splitTopicExcludingSeparatorAsArray(topic)));
}

public static List<String> splitTopicExcludingSeparatorIncludingLeagingSlashAsList(String topic) {
public static List<String> splitTopicExcludingSeparatorAsList(String topic, boolean cutOffLeadingSlash) {
return new ArrayList<String>(
Arrays.asList(Mapping.splitTopicExcludingSeparatorIncludingLeagingSlashAsArray(topic)));
Arrays.asList(Mapping.splitTopicExcludingSeparatorAsArray(topic, cutOffLeadingSlash)));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void enrichPayload(ProcessingContext<T> context) {
String tenant = context.getTenant();
Object payloadObject = context.getPayload();

List<String> splitTopicAsList = Mapping.splitTopicExcludingSeparatorIncludingLeagingSlashAsList(context.getTopic());
List<String> splitTopicAsList = Mapping.splitTopicExcludingSeparatorAsList(context.getTopic(), false);
if (payloadObject instanceof Map) {
((Map) payloadObject).put(Mapping.TOKEN_TOPIC_LEVEL, splitTopicAsList);
if (context.isSupportsMessageContext() && context.getKey() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public ProcessingContext<T> substituteInTargetAndSend(ProcessingContext<T> conte
* step 0 patch payload with dummy property _TOPIC_LEVEL_ in case the content
* is required in the payload for a substitution
*/
List<String> splitTopicExAsList = Mapping.splitTopicExcludingSeparatorIncludingLeagingSlashAsList(context.getTopic());
List<String> splitTopicExAsList = Mapping.splitTopicExcludingSeparatorAsList(context.getTopic(), false);
payloadTarget.put("$", Mapping.TOKEN_TOPIC_LEVEL, splitTopicExAsList);
if (mapping.supportsMessageContext) {
Map<String, String> cod = new HashMap<String, String>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
RepairStrategy,
getPathTargetForDeviceIdentifiers, transformGenericPath2C8YPath
} from '../../../shared';
import { splitTopicExcludingSeparatorIncludingLeagingSlash } from '../../shared/util';
import { splitTopicExcludingSeparator } from '../../shared/util';
import { C8YAgent } from '../c8y-agent.service';
import {
IDENTITY,
Expand Down Expand Up @@ -65,7 +65,7 @@ export abstract class BaseProcessorInbound {

enrichPayload(context: ProcessingContext): void {
const { payload } = context;
const topicLevels = splitTopicExcludingSeparatorIncludingLeagingSlash(context.topic);
const topicLevels = splitTopicExcludingSeparator(context.topic, false);
payload[TOKEN_TOPIC_LEVEL] = topicLevels;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import * as _ from 'lodash';
import { API, Mapping, MappingType, RepairStrategy } from '../../../shared';
import {
TOKEN_TOPIC_LEVEL,
splitTopicExcludingSeparatorIncludingLeagingSlash,
splitTopicExcludingSeparator,
splitTopicIncludingSeparator
} from '../../shared/util';
import { C8YAgent } from '../c8y-agent.service';
Expand Down Expand Up @@ -72,8 +72,8 @@ export abstract class BaseProcessorOutbound {
* step 0 patch payload with dummy property _TOPIC_LEVEL_ in case the content
* is required in the payload for a substitution
*/
const splitTopicExAsList: string[] = splitTopicExcludingSeparatorIncludingLeagingSlash(
context.topic
const splitTopicExAsList: string[] = splitTopicExcludingSeparator(
context.topic, false
);
payloadTarget[TOKEN_TOPIC_LEVEL] = splitTopicExAsList;
const deviceSource: string = 'undefined';
Expand Down
45 changes: 23 additions & 22 deletions dynamic-mapping-ui/src/mapping/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,27 @@ export const TOKEN_CONTEXT_DATA = '_CONTEXT_DATA_';
export const CONTEXT_DATA_KEY_NAME = 'key';
export const TIME = 'time';

export function splitTopicExcludingSeparator(topic: string): string[] {
let topix = topic;
topix = topix.trim().replace(/(\/{1,}$)|(^\/{1,})/g, '');
return topix.split(/\//g);
}

export function splitTopicExcludingSeparatorIncludingLeagingSlash(topic: string): string[] {
let topix = topic;
topix = topix.trim().replace(/(\/{1,}$)/g, ''); // Remove trailing slashes
if (topix.startsWith('//')) { // If there are multiple leading slashes
topix = '/' + topix.replace(/^\/+/, ''); // Replace with single slash
}
export function splitTopicExcludingSeparator(topic: string, cutOffLeadingSlash: boolean): string[] {
let topix = topic.trim();

// Special handling for the first slash
if (topix.startsWith('/')) {
const parts = topix.substring(1).split(/\//g);
return ['/'].concat(parts);
if (cutOffLeadingSlash) {
// Original behavior: remove both leading and trailing slashes
topix = topix.replace(/(\/{1,}$)|(^\/{1,})/g, '');
return topix.split(/\//g);
} else {
// New behavior: keep leading slash, remove only trailing slashes
topix = topix.replace(/\/{1,}$/g, '');
if (topix.startsWith('//')) {
topix = '/' + topix.replace(/^\/+/, '');
}

if (topix.startsWith('/')) {
const parts = topix.substring(1).split(/\//g);
return ['/'].concat(parts);
}

return topix.split(/\//g);
}

return topix.split(/\//g);
}

export function splitTopicIncludingSeparator(topic: string): string[] {
Expand Down Expand Up @@ -204,9 +205,9 @@ export function checkTopicsInboundAreValid(control: AbstractControl) {
};
}

const splitTT: string[] = splitTopicExcludingSeparator(mappingTopic.value);
const splitTT: string[] = splitTopicExcludingSeparator(mappingTopic.value, false);
const splitTTS: string[] = splitTopicExcludingSeparator(
mappingTopicSample.value
mappingTopicSample.value, false
);
if (splitTT.length != splitTTS.length) {
errors = {
Expand Down Expand Up @@ -322,9 +323,9 @@ export function checkTopicsOutboundAreValid(control: AbstractControl) {
};
}

const splitPT: string[] = splitTopicExcludingSeparator(publishTopic.value);
const splitPT: string[] = splitTopicExcludingSeparator(publishTopic.value, false);
const splitTTS: string[] = splitTopicExcludingSeparator(
publishTopicSample.value
publishTopicSample.value, false
);
if (splitPT.length != splitTTS.length) {
errors = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import {
expandExternalTemplate,
isTypeOf,
reduceSourceTemplate,
splitTopicExcludingSeparatorIncludingLeagingSlash
splitTopicExcludingSeparator
} from '../shared/util';
import { EditSubstitutionComponent } from '../substitution/edit/edit-substitution-modal.component';
import { SubstitutionRendererComponent } from '../substitution/substitution-grid.component';
Expand Down Expand Up @@ -566,8 +566,8 @@ export class MappingStepperComponent implements OnInit, OnDestroy {
this.mapping
);
} else {
const levels: string[] = splitTopicExcludingSeparatorIncludingLeagingSlash(
this.mapping.mappingTopicSample
const levels: string[] = splitTopicExcludingSeparator(
this.mapping.mappingTopicSample, false
);
this.targetTemplate = expandExternalTemplate(
JSON.parse(getExternalTemplate(this.mapping)),
Expand Down Expand Up @@ -694,10 +694,10 @@ export class MappingStepperComponent implements OnInit, OnDestroy {
}

private expandTemplates() {
const levels: string[] = splitTopicExcludingSeparatorIncludingLeagingSlash(
const levels: string[] = splitTopicExcludingSeparator(
this.mapping.direction == Direction.INBOUND
? this.mapping.mappingTopicSample
: this.mapping.publishTopicSample
: this.mapping.publishTopicSample, false
);

if (
Expand Down Expand Up @@ -772,7 +772,7 @@ export class MappingStepperComponent implements OnInit, OnDestroy {
this.sourceTemplate = expandExternalTemplate(
this.sourceTemplate,
this.mapping,
splitTopicExcludingSeparatorIncludingLeagingSlash(this.mapping.mappingTopicSample)
splitTopicExcludingSeparator(this.mapping.mappingTopicSample, false)
);
} else {
this.sourceTemplate = expandC8YTemplate(
Expand Down Expand Up @@ -800,7 +800,7 @@ export class MappingStepperComponent implements OnInit, OnDestroy {
this.sourceTemplate = expandExternalTemplate(
this.sourceTemplate,
this.mapping,
splitTopicExcludingSeparatorIncludingLeagingSlash(this.mapping.mappingTopicSample)
splitTopicExcludingSeparator(this.mapping.mappingTopicSample, false)
);
} else {
this.sourceTemplate = expandC8YTemplate(
Expand Down

0 comments on commit 9ed5663

Please sign in to comment.