Skip to content

Commit

Permalink
ref[es]: rename get set method
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 14, 2024
1 parent 290812a commit 774be53
Show file tree
Hide file tree
Showing 9 changed files with 765 additions and 757 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public Triple<String, String, String> field(Field field, IFieldRegistration fiel
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeBoolean({});", objectStr)).append(LS);
builder.append(StringUtils.format("buffer.writeBool({});", objectStr)).append(LS);
}

@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.localVariableId++;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readBoolean(); ", result)).append(LS);
builder.append(StringUtils.format("const {} = buffer.readBool(); ", result)).append(LS);
return result;
}
}
34 changes: 19 additions & 15 deletions protocol/src/main/resources/ecmascript/buffer/ByteBuffer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class ByteBuffer {
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
}

getBuffer() {
return this.buffer;
}

getWriteOffset() {
return this.writeOffset;
}
Expand Down Expand Up @@ -120,7 +124,7 @@ class ByteBuffer {
return this.writeOffset > this.readOffset;
}

writeBoolean(value) {
writeBool(value) {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
}
Expand All @@ -133,7 +137,7 @@ class ByteBuffer {
this.writeOffset++;
}

readBoolean() {
readBool() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
Expand Down Expand Up @@ -386,7 +390,7 @@ class ByteBuffer {

writePacketFlag(value) {
const flag = (value === null) || (value === undefined);
this.writeBoolean(!flag);
this.writeBool(!flag);
return flag;
}

Expand All @@ -400,23 +404,23 @@ class ByteBuffer {
return protocolRegistration.read(this);
}

writeBooleanArray(array) {
writeBoolArray(array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeBoolean(element);
this.writeBool(element);
});
}
}

readBooleanArray() {
readBoolArray() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readBoolean());
array.push(this.readBool());
}
}
return array;
Expand Down Expand Up @@ -601,12 +605,12 @@ class ByteBuffer {
}

// ---------------------------------------------list-------------------------------------------
writeBooleanList(list) {
this.writeBooleanArray(list);
writeBoolList(list) {
this.writeBoolArray(list);
}

readBooleanList() {
return this.readBooleanArray();
readBoolList() {
return this.readBoolArray();
}

writeByteList(list) {
Expand Down Expand Up @@ -674,19 +678,19 @@ class ByteBuffer {
}

// ---------------------------------------------set-------------------------------------------
writeBooleanSet(set) {
writeBoolSet(set) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeBoolean(element);
this.writeBool(element);
});
}
}

