From 8dd5ddbe3d9a16f2ccb72fd10d9ce720a0074b38 Mon Sep 17 00:00:00 2001 From: godotg Date: Mon, 15 Jul 2024 15:10:03 +0800 Subject: [PATCH] ref[protocol]: refactor field method --- .../serializer/javascript/CodeGenerateJavaScript.java | 8 ++++---- .../protocol/serializer/javascript/IJsSerializer.java | 4 ++-- .../protocol/serializer/javascript/JsArraySerializer.java | 2 +- .../protocol/serializer/javascript/JsBoolSerializer.java | 2 +- .../protocol/serializer/javascript/JsByteSerializer.java | 2 +- .../serializer/javascript/JsDoubleSerializer.java | 2 +- .../protocol/serializer/javascript/JsFloatSerializer.java | 2 +- .../protocol/serializer/javascript/JsIntSerializer.java | 2 +- .../protocol/serializer/javascript/JsListSerializer.java | 2 +- .../protocol/serializer/javascript/JsLongSerializer.java | 2 +- .../protocol/serializer/javascript/JsMapSerializer.java | 2 +- .../serializer/javascript/JsObjectProtocolSerializer.java | 2 +- .../protocol/serializer/javascript/JsSetSerializer.java | 2 +- .../protocol/serializer/javascript/JsShortSerializer.java | 2 +- .../serializer/javascript/JsStringSerializer.java | 2 +- 15 files changed, 19 insertions(+), 19 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/CodeGenerateJavaScript.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/CodeGenerateJavaScript.java index 785665b83..82be7e907 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/CodeGenerateJavaScript.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/CodeGenerateJavaScript.java @@ -253,10 +253,10 @@ private String protocol_field_definition(ProtocolRegistration registration) { for (var fieldNote : fieldNotes) { fieldDefinitionBuilder.append(fieldNote).append(LS); } - var pair = jsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration); - fieldDefinitionBuilder.append(StringUtils.format("this.{} = {}; // {}", fieldName, pair.getValue(), pair.getKey())) - .append(LS); - + var fieldTypeDefaultValue = jsSerializer(fieldRegistration.serializer()).fieldTypeDefaultValue(field, fieldRegistration); + var fieldType = fieldTypeDefaultValue.getKey(); + var fieldDefaultValue = fieldTypeDefaultValue.getValue(); + fieldDefinitionBuilder.append(StringUtils.format("this.{} = {}; // {}", fieldName, fieldDefaultValue, fieldType)).append(LS); } return fieldDefinitionBuilder.toString(); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/IJsSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/IJsSerializer.java index 62cac9397..6f6590b13 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/IJsSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/IJsSerializer.java @@ -24,9 +24,9 @@ public interface IJsSerializer { /** - * 获取属性的类型,名称,默认值 + * 获取属性的类型,默认值 */ - Pair field(Field field, IFieldRegistration fieldRegistration); + Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration); void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsArraySerializer.java index 91b110dc6..7d0487b95 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsArraySerializer.java @@ -32,7 +32,7 @@ */ public class JsArraySerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { var type = StringUtils.format("Array<{}>", CodeGenerateTypeScript.toTsClassName(field.getType().getComponentType().getSimpleName())); return new Pair<>(type, "[]"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsBoolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsBoolSerializer.java index 0bd43f53a..4ee0712cf 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsBoolSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsBoolSerializer.java @@ -27,7 +27,7 @@ */ public class JsBoolSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>("boolean", "false"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsByteSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsByteSerializer.java index 7e924f4e2..0e73718a1 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsByteSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsByteSerializer.java @@ -27,7 +27,7 @@ */ public class JsByteSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>("number", "0"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsDoubleSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsDoubleSerializer.java index ae41dd24f..48de010ea 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsDoubleSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsDoubleSerializer.java @@ -27,7 +27,7 @@ */ public class JsDoubleSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>("number", "0"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsFloatSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsFloatSerializer.java index f252531d3..21eadc460 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsFloatSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsFloatSerializer.java @@ -27,7 +27,7 @@ */ public class JsFloatSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>("number", "0"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsIntSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsIntSerializer.java index 2f9e413cb..b811728e4 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsIntSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsIntSerializer.java @@ -27,7 +27,7 @@ */ public class JsIntSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>("number", "0"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsListSerializer.java index 17818ab00..0eb778919 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsListSerializer.java @@ -31,7 +31,7 @@ */ public class JsListSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), "[]"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsLongSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsLongSerializer.java index 63f69e950..44ce1d546 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsLongSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsLongSerializer.java @@ -27,7 +27,7 @@ */ public class JsLongSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>("number", "0"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsMapSerializer.java index c9f4b585f..21f0e72bd 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsMapSerializer.java @@ -31,7 +31,7 @@ */ public class JsMapSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), "new Map()"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsObjectProtocolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsObjectProtocolSerializer.java index 40c3d20c5..216c4bf7b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsObjectProtocolSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsObjectProtocolSerializer.java @@ -30,7 +30,7 @@ */ public class JsObjectProtocolSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()); var type = StringUtils.format("{} | null", protocolSimpleName); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsSetSerializer.java index e1e177e0f..b64eee62b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsSetSerializer.java @@ -31,7 +31,7 @@ */ public class JsSetSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), "new Set()"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsShortSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsShortSerializer.java index 0d5cb2026..82e1134cc 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsShortSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsShortSerializer.java @@ -27,7 +27,7 @@ */ public class JsShortSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>("number", "0"); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsStringSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsStringSerializer.java index 300aa4a97..0aee387f0 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsStringSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/JsStringSerializer.java @@ -27,7 +27,7 @@ */ public class JsStringSerializer implements IJsSerializer { @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { + public Pair fieldTypeDefaultValue(Field field, IFieldRegistration fieldRegistration) { return new Pair<>("string", "\"\""); }