-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Adding auto generated toString method for structs #2169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gtebrean
wants to merge
14
commits into
LFDT-web3j:main
Choose a base branch
from
aceppaluni:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7c21792
Adding auto generated toString method for structs
aceppaluni b9813ca
Merge branch 'LFDT-web3j:main' into main
aceppaluni 51e79df
Merge branch 'LFDT-web3j:main' into main
aceppaluni 0264b18
implement toString method for structs
aceppaluni 93f4a0b
Merge branch 'LFDT-web3j:main' into main
aceppaluni 09ea467
Merge branch 'LFDT-web3j:main' into main
aceppaluni 36471c2
Merge branch 'LFDT-web3j:main' into main
aceppaluni 0b8f0e8
Updated dynamicstruct file overriding toString
aceppaluni 94f5abf
Merge branch 'LFDT-web3j:main' into main
aceppaluni 9172fc1
Merge branch 'LFDT-web3j:main' into main
aceppaluni 2e8ae15
Merge branch 'LFDT-web3j:main' into main
aceppaluni d2a4e17
fix: verison fix
aceppaluni a29ad75
Merge branch 'main' of https://github.com/aceppaluni/web3j
aceppaluni 7e87638
added imports to the Dynamic struct class, modified test templates to…
FangshuoCao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright 2025 Web3 Labs Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
| package org.web3j.abi.datatypes; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** Base class for Solidity structs. Should be extended by generated struct wrappers. */ | ||
| public abstract class Struct extends DynamicStruct { | ||
| protected Struct(List<Type> values) { | ||
| super(values.toArray(new Type[0])); | ||
| } | ||
|
|
||
| protected Struct(Type... values) { | ||
| super(values); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
abi/src/test/java/org/web3j/abi/datatypes/DynamicStructTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2025 Web3 Labs Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
| package org.web3j.codegen; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.web3j.abi.datatypes.Address; | ||
| import org.web3j.abi.datatypes.DynamicStruct; | ||
| import org.web3j.abi.datatypes.generated.Uint256; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class DynamicStructTest { | ||
|
|
||
| @Test | ||
| public void testToStringForStruct() { | ||
| Uint256 value1 = new Uint256(123); | ||
| Address value2 = new Address("0x1234567890123456789012345678901234567890"); | ||
| DynamicStruct struct = new DynamicStruct(value1, value2); | ||
|
|
||
| String output = struct.toString(); | ||
| System.out.println("toString output: " + output); | ||
|
|
||
| // Optional: add assertion | ||
| // assertTrue(output.contains("field0: 123")); | ||
| // assertTrue(output.contains("field1: 0x1234567890123456789012345678901234567890")); | ||
|
|
||
| // Assertions | ||
| // assertTrue(output.contains("123")); | ||
| // assertTrue(output.contains("0x1234567890123456789012345678901234567890")); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
abi/src/test/java/org/web3j/abi/datatypes/TypeReferenceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright 2025 Web3 Labs Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.web3j.abi.TypeReference; | ||
| import org.web3j.abi.datatypes.Struct; | ||
| import org.web3j.abi.datatypes.Utf8String; | ||
| import org.web3j.abi.datatypes.generated.Uint256; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class TypeReferenceTest { | ||
| @Test | ||
| public void testStructTypeReferenceToString() { | ||
| List<String> fieldNames = Arrays.asList("name", "balance"); | ||
| List<TypeReference<?>> fieldTypes = | ||
| Arrays.asList( | ||
| TypeReference.create(Utf8String.class), | ||
| TypeReference.create(Uint256.class)); | ||
|
|
||
| TypeReference.StructTypeReference<Struct> ref = | ||
| new TypeReference.StructTypeReference<>("User", fieldNames, fieldTypes); | ||
| // String expected = "StructType(User) {name: class org.web3j.abi.datatypes.Utf8String, | ||
| // balance: class org.web3j.abi.datatypes.Uint256}"; | ||
| String expected = | ||
| "StructType(User) {name: class org.web3j.abi.datatypes.Utf8String, balance: class org.web3j.abi.datatypes.generated.Uint256}"; | ||
| // System.out.println("Actual: " + ref.toString()); | ||
| // System.out.println("Expected: " + expected); | ||
| assertEquals(expected, ref.toString()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, no comment needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Julius278 Will remove :)