From 07f58abe3dd4fbc5de3134966fa37ef4a5f3d4d0 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 9 Jul 2022 09:48:15 +0300 Subject: [PATCH] result: provide (*Iterator).UnmarshalJSON It makes Iterator more symmetric and simplifies (*Invoke).UnmarshalJSON code. No functional changes. --- pkg/rpc/response/result/invoke.go | 69 ++++++++++++++++--------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/pkg/rpc/response/result/invoke.go b/pkg/rpc/response/result/invoke.go index 2cd78bb13..ef579f600 100644 --- a/pkg/rpc/response/result/invoke.go +++ b/pkg/rpc/response/result/invoke.go @@ -92,6 +92,36 @@ func (r Iterator) MarshalJSON() ([]byte, error) { return json.Marshal(iaux) } +// UnmarshalJSON implements the json.Unmarshaler. +func (r *Iterator) UnmarshalJSON(data []byte) error { + iteratorAux := new(iteratorAux) + err := json.Unmarshal(data, iteratorAux) + if err != nil { + return err + } + if len(iteratorAux.Interface) != 0 { + if iteratorAux.Interface != iteratorInterfaceName { + return fmt.Errorf("unknown InteropInterface: %s", iteratorAux.Interface) + } + var iID uuid.UUID + iID, err = uuid.Parse(iteratorAux.ID) + if err != nil { + return fmt.Errorf("failed to unmarshal iterator ID: %w", err) + } + r.ID = &iID + } else { + r.Values = make([]stackitem.Item, len(iteratorAux.Value)) + for j := range r.Values { + r.Values[j], err = stackitem.FromJSONWithTypes(iteratorAux.Value[j]) + if err != nil { + return fmt.Errorf("failed to unmarshal iterator values: %w", err) + } + } + r.Truncated = iteratorAux.Truncated + } + return nil +} + // MarshalJSON implements the json.Marshaler. func (r Invoke) MarshalJSON() ([]byte, error) { var ( @@ -171,41 +201,12 @@ func (r *Invoke) UnmarshalJSON(data []byte) error { break } if st[i].Type() == stackitem.InteropT { - iteratorAux := new(iteratorAux) - if json.Unmarshal(arr[i], iteratorAux) == nil { - if len(iteratorAux.Interface) != 0 { - if iteratorAux.Interface != iteratorInterfaceName { - err = fmt.Errorf("unknown InteropInterface: %s", iteratorAux.Interface) - break - } - var iID uuid.UUID - iID, err = uuid.Parse(iteratorAux.ID) // iteratorAux.ID is always non-empty, see https://github.com/neo-project/neo-modules/pull/715#discussion_r897635424. - if err != nil { - err = fmt.Errorf("failed to unmarshal iterator ID: %w", err) - break - } - // It's impossible to restore initial iterator type; also iterator is almost - // useless outside the VM, thus let's replace it with a special structure. - st[i] = stackitem.NewInterop(Iterator{ - ID: &iID, - }) - } else { - iteratorValues := make([]stackitem.Item, len(iteratorAux.Value)) - for j := range iteratorValues { - iteratorValues[j], err = stackitem.FromJSONWithTypes(iteratorAux.Value[j]) - if err != nil { - err = fmt.Errorf("failed to unmarshal iterator values: %w", err) - break - } - } - // It's impossible to restore initial iterator type; also iterator is almost - // useless outside the VM, thus let's replace it with a special structure. - st[i] = stackitem.NewInterop(Iterator{ - Values: iteratorValues, - Truncated: iteratorAux.Truncated, - }) - } + var iter = Iterator{} + err = json.Unmarshal(arr[i], &iter) + if err != nil { + break } + st[i] = stackitem.NewInterop(iter) } } if err != nil {