Skip to content

Commit

Permalink
Update HasFeature to use JSON array all the way
Browse files Browse the repository at this point in the history
  • Loading branch information
artemlos committed Mar 6, 2019
1 parent ed7d850 commit 727ca33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/main/java/io/cryptolens/methods/Helpers.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.cryptolens.methods;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import io.cryptolens.internal.BasicResult;
import io.cryptolens.models.ActivatedMachine;
import io.cryptolens.models.LicenseKey;
Expand All @@ -13,11 +12,9 @@
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;

import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.HashSet;


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

/**
* <p>Uses the notes field to determine if a certain feature exists (instead of the 8 feature flags).</p>
* <p><strong>Formatting: </strong> <p>The notes field needs to be formatted as a JSON array of strings or objects that contain </p>
* For example, ["f1", "f2"] means f1 and f2 are true. You can also have feature bundling, eg. ["f1", {"f2": ["voice","image"]}],
* <p><strong>Formatting: </strong> <p>The notes field needs to be formatted as a JSON array of strings or of JSON arrays
* where the first element specifies the feature name and the second element is a list of features.</p>
* For example, ["f1", "f2"] means f1 and f2 are true. You can also have feature bundling, eg. ["f1", ["f2",["voice","image"]]],
* 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
* ["f1", {"f2":[{"voice":["all"]}, "image"]}] means f2.voice.all is true as well as f2.voice and f2.
* ["f1", ["f2",[["voice",["all"]], "image"]]] means f2.voice.all is true as well as f2.voice and f2.
* The dots symbol is used to specify the "sub-features".
* </p>
* @param licenseKey a license key object.
Expand All @@ -227,13 +225,14 @@ public static boolean HasFeature(LicenseKey licenseKey, String featureName) {

boolean found = false;
for(int i = 0; i < featurePath.length; i++) {
found = false;
int index = -1;
for(int j = 0; j < array.size(); j++) {

if(!array.get(j).isJsonObject() && array.get(j).getAsString().equals(featurePath[i])) {
if(!array.get(j).isJsonArray() && array.get(j).getAsString().equals(featurePath[i])) {
found = true;
break;
} else if (array.get(j).isJsonObject() && array.get(j).getAsJsonObject().keySet().contains(featurePath[i])){
} else if (array.get(j).isJsonArray() && array.get(j).getAsJsonArray().get(0).getAsString().equals(featurePath[i])){
found = true;
index = j;
break;
Expand All @@ -244,8 +243,9 @@ public static boolean HasFeature(LicenseKey licenseKey, String featureName) {
}
if(i + 1 < featurePath.length && index != -1) {
// still have some sub features to go through.
array = array.get(index).getAsJsonObject().get(featurePath[i]).getAsJsonArray();
found = false;
// TODO: need to check if it's actually a json array or null?
// TODO: try catch
array = array.get(index).getAsJsonArray().get(1).getAsJsonArray();
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/test/java/io/cryptolens/HelpersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ public static Test suite()
public void testApp()
{
LicenseKey license = new LicenseKey();
license.Notes = "[\"test\", {\"module\":[\"A\"]}]";
license.Notes = "[\"test\", [\"module\",[\"A\"]]]";

assertTrue(Helpers.HasFeature(license, "test"));
assertTrue(Helpers.HasFeature(license, "module"));
assertTrue(Helpers.HasFeature(license, "module.A"));
assertFalse(Helpers.HasFeature(license, "module.B"));

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

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

assertFalse(Helpers.HasFeature(license, "f2.voice.all.test"));
assertFalse(Helpers.HasFeature(license, "f2.voice.all.test.a"));
assertFalse(Helpers.HasFeature(license, "f2.A.all.test"));
assertFalse(Helpers.HasFeature(license, "aa.voice.all.test"));

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

Expand Down

0 comments on commit 727ca33

Please sign in to comment.