mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-18 22:13:59 +00:00
feat[py]: python compatible field support
This commit is contained in:
1 parent
c264f8dae1
commit
dee86d0cb4
4 files changed
+21
-53
No files matched your search
@@ -153,11 +153,20 @@ public abstract class GeneratePyUtils {
|
|||||||
var fields = registration.getFields();
|
var fields = registration.getFields();
|
||||||
var fieldRegistrations = registration.getFieldRegistrations();
|
var fieldRegistrations = registration.getFieldRegistrations();
|
||||||
var pyBuilder = new StringBuilder();
|
var pyBuilder = new StringBuilder();
|
||||||
|
if (registration.isCompatible()) {
|
||||||
|
pyBuilder.append("beforeWriteIndex = buffer.getWriteOffset()").append(LS);
|
||||||
|
pyBuilder.append(TAB + TAB).append(StringUtils.format("buffer.writeInt({})", registration.getPredictionLength())).append(LS);
|
||||||
|
} else {
|
||||||
|
pyBuilder.append(TAB + TAB).append("buffer.writeInt(-1)").append(LS);
|
||||||
|
}
|
||||||
for (var i = 0; i < fields.length; i++) {
|
for (var i = 0; i < fields.length; i++) {
|
||||||
var field = fields[i];
|
var field = fields[i];
|
||||||
var fieldRegistration = fieldRegistrations[i];
|
var fieldRegistration = fieldRegistrations[i];
|
||||||
pySerializer(fieldRegistration.serializer()).writeObject(pyBuilder, "packet." + field.getName(), 2, field, fieldRegistration);
|
pySerializer(fieldRegistration.serializer()).writeObject(pyBuilder, "packet." + field.getName(), 2, field, fieldRegistration);
|
||||||
}
|
}
|
||||||
|
if (registration.isCompatible()) {
|
||||||
|
pyBuilder.append(TAB + TAB).append(StringUtils.format("buffer.adjustPadding({}, beforeWriteIndex)", registration.getPredictionLength())).append(LS);
|
||||||
|
}
|
||||||
return pyBuilder.toString();
|
return pyBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,9 +178,10 @@ public abstract class GeneratePyUtils {
|
|||||||
var field = fields[i];
|
var field = fields[i];
|
||||||
var fieldRegistration = fieldRegistrations[i];
|
var fieldRegistration = fieldRegistrations[i];
|
||||||
if (field.isAnnotationPresent(Compatible.class)) {
|
if (field.isAnnotationPresent(Compatible.class)) {
|
||||||
pyBuilder.append(TAB + TAB).append("if not buffer.isReadable():").append(LS);
|
pyBuilder.append(TAB + TAB).append("if buffer.compatibleRead(beforeReadIndex, length):").append(LS);
|
||||||
pyBuilder.append(TAB + TAB + TAB).append("return packet").append(LS);
|
var compatibleReadObject = pySerializer(fieldRegistration.serializer()).readObject(pyBuilder, 3, field, fieldRegistration);
|
||||||
pyBuilder.append(TAB + TAB).append("pass").append(LS);
|
pyBuilder.append(TAB + TAB+ TAB).append(StringUtils.format("packet.{} = {}", field.getName(), compatibleReadObject)).append(LS);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
var readObject = pySerializer(fieldRegistration.serializer()).readObject(pyBuilder, 2, field, fieldRegistration);
|
var readObject = pySerializer(fieldRegistration.serializer()).readObject(pyBuilder, 2, field, fieldRegistration);
|
||||||
pyBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS);
|
pyBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS);
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2020 The zfoo Authors
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
||||||
* in compliance with the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
|
||||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.zfoo.protocol.serializer.python;
|
|
||||||
|
|
||||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
|
||||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
|
||||||
|
|
||||||
import com.zfoo.protocol.util.StringUtils;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
import static com.zfoo.protocol.util.FileUtils.LS;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author godotg
|
|
||||||
*/
|
|
||||||
public class PyCharSerializer implements IPySerializer {
|
|
||||||
@Override
|
|
||||||
public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistration) {
|
|
||||||
return "\"\"";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
|
||||||
GenerateProtocolFile.addTab(builder, deep);
|
|
||||||
builder.append(StringUtils.format("buffer.writeChar({})", objectStr)).append(LS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
|
||||||
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
|
||||||
GenerateProtocolFile.addTab(builder, deep);
|
|
||||||
builder.append(StringUtils.format("{} = buffer.readChar()", result)).append(LS);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -32,7 +32,7 @@ class ByteBuffer():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def compatibleRead(self, beforeReadIndex, length):
|
def compatibleRead(self, beforeReadIndex, length):
|
||||||
return length != -1 && self.getReadOffset() < length + beforeReadIndex
|
return length != -1 and self.getReadOffset() < length + beforeReadIndex
|
||||||
|
|
||||||
def getWriteOffset(self):
|
def getWriteOffset(self):
|
||||||
return self.writeOffset
|
return self.writeOffset
|
||||||
|
|||||||
@@ -8,16 +8,21 @@ class {}:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def write(cls, buffer, packet):
|
def write(cls, buffer, packet):
|
||||||
if buffer.writePacketFlag(packet):
|
if packet is None:
|
||||||
|
buffer.writeInt(0)
|
||||||
return
|
return
|
||||||
{}
|
{}
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls, buffer):
|
def read(cls, buffer):
|
||||||
if not buffer.readBool():
|
length = buffer.readInt()
|
||||||
|
if length == 0:
|
||||||
return None
|
return None
|
||||||
|
beforeReadIndex = buffer.getReadOffset()
|
||||||
packet = {}()
|
packet = {}()
|
||||||
{}
|
{}
|
||||||
|
if length > 0:
|
||||||
|
buffer.setReadOffset(beforeReadIndex + length)
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
Reference in new issue
Block a user