Skip to content

Commit

Permalink
Revise the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Oct 22, 2024
1 parent aa9f92f commit e0ba8fe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 44 deletions.
5 changes: 0 additions & 5 deletions packages/sdk/src/document/json/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ export function buildCRDTElement(
} else if (value instanceof Tree) {
element = CRDTTree.create(value.buildRoot(context), createdAt);
value.initialize(context, element as CRDTTree);
} else if (value instanceof Map) {
element = CRDTObject.create(
createdAt,
ObjectProxy.buildObjectMembersFromMap(context, value!),
);
} else {
element = CRDTObject.create(
createdAt,
Expand Down
24 changes: 0 additions & 24 deletions packages/sdk/src/document/json/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,30 +169,6 @@ export class ObjectProxy {
);
}

/**
* `buildObjectMembersFromMap` constructs an object where all values from the
* user-provided object are transformed into CRDTElements.
* This function takes an object and iterates through its values,
* converting each value into a corresponding CRDTElement.
*/
public static buildObjectMembersFromMap(
context: ChangeContext,
value: Map<string, any>,
): { [key: string]: CRDTElement } {
const members: { [key: string]: CRDTElement } = {};
for (const [k, v] of value) {
if (k.includes('.')) {
throw new YorkieError(
Code.ErrInvalidObjectKey,
`key must not contain the '.'.`,
);
}
const createdAt = context.issueTimeTicket();
members[k] = buildCRDTElement(context, v, createdAt);
}
return members;
}

/**
* `buildObjectMembers` constructs an object where all values from the
* user-provided object are transformed into CRDTElements.
Expand Down
48 changes: 33 additions & 15 deletions packages/sdk/test/integration/document_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,16 +1180,14 @@ describe('Document', function () {

describe('With various types', () => {
interface TestCase {
caseName: string;
name: string;
input: JSONElement;
expectedJSON: string;
}

// TODO(raararaara): Need test cases for Double, Bytes, Date, Object, Array
const testCases: Array<TestCase> = [
// Custom CRDT Types
{
caseName: 'json.Tree',
name: 'json.Tree',
input: new Tree({
type: 'doc',
children: [
Expand All @@ -1199,40 +1197,60 @@ describe('Document', function () {
expectedJSON: `{"k":{"type":"doc","children":[{"type":"p","children":[{"type":"text","value":"ab"}]}]}}`,
},
{
caseName: 'json.Text',
name: 'json.Text',
input: new Text(),
expectedJSON: `{"k":[]}`,
},
{
caseName: 'json.Counter',
name: 'json.Counter',
input: new Counter(CounterType.IntegerCnt, 1),
expectedJSON: `{"k":1}`,
},
// Primitives
{
caseName: 'null',
name: 'null',
input: null,
expectedJSON: `{"k":null}`,
},
{
caseName: 'boolean',
name: 'boolean',
input: true,
expectedJSON: `{"k":true}`,
},
{
caseName: 'number',
name: 'number',
input: 1,
expectedJSON: `{"k":1}`,
},
{
caseName: 'Long',
name: 'Long',
input: Long.MAX_VALUE,
expectedJSON: `{"k":9223372036854775807}`,
},
{
name: 'Object',
input: { k: 'v' },
expectedJSON: `{"k":{"k":"v"}}`,
},
{
name: 'Array',
input: [1, 2],
expectedJSON: `{"k":[1,2]}`,
},
{
name: 'Bytes',
input: new Uint8Array([1, 2]),
expectedJSON: `{"k":1,2}`,
},
// TODO(hackerwins): Encode Date type to JSON
// {
// name: 'Date',
// input: new Date(0),
// expectedJSON: `{"k":"1970-01-01T00:00:00.000Z"}`,
// },
];

it('Can support various types', async function ({ task }) {
for (const { caseName, input, expectedJSON } of testCases) {
for (const { name: caseName, input, expectedJSON } of testCases) {
it(`Can support various types: ${caseName}`, async function ({ task }) {
const c1 = new yorkie.Client(testRPCAddr);
await c1.activate();
const docKey = toDocKey(
Expand All @@ -1248,8 +1266,8 @@ describe('Document', function () {
assert.equal(doc.toSortedJSON(), expectedJSON);

await c1.deactivate();
}
});
});
}
});
});
});

0 comments on commit e0ba8fe

Please sign in to comment.