mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-16 14:23:38 +00:00
ref[protocol]: refactor field method
This commit is contained in:
@@ -276,11 +276,10 @@ public class CodeGenerateCpp implements ICodeGenerate {
|
||||
var field = sequencedFields.get(i);
|
||||
IFieldRegistration fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
|
||||
var fieldName = field.getName();
|
||||
var propertyTypeAndName = cppSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
|
||||
var propertyType = propertyTypeAndName.getKey();
|
||||
var propertyName = propertyTypeAndName.getValue();
|
||||
var fieldTypeValue = cppSerializer(fieldRegistration.serializer()).fieldTypeValue(field, fieldRegistration);
|
||||
var fieldType = fieldTypeValue.getKey();
|
||||
|
||||
var propertyFullName = StringUtils.format("{} {};", propertyType, propertyName);
|
||||
var propertyFullName = StringUtils.format("{} {};", fieldType, fieldName);
|
||||
// 生成注释
|
||||
var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.Cpp);
|
||||
for (var fieldNote : fieldNotes) {
|
||||
|
||||
@@ -32,9 +32,9 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppArraySerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = CodeGenerateCpp.toCppClassName(field.getType().getComponentType().getSimpleName());
|
||||
return new Pair<>(StringUtils.format("vector<{}>", type), field.getName());
|
||||
return new Pair<>(StringUtils.format("vector<{}>", type), "null");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,8 +28,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppBoolSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("bool", field.getName());
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("bool", "false");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,8 +28,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppByteSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int8_t", field.getName());
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int8_t", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,8 +28,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppDoubleSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("double", field.getName());
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("double", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,8 +28,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppFloatSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("float", field.getName());
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("float", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,8 +28,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppIntSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int32_t", field.getName());
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int32_t", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,9 +32,9 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppListSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = CodeGenerateCpp.toCppClassName(field.getGenericType().toString());
|
||||
return new Pair<>(type, field.getName());
|
||||
return new Pair<>(type, "null");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,8 +28,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppLongSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int64_t", field.getName());
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int64_t", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,9 +32,9 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppMapSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = CodeGenerateCpp.toCppClassName(field.getGenericType().toString());
|
||||
return new Pair<>(type, field.getName());
|
||||
return new Pair<>(type, "null");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+2
-2
@@ -32,11 +32,11 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppObjectProtocolSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
|
||||
var type = StringUtils.format("{}", protocolSimpleName);
|
||||
return new Pair<>(type, field.getName());
|
||||
return new Pair<>(type, "null");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,9 +32,9 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppSetSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = CodeGenerateCpp.toCppClassName(field.getGenericType().toString());
|
||||
return new Pair<>(type, field.getName());
|
||||
return new Pair<>(type, "null");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,8 +28,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppShortSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int16_t", field.getName());
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("int16_t", "0");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,8 +28,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class CppStringSerializer implements ICppSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("string", field.getName());
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("string", "null");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,9 +24,9 @@ import java.lang.reflect.Field;
|
||||
public interface ICppSerializer {
|
||||
|
||||
/**
|
||||
* 获取属性的类型和名称
|
||||
* 获取属性的类型和默认值
|
||||
*/
|
||||
Pair<String, String> field(Field field, IFieldRegistration fieldRegistration);
|
||||
Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration);
|
||||
|
||||
void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration);
|
||||
|
||||
|
||||
+1
-1
@@ -293,7 +293,7 @@ public class CodeGenerateTypeScript implements ICodeGenerate {
|
||||
for (var fieldNote : fieldNotes) {
|
||||
tsBuilder.append(fieldNote).append(LS);
|
||||
}
|
||||
var pair = tsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
|
||||
var pair = tsSerializer(fieldRegistration.serializer()).fieldTypeValue(field, fieldRegistration);
|
||||
tsBuilder.append(StringUtils.format("{}: {} = {};", fieldName, pair.getKey(), pair.getValue())).append(LS);
|
||||
}
|
||||
return tsBuilder.toString();
|
||||
|
||||
@@ -24,9 +24,9 @@ import java.lang.reflect.Field;
|
||||
public interface ITsSerializer {
|
||||
|
||||
/**
|
||||
* 获取属性的类型,名称,默认值
|
||||
* 获取属性的类型,默认值
|
||||
*/
|
||||
Pair<String, String> field(Field field, IFieldRegistration fieldRegistration);
|
||||
Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration);
|
||||
|
||||
void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration);
|
||||
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsArraySerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = StringUtils.format("Array<{}>", CodeGenerateTypeScript.toTsClassName(field.getType().getComponentType().getSimpleName()));
|
||||
return new Pair<>(type, "[]");
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsBoolSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("boolean", "false");
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsByteSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("number", "0");
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsDoubleSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("number", "0");
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsFloatSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("number", "0");
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsIntSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("number", "0");
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsListSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = StringUtils.format("{}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
|
||||
return new Pair<>(type, "[]");
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsLongSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("number", "0");
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsMapSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = StringUtils.format("{}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
|
||||
return new Pair<>(type, "new Map()");
|
||||
}
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsObjectProtocolSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
|
||||
var type = StringUtils.format("{} | null", protocolSimpleName);
|
||||
|
||||
@@ -31,7 +31,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsSetSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
var type = StringUtils.format("{}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
|
||||
return new Pair<>(type, "new Set()");
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsShortSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("number", "0");
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
public class TsStringSerializer implements ITsSerializer {
|
||||
|
||||
@Override
|
||||
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
|
||||
public Pair<String, String> fieldTypeValue(Field field, IFieldRegistration fieldRegistration) {
|
||||
return new Pair<>("string", "''");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user