Skip to content

Commit

Permalink
ref[javascript]: 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 6327f45 commit 290812a
Show file tree
Hide file tree
Showing 9 changed files with 793 additions and 784 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;
}
}
60 changes: 32 additions & 28 deletions protocol/src/main/resources/javascript/buffer/ByteBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ const ByteBuffer = function() {
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
}

this.getBuffer = function() {
return this.buffer;
};

this.writeBytes = function(byteArray) {
const length = byteArray.byteLength;
this.ensureCapacity(length);
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
this.writeOffset += length;
};

this.toBytes = function() {
const result = new ArrayBuffer(this.writeOffset);
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
return result;
};

this.getWriteOffset = function() {
return this.writeOffset;
}
Expand Down Expand Up @@ -120,7 +137,7 @@ const ByteBuffer = function() {
return this.writeOffset > this.readOffset;
};

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

this.readBoolean = function() {
this.readBool = function() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
};

this.writeBytes = function(byteArray) {
const length = byteArray.byteLength;
this.ensureCapacity(length);
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
this.writeOffset += length;
};

this.toBytes = function() {
const result = new ArrayBuffer(this.writeOffset);
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
return result;
};

this.writeByte = function(value) {
this.ensureCapacity(1);
this.bufferView.setInt8(this.writeOffset, value);
Expand Down Expand Up @@ -386,7 +390,7 @@ const ByteBuffer = function() {

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

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

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

this.readBooleanArray = function() {
this.readBoolArray = function() {
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 @@ const ByteBuffer = function() {
};

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

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

this.writeByteList = function(list) {
Expand Down Expand Up @@ -674,19 +678,19 @@ const ByteBuffer = function() {
};

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

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

this.writeByteSet = function(set) {
Expand Down
14 changes: 7 additions & 7 deletions protocol/src/test/javascript/main.js
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
60 changes: 32 additions & 28 deletions protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ const ByteBuffer = function() {
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
}

this.getBuffer = function() {
return this.buffer;
};

this.writeBytes = function(byteArray) {
const length = byteArray.byteLength;
this.ensureCapacity(length);
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
this.writeOffset += length;
};

this.toBytes = function() {
const result = new ArrayBuffer(this.writeOffset);
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
return result;
};

this.getWriteOffset = function() {
return this.writeOffset;
}
Expand Down Expand Up @@ -120,7 +137,7 @@ const ByteBuffer = function() {
return this.writeOffset > this.readOffset;
};

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

this.readBoolean = function() {
this.readBool = function() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
};

this.writeBytes = function(byteArray) {
const length = byteArray.byteLength;
this.ensureCapacity(length);
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
this.writeOffset += length;
};

this.toBytes = function() {
const result = new ArrayBuffer(this.writeOffset);
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
return result;
};

this.writeByte = function(value) {
this.ensureCapacity(1);
this.bufferView.setInt8(this.writeOffset, value);
Expand Down Expand Up @@ -386,7 +390,7 @@ const ByteBuffer = function() {

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

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

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

this.readBooleanArray = function() {
this.readBoolArray = function() {
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 @@ const ByteBuffer = function() {
};

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

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

this.writeByteList = function(list) {
Expand Down Expand Up @@ -674,19 +678,19 @@ const ByteBuffer = function() {
};

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

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

this.writeByteSet = function(set) {
Expand Down
16 changes: 8 additions & 8 deletions protocol/src/test/javascript/zfoojs/packet/ComplexObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ ComplexObject.write = function(buffer, packet) {
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 @@ -290,13 +290,13 @@ ComplexObject.read = function(buffer) {
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
7 changes: 4 additions & 3 deletions protocol/src/test/javascript/zfoojs/packet/NormalObject.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
const NormalObject = function() {
this.a = 0; // number
this.aaa = []; // Array<number>
this.b = 0; // number
// 整数类型
this.c = 0; // number
this.d = 0; // number
this.e = 0; // number
Expand Down Expand Up @@ -42,7 +43,7 @@ NormalObject.write = function(buffer, packet) {
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 +80,7 @@ NormalObject.read = function(buffer) {
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
Loading

0 comments on commit 290812a

Please sign in to comment.