github.com/theQRL/go-zond@v0.1.1/beacon/types/gen_header_json.go (about)

     1  // Code generated by github.com/fjl/gencodec. DO NOT EDIT.
     2  
     3  package types
     4  
     5  import (
     6  	"encoding/json"
     7  	"errors"
     8  
     9  	"github.com/theQRL/go-zond/common"
    10  )
    11  
    12  var _ = (*headerMarshaling)(nil)
    13  
    14  // MarshalJSON marshals as JSON.
    15  func (h Header) MarshalJSON() ([]byte, error) {
    16  	type Header struct {
    17  		Slot          common.Decimal `gencodec:"required" json:"slot"`
    18  		ProposerIndex common.Decimal `gencodec:"required" json:"proposer_index"`
    19  		ParentRoot    common.Hash    `gencodec:"required" json:"parent_root"`
    20  		StateRoot     common.Hash    `gencodec:"required" json:"state_root"`
    21  		BodyRoot      common.Hash    `gencodec:"required" json:"body_root"`
    22  	}
    23  	var enc Header
    24  	enc.Slot = common.Decimal(h.Slot)
    25  	enc.ProposerIndex = common.Decimal(h.ProposerIndex)
    26  	enc.ParentRoot = h.ParentRoot
    27  	enc.StateRoot = h.StateRoot
    28  	enc.BodyRoot = h.BodyRoot
    29  	return json.Marshal(&enc)
    30  }
    31  
    32  // UnmarshalJSON unmarshals from JSON.
    33  func (h *Header) UnmarshalJSON(input []byte) error {
    34  	type Header struct {
    35  		Slot          *common.Decimal `gencodec:"required" json:"slot"`
    36  		ProposerIndex *common.Decimal `gencodec:"required" json:"proposer_index"`
    37  		ParentRoot    *common.Hash    `gencodec:"required" json:"parent_root"`
    38  		StateRoot     *common.Hash    `gencodec:"required" json:"state_root"`
    39  		BodyRoot      *common.Hash    `gencodec:"required" json:"body_root"`
    40  	}
    41  	var dec Header
    42  	if err := json.Unmarshal(input, &dec); err != nil {
    43  		return err
    44  	}
    45  	if dec.Slot == nil {
    46  		return errors.New("missing required field 'slot' for Header")
    47  	}
    48  	h.Slot = uint64(*dec.Slot)
    49  	if dec.ProposerIndex == nil {
    50  		return errors.New("missing required field 'proposer_index' for Header")
    51  	}
    52  	h.ProposerIndex = uint64(*dec.ProposerIndex)
    53  	if dec.ParentRoot == nil {
    54  		return errors.New("missing required field 'parent_root' for Header")
    55  	}
    56  	h.ParentRoot = *dec.ParentRoot
    57  	if dec.StateRoot == nil {
    58  		return errors.New("missing required field 'state_root' for Header")
    59  	}
    60  	h.StateRoot = *dec.StateRoot
    61  	if dec.BodyRoot == nil {
    62  		return errors.New("missing required field 'body_root' for Header")
    63  	}
    64  	h.BodyRoot = *dec.BodyRoot
    65  	return nil
    66  }