Skip to content

Commit

Permalink
mobile: added MakeStructsFromJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed May 5, 2020
1 parent 89153af commit 340d893
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
14 changes: 7 additions & 7 deletions accounts/abi/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func formatSliceString(kind reflect.Kind, sliceSize int) string {
// type in t.
func sliceTypeCheck(t Type, val reflect.Value) error {
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
return typeErr(formatSliceString(t.getType().Kind(), t.Size), val.Type())
return typeErr(formatSliceString(t.GetType().Kind(), t.Size), val.Type())
}

if t.T == ArrayTy && val.Len() != t.Size {
return typeErr(formatSliceString(t.Elem.getType().Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
}

if t.Elem.T == SliceTy || t.Elem.T == ArrayTy {
Expand All @@ -52,8 +52,8 @@ func sliceTypeCheck(t Type, val reflect.Value) error {
}
}

if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.getType().Kind() {
return typeErr(formatSliceString(t.Elem.getType().Kind(), t.Size), val.Type())
if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.GetType().Kind() {
return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), val.Type())
}
return nil
}
Expand All @@ -66,10 +66,10 @@ func typeCheck(t Type, value reflect.Value) error {
}

// Check base type validity. Element types will be checked later on.
if t.getType().Kind() != value.Kind() {
return typeErr(t.getType().Kind(), value.Kind())
if t.GetType().Kind() != value.Kind() {
return typeErr(t.GetType().Kind(), value.Kind())
} else if t.T == FixedBytesTy && t.Size != value.Len() {
return typeErr(t.getType(), value.Type())
return typeErr(t.GetType(), value.Type())
} else {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions accounts/abi/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
}
fields = append(fields, reflect.StructField{
Name: ToCamelCase(c.Name), // reflect.StructOf will panic for any exported field.
Type: cType.getType(),
Type: cType.GetType(),
Tag: reflect.StructTag("json:\"" + c.Name + "\""),
})
elems = append(elems, &cType)
Expand Down Expand Up @@ -211,7 +211,7 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
return
}

func (t Type) getType() reflect.Type {
func (t Type) GetType() reflect.Type {
switch t.T {
case IntTy:
return reflectIntType(false, t.Size)
Expand All @@ -222,9 +222,9 @@ func (t Type) getType() reflect.Type {
case StringTy:
return reflect.TypeOf("")
case SliceTy:
return reflect.SliceOf(t.Elem.getType())
return reflect.SliceOf(t.Elem.GetType())
case ArrayTy:
return reflect.ArrayOf(t.Size, t.Elem.getType())
return reflect.ArrayOf(t.Size, t.Elem.GetType())
case TupleTy:
return t.TupleType
case AddressTy:
Expand Down
8 changes: 4 additions & 4 deletions accounts/abi/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func ReadFixedBytes(t Type, word []byte) (interface{}, error) {
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
}
// convert
array := reflect.New(t.getType()).Elem()
array := reflect.New(t.GetType()).Elem()

reflect.Copy(array, reflect.ValueOf(word[0:t.Size]))
return array.Interface(), nil
Expand All @@ -131,10 +131,10 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error)

if t.T == SliceTy {
// declare our slice
refSlice = reflect.MakeSlice(t.getType(), size, size)
refSlice = reflect.MakeSlice(t.GetType(), size, size)
} else if t.T == ArrayTy {
// declare our array
refSlice = reflect.New(t.getType()).Elem()
refSlice = reflect.New(t.GetType()).Elem()
} else {
return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage")
}
Expand All @@ -158,7 +158,7 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error)
}

func forTupleUnpack(t Type, output []byte) (interface{}, error) {
retval := reflect.New(t.getType()).Elem()
retval := reflect.New(t.GetType()).Elem()
virtualArgs := 0
for index, elem := range t.TupleElems {
marshalledValue, err := ToGoType((index+virtualArgs)*32, *elem, output)
Expand Down
39 changes: 39 additions & 0 deletions mobile/structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package geth

import (
"encoding/json"
"reflect"

"github.com/ethereum/go-ethereum/accounts/abi"
)

// MakeInterfacesFromJSON creates a new Interface slice that contains the
// prototype of types defined in the input json.
func MakeInterfacesFromJSON(data []byte) (Interfaces, error) {
var field struct {
Inputs []abi.Argument
}
if err := json.Unmarshal(data, &field); err != nil {
return Interfaces{}, err
}
var out []interface{}
for _, in := range field.Inputs {
out = append(out, Interface{reflect.New(in.Type.GetType())})
}
return Interfaces{out}, nil
}

0 comments on commit 340d893

Please sign in to comment.