Skip to content

Commit

Permalink
ref[js]: refactor field method
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 15, 2024
1 parent 5062143 commit 21dc74b
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ private String protocol_field_definition(ProtocolRegistration registration) {
for (var fieldNote : fieldNotes) {
fieldDefinitionBuilder.append(fieldNote).append(LS);
}
var triple = jsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
fieldDefinitionBuilder.append(StringUtils.format("this.{} = {}; // {}", fieldName, triple.getRight(), triple.getLeft()))
var pair = jsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
fieldDefinitionBuilder.append(StringUtils.format("this.{} = {}; // {}", fieldName, pair.getValue(), pair.getKey()))
.append(LS);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package com.zfoo.protocol.serializer.javascript;

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 IJsSerializer {
/**
* 获取属性的类型,名称,默认值
*/
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,6 +14,7 @@
package com.zfoo.protocol.serializer.javascript;

import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.ArrayField;
import com.zfoo.protocol.registration.field.IFieldRegistration;
Expand All @@ -31,9 +32,9 @@
*/
public class JsArraySerializer implements IJsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format("Array<{}>", CodeGenerateTypeScript.toTsClassName(field.getType().getComponentType().getSimpleName()));
return new Triple<>(type, field.getName(), "[]");
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.javascript;

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 @@ -27,8 +27,8 @@
*/
public class JsBoolSerializer implements IJsSerializer {
@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.javascript;

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 @@ -27,8 +27,8 @@
*/
public class JsByteSerializer implements IJsSerializer {
@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.javascript;

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 @@ -27,8 +27,8 @@
*/
public class JsDoubleSerializer implements IJsSerializer {
@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.javascript;

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 @@ -27,8 +27,8 @@
*/
public class JsFloatSerializer implements IJsSerializer {
@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.javascript;

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 @@ -27,8 +27,8 @@
*/
public class JsIntSerializer implements IJsSerializer {
@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.javascript;

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,8 +31,8 @@
*/
public class JsListSerializer implements IJsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), field.getName(), "[]");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), "[]");
}

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

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 @@ -27,8 +27,8 @@
*/
public class JsLongSerializer implements IJsSerializer {
@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.javascript;

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,8 +31,8 @@
*/
public class JsMapSerializer implements IJsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), field.getName(), "new Map()");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), "new Map()");
}

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

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.registration.field.ObjectProtocolField;
Expand All @@ -29,11 +30,11 @@
*/
public class JsObjectProtocolSerializer implements IJsSerializer {
@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");
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.javascript;

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,8 +31,8 @@
*/
public class JsSetSerializer implements IJsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), field.getName(), "new Set()");
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>(CodeGenerateTypeScript.toTsClassName(field.getGenericType().toString()), "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.javascript;

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 @@ -27,8 +27,8 @@
*/
public class JsShortSerializer implements IJsSerializer {
@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.javascript;

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 @@ -27,8 +27,8 @@
*/
public class JsStringSerializer implements IJsSerializer {
@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 21dc74b

Please sign in to comment.