Skip to content

Commit

Permalink
🐳 perf(i18n): #376 TextDocumentGenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Nov 2, 2024
1 parent b69225c commit 4adfe94
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public void onAfter(BarSkeleton barSkeleton) {
}
}


/**
* proto 协议类型添检测
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.iohao.game.action.skeleton.core.doc;

import com.iohao.game.action.skeleton.core.CmdKit;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -31,14 +32,16 @@
@Getter
@Setter
public final class ActionMemberCmdDocument {
int cmd;
int subCmd;
final int cmd;
final int subCmd;
final int cmdMerge;
String comment;
String memberName;

ActionMemberCmdDocument(int cmd, int subCmd, String memberName, String comment) {
this.cmd = cmd;
this.subCmd = subCmd;
this.cmdMerge = CmdKit.merge(cmd, subCmd);
this.comment = comment;
this.memberName = memberName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public final class BroadcastDocument {
String methodDescription;
/** 方法名 */
String methodName;
String cmdMethodName;

/** 业务类型 */
Class<?> dataClass;
Expand All @@ -67,6 +68,7 @@ public final class BroadcastDocument {
String dataActualTypeNameSimple;

String exampleCode;
String exampleCodeAction;

public int getCmdMerge() {
return this.cmdInfo.getCmdMerge();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public BroadcastDocument build() {
// 方法相关
.setMethodDescription(this.methodDescription)
.setMethodName(theMethodName)
.setCmdMethodName(StrKit.firstCharToLowerCase(theMethodName))
// 业务参数相关
.setDataClass(this.dataClass)
.setDataClassName(this.dataClassName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -78,6 +75,12 @@ private List<ErrorCodeDocument> analyseActionErrorEnumDocument(Class<? extends M
errorCodeDocument.setName(code.name());
errorCodeDocument.setValue(code.getCode());
errorCodeDocument.setDescription(code.getMsg());

// i18n
if (Locale.getDefault() == Locale.US) {
errorCodeDocument.setDescription(code.name());
}

return errorCodeDocument;
}).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.iohao.game.action.skeleton.core.exception.ActionErrorEnum;
import com.iohao.game.action.skeleton.core.exception.MsgExceptionInfo;
import com.iohao.game.common.kit.MoreKit;
import com.iohao.game.common.kit.RuntimeKit;
import com.iohao.game.common.kit.StrKit;
import com.iohao.game.common.kit.exception.ThrowKit;
import com.thoughtworks.qdox.model.JavaClass;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -167,6 +169,11 @@ private IoGameDocument analyse() {
*/
public void addDocumentGenerate(DocumentGenerate documentGenerate) {
var key = documentGenerate.getClass();

if (documentGenerateMap.containsKey(key)) {
ThrowKit.ofRuntimeException("%s exist".formatted(key));
}

IoGameDocumentHelper.documentGenerateMap.putIfAbsent(key, documentGenerate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package com.iohao.game.action.skeleton.core.doc;

import com.iohao.game.action.skeleton.i18n.Bundle;
import com.iohao.game.action.skeleton.i18n.MessageKey;
import com.iohao.game.common.kit.StrKit;
import com.iohao.game.common.kit.io.FileKit;
import com.iohao.game.common.kit.time.FormatTimeKit;
Expand Down Expand Up @@ -89,10 +91,12 @@ public void generate(IoGameDocument ioGameDocument) {

private void gameDocURLDescription() {
// 加上游戏文档格式说明
String title = Bundle.getMessage(MessageKey.textDocumentTitle);

String gameDocInfo = """
==================== 游戏文档格式说明 ====================
==================== %s ====================
https://www.yuque.com/iohao/game/irth38#cJLdC
""";
""".formatted(title);

this.docContentJoiner.add("generate %s".formatted(FormatTimeKit.format()));
this.docContentJoiner.add(gameDocInfo);
Expand All @@ -105,33 +109,47 @@ private void extractedBroadcastDoc(Map<Integer, BroadcastDocument> broadcastDocu
return;
}

this.docContentJoiner.add("==================== 其它广播推送 ====================");
String title = Bundle.getMessage(MessageKey.textDocumentBroadcastTitle);
this.docContentJoiner.add("==================== %s ====================".formatted(title));


for (BroadcastDocument broadcastDocument : broadcastDocumentList) {

String template = "路由: {cmd} - {subCmd} --- 广播推送: {dataClass} {dataDescription}";
String template = "{textDocumentCmd}: {cmd} - {subCmd} --- {textDocumentBroadcast}: {dataClass} {dataDescription}";

if (StrKit.isNotEmpty(broadcastDocument.getMethodDescription())) {
template = "路由: {cmd} - {subCmd} --- 广播推送: {dataClass} {dataDescription},({description})";
template = "{textDocumentCmd}: {cmd} - {subCmd} --- {textDocumentBroadcast}: {dataClass} {dataDescription},({description})";
}

var stringObjectMap = new HashMap<>();
stringObjectMap.put("cmd", broadcastDocument.getCmd());
stringObjectMap.put("subCmd", broadcastDocument.getSubCmd());
stringObjectMap.put("dataClass", broadcastDocument.getDataClassName());
stringObjectMap.put("description", broadcastDocument.getMethodDescription());
stringObjectMap.put("dataDescription", broadcastDocument.getDataDescription());
var paramMap = toMap(broadcastDocument);

String format = StrKit.format(template, stringObjectMap);
String format = StrKit.format(template, paramMap);
this.docContentJoiner.add(format);
}

this.docContentJoiner.add("");
}

private HashMap<Object, Object> toMap(BroadcastDocument broadcastDocument) {
String textDocumentCmd = Bundle.getMessage(MessageKey.textDocumentCmd);
String textDocumentBroadcast = Bundle.getMessage(MessageKey.textDocumentBroadcast);

var map = new HashMap<>();
map.put("cmd", broadcastDocument.getCmd());
map.put("subCmd", broadcastDocument.getSubCmd());
map.put("dataClass", broadcastDocument.getDataClassName());
map.put("description", broadcastDocument.getMethodDescription());
map.put("dataDescription", broadcastDocument.getDataDescription());
map.put("textDocumentCmd", textDocumentCmd);
map.put("textDocumentBroadcast", textDocumentBroadcast);

return map;
}

private void extractedErrorCode(IoGameDocument ioGameDocument) {
String title = Bundle.getMessage(MessageKey.textDocumentErrorCodeTitle);

this.docContentJoiner.add("==================== 错误码 ====================");
this.docContentJoiner.add("==================== %s ====================".formatted(title));

for (ErrorCodeDocument errorCodeDocument : ioGameDocument.getErrorCodeDocumentList()) {
String format = "%s : %s : %s".formatted(errorCodeDocument.getValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,11 @@ public interface MessageKey {
String cmdMergeLimit = "cmdMergeLimit";
/* see ProtobufCheckActionParserListener.java */
String protobufAnnotationCheck = "protobufAnnotationCheck";

/* see TextDocumentGenerate.java */
String textDocumentTitle = "textDocumentTitle";
String textDocumentBroadcastTitle = "textDocumentBroadcastTitle";
String textDocumentCmd = "textDocumentCmd";
String textDocumentBroadcast = "textDocumentBroadcast";
String textDocumentErrorCodeTitle = "textDocumentErrorCodeTitle";
}
7 changes: 6 additions & 1 deletion common/common-core/src/main/resources/iohao.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ timeRangeInOutMinuteTitle=[%d~%d minutes, execute:%d]
cmdMergeLimit=%s exceeds the maximum default value. Please set the capacity manually if necessary. Default maximum capacity %d, current capacity %d
# see ProtobufCheckActionParserListener
protobufAnnotationCheck=Note that the protocol class does not have the ProtobufClass annotation added

# see TextDocumentGenerate
textDocumentTitle=Game Document Format Description
textDocumentBroadcastTitle=Other broadcast interfaces
textDocumentCmd=Routing
textDocumentBroadcast=Broadcast
textDocumentErrorCodeTitle=Error Code
6 changes: 6 additions & 0 deletions common/common-core/src/main/resources/iohao_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ timeRangeInOutMinuteTitle=[%d~%d分钟,%d 次]
cmdMergeLimit=%s 超过最大默认值,如有需要请手动设置容量。默认最大容量 %d. 当前容量 %d
# see ProtobufCheckActionParserListener
protobufAnnotationCheck=注意,协议类没有添加 ProtobufClass 注解
# see TextDocumentGenerate
textDocumentTitle=游戏文档格式说明
textDocumentBroadcastTitle=其它广播推送
textDocumentCmd=路由
textDocumentBroadcast=广播
textDocumentErrorCodeTitle=错误码
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import lombok.NonNull;
import lombok.experimental.UtilityClass;

import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Map;

/**
Expand All @@ -48,6 +50,17 @@ public String firstCharToUpperCase(String value) {
return value;
}

public String firstCharToLowerCase(String value) {
char firstChar = value.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') {
char[] arr = value.toCharArray();
arr[0] += 32;
return String.valueOf(arr);
}

return value;
}

public String format(@NonNull CharSequence template, @NonNull Map<?, ?> map) {
return AdapterHuUtils.format(template, map);
}
Expand Down

0 comments on commit 4adfe94

Please sign in to comment.