Commit 30fdff6f authored by Joshua Gutow's avatar Joshua Gutow

op-node/derivation: JSON Encoding for Frames

parent 1786f370
...@@ -24,10 +24,10 @@ const MaxFrameLen = 1_000_000 ...@@ -24,10 +24,10 @@ const MaxFrameLen = 1_000_000
// is_last = bool // is_last = bool
type Frame struct { type Frame struct {
ID ChannelID ID ChannelID `json:"id"`
FrameNumber uint16 FrameNumber uint16 `json:"frame_number"`
Data []byte Data []byte `json:"data"`
IsLast bool IsLast bool `'json:"is_last"`
} }
// MarshalBinary writes the frame to `w`. // MarshalBinary writes the frame to `w`.
......
package derive package derive
import ( import (
"encoding/json" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
) )
...@@ -46,6 +46,18 @@ func (id ChannelID) TerminalString() string { ...@@ -46,6 +46,18 @@ func (id ChannelID) TerminalString() string {
return fmt.Sprintf("%x..%x", id[:3], id[13:]) return fmt.Sprintf("%x..%x", id[:3], id[13:])
} }
func (id ChannelID) MarshalJSON() ([]byte, error) { func (id ChannelID) MarshalText() ([]byte, error) {
return json.Marshal(id.String()) return []byte(id.String()), nil
}
func (id *ChannelID) UnmarshalText(text []byte) error {
h, err := hex.DecodeString(string(text))
if err != nil {
return err
}
if len(h) != ChannelIDLength {
return errors.New("invalid length")
}
copy(id[:], h)
return nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment