Skip to content

Commit

Permalink
ref[TypeScript]: refactor field method
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 15, 2024
1 parent 21dc74b commit 1ba4710
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ private String protocol_field_definition(ProtocolRegistration registration) {
for (var fieldNote : fieldNotes) {
tsBuilder.append(fieldNote).append(LS);
}
var triple = tsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
tsBuilder.append(StringUtils.format("{}{} = {};", triple.getMiddle(), triple.getLeft(), triple.getRight())).append(LS);
var pair = tsSerializer(fieldRegistration.serializer()).field(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 @@ -13,7 +13,7 @@

package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;

import java.lang.reflect.Field;
Expand All @@ -26,7 +26,7 @@ public interface ITsSerializer {
/**
* 获取属性的类型,名称,默认值
*/
Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration);
Pair<String, String> field(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 @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.ArrayField;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
Expand All @@ -31,9 +31,9 @@
public class TsArraySerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format(": Array<{}>", CodeGenerateTypeScript.toTsClassName(field.getType().getComponentType().getSimpleName()));
return new Triple<>(type, field.getName(), "[]");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format("Array<{}>", CodeGenerateTypeScript.toTsClassName(field.getType().getComponentType().getSimpleName()));
return new Pair<>(type, "[]");
}

@Override
Expand Down Expand Up @@ -75,7 +75,7 @@ public String readObject(StringBuilder builder, int deep, Field field, IFieldReg

ArrayField arrayField = (ArrayField) fieldRegistration;
String result = "result" + GenerateProtocolFile.localVariableId++;
var typeName = StringUtils.format("Array<{}>", CodeGenerateTypeScript.toTsClassName(arrayField.getType().getSimpleName()));
var typeName = StringUtils.format("Array<{}>", CodeGenerateTypeScript.toTsClassName(arrayField.getType().getSimpleName()));
builder.append(StringUtils.format("const {} = new {};", result, typeName)).append(LS);

String i = "index" + GenerateProtocolFile.localVariableId++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;

Expand All @@ -28,8 +28,8 @@
public class TsBoolSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": boolean", field.getName(), "false");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("boolean", "false");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;

Expand All @@ -28,8 +28,8 @@
public class TsByteSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", "0");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;

Expand All @@ -28,8 +28,8 @@
public class TsDoubleSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", "0");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;

Expand All @@ -28,8 +28,8 @@
public class TsFloatSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", "0");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
Expand All @@ -28,8 +29,8 @@
public class TsIntSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", "0");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ListField;
import com.zfoo.protocol.serializer.CodeLanguage;
Expand All @@ -31,9 +31,9 @@
public class TsListSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format(": {}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
return new Triple<>(type, field.getName(), "[]");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format("{}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
return new Pair<>(type, "[]");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;

Expand All @@ -28,8 +28,8 @@
public class TsLongSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", "0");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.MapField;
import com.zfoo.protocol.serializer.CodeLanguage;
Expand All @@ -31,9 +31,9 @@
public class TsMapSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format(": {}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
return new Triple<>(type, field.getName(), "new Map()");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format("{}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
return new Pair<>(type, "new Map()");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ObjectProtocolField;
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
Expand All @@ -30,11 +30,11 @@
public class TsObjectProtocolSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
var type = StringUtils.format(": {} | null", protocolSimpleName);
return new Triple<>(type, field.getName(), "null");
var type = StringUtils.format("{} | null", protocolSimpleName);
return new Pair<>(type, "null");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.SetField;
import com.zfoo.protocol.serializer.CodeLanguage;
Expand All @@ -31,9 +31,9 @@
public class TsSetSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format(": {}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
return new Triple<>(type, field.getName(), "new Set()");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format("{}", CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()));
return new Pair<>(type, "new Set()");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;

Expand All @@ -28,8 +28,8 @@
public class TsShortSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", "0");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;

Expand All @@ -28,8 +28,8 @@
public class TsStringSerializer implements ITsSerializer {

@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": string", field.getName(), "''");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("string", "''");
}

@Override
Expand Down

0 comments on commit 1ba4710

Please sign in to comment.