Skip to content

Commit

Permalink
Merge pull request #21 from kyonRay/main
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
kyonRay authored Oct 16, 2023
2 parents 285ff8e + eada193 commit c2d6918
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 42 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ sourceSets {
// integrationTest.mustRunAfter test
allprojects {
group = 'org.fisco-bcos.code-generator'
version = '1.1.0'
version = '1.2.0'
apply plugin: 'maven-publish'
apply plugin: 'idea'
apply plugin: 'eclipse'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,10 @@ private FieldSpec createDefinition(String type, String binayArrayName) {
.build();
}

private FieldSpec createEventDefinition(String name, List<NamedTypeName> parameters) {
private FieldSpec createEventDefinition(
String rawEventName, String name, List<NamedTypeName> parameters) {

CodeBlock initializer = buildVariableLengthEventInitializer(name, parameters);
CodeBlock initializer = buildVariableLengthEventInitializer(rawEventName, parameters);

return FieldSpec.builder(Event.class, buildEventDefinitionName(name))
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
Expand All @@ -270,6 +271,20 @@ private static boolean isOverLoadFunction(
return count > 1;
}

private static boolean isOverLoadEvent(String name, List<ABIDefinition> functionDefinitions) {
int count = 0;
for (ABIDefinition functionDefinition : functionDefinitions) {
if (!functionDefinition.getType().equals("event")) {
continue;
}

if (functionDefinition.getName().equals(name)) {
count += 1;
}
}
return count > 1;
}

private List<MethodSpec> buildFunctionDefinitions(
TypeSpec.Builder classBuilder, List<ABIDefinition> functionDefinitions)
throws ClassNotFoundException {
Expand Down Expand Up @@ -302,7 +317,11 @@ private List<MethodSpec> buildFunctionDefinitions(
}
}
} else if (functionDefinition.getType().equals("event")) {
methodSpecs.addAll(buildEventFunctions(functionDefinition, classBuilder));
boolean isOverloadEvent =
isOverLoadEvent(functionDefinition.getName(), functionDefinitions);

methodSpecs.addAll(
buildEventFunctions(functionDefinition, classBuilder, isOverloadEvent));
}
}

Expand Down Expand Up @@ -926,7 +945,12 @@ private void buildConstantFunction(
nativeReturnTypeName = structClassNameMap.get(outputType.structIdentifier());
} else if (outputType.getType().startsWith("tuple")
&& outputType.getType().contains("[")) {
nativeReturnTypeName = typeName;
TypeName argument =
((ParameterizedTypeName) buildStructArrayTypeName(outputType))
.typeArguments.get(0);
nativeReturnTypeName =
ParameterizedTypeName.get(
ClassName.get(List.class), ClassName.get("", argument.toString()));
} else {
nativeReturnTypeName = this.getWrapperRawType(typeName);
}
Expand Down Expand Up @@ -972,14 +996,25 @@ private void buildConstantFunction(
nativeReturnTypeName);
}
} else {
List<TypeName> returnTypes = buildReturnTypes(outputParameterTypes);
List<TypeName> returnTypes = new ArrayList<>();
for (int i = 0; i < functionDefinition.getOutputs().size(); ++i) {
ABIDefinition.NamedType outputType = functionDefinition.getOutputs().get(i);
if (outputType.getType().equals("tuple")) {
returnTypes.add(structClassNameMap.get(outputType.structIdentifier()));
} else if (outputType.getType().startsWith("tuple")
&& outputType.getType().contains("[")) {
returnTypes.add(buildStructArrayTypeName(outputType));
} else {
returnTypes.add(getNativeType(outputParameterTypes.get(i)));
}
}

ParameterizedTypeName parameterizedTupleType =
ParameterizedTypeName.get(
ClassName.get(
"org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated",
"Tuple" + returnTypes.size()),
returnTypes.toArray(new TypeName[returnTypes.size()]));
returnTypes.toArray(new TypeName[0]));

methodBuilder.returns(parameterizedTupleType);

