Skip to content

Commit 40a4da0

Browse files
authored
v5.2.0
**Pricing2Yaml 2.1** Refer to the [docs](https://pricing4saas-docs.vercel.app) for more information
2 parents 4e45a0b + 0251cde commit 40a4da0

16 files changed

Lines changed: 107 additions & 34 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<groupId>io.github.isa-group</groupId>
99
<artifactId>Pricing4Java</artifactId>
10-
<version>5.1.1</version>
10+
<version>5.2.0</version>
1111

1212
<name>${project.groupId}:${project.artifactId}</name>
1313
<description>A pricing driven feature toggling library for java</description>

src/main/java/io/github/isagroup/models/PricingManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
@EqualsAndHashCode
1818
public class PricingManager {
1919

20-
private Version version;
20+
private Version syntaxVersion;
2121
private String saasName;
2222
private String url;
2323
private LocalDate createdAt;
24+
private String version;
2425
private String currency;
2526
private List<String> tags;
2627
private Map<String, Double> billing;

src/main/java/io/github/isagroup/services/parsing/PricingManagerParser.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.time.LocalDate;
44
import java.time.ZoneId;
5+
import java.time.format.DateTimeFormatter;
56
import java.time.format.DateTimeParseException;
67
import java.util.Date;
78
import java.util.HashMap;
@@ -59,29 +60,29 @@ protected static Double evaluateFormula(String price, PricingManager pricingMana
5960

6061
private static void setBasicAttributes(Map<String, Object> yamlConfigMap, PricingManager pricingManager) {
6162

62-
// -------------- version --------------
63+
// -------------- syntaxVersion --------------
6364

64-
Version version = null;
65+
Version syntaxVersion = null;
6566

6667
try {
67-
if (yamlConfigMap.get("version") == null) {
68+
if (yamlConfigMap.get("syntaxVersion") == null) {
6869
throw new PricingParsingException(
69-
"The version field of the pricing must not be null or undefined. Please ensure that the version field is present and correctly formatted");
70-
} else if (yamlConfigMap.get("version") instanceof Double
71-
|| yamlConfigMap.get("version") instanceof String) {
70+
"The syntaxVersion field of the pricing must not be null or undefined. Please ensure that the syntaxVersion field is present and correctly formatted");
71+
} else if (yamlConfigMap.get("syntaxVersion") instanceof Double
72+
|| yamlConfigMap.get("syntaxVersion") instanceof String) {
7273

73-
version = Version.version(yamlConfigMap.get("version"));
74+
syntaxVersion = Version.version(yamlConfigMap.get("syntaxVersion"));
7475

7576
} else {
7677
throw new PricingParsingException(
77-
String.format("'version' detected type is %s but 'version' type must be Double or String",
78-
yamlConfigMap.get("version").getClass().getSimpleName()));
78+
String.format("'syntaxVersion' detected type is %s but 'syntaxVersion' type must be Double or String",
79+
yamlConfigMap.get("syntaxVersion").getClass().getSimpleName()));
7980
}
8081
} catch (VersionException e) {
8182
throw new PricingParsingException(e.getMessage());
8283
}
8384

84-
pricingManager.setVersion(version);
85+
pricingManager.setSyntaxVersion(syntaxVersion);
8586

8687
// -------------- saasName --------------
8788

@@ -141,6 +142,17 @@ private static void setBasicAttributes(Map<String, Object> yamlConfigMap, Pricin
141142
yamlConfigMap.get("createdAt").getClass().getSimpleName()));
142143
}
143144

145+
// -------------- version --------------
146+
147+
if (yamlConfigMap.get("version") == null) {
148+
pricingManager.setVersion(pricingManager.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
149+
}else{
150+
if (!(yamlConfigMap.get("version") instanceof String)) {
151+
throw new PricingParsingException("'version' has to be a string");
152+
}
153+
pricingManager.setVersion((String) yamlConfigMap.get("version"));
154+
}
155+
144156
// -------------- currency --------------
145157

146158
if (yamlConfigMap.get("currency") == null) {

src/main/java/io/github/isagroup/services/serializer/PricingManagerSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public PricingManagerSerializer() {
2222

2323
public Map<String, Object> serialize(PricingManager pricingManager) throws SerializerException {
2424

25-
serializedPricingManager.put("version", Version.LATEST.toString());
25+
serializedPricingManager.put("syntaxVersion", Version.LATEST.toString());
2626
serializedPricingManager.put("saasName", pricingManager.getSaasName());
2727
serializedPricingManager.put("createdAt", pricingManager.getCreatedAt().toString());
28+
serializedPricingManager.put("version", pricingManager.getVersion());
2829
serializedPricingManager.put("url", pricingManager.getUrl());
2930
serializedPricingManager.put("tags", pricingManager.getTags());
3031
serializedPricingManager.put("billing", pricingManager.getBilling());
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.isagroup.services.updaters;
2+
3+
import java.util.Map;
4+
5+
import io.github.isagroup.exceptions.UpdateException;
6+
import io.github.isagroup.exceptions.VersionException;
7+
8+
public class V20ToV21Updater extends VersionUpdater {
9+
10+
public V20ToV21Updater(Updater updater) {
11+
super(Version.V2_0, updater);
12+
}
13+
14+
@Override
15+
public void update(Map<String, Object> configFile) throws UpdateException {
16+
17+
try {
18+
if (Version.version(configFile.get("version")).compare(this.getSource()) < 0) {
19+
super.update(configFile);
20+
21+
}
22+
} catch (VersionException e) {
23+
throw new UpdateException(e.getMessage(), configFile);
24+
}
25+
26+
refactorPricingVersion(configFile);
27+
}
28+
29+
private void refactorPricingVersion(Map<String,Object> configFile) {
30+
configFile.put("syntaxVersion", "2.1");
31+
32+
try{
33+
configFile.put("version", configFile.get("createdAt").toString());
34+
}catch(Exception e){
35+
configFile.put("version", "latest");
36+
}
37+
}
38+
39+
}

src/main/java/io/github/isagroup/services/updaters/Version.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
import java.util.regex.Pattern;
77

88
public enum Version {
9-
V1_0(1, 0), V1_1(1, 1), V2_0(2, 0);
9+
V1_0(1, 0), V1_1(1, 1), V2_0(2, 0), V2_1(2, 1);
1010

1111
private final int major;
1212
private final int minor;
1313

14-
public static final Version LATEST = V2_0;
14+
public static final Version LATEST = V2_1;
1515

1616
Version(int major, int minor) {
1717
if (!isValid(major, minor)) {
18-
throw new IllegalStateException(String.format("Version of yaml %d.%d is unsupported", major, minor));
18+
throw new IllegalStateException(String.format("Version of Pricing2Yaml %d.%d is unsupported", major, minor));
1919
}
2020

2121
this.major = major;
@@ -32,7 +32,7 @@ public static Version version(Object version) {
3232
} else if (version instanceof String) {
3333
return Version.version((String) version);
3434
} else {
35-
throw new VersionException("Cannot parse " + version + " to a version");
35+
throw new VersionException("Cannot parse " + version + " to a syntax version");
3636
}
3737

3838
}
@@ -45,7 +45,7 @@ private static Version version(Double version) {
4545
private static Version version(String version) {
4646

4747
if (version.isEmpty() || version.isBlank()) {
48-
throw new VersionException("Version is blank");
48+
throw new VersionException("SyntaxVersion is blank");
4949
}
5050

5151
String regex = "(\\d+)\\.(\\d+)";
@@ -54,7 +54,7 @@ private static Version version(String version) {
5454
Matcher matcher = pattern.matcher(version);
5555

5656
if (!matcher.matches()) {
57-
throw new VersionException("Invalid version \"" + version + "\", use <major>.<minor> version format");
57+
throw new VersionException("Invalid syntax version \"" + version + "\", use <major>.<minor> version format");
5858
}
5959

6060
int major;
@@ -77,7 +77,7 @@ private static Version version(String version) {
7777
Version versionObj = Version.version(major, minor);
7878

7979
if (versionObj == null) {
80-
throw new VersionException("Unsupported version " + major + "." + minor);
80+
throw new VersionException("Unsupported syntax version " + major + "." + minor);
8181
}
8282

8383
return versionObj;
@@ -97,6 +97,9 @@ public static Version version(int major, int minor) {
9797
if (minor == 0) {
9898
return V2_0;
9999
}
100+
if (minor == 1) {
101+
return V2_1;
102+
}
100103
}
101104

102105
return null;
@@ -112,7 +115,7 @@ public int getMinor() {
112115

113116
public static boolean isValid(int major, int minor) {
114117
boolean oneDotVersions = major == 1 && (minor == 0 || minor == 1);
115-
boolean twoDotVersions = major == 2 && minor == 0;
118+
boolean twoDotVersions = major == 2 && (minor == 0 || minor == 1);
116119
return oneDotVersions || twoDotVersions;
117120
}
118121

src/main/java/io/github/isagroup/services/updaters/YamlUpdater.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.LinkedHashMap;
44
import java.util.Map;
55

6+
import io.github.isagroup.exceptions.PricingParsingException;
67
import io.github.isagroup.exceptions.UpdateException;
78
import io.github.isagroup.exceptions.VersionException;
89

@@ -13,19 +14,35 @@ public class YamlUpdater {
1314
static {
1415
updaters.put(Version.V1_0, new V10ToV11Updater(null));
1516
updaters.put(Version.V1_1, new V11ToV20Updater(updaters.get(Version.V1_0)));
17+
updaters.put(Version.V2_0, new V20ToV21Updater(updaters.get(Version.V1_1)));
1618
}
1719

1820
public static void update(Map<String, Object> configFile) throws UpdateException {
1921

20-
if (configFile.get("version") == null) {
21-
throw new VersionException("The version field of the pricing must not be null or undefined. Please ensure that the version field is present and correctly formatted");
22+
if (configFile.get("syntaxVersion") == null && configFile.get("version") == null) {
23+
throw new VersionException("The syntax version field of the pricing must not be null or undefined. Please ensure that the version field is present and correctly formatted");
2224
}
2325

24-
Version version = Version.version(configFile.get("version"));
25-
if (updaters.get(version) == null) {
26-
return;
26+
27+
Object versionField = configFile.get("syntaxVersion");
28+
29+
if (versionField == null) {
30+
versionField = configFile.get("version");
31+
}
32+
33+
if (versionField instanceof String string && (string.isBlank() || string.isEmpty())){
34+
throw new PricingParsingException("The syntax version field of the pricing must not be empty. Please ensure that the syntax version field is present and correctly formatted");
2735
}
2836

29-
updaters.get(Version.V1_1).update(configFile);
37+
if (versionField instanceof Double || versionField instanceof String) {
38+
Version version = Version.version(versionField);
39+
if (updaters.get(version) == null) {
40+
return;
41+
}
42+
43+
updaters.get(Version.V2_0).update(configFile);
44+
}else{
45+
throw new PricingParsingException("The syntax version field of the pricing must be a string or a double. Please ensure that the version field is present and correctly formatted");
46+
}
3047
}
3148
}

src/test/java/io/github/isagroup/parsing/positive/PricingManagerParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,6 @@ void givenVersionShouldParse(String input) {
169169

170170
String path = String.format(TEST_CASES + "version/%s.yml", input);
171171
PricingManager pricingManager = YamlUtils.retrieveManagerFromYaml(path);
172-
assertInstanceOf(Version.class, pricingManager.getVersion());
172+
assertInstanceOf(Version.class, pricingManager.getSyntaxVersion());
173173
}
174174
}

src/test/java/io/github/isagroup/updaters/UpdatersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void givenInvalidVersionFormatShouldThrow() {
4545
try {
4646
Version.version("alpha");
4747
} catch (VersionException e) {
48-
assertEquals("Invalid version \"alpha\", use <major>.<minor> version format", e.getMessage());
48+
assertEquals("Invalid syntax version \"alpha\", use <major>.<minor> version format", e.getMessage());
4949
}
5050
}
5151

src/test/resources/negative-parsing-tests.csv

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ Throw an error if 'url' is a plain string;parsing/negative/url/invalid-string-ur
7272
Throw an error if 'url' is not a string;parsing/negative/url/number-url.yml;If 'url' field is used, it has to be a string
7373
# variables
7474
Throw an error if 'variables' is not a map;parsing/negative/variables/variables-is-boolean.yml;variables must be a map but found Boolean instead
75-
# version
76-
Throw an error if 'version' is bad formated;parsing/negative/version/pricing-invalid-format-version.yml;Invalid version "1.0.0", use <major>.<minor> version format
77-
Throw an error if 'version' is not a string like <major>.<minor>;parsing/negative/version/pricing-invalid-type-version.yml;Cannot parse 100 to a version
78-
Throw an error if 'version' is missing;parsing/negative/version/pricing-no-version.yml;The version field of the pricing must not be null or undefined. Please ensure that the version field is present and correctly formatted
79-
Throw an error if 'version' is null;parsing/negative/version/pricing-null-version.yml;The version field of the pricing must not be null or undefined. Please ensure that the version field is present and correctly formatted
75+
# syntaxVersion
76+
Throw an error if 'syntaxVersion' is bad formated;parsing/negative/syntaxVersion/pricing-invalid-format-version.yml;Invalid syntax version "1.0.0", use <major>.<minor> version format
77+
Throw an error if 'syntaxVersion' is not a string like <major>.<minor>;parsing/negative/syntaxVersion/pricing-invalid-type-version.yml;The syntax version field of the pricing must be a string or a double. Please ensure that the version field is present and correctly formatted
78+
Throw an error if 'syntaxVersion' is missing;parsing/negative/syntaxVersion/pricing-no-version.yml;The syntax version field of the pricing must not be null or undefined. Please ensure that the version field is present and correctly formatted
79+
Throw an error if 'syntaxVersion' is null;parsing/negative/syntaxVersion/pricing-null-version.yml;The syntax version field of the pricing must not be null or undefined. Please ensure that the version field is present and correctly formatted
8080
# other checks
8181
Throw an error if 'plans' and 'addOns' are null at the same time;parsing/negative/null-plans-and-addons.yml;The pricing manager does not have any plans or add ons

0 commit comments

Comments
 (0)