Skip to content

Commit

Permalink
ref[protocol]: refactor field method
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 15, 2024
1 parent 1ba4710 commit 9179b85
Show file tree
Hide file tree
Showing 30 changed files with 47 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,10 @@ private String protocol_field_definition(ProtocolRegistration registration) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private String protocol_field_definition(ProtocolRegistration registration) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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, "[]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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, "[]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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()");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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()");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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", "''");
}

Expand Down

0 comments on commit 9179b85

Please sign in to comment.