Skip to content

Commit 60a999f

Browse files
committed
accounts/abi: Modified unpackAtomic to accept struct lvalues
1 parent 13b566e commit 60a999f

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

accounts/abi/abi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
8686
}
8787
return method.Outputs.Unpack(v, output)
8888
} else if event, ok := abi.Events[name]; ok {
89-
return event.Inputs.unpackTuple(v, output)
89+
return event.Inputs.Unpack(v, output)
9090
}
9191
return fmt.Errorf("abi: could not locate named method or event")
9292
}

accounts/abi/argument.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,8 @@ func (arguments Arguments) unpackTuple(v interface{}, marshalledValues []interfa
113113
}
114114
// If the output interface is a struct, make sure names don't collide
115115
if kind == reflect.Struct {
116-
exists := make(map[string]bool)
117-
for _, arg := range arguments {
118-
field := capitalise(arg.Name)
119-
if field == "" {
120-
return fmt.Errorf("abi: purely underscored output cannot unpack to struct")
121-
}
122-
if exists[field] {
123-
return fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", field)
124-
}
125-
exists[field] = true
116+
if err := requireUniqueStructFieldNames(arguments); err != nil {
117+
return err
126118
}
127119
}
128120
for i, arg := range arguments.NonIndexed() {
@@ -131,14 +123,9 @@ func (arguments Arguments) unpackTuple(v interface{}, marshalledValues []interfa
131123

132124
switch kind {
133125
case reflect.Struct:
134-
name := capitalise(arg.Name)
135-
for j := 0; j < typ.NumField(); j++ {
136-
// TODO read tags: `abi:"fieldName"`
137-
if typ.Field(j).Name == name {
138-
if err := set(value.Field(j), reflectValue, arg); err != nil {
139-
return err
140-
}
141-
}
126+
err := unpackStruct(value, reflectValue, arg)
127+
if err != nil {
128+
return err
142129
}
143130
case reflect.Slice, reflect.Array:
144131
if value.Len() < i {
@@ -165,8 +152,20 @@ func (arguments Arguments) unpackAtomic(v interface{}, marshalledValues []interf
165152
return fmt.Errorf("abi: wrong length, expected single value, got %d", len(marshalledValues))
166153
}
167154
elem := reflect.ValueOf(v).Elem()
155+
kind := elem.Kind()
168156
reflectValue := reflect.ValueOf(marshalledValues[0])
157+
158+
if kind == reflect.Struct {
159+
//make sure names don't collide
160+
if err := requireUniqueStructFieldNames(arguments); err != nil {
161+
return err
162+
}
163+
164+
return unpackStruct(elem, reflectValue, arguments[0])
165+
}
166+
169167
return set(elem, reflectValue, arguments.NonIndexed()[0])
168+
170169
}
171170

172171
// Computes the full size of an array;
@@ -278,3 +277,18 @@ func capitalise(input string) string {
278277
}
279278
return strings.ToUpper(input[:1]) + input[1:]
280279
}
280+
281+
//unpackStruct extracts each argument into its corresponding struct field
282+
func unpackStruct(value, reflectValue reflect.Value, arg Argument) error {
283+
name := capitalise(arg.Name)
284+
typ := value.Type()
285+
for j := 0; j < typ.NumField(); j++ {
286+
// TODO read tags: `abi:"fieldName"`
287+
if typ.Field(j).Name == name {
288+
if err := set(value.Field(j), reflectValue, arg); err != nil {
289+
return err
290+
}
291+
}
292+
}
293+
return nil
294+
}

accounts/abi/reflect.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,19 @@ func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind,
110110
}
111111
return nil
112112
}
113+
114+
// requireUniqueStructFieldNames makes sure field names don't collide
115+
func requireUniqueStructFieldNames(args Arguments) error {
116+
exists := make(map[string]bool)
117+
for _, arg := range args {
118+
field := capitalise(arg.Name)
119+
if field == "" {
120+
return fmt.Errorf("abi: purely underscored output cannot unpack to struct")
121+
}
122+
if exists[field] {
123+
return fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", field)
124+
}
125+
exists[field] = true
126+
}
127+
return nil
128+
}

0 commit comments

Comments
 (0)