forked from TrueCloudLab/neoneo-go
smartcontract: add FromStackItem wrapper for RPC bindings
Make it exported and reusable from the user's code. Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
parent
b5ec1271d5
commit
36af361c2b
2 changed files with 318 additions and 158 deletions
454
cli/smartcontract/testdata/structs/rpcbindings.out
vendored
454
cli/smartcontract/testdata/structs/rpcbindings.out
vendored
File diff suppressed because it is too large
Load diff
|
@ -221,25 +221,35 @@ func itemTo{{toTypeName $name}}(item stackitem.Item, err error) (*{{toTypeName $
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
var res = new({{toTypeName $name}})
|
||||||
|
err = res.FromStackItem(item)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromStackItem retrieves fields of {{toTypeName $name}} from the given stack item
|
||||||
|
// and returns an error if so.
|
||||||
|
func (res *{{toTypeName $name}}) FromStackItem(item stackitem.Item) error {
|
||||||
arr, ok := item.Value().([]stackitem.Item)
|
arr, ok := item.Value().([]stackitem.Item)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("not an array")
|
return errors.New("not an array")
|
||||||
}
|
}
|
||||||
if len(arr) != {{len $typ.Fields}} {
|
if len(arr) != {{len $typ.Fields}} {
|
||||||
return nil, errors.New("wrong number of structure elements")
|
return errors.New("wrong number of structure elements")
|
||||||
}
|
}
|
||||||
|
|
||||||
var res = new({{toTypeName $name}})
|
{{if len .Fields}} var (
|
||||||
{{if len .Fields}} var index = -1
|
index = -1
|
||||||
|
err error
|
||||||
|
)
|
||||||
{{- range $m := $typ.Fields}}
|
{{- range $m := $typ.Fields}}
|
||||||
index++
|
index++
|
||||||
res.{{.Field}}, err = {{etTypeConverter .ExtendedType "arr[index]"}}
|
res.{{.Field}}, err = {{etTypeConverter .ExtendedType "arr[index]"}}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("field {{.Field}}: %w", err)
|
return fmt.Errorf("field {{.Field}}: %w", err)
|
||||||
}
|
}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
return res, err
|
return nil
|
||||||
}
|
}
|
||||||
{{end}}`
|
{{end}}`
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue