-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DVC-6570 run all bucketing tests against variableForUser() and variab…
…leForUserPB() (#434) * run bucketing tests against variableForUser() and variableForUserPB() * cleanup
- Loading branch information
1 parent
3f0d889
commit 3ff5363
Showing
4 changed files
with
194 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
lib/shared/bucketing-assembly-script/__tests__/protobufVariableHelper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import path from 'path' | ||
import protobuf from 'protobufjs' | ||
import { SDKVariable, VariableType as VariableTypeStr } from '@devcycle/types' | ||
import { | ||
variableForUser_PB, VariableType | ||
} from './bucketingImportHelper' | ||
|
||
// TODO replace all this PB importing + types once we import the pb file directly into static JS Classes | ||
const protoFile = '../protobuf/variableForUserParams.proto' | ||
const filePath = path.resolve(__dirname, protoFile) | ||
const root = protobuf.loadSync(filePath) | ||
|
||
const VariableForUserParams_PB = root.lookupType('VariableForUserParams_PB') | ||
if (!VariableForUserParams_PB) throw new Error('VariableForUserParams_PB not found') | ||
|
||
const SDKVariable_PB = root.lookupType('SDKVariable_PB') | ||
if (!SDKVariable_PB) throw new Error('SDKVariable_PB not found') | ||
|
||
type SDKVariable_PB_Type = { | ||
_id: string | ||
type: number | ||
key: string | ||
boolValue: boolean | ||
doubleValue: number | ||
stringValue: string | ||
} | ||
|
||
const pbSDKVariableToJS = (pbSDKVariable: SDKVariable_PB_Type): SDKVariable => { | ||
if (pbSDKVariable.type === 0) { | ||
return { | ||
_id: pbSDKVariable._id, | ||
key: pbSDKVariable.key, | ||
value: pbSDKVariable.boolValue, | ||
type: VariableTypeStr.boolean | ||
} | ||
} else if (pbSDKVariable.type === 1) { | ||
return { | ||
_id: pbSDKVariable._id, | ||
key: pbSDKVariable.key, | ||
value: pbSDKVariable.doubleValue, | ||
type: VariableTypeStr.number | ||
} | ||
} else if (pbSDKVariable.type === 2) { | ||
return { | ||
_id: pbSDKVariable._id, | ||
key: pbSDKVariable.key, | ||
value: pbSDKVariable.stringValue, | ||
type: VariableTypeStr.string | ||
} | ||
} else if (pbSDKVariable.type === 3) { | ||
return { | ||
_id: pbSDKVariable._id, | ||
key: pbSDKVariable.key, | ||
value: JSON.parse(pbSDKVariable.stringValue), | ||
type: VariableTypeStr.json | ||
} | ||
} | ||
throw new Error(`Unknown variable type: ${pbSDKVariable.type}`) | ||
} | ||
|
||
enum CustomDataTypePB { | ||
Bool, | ||
Num, | ||
Str, | ||
Null | ||
} | ||
|
||
type CustomDataValuePB = { | ||
type: CustomDataTypePB, | ||
boolValue?: boolean, | ||
doubleValue?: number, | ||
stringValue?: string | ||
} | ||
|
||
const customDataToPB = (customData: any): Record<string, CustomDataValuePB> | undefined => { | ||
if (!customData) return undefined | ||
|
||
const customDataPB: Record<string, CustomDataValuePB> = {} | ||
for (const [key, value] of Object.entries(customData)) { | ||
if (typeof value === 'boolean') { | ||
customDataPB[key] = { type: CustomDataTypePB.Bool, boolValue: value } | ||
} else if (typeof value === 'number') { | ||
customDataPB[key] = { type: CustomDataTypePB.Num, doubleValue: value } | ||
} else if (typeof value === 'string') { | ||
customDataPB[key] = { type: CustomDataTypePB.Str, stringValue: value } | ||
} else if (value === null) { | ||
customDataPB[key] = { type: CustomDataTypePB.Null } | ||
} else { | ||
throw new Error(`Unknown custom data type: ${typeof value}`) | ||
} | ||
} | ||
return customDataPB | ||
} | ||
|
||
export type VariableForUserArgs = { sdkKey: string, user: any, variableKey: string, variableType: VariableType } | ||
|
||
export const variableForUserPB = ( | ||
{ sdkKey, user, variableKey, variableType }: VariableForUserArgs | ||
): SDKVariable | null => { | ||
const customData = customDataToPB(user.customData) | ||
const privateCustomData = customDataToPB(user.privateCustomData) | ||
const params = { | ||
sdkKey, | ||
variableKey, | ||
variableType, | ||
user: { | ||
userId: user.user_id, | ||
email: user.email ? { value: user.email, isNull: false } : undefined, | ||
name: user.name ? { value: user.name, isNull: false } : undefined, | ||
language: user.language ? { value: user.language, isNull: false } : undefined, | ||
country: user.country ? { value: user.country, isNull: false } : undefined, | ||
appBuild: user.appBuild ? { value: user.appBuild, isNull: false } : undefined, | ||
appVersion: user.appVersion ? { value: user.appVersion, isNull: false } : undefined, | ||
deviceModel: user.deviceModel ? { value: user.deviceModel, isNull: false } : undefined, | ||
customData: customData ? { value: customData, isNull: false } : undefined, | ||
privateCustomData: privateCustomData ? { value: privateCustomData, isNull: false } : undefined | ||
}, | ||
shouldTrackEvent: true | ||
} | ||
const err = VariableForUserParams_PB.verify(params) | ||
if (err) throw new Error(err) | ||
|
||
const pbMsg = VariableForUserParams_PB.create(params) | ||
const buffer = VariableForUserParams_PB.encode(pbMsg).finish() | ||
const resultBuffer = variableForUser_PB(buffer) | ||
if (!resultBuffer) return null | ||
const pbSDKVariable = SDKVariable_PB.decode(resultBuffer!) as unknown as SDKVariable_PB_Type | ||
return pbSDKVariableToJS(pbSDKVariable) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3ff5363
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
js-sdks-async-with-provider – ./
js-sdks-async-with-provider-devcyclehq.vercel.app
dvc-async-with-provider.vercel.app
js-sdks-async-with-provider-git-main-devcyclehq.vercel.app
3ff5363
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
js-sdks-web-elements – ./
js-sdks-web-elements-devcyclehq.vercel.app
js-sdks-web-elements.vercel.app
js-sdks-web-elements-git-main-devcyclehq.vercel.app
3ff5363
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
js-sdks-with-provider – ./
dvc-with-provider.vercel.app
js-sdks-with-provider-devcyclehq.vercel.app
js-sdks-with-provider-git-main-devcyclehq.vercel.app
3ff5363
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
js-sdks-next-js – ./
dvc-nextjs.vercel.app
js-sdks-next-js-devcyclehq.vercel.app
js-sdks-next-js-git-main-devcyclehq.vercel.app