Skip to content

Commit 727ca33

Browse files
committed
Update HasFeature to use JSON array all the way
1 parent ed7d850 commit 727ca33

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/main/java/io/cryptolens/methods/Helpers.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package io.cryptolens.methods;
22

3-
import com.google.gson.Gson;
43
import com.google.gson.JsonArray;
54
import com.google.gson.JsonParser;
6-
import com.google.gson.reflect.TypeToken;
5+
76
import io.cryptolens.internal.BasicResult;
87
import io.cryptolens.models.ActivatedMachine;
98
import io.cryptolens.models.LicenseKey;
@@ -13,11 +12,9 @@
1312
import oshi.hardware.HardwareAbstractionLayer;
1413
import oshi.software.os.OperatingSystem;
1514

16-
import java.lang.reflect.Type;
1715
import java.nio.charset.StandardCharsets;
1816
import java.security.MessageDigest;
19-
import java.util.HashMap;
20-
import java.util.HashSet;
17+
2118

2219
/**
2320
* A collection of helper methods that operate on a license key.
@@ -209,10 +206,11 @@ public class MyClass<T>
209206

210207
/**
211208
* <p>Uses the notes field to determine if a certain feature exists (instead of the 8 feature flags).</p>
212-
* <p><strong>Formatting: </strong> <p>The notes field needs to be formatted as a JSON array of strings or objects that contain </p>
213-
* For example, ["f1", "f2"] means f1 and f2 are true. You can also have feature bundling, eg. ["f1", {"f2": ["voice","image"]}],
209+
* <p><strong>Formatting: </strong> <p>The notes field needs to be formatted as a JSON array of strings or of JSON arrays
210+
* where the first element specifies the feature name and the second element is a list of features.</p>
211+
* For example, ["f1", "f2"] means f1 and f2 are true. You can also have feature bundling, eg. ["f1", ["f2",["voice","image"]]],
214212
* which means that f1 and f2 are true, as well as f2.limited and f2.image. You can set any depth, eg. you can have
215-
* ["f1", {"f2":[{"voice":["all"]}, "image"]}] means f2.voice.all is true as well as f2.voice and f2.
213+
* ["f1", ["f2",[["voice",["all"]], "image"]]] means f2.voice.all is true as well as f2.voice and f2.
216214
* The dots symbol is used to specify the "sub-features".
217215
* </p>
218216
* @param licenseKey a license key object.
@@ -227,13 +225,14 @@ public static boolean HasFeature(LicenseKey licenseKey, String featureName) {
227225

228226
boolean found = false;
229227
for(int i = 0; i < featurePath.length; i++) {
228+
found = false;
230229
int index = -1;
231230
for(int j = 0; j < array.size(); j++) {
232231

233-
if(!array.get(j).isJsonObject() && array.get(j).getAsString().equals(featurePath[i])) {
232+
if(!array.get(j).isJsonArray() && array.get(j).getAsString().equals(featurePath[i])) {
234233
found = true;
235234
break;
236-
} else if (array.get(j).isJsonObject() && array.get(j).getAsJsonObject().keySet().contains(featurePath[i])){
235+
} else if (array.get(j).isJsonArray() && array.get(j).getAsJsonArray().get(0).getAsString().equals(featurePath[i])){
237236
found = true;
238237
index = j;
239238
break;
@@ -244,8 +243,9 @@ public static boolean HasFeature(LicenseKey licenseKey, String featureName) {
244243
}
245244
if(i + 1 < featurePath.length && index != -1) {
246245
// still have some sub features to go through.
247-
array = array.get(index).getAsJsonObject().get(featurePath[i]).getAsJsonArray();
248-
found = false;
246+
// TODO: need to check if it's actually a json array or null?
247+
// TODO: try catch
248+
array = array.get(index).getAsJsonArray().get(1).getAsJsonArray();
249249
}
250250
}
251251

src/test/java/io/cryptolens/HelpersTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ public static Test suite()
3636
public void testApp()
3737
{
3838
LicenseKey license = new LicenseKey();
39-
license.Notes = "[\"test\", {\"module\":[\"A\"]}]";
39+
license.Notes = "[\"test\", [\"module\",[\"A\"]]]";
4040

4141
assertTrue(Helpers.HasFeature(license, "test"));
4242
assertTrue(Helpers.HasFeature(license, "module"));
4343
assertTrue(Helpers.HasFeature(license, "module.A"));
4444
assertFalse(Helpers.HasFeature(license, "module.B"));
4545

46-
license.Notes = "[\"f1\", {\"f2\":[{\"voice\":[\"all\"]}]}]";
46+
license.Notes = "[\"f1\", [\"f2\",[[\"voice\",[\"all\"]]]]]";
4747

4848
assertTrue(Helpers.HasFeature(license, "f2.voice.all"));
4949

50+
assertFalse(Helpers.HasFeature(license, "f2.voice.all.test"));
51+
assertFalse(Helpers.HasFeature(license, "f2.voice.all.test.a"));
52+
assertFalse(Helpers.HasFeature(license, "f2.A.all.test"));
53+
assertFalse(Helpers.HasFeature(license, "aa.voice.all.test"));
5054

5155
String[] featurePath = "moduleA.video".split(".");
5256

0 commit comments

Comments
 (0)