Skip to content

Commit

Permalink
add variableForUser_PB_Preallocated method for preallocated buffer le…
Browse files Browse the repository at this point in the history
…ngths (#431)
  • Loading branch information
jonathannorris authored Mar 9, 2023
1 parent 77a1335 commit 2951dba
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
variableForUser_PB,
variableForUser_PB_Preallocated,
VariableType,
testVariableForUserParams_PB,
testDVCUser_PB,
Expand All @@ -26,6 +27,16 @@ describe('protobuf variable tests', () => {
return variableForUser_PB(buffer)
}

const callVariableForUser_PB_Preallocated = (params: any): Uint8Array | null => {
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 combinedBuffer = Buffer.concat([buffer, new Uint8Array(100)])
return variableForUser_PB_Preallocated(combinedBuffer, buffer.length)
}

const callTestVariableForUserParams_PB = (params: any): Uint8Array | null => {
const err = VariableForUserParams_PB.verify(params)
if (err) throw new Error(err)
Expand Down Expand Up @@ -70,32 +81,40 @@ describe('protobuf variable tests', () => {
initSDK(sdkKey, config)
})

it('should write protobuf message', () => {
const params = {
sdkKey: sdkKey,
variableKey: 'swagTest',
variableType: 2,
shouldTrackEvent: true,
user: {
userId: 'asuh',
country: { value: 'canada', isNull: false },
email: { value: 'test', isNull: false },
}
const varForUserParams = {
sdkKey: sdkKey,
variableKey: 'swagTest',
variableType: 2,
shouldTrackEvent: true,
user: {
userId: 'asuh',
country: { value: 'canada', isNull: false },
email: { value: 'test', isNull: false },
}
const resultBuffer = callVariableForUser_PB(params)
}
const varForUserExpected = {
'_id': '615356f120ed334a6054564c',
'boolValue': false,
'doubleValue': 0,
'evalReason': {
'isNull': true,
'value': '',
},
'key': 'swagTest',
'stringValue': 'YEEEEOWZA',
'type': VariableType.String,
}

it('should write protobuf message to variableForUser_PB', () => {
const resultBuffer = callVariableForUser_PB(varForUserParams)
expect(resultBuffer).not.toBeNull()
expect(SDKVariable_PB.decode(resultBuffer!)).toEqual({
'_id': '615356f120ed334a6054564c',
'boolValue': false,
'doubleValue': 0,
'evalReason': {
'isNull': true,
'value': '',
},
'key': 'swagTest',
'stringValue': 'YEEEEOWZA',
'type': VariableType.String,
})
expect(SDKVariable_PB.decode(resultBuffer!)).toEqual(varForUserExpected)
})

it('should write preallocated protobuf message to variableForUser_PB_Preallocated', () => {
const resultBuffer = callVariableForUser_PB_Preallocated(varForUserParams)
expect(resultBuffer).not.toBeNull()
expect(SDKVariable_PB.decode(resultBuffer!)).toEqual(varForUserExpected)
})

describe('protobuf type tests', () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/shared/bucketing-assembly-script/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function variableTypeFromPB(pb: VariableType_PB): VariableType {
}
}

export function variableForUser_PB_Preallocated(protobuf: Uint8Array, length: i32): Uint8Array | null {
return variableForUser_PB(protobuf.slice(0, length))
}

export function variableForUser_PB(protobuf: Uint8Array): Uint8Array | null {
const params: VariableForUserParams_PB = decodeVariableForUserParams_PB(protobuf)
const user = params.user
Expand Down

4 comments on commit 2951dba

@vercel
Copy link

@vercel vercel bot commented on 2951dba Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2951dba Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2951dba Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2951dba Mar 9, 2023

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 – ./

js-sdks-next-js-git-main-devcyclehq.vercel.app
dvc-nextjs.vercel.app
js-sdks-next-js-devcyclehq.vercel.app

Please sign in to comment.