Skip to content

Commit

Permalink
Merge branch 'weighted-ig-generation-formatter' of https://github.com…
Browse files Browse the repository at this point in the history
…/CyberDrudge/ranger into add-suffix-formatter
  • Loading branch information
Satyam Saxena committed Feb 27, 2025
2 parents fb88bce + dea19d7 commit eb1ed29
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public Base36IdFormatter(IdFormatter idFormatter) {
}

@Override
public int getType() {
return 1;
public IdParserType getType() {
throw new UnsupportedOperationException();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class DefaultIdFormatter implements IdFormatter {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormat.forPattern("yyMMddHHmmssSSS");

@Override
public int getType() {
return 0;
public IdParserType getType() {
return IdParserType.DEFAULT;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public interface IdFormatter {

int getType();
IdParserType getType();

String format(final DateTime dateTime,
final int nodeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public static IdFormatter original() {
return originalIdFormatter;
}

public static IdFormatter suffix() {
return suffixIdFormatter;
}

public static IdFormatter base36() {
return base36IdFormatter;
}

public static IdFormatter suffix() {
return suffixIdFormatter;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.appform.ranger.discovery.bundle.id.formatter;

import lombok.Getter;

@Getter
public enum IdParserType {
DEFAULT (0),
SUFFIX (11);

private final int value;

IdParserType(final int value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;


@Slf4j
@UtilityClass
public class IdParsers {
private static final int MINIMUM_ID_LENGTH = 22;
private static final Pattern PATTERN = Pattern.compile("(.*)([0-9]{22})");
private static final Pattern PATTERN = Pattern.compile("([A-Za-z]*)([0-9]{22})([0-9]{2})?(.*)");

private final Map<Integer, IdFormatter> parserRegistry = Map.of(
IdFormatters.original().getType().getValue(), IdFormatters.original(),
IdFormatters.suffix().getType().getValue(), IdFormatters.suffix()
);

/**
* Parse the given string to get ID
Expand All @@ -44,7 +51,18 @@ public Optional<Id> parse(final String idString) {
if (!matcher.find()) {
return Optional.empty();
}
return IdFormatters.original().parse(idString);

val parserType = matcher.group(3);
if (parserType == null) {
return IdFormatters.original().parse(idString);
}

val parser = parserRegistry.get(Integer.parseInt(matcher.group(3)));
if (parser == null) {
log.warn("Could not parse idString {}, Invalid formatter type {}", idString, parserType);
return Optional.empty();
}
return parser.parse(idString);
} catch (Exception e) {
log.warn("Could not parse idString {}", e.getMessage());
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
import java.util.regex.Pattern;

public class SuffixIdFormatter implements IdFormatter {
private static final int TYPE = 10;
private static final Pattern PATTERN = Pattern.compile("([A-Za-z]*)([0-9]{15})([0-9]{4})([0-9]{3})([0-9]{2})([0-9]*)");
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormat.forPattern("yyMMddHHmmssSSS");

@Override
public int getType() {
return TYPE;
public IdParserType getType() {
return IdParserType.SUFFIX;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ void testParseSuccess() {
id.getGeneratedDate());
}

@Test
void testParseFailAfterGeneration() {
val generatedId = IdGenerator.generate("TEST123");
val parsedId = IdGenerator.parse(generatedId.getId()).orElse(null);
Assertions.assertNull(parsedId);
}

@Test
void testParseSuccessAfterGeneration() {
val prefix = "TEST";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;

public class IdFormattersTest {
public class IdParsersTest {

@Test
void testDefaultId() throws ParseException {
Expand Down

0 comments on commit eb1ed29

Please sign in to comment.