Skip to content

Commit 7b8af62

Browse files
authored
feat(compass-components): add special handling of vector sub-type serialization COMPASS-8257 (#6834)
1 parent e8fce74 commit 7b8af62

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

packages/compass-components/src/components/bson-value.spec.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ describe('BSONValue', function () {
4545
value: Binary.createFromHexString('3132303d', Binary.SUBTYPE_UUID),
4646
expected: "UUID('3132303d')",
4747
},
48+
{
49+
type: 'Binary',
50+
value: Binary.fromInt8Array(new Int8Array([1, 2, 3])),
51+
expected: 'Binary.fromInt8Array(new Int8Array([1, 2, 3]))',
52+
},
53+
{
54+
type: 'Binary',
55+
value: Binary.fromFloat32Array(new Float32Array([1.1, 2.2, 3.3])),
56+
expected: 'Binary.fromFloat32Array(new Float32Array([1.1, 2.2, 3.3]))',
57+
},
58+
{
59+
type: 'Binary',
60+
value: Binary.fromPackedBits(new Uint8Array([1, 2, 3])),
61+
expected: 'Binary.fromPackedBits(new Uint8Array([1, 2, 3]))',
62+
},
4863
{
4964
type: 'Code',
5065
value: new Code('var a = 1', { foo: 2 }),

packages/compass-components/src/components/bson-value.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,37 @@ const BinaryValue: React.FunctionComponent<PropsByValueType<'Binary'>> = ({
157157

158158
return { stringifiedValue: `UUID('${uuid}')` };
159159
}
160+
if (value.sub_type === Binary.SUBTYPE_VECTOR) {
161+
const vectorType = value.buffer[0];
162+
if (vectorType === Binary.VECTOR_TYPE.Int8) {
163+
const truncatedSerializedBuffer = truncate(
164+
value.toInt8Array().slice(0, 100).join(', '),
165+
100
166+
);
167+
return {
168+
stringifiedValue: `Binary.fromInt8Array(new Int8Array([${truncatedSerializedBuffer}]))`,
169+
};
170+
} else if (vectorType === Binary.VECTOR_TYPE.Float32) {
171+
const truncatedSerializedBuffer = truncate(
172+
[...value.toFloat32Array().slice(0, 100)]
173+
// Using a limited precision and removing trailing zeros for better displaying
174+
.map((num) => num.toPrecision(8).replace(/\.?0+$/, ''))
175+
.join(', '),
176+
100
177+
);
178+
return {
179+
stringifiedValue: `Binary.fromFloat32Array(new Float32Array([${truncatedSerializedBuffer}]))`,
180+
};
181+
} else if (vectorType === Binary.VECTOR_TYPE.PackedBit) {
182+
const truncatedSerializedBuffer = truncate(
183+
value.toPackedBits().slice(0, 100).join(', '),
184+
100
185+
);
186+
return {
187+
stringifiedValue: `Binary.fromPackedBits(new Uint8Array([${truncatedSerializedBuffer}]))`,
188+
};
189+
}
190+
}
160191
return {
161192
stringifiedValue: `Binary.createFromBase64('${truncate(
162193
value.toString('base64'),

0 commit comments

Comments
 (0)