From 36577396ec48028404df7ea3a59f31fdeba264e7 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 14 Jan 2025 10:56:08 +0800 Subject: [PATCH] accounts/abi: add the original name as json-structtag for tuples (#19340) --- accounts/abi/type.go | 1 + accounts/abi/type_test.go | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/accounts/abi/type.go b/accounts/abi/type.go index 325cd724e4..90ebb75ab2 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -187,6 +187,7 @@ func NewType(t string, components []ArgumentMarshaling) (typ Type, err error) { fields = append(fields, reflect.StructField{ Name: ToCamelCase(c.Name), // reflect.StructOf will panic for any exported field. Type: cType.Type, + Tag: reflect.StructTag("json:\"" + c.Name + "\""), }) elems = append(elems, &cType) names = append(names, c.Name) diff --git a/accounts/abi/type_test.go b/accounts/abi/type_test.go index fdedad0f66..2f663d51d7 100644 --- a/accounts/abi/type_test.go +++ b/accounts/abi/type_test.go @@ -95,8 +95,14 @@ func TestTypeRegexp(t *testing.T) { // {"fixed[2]", nil, Type{}}, // {"fixed128x128[]", nil, Type{}}, // {"fixed128x128[2]", nil, Type{}}, - {"tuple", []ArgumentMarshaling{{Name: "a", Type: "int64"}}, Type{Kind: reflect.Struct, T: TupleTy, Type: reflect.TypeOf(struct{ A int64 }{}), stringKind: "(int64)", + {"tuple", []ArgumentMarshaling{{Name: "a", Type: "int64"}}, Type{Kind: reflect.Struct, T: TupleTy, Type: reflect.TypeOf(struct { + A int64 `json:"a"` + }{}), stringKind: "(int64)", TupleElems: []*Type{{Kind: reflect.Int64, T: IntTy, Type: reflect.TypeOf(int64(0)), Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"a"}}}, + {"tuple with long name", []ArgumentMarshaling{{Name: "aTypicalParamName", Type: "int64"}}, Type{Kind: reflect.Struct, T: TupleTy, Type: reflect.TypeOf(struct { + ATypicalParamName int64 `json:"aTypicalParamName"` + }{}), stringKind: "(int64)", + TupleElems: []*Type{{Kind: reflect.Int64, T: IntTy, Type: reflect.TypeOf(int64(0)), Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"aTypicalParamName"}}}, } for _, tt := range tests {