Skip to content

Commit

Permalink
feat[golang]: compatible field of inside protocol class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 15, 2023
1 parent 19559f7 commit efd0178
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ public static void createCppProtocolFile(ProtocolRegistration registration) thro
, valueOfMethod.getValue().trim(), protocolId, protocolClazzName, operator.trim(),
protocolClazzName, protocolId, protocolClazzName, writeObject.trim(), protocolClazzName, readObject.trim());

var protocolOutputPath = StringUtils.format("{}/{}/{}.h"
var outputPath = StringUtils.format("{}/{}/{}.h"
, GenerateCppUtils.protocolOutputPath
, GenerateProtocolPath.getProtocolPath(protocolId)
, protocolClazzName);
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true);
FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ public static void createCsProtocolFile(ProtocolRegistration registration) throw
, protocolId, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, writeObject.trim()
, protocolClazzName, protocolClazzName, readObject.trim());

var protocolOutputPath = StringUtils.format("{}/{}/{}.cs"
var outputPath = StringUtils.format("{}/{}/{}.cs"
, protocolOutputRootPath
, GenerateProtocolPath.getCapitalizeProtocolPath(protocolId)
, protocolClazzName);
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true);
FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true);
}

private static String fieldDefinition(ProtocolRegistration registration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.cpp.GenerateCppUtils;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.FileUtils;
Expand Down Expand Up @@ -114,39 +115,25 @@ public static void createGoProtocolFile(ProtocolRegistration registration) throw
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();

var protocolTemplate = ArrayUtils.isEmpty(registration.getFields())
? ClassUtils.getFileFromClassPathToString("go/ProtocolTemplateEmpty.go")
: ClassUtils.getFileFromClassPathToString("go/ProtocolTemplate.go");
var protocolTemplate = ClassUtils.getFileFromClassPathToString("go/ProtocolTemplate.go");

var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Go);
var fieldDefinition = fieldDefinition(registration);
var writeObject = writeObject(registration);
var readObject = readObject(registration);
if (ArrayUtils.isEmpty(registration.getFields())) {
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim()
, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName);
} else {
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim()
, protocolClazzName, protocolId, protocolClazzName, protocolClazzName
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim());
}

var protocolPath = GenerateProtocolPath.getProtocolPath(protocolId);
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim()
, protocolClazzName, protocolId, protocolClazzName, protocolClazzName
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim());

var protocolPath = GenerateProtocolPath.protocolAbsolutePath(protocolId);
if (StringUtils.isEmpty(protocolPath)) {
protocolPath = protocolClazzName;
} else if (protocolPath.contains(StringUtils.SLASH)) {
protocolPath = StringUtils.substringAfterLast(protocolPath, StringUtils.SLASH);
}

var protocolOutputPath = StringUtils.format("{}/{}.go"
, protocolOutputRootPath
, protocolPath);
var protocolFile = new File(protocolOutputPath);
if (protocolFile.exists()) {
FileUtils.writeStringToFile(protocolFile, StringUtils.substringAfterFirst(protocolTemplate, "package protocol"), true);
} else {
FileUtils.writeStringToFile(protocolFile, protocolTemplate, true);
}
var outputPath = StringUtils.format("{}/{}/{}.go", protocolOutputPath, protocolOutputRootPath, protocolClazzName);
FileUtils.writeStringToFile(outputPath, protocolTemplate, true);
}

private static String fieldDefinition(ProtocolRegistration registration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ public static void createLuaProtocolFile(ProtocolRegistration registration) thro
, valueOfMethod.getKey().trim(), valueOfMethod.getValue().trim(), protocolClazzName, protocolId
, protocolClazzName, writePacket.trim(), protocolClazzName, protocolClazzName, readPacket.trim(), protocolClazzName);

var protocolOutputPath = StringUtils.format("{}/{}/{}.lua"
var outputPath = StringUtils.format("{}/{}/{}.lua"
, protocolOutputRootPath, GenerateProtocolPath.getCapitalizeProtocolPath(protocolId), protocolClazzName);
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true);
FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true);
}

private static Pair<String, String> valueOfMethod(ProtocolRegistration registration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public static void createProtocolManager(List<IProtocolRegistration> allGenerate

builder.append("}").append(LS);

var protocolOutputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protocolManagerName);
FileUtils.writeStringToFile(new File(protocolOutputPath), builder.toString(), true);
var outputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protocolManagerName);
FileUtils.writeStringToFile(new File(outputPath), builder.toString(), true);
}

public static void createProtocols(List<IProtocolRegistration> allGenerateProtocols) {
Expand Down Expand Up @@ -168,8 +168,8 @@ public static void createProtocols(List<IProtocolRegistration> allGenerateProtoc
}

String protoName = StringUtils.substringAfterLast(protos.getKey(), StringUtils.PERIOD);
var protocolOutputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protoName);
FileUtils.writeStringToFile(new File(protocolOutputPath), builder.toString(), true);
var outputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protoName);
FileUtils.writeStringToFile(new File(outputPath), builder.toString(), true);
}
}

Expand Down
23 changes: 0 additions & 23 deletions protocol/src/main/resources/go/ProtocolTemplateEmpty.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ public void generateAllProtocols() {
var generateLanguages = op.getGenerateLanguages();

// generate the jsProtocol folder and its corresponding js protocol file in the current protocol directory
generateLanguages.add(CodeLanguage.Cpp);
generateLanguages.add(CodeLanguage.Go);
generateLanguages.add(CodeLanguage.JavaScript);
generateLanguages.add(CodeLanguage.TypeScript);
generateLanguages.add(CodeLanguage.Lua);
generateLanguages.add(CodeLanguage.CSharp);
generateLanguages.add(CodeLanguage.GdScript);
generateLanguages.add(CodeLanguage.Python);

// Protobuf needs to specify protobuf.xml to generate a protocol
op.setProtocolParam("protobuf=protobuf.xml");
generateLanguages.add(CodeLanguage.Protobuf);

// Initialize and then generate the protocol
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), op);
Expand Down

0 comments on commit efd0178

Please sign in to comment.