readBooleanSet() {
return new Set(this.readBooleanArray());
readBoolSet() {
return new Set(this.readBoolArray());
}

writeByteSet(set) {
Expand Down
14 changes: 7 additions & 7 deletions protocol/src/test/ecmascript/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ function assert(flag) {
}
}

const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes');
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes');
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes');
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes');
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes');
const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes');
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes');
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes');
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes');
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes');

const arrayBytes = new Uint8Array(data.length);
data.copy(arrayBytes, 0, 0, data.length);
Expand Down Expand Up @@ -43,8 +43,8 @@ function byteBufferTest() {
// boolean
const testBoolean = [false, true];
testBoolean.forEach((value) => {
buffer.writeBoolean(value);
assert(buffer.readBoolean() == value);
buffer.writeBool(value);
assert(buffer.readBool() == value);
});

assert(buffer.writeOffset == testBoolean.length);
Expand Down
34 changes: 19 additions & 15 deletions protocol/src/test/ecmascript/zfooes/buffer/ByteBuffer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class ByteBuffer {
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
}

getBuffer() {
return this.buffer;
}

getWriteOffset() {
return this.writeOffset;
}
Expand Down Expand Up @@ -120,7 +124,7 @@ class ByteBuffer {
return this.writeOffset > this.readOffset;
}

writeBoolean(value) {
writeBool(value) {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
}
Expand All @@ -133,7 +137,7 @@ class ByteBuffer {
this.writeOffset++;
}

readBoolean() {
readBool() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
Expand Down Expand Up @@ -386,7 +390,7 @@ class ByteBuffer {

writePacketFlag(value) {
const flag = (value === null) || (value === undefined);
this.writeBoolean(!flag);
this.writeBool(!flag);
return flag;
}

Expand All @@ -400,23 +404,23 @@ class ByteBuffer {
return protocolRegistration.read(this);
}

writeBooleanArray(array) {
writeBoolArray(array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeBoolean(element);
this.writeBool(element);
});
}
}

readBooleanArray() {
readBoolArray() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readBoolean());
array.push(this.readBool());
}
}
return array;
Expand Down Expand Up @@ -601,12 +605,12 @@ class ByteBuffer {
}

// ---------------------------------------------list-------------------------------------------
writeBooleanList(list) {
this.writeBooleanArray(list);
writeBoolList(list) {
this.writeBoolArray(list);
}

readBooleanList() {
return this.readBooleanArray();
readBoolList() {
return this.readBoolArray();
}

writeByteList(list) {
Expand Down Expand Up @@ -674,19 +678,19 @@ class ByteBuffer {
}

// ---------------------------------------------set-------------------------------------------
writeBooleanSet(set) {
writeBoolSet(set) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeBoolean(element);
this.writeBool(element);
});
}
}

readBooleanSet() {
return new Set(this.readBooleanArray());
readBoolSet() {
return new Set(this.readBoolArray());
}

writeByteSet(set) {
Expand Down
16 changes: 8 additions & 8 deletions protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ export class ComplexObjectRegistration {
buffer.writeDouble(packet.ff);
buffer.writeDoubleArray(packet.fff);
buffer.writeDoubleArray(packet.ffff);
buffer.writeBoolean(packet.g);
buffer.writeBoolean(packet.gg);
buffer.writeBooleanArray(packet.ggg);
buffer.writeBooleanArray(packet.gggg);
buffer.writeBool(packet.g);
buffer.writeBool(packet.gg);
buffer.writeBoolArray(packet.ggg);
buffer.writeBoolArray(packet.gggg);
buffer.writeString(packet.jj);
buffer.writeStringArray(packet.jjj);
buffer.writePacket(packet.kk, 102);
Expand Down Expand Up @@ -289,13 +289,13 @@ export class ComplexObjectRegistration {
packet.fff = array22;
const array23 = buffer.readDoubleArray();
packet.ffff = array23;
const result24 = buffer.readBoolean();
const result24 = buffer.readBool();
packet.g = result24;
const result25 = buffer.readBoolean();
const result25 = buffer.readBool();
packet.gg = result25;
const array26 = buffer.readBooleanArray();
const array26 = buffer.readBoolArray();
packet.ggg = array26;
const array27 = buffer.readBooleanArray();
const array27 = buffer.readBoolArray();
packet.gggg = array27;
const result28 = buffer.readString();
packet.jj = result28;
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class NormalObjectRegistration {
buffer.writeLong(packet.d);
buffer.writeFloat(packet.e);
buffer.writeDouble(packet.f);
buffer.writeBoolean(packet.g);
buffer.writeBool(packet.g);
buffer.writeString(packet.jj);
buffer.writePacket(packet.kk, 102);
buffer.writeIntList(packet.l);
Expand Down Expand Up @@ -79,7 +79,7 @@ export class NormalObjectRegistration {
packet.e = result5;
const result6 = buffer.readDouble();
packet.f = result6;
const result7 = buffer.readBoolean();
const result7 = buffer.readBool();
packet.g = result7;
const result8 = buffer.readString();
packet.jj = result8;
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ObjectBRegistration {
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(4);
buffer.writeBoolean(packet.flag);
buffer.writeBool(packet.flag);
buffer.writeInt(packet.innerCompatibleValue);
buffer.adjustPadding(4, beforeWriteIndex);
}
Expand All @@ -28,7 +28,7 @@ export class ObjectBRegistration {
}
const beforeReadIndex = buffer.getReadOffset();
const packet = new ObjectB();
const result0 = buffer.readBoolean();
const result0 = buffer.readBool();
packet.flag = result0;
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result1 = buffer.readInt();
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class SimpleObjectRegistration {
}
buffer.writeInt(-1);
buffer.writeInt(packet.c);
buffer.writeBoolean(packet.g);
buffer.writeBool(packet.g);
}

read(buffer) {
Expand All @@ -28,7 +28,7 @@ export class SimpleObjectRegistration {
const packet = new SimpleObject();
const result0 = buffer.readInt();
packet.c = result0;
const result1 = buffer.readBoolean();
const result1 = buffer.readBool();
packet.g = result1;
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
Expand Down
Loading

0 comments on commit 774be53

Please sign in to comment.