Skip to content

Commit ed7d850

Browse files
committed
Add support for bundled features
1 parent c16addc commit ed7d850

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

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

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

33
import com.google.gson.Gson;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonParser;
46
import com.google.gson.reflect.TypeToken;
57
import io.cryptolens.internal.BasicResult;
68
import io.cryptolens.models.ActivatedMachine;
@@ -199,22 +201,58 @@ public static boolean HasFeature(LicenseKey licenseKey, int feature) {
199201
return false;
200202
}
201203

204+
public class MyClass<T>
205+
{
206+
//public String par1;
207+
public T par2;
208+
}
209+
202210
/**
203-
* Use the notes field to determine if a certain feature exists (instead of the 8 feature flags).
204-
* The notes field needs to be formatted as JSON array, eg. ["f1", "f2"] means f1 and f2 are true.
211+
* <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"]}],
214+
* 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.
216+
* The dots symbol is used to specify the "sub-features".
217+
* </p>
205218
* @param licenseKey a license key object.
206219
* @param featureName the name of the feature (case-sensitive).
207220
* @return True if the feature exists and false otherwise.
208221
*/
209222
public static boolean HasFeature(LicenseKey licenseKey, String featureName) {
210223

211-
Type type = new TypeToken<HashSet<String>>(){}.getType();
212-
HashSet<String> features = new Gson().fromJson(licenseKey.Notes, type);
224+
JsonParser parser = new JsonParser();
225+
JsonArray array = parser.parse(licenseKey.Notes).getAsJsonArray();
226+
String[] featurePath = featureName.split("\\.");
227+
228+
boolean found = false;
229+
for(int i = 0; i < featurePath.length; i++) {
230+
int index = -1;
231+
for(int j = 0; j < array.size(); j++) {
232+
233+
if(!array.get(j).isJsonObject() && array.get(j).getAsString().equals(featurePath[i])) {
234+
found = true;
235+
break;
236+
} else if (array.get(j).isJsonObject() && array.get(j).getAsJsonObject().keySet().contains(featurePath[i])){
237+
found = true;
238+
index = j;
239+
break;
240+
}
241+
}
242+
if(!found){
243+
return false;
244+
}
245+
if(i + 1 < featurePath.length && index != -1) {
246+
// still have some sub features to go through.
247+
array = array.get(index).getAsJsonObject().get(featurePath[i]).getAsJsonArray();
248+
found = false;
249+
}
250+
}
213251

214-
if (features != null && features.contains(featureName)) {
215-
return true;
252+
if(!found){
253+
return false;
216254
}
217-
return false;
255+
return true;
218256
}
219257

220258
/**

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

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

4141
assertTrue(Helpers.HasFeature(license, "test"));
42+
assertTrue(Helpers.HasFeature(license, "module"));
43+
assertTrue(Helpers.HasFeature(license, "module.A"));
44+
assertFalse(Helpers.HasFeature(license, "module.B"));
45+
46+
license.Notes = "[\"f1\", {\"f2\":[{\"voice\":[\"all\"]}]}]";
47+
48+
assertTrue(Helpers.HasFeature(license, "f2.voice.all"));
49+
50+
51+
String[] featurePath = "moduleA.video".split(".");
52+
4253
}
4354
}

0 commit comments

Comments
 (0)