github.com/annchain/OG@v0.0.9/trie/node_gen.go (about)

     1  package trie
     2  
     3  // Code generated by github.com/tinylib/msgp DO NOT EDIT.
     4  
     5  import (
     6  	"fmt"
     7  	"strconv"
     8  
     9  	"github.com/tinylib/msgp/msgp"
    10  )
    11  
    12  var nodeTypeSize = 1
    13  
    14  // DecodeMsg implements msgp.Decodable
    15  func (z *FullNode) DecodeMsg(dc *msgp.Reader) (err error) {
    16  	var zb0001 uint32
    17  	zb0001, err = dc.ReadArrayHeader()
    18  	if err != nil {
    19  		return
    20  	}
    21  	if zb0001 != 1 {
    22  		err = msgp.ArrayError{Wanted: 1, Got: zb0001}
    23  		return
    24  	}
    25  	var zb0002 uint32
    26  	zb0002, err = dc.ReadArrayHeader()
    27  	if err != nil {
    28  		return
    29  	}
    30  	if zb0002 != uint32(17) {
    31  		err = msgp.ArrayError{Wanted: uint32(17), Got: zb0002}
    32  		return
    33  	}
    34  	for za0001 := range z.Children {
    35  		err = z.Children[za0001].DecodeMsg(dc)
    36  		if err != nil {
    37  			return
    38  		}
    39  	}
    40  	return
    41  }
    42  
    43  // EncodeMsg implements msgp.Encodable
    44  func (z *FullNode) EncodeMsg(en *msgp.Writer) (err error) {
    45  	// array header, size 1
    46  	err = en.Append(0x91)
    47  	if err != nil {
    48  		return
    49  	}
    50  	err = en.WriteArrayHeader(uint32(17))
    51  	if err != nil {
    52  		return
    53  	}
    54  	for za0001 := range z.Children {
    55  		err = z.Children[za0001].EncodeMsg(en)
    56  		if err != nil {
    57  			return
    58  		}
    59  	}
    60  	return
    61  }
    62  
    63  // MarshalMsg implements msgp.Marshaler
    64  func (z *FullNode) MarshalMsg(b []byte) (o []byte, err error) {
    65  	o = msgp.Require(b, z.Msgsize())
    66  	// array header, size 1
    67  	o = append(o, 0x91)
    68  	o = msgp.AppendArrayHeader(o, uint32(17))
    69  	for za0001 := range z.Children {
    70  		o, err = detectNodeType(z.Children[za0001], o)
    71  		if err != nil {
    72  			return
    73  		}
    74  		if z.Children[za0001] == nil {
    75  			continue
    76  		}
    77  		o, err = z.Children[za0001].MarshalMsg(o)
    78  		if err != nil {
    79  			return
    80  		}
    81  	}
    82  	return
    83  }
    84  
    85  // UnmarshalMsg implements msgp.Unmarshaler
    86  func (z *FullNode) UnmarshalMsg(bts []byte) (o []byte, err error) {
    87  	var zb0001 uint32
    88  	zb0001, bts, err = msgp.ReadArrayHeaderBytes(bts)
    89  	if err != nil {
    90  		return
    91  	}
    92  	if zb0001 != 1 {
    93  		err = msgp.ArrayError{Wanted: 1, Got: zb0001}
    94  		return
    95  	}
    96  	var zb0002 uint32
    97  	zb0002, bts, err = msgp.ReadArrayHeaderBytes(bts)
    98  	if err != nil {
    99  		return
   100  	}
   101  	if zb0002 != uint32(17) {
   102  		err = msgp.ArrayError{Wanted: uint32(17), Got: zb0002}
   103  		return
   104  	}
   105  	for za0001 := range z.Children {
   106  		bts, err = z.GenNode(bts, za0001)
   107  		if err != nil {
   108  			return
   109  		}
   110  	}
   111  	o = bts
   112  	return
   113  }
   114  
   115  // GenNode checks the type of z.Val and initiate an object entity for it.
   116  func (z *FullNode) GenNode(b []byte, i int) (o []byte, err error) {
   117  	t, errc := strconv.Atoi(string(b[0]))
   118  	if errc != nil {
   119  		err = fmt.Errorf("convert byte to int error: %v", errc)
   120  		return
   121  	}
   122  	switch nodetype(t) {
   123  	case nilnode:
   124  		z.Children[i] = nil
   125  		o = b[1:]
   126  	case fullnode:
   127  		n := &FullNode{}
   128  		o, err = n.UnmarshalMsg(b[1:])
   129  		z.Children[i] = n
   130  	case shortnode:
   131  		n := &ShortNode{}
   132  		o, err = n.UnmarshalMsg(b[1:])
   133  		z.Children[i] = n
   134  	case valuenode:
   135  		n := ValueNode{}
   136  		o, err = n.UnmarshalMsg(b[1:])
   137  		z.Children[i] = n
   138  	case hashnode:
   139  		n := HashNode{}
   140  		o, err = n.UnmarshalMsg(b[1:])
   141  		z.Children[i] = n
   142  	default:
   143  		err = fmt.Errorf("unknown nodetype: %v", nodetype(t))
   144  	}
   145  	return
   146  }
   147  
   148  // Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
   149  func (z *FullNode) Msgsize() (s int) {
   150  	s = 1 + msgp.ArrayHeaderSize
   151  	for za0001 := range z.Children {
   152  		s += nodeTypeSize
   153  		if z.Children[za0001] == nil {
   154  			continue
   155  		}
   156  		s += z.Children[za0001].Msgsize()
   157  	}
   158  	return
   159  }
   160  
   161  // DecodeMsg implements msgp.Decodable
   162  func (z HashNode) DecodeMsg(dc *msgp.Reader) (err error) {
   163  	{
   164  		var zb0001 []byte
   165  		zb0001, err = dc.ReadBytes([]byte((z)))
   166  		if err != nil {
   167  			return
   168  		}
   169  		(z) = HashNode(zb0001)
   170  	}
   171  	return
   172  }
   173  
   174  // EncodeMsg implements msgp.Encodable
   175  func (z HashNode) EncodeMsg(en *msgp.Writer) (err error) {
   176  	err = en.WriteBytes([]byte(z))
   177  	if err != nil {
   178  		return
   179  	}
   180  	return
   181  }
   182  
   183  // MarshalMsg implements msgp.Marshaler
   184  func (z HashNode) MarshalMsg(b []byte) (o []byte, err error) {
   185  	o = msgp.Require(b, z.Msgsize())
   186  	o = msgp.AppendBytes(o, []byte(z))
   187  	return
   188  }
   189  
   190  // UnmarshalMsg implements msgp.Unmarshaler
   191  func (z *HashNode) UnmarshalMsg(bts []byte) (o []byte, err error) {
   192  	{
   193  		var zb0001 []byte
   194  		zb0001, bts, err = msgp.ReadBytesBytes(bts, []byte((*z)))
   195  		if err != nil {
   196  			return
   197  		}
   198  		(*z) = HashNode(zb0001)
   199  	}
   200  	o = bts
   201  	return
   202  }
   203  
   204  // Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
   205  func (z HashNode) Msgsize() (s int) {
   206  	s = msgp.BytesPrefixSize + len([]byte(z))
   207  	return
   208  }
   209  
   210  // DecodeMsg implements msgp.Decodable
   211  func (z *ShortNode) DecodeMsg(dc *msgp.Reader) (err error) {
   212  	var zb0001 uint32
   213  	zb0001, err = dc.ReadArrayHeader()
   214  	if err != nil {
   215  		return
   216  	}
   217  	if zb0001 != 2 {
   218  		err = msgp.ArrayError{Wanted: 2, Got: zb0001}
   219  		return
   220  	}
   221  	z.Key, err = dc.ReadBytes(z.Key)
   222  	if err != nil {
   223  		return
   224  	}
   225  	err = z.Val.DecodeMsg(dc)
   226  	if err != nil {
   227  		return
   228  	}
   229  	return
   230  }
   231  
   232  // EncodeMsg implements msgp.Encodable
   233  func (z *ShortNode) EncodeMsg(en *msgp.Writer) (err error) {
   234  	// array header, size 2
   235  	err = en.Append(0x92)
   236  	if err != nil {
   237  		return
   238  	}
   239  	err = en.WriteBytes(z.Key)
   240  	if err != nil {
   241  		return
   242  	}
   243  	err = z.Val.EncodeMsg(en)
   244  	if err != nil {
   245  		return
   246  	}
   247  	return
   248  }
   249  
   250  // MarshalMsg implements msgp.Marshaler
   251  func (z *ShortNode) MarshalMsg(b []byte) (o []byte, err error) {
   252  	o = msgp.Require(b, z.Msgsize())
   253  	// array header, size 2
   254  	o = append(o, 0x92)
   255  	o = msgp.AppendBytes(o, z.Key)
   256  	o, err = detectNodeType(z.Val, o)
   257  	if err != nil {
   258  		return
   259  	}
   260  	if z.Val == nil {
   261  		return
   262  	}
   263  	o, err = z.Val.MarshalMsg(o)
   264  	if err != nil {
   265  		return
   266  	}
   267  	return
   268  }
   269  
   270  // UnmarshalMsg implements msgp.Unmarshaler
   271  func (z *ShortNode) UnmarshalMsg(bts []byte) (o []byte, err error) {
   272  	var zb0001 uint32
   273  	zb0001, bts, err = msgp.ReadArrayHeaderBytes(bts)
   274  	if err != nil {
   275  		return
   276  	}
   277  	if zb0001 != 2 {
   278  		err = msgp.ArrayError{Wanted: 2, Got: zb0001}
   279  		return
   280  	}
   281  	z.Key, bts, err = msgp.ReadBytesBytes(bts, z.Key)
   282  	if err != nil {
   283  		return
   284  	}
   285  	// initiate z.Val
   286  	bts, err = z.GenNode(bts)
   287  	if err != nil {
   288  		return
   289  	}
   290  	o = bts
   291  	return
   292  }
   293  
   294  // Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
   295  func (z *ShortNode) Msgsize() (s int) {
   296  	s = 1 + msgp.BytesPrefixSize + len(z.Key) + nodeTypeSize
   297  	if z.Val != nil {
   298  		s += z.Val.Msgsize()
   299  	}
   300  	return
   301  }
   302  
   303  // GenNode checks the type of z.Val and initiate an object entity for it.
   304  func (z *ShortNode) GenNode(b []byte) (o []byte, err error) {
   305  	t, errc := strconv.Atoi(string(b[0]))
   306  	if errc != nil {
   307  		err = fmt.Errorf("convert byte to int error: %v", errc)
   308  		return
   309  	}
   310  	switch nodetype(t) {
   311  	case nilnode:
   312  		z.Val = nil
   313  	case fullnode:
   314  		n := &FullNode{}
   315  		o, err = n.UnmarshalMsg(b[1:])
   316  		z.Val = n
   317  	case shortnode:
   318  		n := &ShortNode{}
   319  		o, err = n.UnmarshalMsg(b[1:])
   320  		z.Val = n
   321  	case valuenode:
   322  		n := ValueNode{}
   323  		o, err = n.UnmarshalMsg(b[1:])
   324  		z.Val = n
   325  	case hashnode:
   326  		n := HashNode{}
   327  		o, err = n.UnmarshalMsg(b[1:])
   328  		z.Val = n
   329  	default:
   330  		err = fmt.Errorf("unknown nodetype: %v", nodetype(t))
   331  	}
   332  	return
   333  }
   334  
   335  // DecodeMsg implements msgp.Decodable
   336  func (z ValueNode) DecodeMsg(dc *msgp.Reader) (err error) {
   337  	{
   338  		var zb0001 []byte
   339  		zb0001, err = dc.ReadBytes([]byte((z)))
   340  		if err != nil {
   341  			return
   342  		}
   343  		(z) = ValueNode(zb0001)
   344  	}
   345  	return
   346  }
   347  
   348  // EncodeMsg implements msgp.Encodable
   349  func (z ValueNode) EncodeMsg(en *msgp.Writer) (err error) {
   350  	err = en.WriteBytes([]byte(z))
   351  	if err != nil {
   352  		return
   353  	}
   354  	return
   355  }
   356  
   357  // MarshalMsg implements msgp.Marshaler
   358  func (z ValueNode) MarshalMsg(b []byte) (o []byte, err error) {
   359  	o = msgp.Require(b, z.Msgsize())
   360  	o = msgp.AppendBytes(o, []byte(z))
   361  	return
   362  }
   363  
   364  // UnmarshalMsg implements msgp.Unmarshaler
   365  func (z *ValueNode) UnmarshalMsg(bts []byte) (o []byte, err error) {
   366  	{
   367  		var zb0001 []byte
   368  		zb0001, bts, err = msgp.ReadBytesBytes(bts, []byte((*z)))
   369  		if err != nil {
   370  			return
   371  		}
   372  		(*z) = ValueNode(zb0001)
   373  	}
   374  	o = bts
   375  	return
   376  }
   377  
   378  // Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
   379  func (z ValueNode) Msgsize() (s int) {
   380  	s = msgp.BytesPrefixSize + len([]byte(z))
   381  	return
   382  }
   383  
   384  // convert nodetype to byte
   385  func nodetypebyte(t nodetype) byte {
   386  	return ([]byte(strconv.Itoa(int(t))))[0]
   387  }
   388  
   389  func detectNodeType(n Node, b []byte) (o []byte, err error) {
   390  	switch n.(type) {
   391  	case nil:
   392  		o = append(b, nodetypebyte(nilnode))
   393  		return
   394  	case *FullNode:
   395  		o = append(b, nodetypebyte(fullnode))
   396  		return
   397  	case *ShortNode:
   398  		o = append(b, nodetypebyte(shortnode))
   399  		return
   400  	case HashNode:
   401  		o = append(b, nodetypebyte(hashnode))
   402  		return
   403  	case ValueNode:
   404  		o = append(b, nodetypebyte(valuenode))
   405  		return
   406  	default:
   407  		err = fmt.Errorf("unknown nodetype: %v", n)
   408  	}
   409  	return
   410  }