Skip to content

Commit 37f8abc

Browse files
authored
feat: rename Instance class (#1)
1 parent d7f7092 commit 37f8abc

File tree

7 files changed

+1027
-818
lines changed

7 files changed

+1027
-818
lines changed

README.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,26 @@ See example application here: [https://github.com/featurevisor/featurevisor-exam
117117
The SDK can be initialized by passing [datafile](https://featurevisor.com/docs/building-datafiles/) content directly:
118118

119119
```java
120-
import com.featurevisor.sdk.Instance;
121-
import com.featurevisor.types.DatafileContent;
120+
import com.featurevisor.sdk.Featurevisor;
122121

123122
// Load datafile content
124123
String datafileUrl = "https://cdn.yoursite.com/datafile.json";
125-
String datafileContent = "..." // ... load your datafile content
126-
127-
// Parse the JSON into DatafileContent object
128-
DatafileContent datafile = DatafileContent.fromJson(datafileContent);
124+
String datafileContent = "..." // load your datafile content
129125

130126
// Create SDK instance
131-
Instance f = new Instance(
132-
new Instance.InstanceOptions()
133-
.datafile(datafileContent)
127+
Featurevisor f = Featurevisor.createInstance(datafileContent);
128+
```
129+
130+
or by constructing a `Featurevisor.Options` object:
131+
132+
```java
133+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
134+
.datafile(datafileContent)
134135
);
135136
```
136137

138+
We will learn about several different options in the next sections.
139+
137140
## Evaluation types
138141

139142
We can evaluate 3 types of values against a particular [feature](https://featurevisor.com/docs/features/):
@@ -170,8 +173,8 @@ Map<String, Object> initialContext = new HashMap<>();
170173
initialContext.put("deviceId", "123");
171174
initialContext.put("country", "nl");
172175

173-
Instance f = new Instance(new Instance.InstanceOptions()
174-
.datafile(datafile)
176+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
177+
.datafile(datafileContent)
175178
.context(initialContext));
176179
```
177180

@@ -357,7 +360,7 @@ Map<String, Object> anotherFeatureSticky = new HashMap<>();
357360
anotherFeatureSticky.put("enabled", false);
358361
stickyFeatures.put("anotherFeatureKey", anotherFeatureSticky);
359362

360-
Instance f = new Instance(new Instance.InstanceOptions()
363+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
361364
.datafile(datafile)
362365
.sticky(stickyFeatures));
363366
```
@@ -433,7 +436,7 @@ Setting `debug` level will print out all logs, including `info`, `warn`, and `er
433436
```java
434437
import com.featurevisor.sdk.Logger;
435438

436-
Instance f = new Instance(new Instance.InstanceOptions()
439+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
437440
.datafile(datafile)
438441
.logLevel(Logger.LogLevel.DEBUG));
439442
```
@@ -457,7 +460,7 @@ Logger customLogger = Logger.createLogger(new Logger.CreateLoggerOptions()
457460
System.out.println("[" + level + "] " + message);
458461
}));
459462

460-
Instance f = new Instance(new Instance.InstanceOptions()
463+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
461464
.datafile(datafile)
462465
.logger(customLogger));
463466
```
@@ -470,7 +473,7 @@ Logger customLogger = new Logger(Logger.LogLevel.INFO, (level, message, details)
470473
System.out.println("[" + level + "] " + message);
471474
});
472475

473-
Instance f = new Instance(new Instance.InstanceOptions()
476+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
474477
.datafile(datafile)
475478
.logger(customLogger));
476479
```
@@ -637,7 +640,7 @@ You can register hooks at the time of SDK initialization:
637640
List<Map<String, Object>> hooks = new ArrayList<>();
638641
hooks.add(myCustomHook);
639642

640-
Instance f = new Instance(new Instance.InstanceOptions()
643+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
641644
.datafile(datafile)
642645
.hooks(hooks));
643646
```
@@ -662,7 +665,7 @@ That's where child instances come in handy:
662665
Map<String, Object> childContext = new HashMap<>();
663666
childContext.put("userId", "123");
664667

665-
Instance childF = f.spawn(childContext);
668+
ChildInstance childF = f.spawn(childContext);
666669
```
667670

668671
Now you can pass the child instance where your individual request is being handled, and you can continue to evaluate features targeting that specific user alone:

src/main/java/com/featurevisor/cli/CLI.java

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import picocli.CommandLine.Parameters;
77

88
import com.featurevisor.sdk.Featurevisor;
9-
import com.featurevisor.sdk.Instance;
109
import com.featurevisor.sdk.Logger;
1110
import com.featurevisor.sdk.DatafileReader;
1211
import com.featurevisor.types.DatafileContent;
@@ -250,9 +249,9 @@ private TestResult testFeature(Map<String, Object> assertion, String featureKey,
250249
Map<String, Object> sticky = (Map<String, Object>) assertion.getOrDefault("sticky", new HashMap<>());
251250

252251
// Update the SDK instance context and sticky values for this assertion
253-
if (f instanceof Instance) {
254-
((Instance) f).setContext(context, true);
255-
((Instance) f).setSticky(sticky, true);
252+
if (f instanceof Featurevisor) {
253+
((Featurevisor) f).setContext(context, true);
254+
((Featurevisor) f).setSticky(sticky, true);
256255
} else if (f instanceof com.featurevisor.sdk.ChildInstance) {
257256
((com.featurevisor.sdk.ChildInstance) f).setContext(context, true);
258257
((com.featurevisor.sdk.ChildInstance) f).setSticky(sticky, true);
@@ -274,7 +273,7 @@ private TestResult testFeature(Map<String, Object> assertion, String featureKey,
274273

275274
// Test expectedVariation
276275
if (assertion.containsKey("expectedVariation")) {
277-
Instance.OverrideOptions options = new Instance.OverrideOptions();
276+
Featurevisor.OverrideOptions options = new Featurevisor.OverrideOptions();
278277
if (assertion.containsKey("defaultVariationValue")) {
279278
options.setDefaultVariationValue(assertion.get("defaultVariationValue").toString());
280279
}
@@ -308,7 +307,7 @@ private TestResult testFeature(Map<String, Object> assertion, String featureKey,
308307
}
309308
}
310309

311-
Instance.OverrideOptions options = new Instance.OverrideOptions();
310+
Featurevisor.OverrideOptions options = new Featurevisor.OverrideOptions();
312311
if (defaultVariableValues.containsKey(variableKey)) {
313312
options.setDefaultVariableValue(defaultVariableValues.get(variableKey));
314313
}
@@ -347,7 +346,7 @@ private TestResult testFeature(Map<String, Object> assertion, String featureKey,
347346
if (expectedEvaluations.containsKey("variation")) {
348347
@SuppressWarnings("unchecked")
349348
Map<String, Object> variationEvaluation = (Map<String, Object>) expectedEvaluations.get("variation");
350-
Instance.OverrideOptions options = new Instance.OverrideOptions();
349+
Featurevisor.OverrideOptions options = new Featurevisor.OverrideOptions();
351350
if (assertion.containsKey("defaultVariationValue")) {
352351
options.setDefaultVariationValue(assertion.get("defaultVariationValue").toString());
353352
}
@@ -378,7 +377,7 @@ private TestResult testFeature(Map<String, Object> assertion, String featureKey,
378377
@SuppressWarnings("unchecked")
379378
Map<String, Object> expectedEvaluation = (Map<String, Object>) entry.getValue();
380379

381-
Instance.OverrideOptions options = new Instance.OverrideOptions();
380+
Featurevisor.OverrideOptions options = new Featurevisor.OverrideOptions();
382381
if (defaultVariableValues.containsKey(variableKey)) {
383382
options.setDefaultVariableValue(defaultVariableValues.get(variableKey));
384383
}
@@ -427,55 +426,55 @@ private TestResult testFeature(Map<String, Object> assertion, String featureKey,
427426
* Helper methods to work with both Instance and ChildInstance
428427
*/
429428
private boolean isEnabled(Object f, String featureKey, Map<String, Object> context) {
430-
if (f instanceof Instance) {
431-
return ((Instance) f).isEnabled(featureKey, context);
429+
if (f instanceof Featurevisor) {
430+
return ((Featurevisor) f).isEnabled(featureKey, context);
432431
} else if (f instanceof com.featurevisor.sdk.ChildInstance) {
433432
return ((com.featurevisor.sdk.ChildInstance) f).isEnabled(featureKey, context);
434433
}
435434
return false;
436435
}
437436

438-
private Object getVariation(Object f, String featureKey, Map<String, Object> context, Instance.OverrideOptions options) {
439-
if (f instanceof Instance) {
440-
return ((Instance) f).getVariation(featureKey, context, options);
437+
private Object getVariation(Object f, String featureKey, Map<String, Object> context, Featurevisor.OverrideOptions options) {
438+
if (f instanceof Featurevisor) {
439+
return ((Featurevisor) f).getVariation(featureKey, context, options);
441440
} else if (f instanceof com.featurevisor.sdk.ChildInstance) {
442441
return ((com.featurevisor.sdk.ChildInstance) f).getVariation(featureKey, context, options);
443442
}
444443
return null;
445444
}
446445

447-
private Object getVariable(Object f, String featureKey, String variableKey, Map<String, Object> context, Instance.OverrideOptions options) {
448-
if (f instanceof Instance) {
449-
return ((Instance) f).getVariable(featureKey, variableKey, context, options);
446+
private Object getVariable(Object f, String featureKey, String variableKey, Map<String, Object> context, Featurevisor.OverrideOptions options) {
447+
if (f instanceof Featurevisor) {
448+
return ((Featurevisor) f).getVariable(featureKey, variableKey, context, options);
450449
} else if (f instanceof com.featurevisor.sdk.ChildInstance) {
451450
return ((com.featurevisor.sdk.ChildInstance) f).getVariable(featureKey, variableKey, context, options);
452451
}
453452
return null;
454453
}
455454

456455
private com.featurevisor.sdk.Evaluation evaluateFlag(Object f, String featureKey, Map<String, Object> context) {
457-
if (f instanceof Instance) {
458-
return ((Instance) f).evaluateFlag(featureKey, context);
456+
if (f instanceof Featurevisor) {
457+
return ((Featurevisor) f).evaluateFlag(featureKey, context);
459458
} else if (f instanceof com.featurevisor.sdk.ChildInstance) {
460459
// ChildInstance doesn't have evaluateFlag, so we'll skip this test for child instances
461460
return null;
462461
}
463462
return null;
464463
}
465464

466-
private com.featurevisor.sdk.Evaluation evaluateVariation(Object f, String featureKey, Map<String, Object> context, Instance.OverrideOptions options) {
467-
if (f instanceof Instance) {
468-
return ((Instance) f).evaluateVariation(featureKey, context, options);
465+
private com.featurevisor.sdk.Evaluation evaluateVariation(Object f, String featureKey, Map<String, Object> context, Featurevisor.OverrideOptions options) {
466+
if (f instanceof Featurevisor) {
467+
return ((Featurevisor) f).evaluateVariation(featureKey, context, options);
469468
} else if (f instanceof com.featurevisor.sdk.ChildInstance) {
470469
// ChildInstance doesn't have evaluateVariation, so we'll skip this test for child instances
471470
return null;
472471
}
473472
return null;
474473
}
475474

476-
private com.featurevisor.sdk.Evaluation evaluateVariable(Object f, String featureKey, String variableKey, Map<String, Object> context, Instance.OverrideOptions options) {
477-
if (f instanceof Instance) {
478-
return ((Instance) f).evaluateVariable(featureKey, variableKey, context, options);
475+
private com.featurevisor.sdk.Evaluation evaluateVariable(Object f, String featureKey, String variableKey, Map<String, Object> context, Featurevisor.OverrideOptions options) {
476+
if (f instanceof Featurevisor) {
477+
return ((Featurevisor) f).evaluateVariable(featureKey, variableKey, context, options);
479478
} else if (f instanceof com.featurevisor.sdk.ChildInstance) {
480479
// ChildInstance doesn't have evaluateVariable, so we'll skip this test for child instances
481480
return null;
@@ -484,8 +483,8 @@ private com.featurevisor.sdk.Evaluation evaluateVariable(Object f, String featur
484483
}
485484

486485
private com.featurevisor.sdk.ChildInstance spawn(Object f, Map<String, Object> context) {
487-
if (f instanceof Instance) {
488-
return ((Instance) f).spawn(context);
486+
if (f instanceof Featurevisor) {
487+
return ((Featurevisor) f).spawn(context);
489488
} else if (f instanceof com.featurevisor.sdk.ChildInstance) {
490489
// ChildInstance doesn't have spawn, so we'll return null
491490
return null;
@@ -577,11 +576,11 @@ private void test() {
577576
return;
578577
}
579578

580-
// Create SDK instances for each environment
581-
Map<String, Instance> sdkInstancesByEnvironment = new HashMap<>();
579+
// Create SDK instances for each environment
580+
Map<String, Featurevisor> sdkInstancesByEnvironment = new HashMap<>();
582581
for (String environment : environments) {
583582
DatafileContent datafile = datafilesByEnvironment.get(environment);
584-
Instance instance = Featurevisor.createInstance(new Instance.InstanceOptions()
583+
Featurevisor instance = Featurevisor.createInstance(new Featurevisor.Options()
585584
.datafile(datafile)
586585
.logLevel(level));
587586
sdkInstancesByEnvironment.put(environment, instance);
@@ -610,7 +609,7 @@ private void test() {
610609

611610
if (test.containsKey("feature")) {
612611
String environment = (String) assertion.get("environment");
613-
Instance f = sdkInstancesByEnvironment.get(environment);
612+
Featurevisor f = sdkInstancesByEnvironment.get(environment);
614613

615614
// If "at" parameter is provided, create a new SDK instance with the specific hook
616615
if (assertion.containsKey("at")) {
@@ -631,7 +630,7 @@ private void test() {
631630
hooksManager.add(new HooksManager.Hook("at-parameter")
632631
.bucketValue((options) -> (int) (atValue * 1000)));
633632

634-
f = Featurevisor.createInstance(new Instance.InstanceOptions()
633+
f = Featurevisor.createInstance(new Featurevisor.Options()
635634
.datafile(datafile)
636635
.logLevel(level)
637636
.hooks(hooksManager.getAll()));
@@ -710,7 +709,7 @@ private void benchmark() {
710709
Logger.LogLevel level = getLoggerLevel();
711710
Map<String, DatafileContent> datafilesByEnvironment = buildDatafiles(rootDirectoryPath, Arrays.asList(environment));
712711

713-
Instance f = Featurevisor.createInstance(new Instance.InstanceOptions()
712+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
714713
.datafile(datafilesByEnvironment.get(environment))
715714
.logLevel(level));
716715

@@ -773,7 +772,7 @@ private void assessDistribution() {
773772

774773
Map<String, DatafileContent> datafilesByEnvironment = buildDatafiles(rootDirectoryPath, Arrays.asList(environment));
775774

776-
Instance f = Featurevisor.createInstance(new Instance.InstanceOptions()
775+
Featurevisor f = Featurevisor.createInstance(new Featurevisor.Options()
777776
.datafile(datafilesByEnvironment.get(environment))
778777
.logLevel(getLoggerLevel()));
779778

0 commit comments

Comments
 (0)