Skip to content

Commit

Permalink
move obfuscated to client attribute, valueOf eppoValue
Browse files Browse the repository at this point in the history
  • Loading branch information
schmit committed Apr 4, 2024
1 parent c650af5 commit bfd47f6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/client/eppo-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe('EppoClient E2E test', () => {
'test variation assignment splits',
async ({ flag, variationType, defaultValue, subjects }: IAssignmentTestCase) => {
const evaluator = new Evaluator();
const client = new EppoClient(evaluator, storage);
const client = new EppoClient(evaluator, storage, undefined, true);

const typeAssignmentFunctions = {
[VariationType.BOOLEAN]: client.getBoolAssignment.bind(client),
Expand Down
64 changes: 33 additions & 31 deletions src/client/eppo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ export interface IEppoClient {
subjectAttributes?: Record<string, AttributeType>,
): boolean;

getNumericAssignment(
getIntegerAssignment(
subjectKey: string,
flagKey: string,
defaultValue: number,
subjectAttributes?: Record<string, AttributeType>,
): number;

getIntegerAssignment(
getNumericAssignment(
subjectKey: string,
flagKey: string,
defaultValue: number,
Expand Down Expand Up @@ -112,6 +112,7 @@ export default class EppoClient implements IEppoClient {
private queuedEvents: IAssignmentEvent[] = [];
private assignmentLogger: IAssignmentLogger | undefined;
private isGracefulFailureMode = true;
private isObfuscated = false;
private assignmentCache: AssignmentCache<Cacheable> | undefined;
private configurationStore: IConfigurationStore;
private configurationRequestParameters: FlagConfigurationRequestParameters | undefined;
Expand All @@ -122,10 +123,12 @@ export default class EppoClient implements IEppoClient {
evaluator: Evaluator,
configurationStore: IConfigurationStore,
configurationRequestParameters?: FlagConfigurationRequestParameters,
obfuscated = false,
) {
this.evaluator = evaluator;
this.configurationStore = configurationStore;
this.configurationRequestParameters = configurationRequestParameters;
this.isObfuscated = obfuscated;
}

public setConfigurationRequestParameters(
Expand Down Expand Up @@ -193,15 +196,13 @@ export default class EppoClient implements IEppoClient {
flagKey: string,
defaultValue: string,
subjectAttributes: Record<string, AttributeType> = {},
obfuscated = false,
): string {
return (
this.getAssignmentVariation(
subjectKey,
flagKey,
EppoValue.String(defaultValue),
subjectAttributes,
obfuscated,
VariationType.STRING,
).stringValue ?? defaultValue
);
Expand All @@ -212,54 +213,48 @@ export default class EppoClient implements IEppoClient {
flagKey: string,
defaultValue: boolean,
subjectAttributes: Record<string, AttributeType> = {},
obfuscated = false,
): boolean {
return (
this.getAssignmentVariation(
subjectKey,
flagKey,
EppoValue.Bool(defaultValue),
subjectAttributes,
obfuscated,
VariationType.BOOLEAN,
).boolValue ?? defaultValue
);
}

getNumericAssignment(
getIntegerAssignment(
subjectKey: string,
flagKey: string,
defaultValue: number,
subjectAttributes?: Record<string, AttributeType>,
obfuscated = false,
): number {
return (
this.getAssignmentVariation(
subjectKey,
flagKey,
EppoValue.Numeric(defaultValue),
subjectAttributes,
obfuscated,
VariationType.NUMERIC,
VariationType.INTEGER,
).numericValue ?? defaultValue
);
}

getIntegerAssignment(
getNumericAssignment(
subjectKey: string,
flagKey: string,
defaultValue: number,
subjectAttributes?: Record<string, AttributeType>,
obfuscated = false,
): number {
return (
this.getAssignmentVariation(
subjectKey,
flagKey,
EppoValue.Numeric(defaultValue),
subjectAttributes,
obfuscated,
VariationType.INTEGER,
VariationType.NUMERIC,
).numericValue ?? defaultValue
);
}
Expand All @@ -269,34 +264,23 @@ export default class EppoClient implements IEppoClient {
flagKey: string,
defaultValue: object,
subjectAttributes: Record<string, AttributeType> = {},
obfuscated = false,
): object {
return (
this.getAssignmentVariation(
subjectKey,
flagKey,
EppoValue.JSON(defaultValue),
subjectAttributes,
obfuscated,
VariationType.JSON,
).objectValue ?? defaultValue
);
}

private rethrowIfNotGraceful(err: Error, defaultValue?: EppoValue): EppoValue {
if (this.isGracefulFailureMode) {
console.error(`[Eppo SDK] Error getting assignment: ${err.message}`);
return defaultValue ?? EppoValue.Null();
}
throw err;
}

private getAssignmentVariation(
subjectKey: string,
flagKey: string,
defaultValue: EppoValue,
subjectAttributes: Record<string, AttributeType> = {},
obfuscated: boolean,
expectedVariationType: VariationType,
): EppoValue {
try {
Expand All @@ -305,19 +289,26 @@ export default class EppoClient implements IEppoClient {
flagKey,
subjectAttributes,
expectedVariationType,
obfuscated,
);

if (!result.variation) {
return defaultValue;
}

return EppoValue.generateEppoValue(result.variation.value, expectedVariationType);
return EppoValue.valueOf(result.variation.value, expectedVariationType);
} catch (error) {
return this.rethrowIfNotGraceful(error, defaultValue);
}
}

private rethrowIfNotGraceful(err: Error, defaultValue?: EppoValue): EppoValue {
if (this.isGracefulFailureMode) {
console.error(`[Eppo SDK] Error getting assignment: ${err.message}`);
return defaultValue ?? EppoValue.Null();
}
throw err;
}

/**
* [Experimental] Get a detailed return of assignment for a particular subject and flag.
*
Expand All @@ -336,12 +327,11 @@ export default class EppoClient implements IEppoClient {
flagKey: string,
subjectAttributes: Record<string, AttributeType> = {},
expectedVariationType?: VariationType,
obfuscated = false,
): FlagEvaluation {
validateNotBlank(subjectKey, 'Invalid argument: subjectKey cannot be blank');
validateNotBlank(flagKey, 'Invalid argument: flagKey cannot be blank');

const flag: Flag = this.configurationStore.get(obfuscated ? getMD5Hash(flagKey) : flagKey);
const flag = this.getFlag(flagKey);

if (flag === null) {
console.warn(`[Eppo SDK] No assigned variation. Flag not found: ${flagKey}`);
Expand All @@ -361,8 +351,13 @@ export default class EppoClient implements IEppoClient {
return noneResult(flagKey, subjectKey, subjectAttributes);
}

const result = this.evaluator.evaluateFlag(flag, subjectKey, subjectAttributes, obfuscated);
if (obfuscated) {
const result = this.evaluator.evaluateFlag(
flag,
subjectKey,
subjectAttributes,
this.isObfuscated,
);
if (this.isObfuscated) {
// flag.key is obfuscated, replace with requested flag key
result.flagKey = flagKey;
}
Expand All @@ -378,6 +373,13 @@ export default class EppoClient implements IEppoClient {
return result;
}

private getFlag(flagKey: string): Flag | null {
const flag: Flag = this.configurationStore.get(
this.isObfuscated ? getMD5Hash(flagKey) : flagKey,
);
return flag;
}

private checkTypeMatch(expectedType?: VariationType, actualType?: VariationType): boolean {
return expectedType === undefined || actualType === expectedType;
}
Expand Down
5 changes: 1 addition & 4 deletions src/eppo_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ export class EppoValue {
this.objectValue = objectValue;
}

static generateEppoValue(
value: boolean | number | string | object,
valueType: VariationType,
): EppoValue {
static valueOf(value: boolean | number | string | object, valueType: VariationType): EppoValue {
if (value == null) {
return EppoValue.Null();
}
Expand Down

0 comments on commit bfd47f6

Please sign in to comment.