Skip to content

Commit 32e127c

Browse files
committed
Closes #17
1 parent c57ee5b commit 32e127c

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,8 @@ public class MyClass<T>
205205
}
206206

207207
/**
208-
* <p>Uses the notes field to determine if a certain feature exists (instead of the 8 feature flags).</p>
209-
* <p><strong>Formatting: </strong> 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>
208+
* <p>Uses a special data object associated with the license key to determine if a certain feature exists (instead of the 8 feature flags).</p>
209+
* <p><strong>Formatting: </strong> The name of the data object should be 'cryptolens_features' and it should be structured as a JSON array.</p>
211210
* <p>For example,</p> <pre>["f1", "f2"]</pre><p>means f1 and f2 are true. You can also have feature bundling, eg. </p> <pre>["f1", ["f2",["voice","image"]]]</pre>
212211
* <p>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</p>
213212
* <pre>["f1", ["f2",[["voice",["all"]], "image"]]]</pre> <p>means f2.voice.all is true as well as f2.voice and f2.
@@ -218,8 +217,24 @@ public class MyClass<T>
218217
*/
219218
public static boolean HasFeature(LicenseKey licenseKey, String featureName) {
220219

220+
if(licenseKey.DataObjects == null) {
221+
return false;
222+
}
223+
224+
String features = null;
225+
for(int i = 0; i < licenseKey.DataObjects.size(); i++) {
226+
if(licenseKey.DataObjects.get(i).Name.equals("cryptolens_features")) {
227+
features = licenseKey.DataObjects.get(i).StringValue;
228+
}
229+
}
230+
231+
if(features == null) {
232+
// data object not found.
233+
return false;
234+
}
235+
221236
JsonParser parser = new JsonParser();
222-
JsonArray array = parser.parse(licenseKey.Notes).getAsJsonArray();
237+
JsonArray array = parser.parse(features).getAsJsonArray();
223238
String[] featurePath = featureName.split("\\.");
224239

225240
boolean found = false;

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

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

33
import io.cryptolens.methods.Helpers;
4+
import io.cryptolens.models.DataObject;
45
import io.cryptolens.models.LicenseKey;
56
import junit.framework.Test;
67
import junit.framework.TestCase;
78
import junit.framework.TestSuite;
89

10+
import java.util.ArrayList;
11+
912
/**
1013
* Unit test for simple App.
1114
*/
@@ -36,14 +39,21 @@ public static Test suite()
3639
public void testApp()
3740
{
3841
LicenseKey license = new LicenseKey();
39-
license.Notes = "[\"test\", [\"module\",[\"A\"]]]";
42+
43+
license.DataObjects = new ArrayList<>();
44+
45+
DataObject dobj = new DataObject();
46+
47+
dobj.Name = "cryptolens_features";
48+
dobj.StringValue = "[\"test\", [\"module\",[\"A\"]]]";
49+
license.DataObjects.add(dobj);
4050

4151
assertTrue(Helpers.HasFeature(license, "test"));
4252
assertTrue(Helpers.HasFeature(license, "module"));
4353
assertTrue(Helpers.HasFeature(license, "module.A"));
4454
assertFalse(Helpers.HasFeature(license, "module.B"));
4555

46-
license.Notes = "[\"f1\", [\"f2\",[[\"voice\",[\"all\"]]]]]";
56+
license.DataObjects.get(0).StringValue = "[\"f1\", [\"f2\",[[\"voice\",[\"all\"]]]]]";
4757

4858
assertTrue(Helpers.HasFeature(license, "f2.voice.all"));
4959

0 commit comments

Comments
 (0)