-
Notifications
You must be signed in to change notification settings - Fork 23
Add Suffix Formatter #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CyberDrudge
wants to merge
12
commits into
appform-io:main
Choose a base branch
from
CyberDrudge:add-suffix-formatter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
baff7e1
Add formatterRegistry
6f3679f
Add braces
136501b
Add support for suffix
1d8aab8
Add SuffixIdFormatter
3e21810
Merge branch 'main' of https://github.com/appform-io/ranger into weig…
376ee85
Update IdParsersTest
2e906e2
Update Parser test case
6eeb30f
Add Exception for Base36IdFormatter type
60f2ea9
Add test case for numeric prefix
dea19d7
Add IdParserType enum
fb88bce
Merge branch 'main' of https://github.com/appform-io/ranger into add-…
64b9b0f
Merge branch 'weighted-ig-generation-formatter' of https://github.com…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,5 +4,5 @@ | |
| "iterations" : 4, | ||
| "threads" : 1, | ||
| "forks" : 3, | ||
| "mean_ops" : 823635.7718335792 | ||
| "mean_ops" : 824615.1373750888 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,5 +4,5 @@ | |
| "iterations" : 4, | ||
| "threads" : 1, | ||
| "forks" : 3, | ||
| "mean_ops" : 655010.8023043653 | ||
| "mean_ops" : 668869.350006137 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ry-bundle/src/main/java/io/appform/ranger/discovery/bundle/id/formatter/IdParserType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ndle/src/main/java/io/appform/ranger/discovery/bundle/id/formatter/SuffixIdFormatter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright 2024 Authors, Flipkart Internet Pvt. Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.appform.ranger.discovery.bundle.id.formatter; | ||
|
|
||
| import io.appform.ranger.discovery.bundle.id.Id; | ||
| import lombok.val; | ||
| import org.joda.time.DateTime; | ||
| import org.joda.time.format.DateTimeFormat; | ||
| import org.joda.time.format.DateTimeFormatter; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class SuffixIdFormatter implements IdFormatter { | ||
| 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 IdParserType getType() { | ||
| return IdParserType.SUFFIX; | ||
| } | ||
|
|
||
| @Override | ||
| public String format(final DateTime dateTime, | ||
| final int nodeId, | ||
| final int randomNonce) { | ||
| return String.format("%s%04d%03d%02d", DATE_TIME_FORMATTER.print(dateTime), nodeId, randomNonce, getType().getValue()); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Id> parse(final String idString) { | ||
| val matcher = PATTERN.matcher(idString); | ||
| if (!matcher.find()) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(Id.builder() | ||
| .id(idString) | ||
| .prefix(matcher.group(1)) | ||
| .suffix(matcher.group(6)) | ||
| .node(Integer.parseInt(matcher.group(3))) | ||
| .exponent(Integer.parseInt(matcher.group(4))) | ||
| .generatedDate(DATE_TIME_FORMATTER.parseDateTime(matcher.group(2)).toDate()) | ||
| .build()); | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
...dle/src/main/java/io/appform/ranger/discovery/bundle/id/generator/DefaultIdGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| package io.appform.ranger.discovery.bundle.id.generator; | ||
|
|
||
| import io.appform.ranger.discovery.bundle.id.formatter.IdFormatter; | ||
| import io.appform.ranger.discovery.bundle.id.formatter.IdFormatters; | ||
| import io.appform.ranger.discovery.bundle.id.nonce.NonceGenerator; | ||
| import io.appform.ranger.discovery.bundle.id.nonce.RandomNonceGenerator; | ||
|
|
||
| public class DefaultIdGenerator extends IdGeneratorBase { | ||
|
|
||
| public DefaultIdGenerator() { | ||
| super(IdFormatters.original(), new RandomNonceGenerator()); | ||
| } | ||
|
|
||
| public DefaultIdGenerator(final IdFormatter idFormatter) { | ||
| super(idFormatter, new RandomNonceGenerator()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| @Builder | ||
| public class IdGenerationInput { | ||
| String prefix; | ||
| String suffix; | ||
| Domain domain; | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...r-discovery-bundle/src/test/java/io/appform/ranger/discovery/bundle/id/IdParsersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package io.appform.ranger.discovery.bundle.id; | ||
|
|
||
| import io.appform.ranger.discovery.bundle.id.formatter.IdFormatters; | ||
| import io.appform.ranger.discovery.bundle.id.formatter.IdParsers; | ||
| import io.appform.ranger.discovery.bundle.id.generator.DefaultIdGenerator; | ||
| import lombok.val; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
|
|
||
| public class IdParsersTest { | ||
|
|
||
| @Test | ||
| void testDefaultId() throws ParseException { | ||
| val id = "T2407101232336168748798"; | ||
| val parsedId = IdParsers.parse(id).orElse(null); | ||
| Assertions.assertNotNull(parsedId); | ||
| Assertions.assertEquals(id, parsedId.getId()); | ||
| Assertions.assertEquals(798, parsedId.getExponent()); | ||
| Assertions.assertEquals(8748, parsedId.getNode()); | ||
| assertDate("240710123233616", parsedId.getGeneratedDate()); | ||
| } | ||
|
|
||
| private void assertDate(final String dateString, final Date date) throws ParseException { | ||
| Assertions.assertEquals(new SimpleDateFormat("yyMMddHHmmssSSS").parse(dateString), date); | ||
| } | ||
|
|
||
| @Test | ||
| void testParseSuccessAfterGenerationWithSuffix() { | ||
| val idGenerator = new DefaultIdGenerator(IdFormatters.suffix()); | ||
| val prefix = "TEST"; | ||
| val suffix = "007"; | ||
| val generatedId = idGenerator.generate(prefix, suffix); | ||
| val parsedId = IdGenerator.parse(generatedId.getId()).orElse(null); | ||
| Assertions.assertNotNull(parsedId); | ||
| Assertions.assertEquals(prefix, parsedId.getPrefix()); | ||
| Assertions.assertEquals(suffix, parsedId.getSuffix()); | ||
| Assertions.assertEquals(parsedId.getId(), generatedId.getId()); | ||
| Assertions.assertEquals(parsedId.getExponent(), generatedId.getExponent()); | ||
| Assertions.assertEquals(parsedId.getNode(), generatedId.getNode()); | ||
| Assertions.assertEquals(parsedId.getGeneratedDate(), generatedId.getGeneratedDate()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@r0goyal To maintain consistency, shouldnt it be DEFAULT(00), SUFFIX(01) ?