neoneo-go/pkg/rpc/response/result/subscriptions/notification_event.go
Elizaveta Chichindaeva 28908aa3cf [#2442] English Check
Signed-off-by: Elizaveta Chichindaeva <elizaveta@nspcc.ru>
2022-05-04 19:48:27 +03:00

56 lines
1.5 KiB
Go

package subscriptions
import (
"encoding/json"
"errors"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// NotificationEvent represents a wrapper for a notification from script execution.
type NotificationEvent struct {
// Container hash is the hash of script container which is either a block or a transaction.
Container util.Uint256
state.NotificationEvent
}
// notificationEventAux is an auxiliary struct for JSON marshalling.
type notificationEventAux struct {
Container util.Uint256 `json:"container"`
}
// MarshalJSON implements the json.Marshaler interface.
func (ne *NotificationEvent) MarshalJSON() ([]byte, error) {
h, err := json.Marshal(&notificationEventAux{
Container: ne.Container,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal hash: %w", err)
}
exec, err := json.Marshal(ne.NotificationEvent)
if err != nil {
return nil, fmt.Errorf("failed to marshal execution: %w", err)
}
if h[len(h)-1] != '}' || exec[0] != '{' {
return nil, errors.New("can't merge internal jsons")
}
h[len(h)-1] = ','
h = append(h, exec[1:]...)
return h, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (ne *NotificationEvent) UnmarshalJSON(data []byte) error {
aux := new(notificationEventAux)
if err := json.Unmarshal(data, aux); err != nil {
return err
}
if err := json.Unmarshal(data, &ne.NotificationEvent); err != nil {
return err
}
ne.Container = aux.Container
return nil
}