Skip to content

Commit

Permalink
🐳 feat(light-jprotobuf): The fields generated by the .proto file are …
Browse files Browse the repository at this point in the history
…named with underscores
  • Loading branch information
iohao committed Oct 30, 2024
1 parent f75466e commit 708000e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void gameDocURLDescription() {
https://www.yuque.com/iohao/game/irth38#cJLdC
""";

this.docContentJoiner.add("生成时间 %s".formatted(FormatTimeKit.format()));
this.docContentJoiner.add("generate %s".formatted(FormatTimeKit.format()));
this.docContentJoiner.add(gameDocInfo);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.widget.light.protobuf;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldDefaults;

/**
* FieldNameGenerate
*
* @author 渔民小镇
* @date 2024-10-30
* @since 21.20
*/
@Setter(value = AccessLevel.PACKAGE)
@FieldDefaults(level = AccessLevel.PRIVATE)
public final class FieldNameGenerate {
boolean enumType;
@Getter
String fieldName;

public boolean isEnum() {
return enumType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.iohao.game.widget.light.protobuf;

import com.iohao.game.common.kit.StrKit;
import com.iohao.game.widget.light.protobuf.kit.GenerateFileKit;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -64,9 +65,13 @@ private Map<String, String> createParam() {
messageMap.put("comment", this.comment);
messageMap.put("repeated", "");
messageMap.put("fieldProtoType", this.fieldProtoType);
messageMap.put("fieldName", this.fieldName);
messageMap.put("order", String.valueOf(this.order));

FieldNameGenerate fieldNameGenerate = new FieldNameGenerate();
fieldNameGenerate.setEnumType(this.protoJavaParent.getClazz().isEnum());
fieldNameGenerate.setFieldName(this.fieldName);
messageMap.put("fieldName", GenerateFileKit.getFieldNameFunction().apply(fieldNameGenerate));

if (this.repeated) {
messageMap.put("repeated", "repeated ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public String toProtoFile() {
String protoHead = this.regionHead.toProtoHead();

String firstLine = """
// 生成时间 %s
// generate %s
// https://github.com/iohao/ioGame
""".formatted(FormatTimeKit.format());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
package com.iohao.game.widget.light.protobuf.kit;

import com.iohao.game.common.kit.ArrayKit;
import com.iohao.game.widget.light.protobuf.FieldNameGenerate;
import com.iohao.game.widget.light.protobuf.ProtoGenerateFile;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.UtilityClass;

import java.io.File;
import java.util.function.Function;

/**
* proto 文件生成工具
Expand Down Expand Up @@ -78,4 +82,31 @@ public void generate(String protoPackagePath) {

generate(protoPackagePath, generateFolder);
}

@Setter
@Getter
Function<FieldNameGenerate, String> fieldNameFunction = fieldNameGenerate -> {
if (fieldNameGenerate.isEnum()) {
return fieldNameGenerate.getFieldName();
}

// default UnderScoreCase
StringBuilder result = new StringBuilder();
String fieldName = fieldNameGenerate.getFieldName();

for (int i = 0; i < fieldName.length(); i++) {
char c = fieldName.charAt(i);
if (Character.isUpperCase(c)) {
if (i > 0) {
result.append('_');
}

result.append(Character.toLowerCase(c));
} else {
result.append(c);
}
}

return result.toString();
};
}

0 comments on commit 708000e

Please sign in to comment.