Expand Down Expand Up @@ -1067,12 +1102,28 @@ private TypeSpec buildEventResponseObject(

builder.addField(TransactionReceipt.Logs.class, "log", Modifier.PUBLIC);
for (NamedTypeName namedType : indexedParameters) {
TypeName typeName = getEventNativeType(namedType.typeName);
final TypeName typeName;
if (namedType.getType().equals("tuple")) {
typeName = structClassNameMap.get(namedType.structIdentifier());
} else if (namedType.getType().startsWith("tuple")
&& namedType.getType().contains("[")) {
typeName = buildStructArrayTypeName(namedType.namedType);
} else {
typeName = getEventNativeType(namedType.typeName);
}
builder.addField(typeName, namedType.getName(), Modifier.PUBLIC);
}

for (NamedTypeName namedType : nonIndexedParameters) {
TypeName typeName = getNativeType(namedType.typeName);
final TypeName typeName;
if (namedType.getType().equals("tuple")) {
typeName = structClassNameMap.get(namedType.structIdentifier());
} else if (namedType.getType().startsWith("tuple")
&& namedType.getType().contains("[")) {
typeName = buildStructArrayTypeName(namedType.namedType);
} else {
typeName = getNativeType(namedType.typeName);
}
builder.addField(typeName, namedType.getName(), Modifier.PUBLIC);
}

Expand Down Expand Up @@ -1177,9 +1228,10 @@ private MethodSpec buildEventTransactionReceiptFunction(
}

private List<MethodSpec> buildEventFunctions(
ABIDefinition functionDefinition, TypeSpec.Builder classBuilder)
ABIDefinition functionDefinition, TypeSpec.Builder classBuilder, boolean isOverload)
throws ClassNotFoundException {
String functionName = functionDefinition.getName();

String functionName = getInputOutputFunctionName(functionDefinition, isOverload);
List<ABIDefinition.NamedType> inputs = functionDefinition.getInputs();
String responseClassName =
StringUtils.capitaliseFirstLetter(functionName) + "EventResponse";
Expand All @@ -1196,6 +1248,15 @@ private List<MethodSpec> buildEventFunctions(
}
}
for (ABIDefinition.NamedType namedType : inputs) {
final TypeName typeName;
if (namedType.getType().equals("tuple")) {
typeName = structClassNameMap.get(namedType.structIdentifier());
} else if (namedType.getType().startsWith("tuple")
&& namedType.getType().contains("[")) {
typeName = buildStructArrayTypeName(namedType);
} else {
typeName = buildTypeName(namedType.getType());
}
if (namedType.getName() == null || namedType.getName().equals("")) {
String paramName = functionName + "Param" + index;
while (eventParamNameFilter.contains(paramName)) {
Expand All @@ -1205,11 +1266,7 @@ private List<MethodSpec> buildEventFunctions(
eventParamNameFilter.add(paramName);
namedType.setName(paramName);
}
NamedTypeName parameter =
new NamedTypeName(
namedType.getName(),
buildTypeName(namedType.getType()),
namedType.isIndexed());
NamedTypeName parameter = new NamedTypeName(namedType, typeName, namedType.isIndexed());
if (namedType.isIndexed()) {
indexedParameters.add(parameter);
} else {
Expand All @@ -1218,7 +1275,8 @@ private List<MethodSpec> buildEventFunctions(
parameters.add(parameter);
}

classBuilder.addField(createEventDefinition(functionName, parameters));
classBuilder.addField(
createEventDefinition(functionDefinition.getName(), functionName, parameters));

classBuilder.addType(
buildEventResponseObject(
Expand All @@ -1240,9 +1298,6 @@ private CodeBlock buildTypedResponse(
List<NamedTypeName> indexedParameters,
List<NamedTypeName> nonIndexedParameters,
boolean flowable) {
String nativeConversion;

nativeConversion = ".getValue()";

CodeBlock.Builder builder = CodeBlock.builder();
if (flowable) {
Expand All @@ -1251,21 +1306,51 @@ private CodeBlock buildTypedResponse(
builder.addStatement("$L.log = eventValues.getLog()", objectName);
}
for (int i = 0; i < indexedParameters.size(); i++) {
final NamedTypeName namedTypeName = indexedParameters.get(i);
String nativeConversion;
if (structClassNameMap.values().stream()
.noneMatch(name -> name.equals(namedTypeName.getTypeName()))
&& !namedTypeName.getType().startsWith("tuple[")) {
nativeConversion = ".getValue()";
} else {
nativeConversion = "";
}
final TypeName typeName;
if (namedTypeName.getType().equals("tuple")) {
typeName = structClassNameMap.get(namedTypeName.structIdentifier());
} else if (namedTypeName.getType().startsWith("tuple")
&& namedTypeName.getType().contains("[")) {
typeName = buildStructArrayTypeName(namedTypeName.namedType);
} else {
typeName = getEventNativeType(namedTypeName.getTypeName());
}
builder.addStatement(
"$L.$L = ($T) eventValues.getIndexedValues().get($L)" + nativeConversion,
objectName,
indexedParameters.get(i).getName(),
getEventNativeType(indexedParameters.get(i).getTypeName()),
typeName,
i);
}

for (int i = 0; i < nonIndexedParameters.size(); i++) {
final NamedTypeName namedTypeName = nonIndexedParameters.get(i);
String result = "$L.$L = ($T) eventValues.getNonIndexedValues().get($L)";
final TypeName typeName;
if (nonIndexedParameters.get(i).getType().equals("tuple")) {
typeName = structClassNameMap.get(namedTypeName.structIdentifier());
} else if (nonIndexedParameters.get(i).getType().startsWith("tuple")
&& nonIndexedParameters.get(i).getType().contains("[")) {
typeName = buildStructArrayTypeName(namedTypeName.namedType);
} else {
typeName = getNativeType(nonIndexedParameters.get(i).getTypeName());
}
if (structClassNameMap.values().stream()
.noneMatch(name -> name.equals(namedTypeName.getTypeName()))
&& !namedTypeName.getType().startsWith("tuple[")) {
result += ".getValue()";
}
builder.addStatement(
"$L.$L = ($T) eventValues.getNonIndexedValues().get($L)" + nativeConversion,
objectName,
nonIndexedParameters.get(i).getName(),
getNativeType(nonIndexedParameters.get(i).getTypeName()),
i);
result, objectName, nonIndexedParameters.get(i).getName(), typeName, i);
}
return builder.build();
}
Expand Down Expand Up @@ -1493,17 +1578,21 @@ private static String funcNameToConst(String funcName) {

private static class NamedTypeName {
private final TypeName typeName;
private final String name;
private final ABIDefinition.NamedType namedType;
private final boolean indexed;

NamedTypeName(String name, TypeName typeName, boolean indexed) {
this.name = name;
NamedTypeName(ABIDefinition.NamedType namedType, TypeName typeName, boolean indexed) {
this.namedType = namedType;
this.typeName = typeName;
this.indexed = indexed;
}

public String getName() {
return name;
return namedType.getName();
}

public String getType() {
return namedType.getType();
}

public TypeName getTypeName() {
Expand All @@ -1513,6 +1602,10 @@ public TypeName getTypeName() {
public boolean isIndexed() {
return indexed;
}

public int structIdentifier() {
return namedType.structIdentifier();
}
}

private static String getBinaryFuncDefinition() {
Expand Down Expand Up @@ -1747,12 +1840,21 @@ private static TypeName buildStructArrayTypeName(ABIDefinition.NamedType namedTy
.structIdentifier())
.toString();
} else {
structName =
namedType
.getInternalType()
.substring(
namedType.getInternalType().lastIndexOf(" ") + 1,
namedType.getInternalType().indexOf("["));
if (namedType.getInternalType().contains(".")) {
structName =
namedType
.getInternalType()
.substring(
namedType.getInternalType().lastIndexOf(".") + 1,
namedType.getInternalType().indexOf("["));
} else {
structName =
namedType
.getInternalType()
.substring(
namedType.getInternalType().lastIndexOf(" ") + 1,
namedType.getInternalType().indexOf("["));
}
}

return ParameterizedTypeName.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,21 @@ private TypeName buildStructArrayTypeName(ABIDefinition.NamedType namedType) {
namedType.getInternalType().lastIndexOf(".") + 1,
namedType.getInternalType().indexOf("["));
} else {
structName =
namedType
.getInternalType()
.substring(
namedType.getInternalType().lastIndexOf(" ") + 1,
namedType.getInternalType().indexOf("["));
if (namedType.getInternalType().contains(".")) {
structName =
namedType
.getInternalType()
.substring(
namedType.getInternalType().lastIndexOf(".") + 1,
namedType.getInternalType().indexOf("["));
} else {
structName =
namedType
.getInternalType()
.substring(
namedType.getInternalType().lastIndexOf(" ") + 1,
namedType.getInternalType().indexOf("["));
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/fisco/bcos/codegen/v2/test/CodeGenV2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
}
}

@Test
public void helloCodeGen() throws IOException {
final String COMPLEX_ABI_FILE = "HelloWorld.abi";
final String COMPLEX_NAME = "HelloWorld";
codeGenTest(COMPLEX_ABI_FILE, COMPLEX_NAME);
}

@Test
public void complexABICodeGen() throws IOException {
final String COMPLEX_ABI_FILE = "ComplexCodecTest.abi";
Expand All @@ -72,6 +79,13 @@ public void tableABICodeGen() throws IOException {
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void voteABICodeGen() throws IOException {
final String ABI_FILE = "AnonymousVoting.abi";
final String CONTRACT_NAME = "AnonymousVoting";
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void codecTestABICodeGen() throws IOException {
final String ABI_FILE = "CodecTest.abi";
Expand All @@ -86,6 +100,29 @@ public void weidABICodeGen() throws IOException {
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void StructReqRespCodeGen() throws IOException {
// abi from https://github.com/FISCO-BCOS/code-generator/issues/19
final String ABI_FILE = "StructReqRespContract.abi";
final String CONTRACT_NAME = "StructReqRespContract";
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void SupplyChainFinCodeGen() throws IOException {
// abi from https://github.com/FISCO-BCOS/java-sdk/issues/801
final String ABI_FILE = "SupplyChainFin.abi";
final String CONTRACT_NAME = "SupplyChainFin";
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

@Test
public void eventTestCodeGen() throws IOException {
final String ABI_FILE = "EventSubDemo.abi";
final String CONTRACT_NAME = "EventSubDemo";
codeGenTest(ABI_FILE, CONTRACT_NAME);
}

private void codeGenTest(String abiFileName, String contractName) throws IOException {
String abiFile = CodeGenV2Test.class.getClassLoader().getResource(abiFileName).getPath();
String binFile = CodeGenV2Test.class.getClassLoader().getResource(abiFileName).getPath();
Expand Down
Loading

0 comments on commit c2d6918

Please sign in to comment.