mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
accounts/abi: first test and fixed function signature arg for a struct component
Signed-off-by: VoR0220 <catalanor0220@gmail.com>
This commit is contained in:
parent
a16610fc1e
commit
b7583393da
2 changed files with 53 additions and 14 deletions
|
|
@ -141,22 +141,24 @@ func checkForSlices(t string, structComponents ...unmarshalArg) (typ Type, found
|
||||||
|
|
||||||
if strings.Count(t, "[") != 0 {
|
if strings.Count(t, "[") != 0 {
|
||||||
i := strings.LastIndex(t, "[")
|
i := strings.LastIndex(t, "[")
|
||||||
// recursively embed the type
|
|
||||||
var embeddedType Type
|
|
||||||
if len(structComponents) > 0 {
|
|
||||||
embeddedType, err = ParseStructType(t, structComponents...)
|
|
||||||
} else {
|
|
||||||
embeddedType, err = NewType(t[:i])
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return Type{}, false, err
|
|
||||||
}
|
|
||||||
// grab the last cell and create a type from there
|
// grab the last cell and create a type from there
|
||||||
sliced := t[i:]
|
sliced := t[i:]
|
||||||
// grab the slice size with regexp
|
// grab the slice size with regexp
|
||||||
re := regexp.MustCompile("[0-9]+")
|
re := regexp.MustCompile("[0-9]+")
|
||||||
intz := re.FindAllString(sliced, -1)
|
intz := re.FindAllString(sliced, -1)
|
||||||
|
|
||||||
|
// recursively embed the type
|
||||||
|
var embeddedType Type
|
||||||
|
if len(structComponents) > 0 {
|
||||||
|
embeddedType, err = ParseStructType(t[:i], structComponents...)
|
||||||
|
typ.stringKind = embeddedType.String() + sliced
|
||||||
|
} else {
|
||||||
|
embeddedType, err = NewType(t[:i])
|
||||||
|
typ.stringKind = t
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return Type{}, false, err
|
||||||
|
}
|
||||||
if len(intz) == 0 {
|
if len(intz) == 0 {
|
||||||
// is a slice
|
// is a slice
|
||||||
typ.T = SliceTy
|
typ.T = SliceTy
|
||||||
|
|
@ -188,12 +190,14 @@ func ParseStructType(t string, components ...unmarshalArg) (typ Type, err error)
|
||||||
if typ, found, err = checkForSlices(t, components...); found == true || err != nil {
|
if typ, found, err = checkForSlices(t, components...); found == true || err != nil {
|
||||||
return typ, err
|
return typ, err
|
||||||
}
|
}
|
||||||
typ.stringKind = t
|
// need to concatenate the different type strings together
|
||||||
|
//typ.stringKind = t
|
||||||
typ.T = StructTy
|
typ.T = StructTy
|
||||||
typ.Kind = reflect.Struct
|
typ.Kind = reflect.Struct
|
||||||
// create the struct type
|
// create the struct type
|
||||||
var fields []reflect.StructField
|
var fields []reflect.StructField
|
||||||
for i, component := range components {
|
var typeStrings []string
|
||||||
|
for _, component := range components {
|
||||||
// it's a embedded struct type
|
// it's a embedded struct type
|
||||||
var fieldType Type
|
var fieldType Type
|
||||||
|
|
||||||
|
|
@ -202,13 +206,14 @@ func ParseStructType(t string, components ...unmarshalArg) (typ Type, err error)
|
||||||
} else {
|
} else {
|
||||||
fieldType, err = NewType(component.Type)
|
fieldType, err = NewType(component.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Type{}, err
|
return Type{}, err
|
||||||
}
|
}
|
||||||
fields[i] = reflect.StructField{Name: component.Name, Type: fieldType.Type, Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, component.Name))}
|
typeStrings = append(typeStrings, fieldType.String())
|
||||||
|
fields = append(fields, reflect.StructField{Name: strings.Title(component.Name), Type: fieldType.Type, Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, component.Name))})
|
||||||
}
|
}
|
||||||
typ.Type = reflect.StructOf(fields)
|
typ.Type = reflect.StructOf(fields)
|
||||||
|
typ.stringKind = "(" + strings.Join(typeStrings, ",") + ")"
|
||||||
return typ, nil
|
return typ, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,40 @@ func TestTypeRegexp(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sample structs for struct parsing test
|
||||||
|
|
||||||
|
type (
|
||||||
|
S struct {
|
||||||
|
A *big.Int
|
||||||
|
B []*big.Int
|
||||||
|
C []struct {
|
||||||
|
X *big.Int
|
||||||
|
Y *big.Int
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStructParse(t *testing.T) {
|
||||||
|
|
||||||
|
for i, test := range []struct {
|
||||||
|
input unmarshalArg
|
||||||
|
expectedOutput Type
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
unmarshalArg{Name: "s", Type: "tuple", Components: []unmarshalArg{unmarshalArg{Name: "a", Type: "uint256"}, unmarshalArg{Name: "b", Type: "uint256[]"}, unmarshalArg{Name: "c", Type: "tuple[]", Components: []unmarshalArg{unmarshalArg{Name: "x", Type: "uint256"}, unmarshalArg{Name: "y", Type: "uint256"}}}}},
|
||||||
|
Type{Kind: reflect.Struct, T: StructTy, Type: reflect.TypeOf(S{}), stringKind: "(uint256,uint256[],(uint256,uint256)[])"},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
newStruct, err := ParseStructType(test.input.Name, test.input.Components...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(newStruct, test.expectedOutput) {
|
||||||
|
t.Errorf("test %v: parsed type mismatch:\nGOT %v\nWANT %v ", i, newStruct, test.expectedOutput)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTypeCheck(t *testing.T) {
|
func TestTypeCheck(t *testing.T) {
|
||||||
for i, test := range []struct {
|
for i, test := range []struct {
|
||||||
typ string
|
typ string
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue