81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package main
|
|
|
|
import "encoding/json"
|
|
import "github.com/joeshaw/iso8601"
|
|
|
|
// TODO(sneak)
|
|
//func (r *SteemOpInBlock) UnmarshalJSON() ([]byte, error) {
|
|
//}
|
|
|
|
type OperationObject struct {
|
|
TransactionID string `json:"trx_id"`
|
|
BlockNumber uint64 `json:"block"`
|
|
TransactionInBlock uint64 `json:"trx_in_block"`
|
|
OpInTx int `json:"op_in_trx"`
|
|
VirtualOperation int `json:"virtual_op"`
|
|
Timestamp iso8601.Time `json:"timestamp"`
|
|
Operation []json.RawMessage `json:"op"`
|
|
}
|
|
|
|
/*
|
|
type operationTuple struct {
|
|
Type OpType
|
|
Data Operation
|
|
}
|
|
|
|
func (op *operationTuple) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal([]interface{}{
|
|
op.Type,
|
|
op.Data,
|
|
})
|
|
}
|
|
|
|
func (op *operationTuple) UnmarshalJSON(data []byte) error {
|
|
// The operation object is [opType, opBody].
|
|
raw := make([]*json.RawMessage, 2)
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
return errors.Wrapf(err, "failed to unmarshal operation object: %v", string(data))
|
|
}
|
|
if len(raw) != 2 {
|
|
return errors.Errorf("invalid operation object: %v", string(data))
|
|
}
|
|
|
|
// Unmarshal the type.
|
|
var opType OpType
|
|
if err := json.Unmarshal(*raw[0], &opType); err != nil {
|
|
return errors.Wrapf(err, "failed to unmarshal Operation.Type: %v", string(*raw[0]))
|
|
}
|
|
|
|
// Unmarshal the data.
|
|
var opData Operation
|
|
template, ok := dataObjects[opType]
|
|
if ok {
|
|
opData = reflect.New(
|
|
reflect.Indirect(reflect.ValueOf(template)).Type(),
|
|
).Interface().(Operation)
|
|
|
|
if err := json.Unmarshal(*raw[1], opData); err != nil {
|
|
return errors.Wrapf(err, "failed to unmarshal Operation.Data: %v", string(*raw[1]))
|
|
}
|
|
} else {
|
|
opData = &UnknownOperation{opType, raw[1]}
|
|
}
|
|
|
|
// Update fields.
|
|
op.Type = opType
|
|
op.Data = opData
|
|
return nil
|
|
}
|
|
*/
|
|
type GetOpsInBlockRequestParams struct {
|
|
BlockNum int
|
|
VirtualOps bool
|
|
}
|
|
|
|
type GetOpsInBlockResponse []OperationObject
|
|
|
|
func (r *GetOpsInBlockRequestParams) MarshalJSON() ([]byte, error) {
|
|
arr := []interface{}{r.BlockNum, r.VirtualOps}
|
|
return json.Marshal(arr)
|
|
}
|