Skip to content

Commit

Permalink
Fix normalizedType
Browse files Browse the repository at this point in the history
Signed-off-by: tonykwok1992 <[email protected]>
  • Loading branch information
tonykwok1992 committed Jun 17, 2024
1 parent 5c49ed3 commit 3085bf1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,6 @@ private List<TypeSpec> buildStructTypes(final List<AbiDefinition> functionDefini
private static String getStructName(String internalType) {
final String fullStructName = internalType.substring(internalType.lastIndexOf(" ") + 1);
String tempStructName = fullStructName.substring(fullStructName.lastIndexOf(".") + 1);
int arrayPos = tempStructName.indexOf("[");
if (arrayPos > -1) {
tempStructName = tempStructName.substring(0, arrayPos);
}
final String structName =
SourceVersion.isName(tempStructName) ? tempStructName : "_" + tempStructName;
return structName;
Expand All @@ -611,6 +607,7 @@ private String adjustToNativeTypeIfNecessary(NamedType component) {
}

private NamedType normalizeNamedType(NamedType namedType) {
// dynamic array
if (namedType.getType().endsWith("[]") && namedType.getInternalType().endsWith("[]")) {
return new NamedType(
namedType.getName(),
Expand All @@ -620,6 +617,18 @@ private NamedType normalizeNamedType(NamedType namedType) {
.getInternalType()
.substring(0, namedType.getInternalType().length() - 2),
namedType.isIndexed());
} else if (namedType.getType().startsWith("tuple[")
&& namedType.getInternalType().contains("[")
&& namedType.getInternalType().endsWith("]")) { // static array

return new NamedType(
namedType.getName(),
namedType.getType().substring(0, namedType.getType().indexOf("[")),
namedType.getComponents(),
namedType
.getInternalType()
.substring(0, namedType.getInternalType().indexOf("[")),
namedType.isIndexed());
} else {
return namedType;
}
Expand All @@ -637,38 +646,21 @@ private List<AbiDefinition.NamedType> extractStructs(
parameters.addAll(definition.getOutputs());
return parameters.stream()
.map(this::normalizeNamedType)
.filter(namedType -> namedType.getType().startsWith("tuple"));
.filter(namedType -> namedType.getType().equals("tuple"));
})
.forEach(
namedType -> {
if (namedType.getType().startsWith("tuple[")) {
structMap.putIfAbsent(
namedType.flattenNamedType().structIdentifier(), namedType);
} else {
structMap.put(namedType.structIdentifier(), namedType);
}

structMap.put(namedType.structIdentifier(), namedType);
extractNested(namedType).stream()
.map(this::normalizeNamedType)
.filter(
nestedNamedStruct ->
nestedNamedStruct.getType().startsWith("tuple"))
nestedNamedStruct.getType().equals("tuple"))
.forEach(
nestedNamedType -> {
if (nestedNamedType
.getType()
.startsWith("tuple[")) {
structMap.putIfAbsent(
nestedNamedType
.flattenNamedType()
.structIdentifier(),
nestedNamedType);
} else {
nestedNamedType ->
structMap.put(
nestedNamedType.structIdentifier(),
nestedNamedType);
}
});
nestedNamedType));
});

return structMap.values().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,6 @@ public boolean isDynamic() {
return components.stream().anyMatch(NamedType::isDynamic);
}

public NamedType flattenNamedType() {
int arrayPos = this.type.indexOf("[");
if(arrayPos > 0) return new NamedType(
this.name,
this.type.substring(0, arrayPos),
this.components,
this.internalType.substring(0, this.internalType.indexOf("[")),
this.indexed);
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down

0 comments on commit 3085bf1

Please sign in to comment.