github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/proto/encode_gogo.go (about)

     1  // Extensions for Protocol Buffers to create more go like structures.
     2  //
     3  // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
     4  // http://code.google.com/p/gogoprotobuf/gogoproto
     5  //
     6  // Go support for Protocol Buffers - Google's data interchange format
     7  //
     8  // Copyright 2010 The Go Authors.  All rights reserved.
     9  // http://code.google.com/p/goprotobuf/
    10  //
    11  // Redistribution and use in source and binary forms, with or without
    12  // modification, are permitted provided that the following conditions are
    13  // met:
    14  //
    15  //     * Redistributions of source code must retain the above copyright
    16  // notice, this list of conditions and the following disclaimer.
    17  //     * Redistributions in binary form must reproduce the above
    18  // copyright notice, this list of conditions and the following disclaimer
    19  // in the documentation and/or other materials provided with the
    20  // distribution.
    21  //     * Neither the name of Google Inc. nor the names of its
    22  // contributors may be used to endorse or promote products derived from
    23  // this software without specific prior written permission.
    24  //
    25  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    26  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    27  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    28  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    29  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    30  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    31  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    32  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    33  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    34  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    35  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    36  
    37  package proto
    38  
    39  import (
    40  	"reflect"
    41  )
    42  
    43  type Sizer interface {
    44  	Size() int
    45  }
    46  
    47  func (o *Buffer) enc_ext_slice_byte(p *Properties, base structPointer) error {
    48  	s := *structPointer_Bytes(base, p.field)
    49  	if s == nil {
    50  		return ErrNil
    51  	}
    52  	o.buf = append(o.buf, s...)
    53  	return nil
    54  }
    55  
    56  func size_ext_slice_byte(p *Properties, base structPointer) (n int) {
    57  	s := *structPointer_Bytes(base, p.field)
    58  	if s == nil {
    59  		return 0
    60  	}
    61  	n += len(s)
    62  	return
    63  }
    64  
    65  // Encode a reference to bool pointer.
    66  func (o *Buffer) enc_ref_bool(p *Properties, base structPointer) error {
    67  	v := structPointer_RefBool(base, p.field)
    68  	if v == nil {
    69  		return ErrNil
    70  	}
    71  	x := 0
    72  	if *v {
    73  		x = 1
    74  	}
    75  	o.buf = append(o.buf, p.tagcode...)
    76  	p.valEnc(o, uint64(x))
    77  	return nil
    78  }
    79  
    80  func size_ref_bool(p *Properties, base structPointer) int {
    81  	v := structPointer_RefBool(base, p.field)
    82  	if v == nil {
    83  		return 0
    84  	}
    85  	return len(p.tagcode) + 1 // each bool takes exactly one byte
    86  }
    87  
    88  // Encode a reference to int32 pointer.
    89  func (o *Buffer) enc_ref_int32(p *Properties, base structPointer) error {
    90  	v := structPointer_RefWord32(base, p.field)
    91  	if refWord32_IsNil(v) {
    92  		return ErrNil
    93  	}
    94  	x := refWord32_Get(v)
    95  	o.buf = append(o.buf, p.tagcode...)
    96  	p.valEnc(o, uint64(x))
    97  	return nil
    98  }
    99  
   100  func size_ref_int32(p *Properties, base structPointer) (n int) {
   101  	v := structPointer_RefWord32(base, p.field)
   102  	if refWord32_IsNil(v) {
   103  		return 0
   104  	}
   105  	x := refWord32_Get(v)
   106  	n += len(p.tagcode)
   107  	n += p.valSize(uint64(x))
   108  	return
   109  }
   110  
   111  // Encode a reference to an int64 pointer.
   112  func (o *Buffer) enc_ref_int64(p *Properties, base structPointer) error {
   113  	v := structPointer_RefWord64(base, p.field)
   114  	if refWord64_IsNil(v) {
   115  		return ErrNil
   116  	}
   117  	x := refWord64_Get(v)
   118  	o.buf = append(o.buf, p.tagcode...)
   119  	p.valEnc(o, x)
   120  	return nil
   121  }
   122  
   123  func size_ref_int64(p *Properties, base structPointer) (n int) {
   124  	v := structPointer_RefWord64(base, p.field)
   125  	if refWord64_IsNil(v) {
   126  		return 0
   127  	}
   128  	x := refWord64_Get(v)
   129  	n += len(p.tagcode)
   130  	n += p.valSize(x)
   131  	return
   132  }
   133  
   134  // Encode a reference to a string pointer.
   135  func (o *Buffer) enc_ref_string(p *Properties, base structPointer) error {
   136  	v := structPointer_RefString(base, p.field)
   137  	if v == nil {
   138  		return ErrNil
   139  	}
   140  	x := *v
   141  	o.buf = append(o.buf, p.tagcode...)
   142  	o.EncodeStringBytes(x)
   143  	return nil
   144  }
   145  
   146  func size_ref_string(p *Properties, base structPointer) (n int) {
   147  	v := structPointer_RefString(base, p.field)
   148  	if v == nil {
   149  		return 0
   150  	}
   151  	x := *v
   152  	n += len(p.tagcode)
   153  	n += sizeStringBytes(x)
   154  	return
   155  }
   156  
   157  // Encode a reference to a message struct.
   158  func (o *Buffer) enc_ref_struct_message(p *Properties, base structPointer) error {
   159  	var state errorState
   160  	structp := structPointer_GetRefStructPointer(base, p.field)
   161  	if structPointer_IsNil(structp) {
   162  		return ErrNil
   163  	}
   164  
   165  	// Can the object marshal itself?
   166  	if p.isMarshaler {
   167  		m := structPointer_Interface(structp, p.stype).(Marshaler)
   168  		data, err := m.Marshal()
   169  		if err != nil && !state.shouldContinue(err, nil) {
   170  			return err
   171  		}
   172  		o.buf = append(o.buf, p.tagcode...)
   173  		o.EncodeRawBytes(data)
   174  		return nil
   175  	}
   176  
   177  	o.buf = append(o.buf, p.tagcode...)
   178  	return o.enc_len_struct(p.stype, p.sprop, structp, &state)
   179  }
   180  
   181  //TODO this is only copied, please fix this
   182  func size_ref_struct_message(p *Properties, base structPointer) int {
   183  	structp := structPointer_GetRefStructPointer(base, p.field)
   184  	if structPointer_IsNil(structp) {
   185  		return 0
   186  	}
   187  
   188  	// Can the object marshal itself?
   189  	if p.isMarshaler {
   190  		m := structPointer_Interface(structp, p.stype).(Marshaler)
   191  		data, _ := m.Marshal()
   192  		n0 := len(p.tagcode)
   193  		n1 := sizeRawBytes(data)
   194  		return n0 + n1
   195  	}
   196  
   197  	n0 := len(p.tagcode)
   198  	n1 := size_struct(p.stype, p.sprop, structp)
   199  	n2 := sizeVarint(uint64(n1)) // size of encoded length
   200  	return n0 + n1 + n2
   201  }
   202  
   203  // Encode a slice of references to message struct pointers ([]struct).
   204  func (o *Buffer) enc_slice_ref_struct_message(p *Properties, base structPointer) error {
   205  	var state errorState
   206  	ss := structPointer_GetStructPointer(base, p.field)
   207  	ss1 := structPointer_GetRefStructPointer(ss, field(0))
   208  	size := p.stype.Size()
   209  	l := structPointer_Len(base, p.field)
   210  	for i := 0; i < l; i++ {
   211  		structp := structPointer_Add(ss1, field(uintptr(i)*size))
   212  		if structPointer_IsNil(structp) {
   213  			return ErrRepeatedHasNil
   214  		}
   215  
   216  		// Can the object marshal itself?
   217  		if p.isMarshaler {
   218  			m := structPointer_Interface(structp, p.stype).(Marshaler)
   219  			data, err := m.Marshal()
   220  			if err != nil && !state.shouldContinue(err, nil) {
   221  				return err
   222  			}
   223  			o.buf = append(o.buf, p.tagcode...)
   224  			o.EncodeRawBytes(data)
   225  			continue
   226  		}
   227  
   228  		o.buf = append(o.buf, p.tagcode...)
   229  		err := o.enc_len_struct(p.stype, p.sprop, structp, &state)
   230  		if err != nil && !state.shouldContinue(err, nil) {
   231  			if err == ErrNil {
   232  				return ErrRepeatedHasNil
   233  			}
   234  			return err
   235  		}
   236  
   237  	}
   238  	return state.err
   239  }
   240  
   241  //TODO this is only copied, please fix this
   242  func size_slice_ref_struct_message(p *Properties, base structPointer) (n int) {
   243  	ss := structPointer_GetStructPointer(base, p.field)
   244  	ss1 := structPointer_GetRefStructPointer(ss, field(0))
   245  	size := p.stype.Size()
   246  	l := structPointer_Len(base, p.field)
   247  	n += l * len(p.tagcode)
   248  	for i := 0; i < l; i++ {
   249  		structp := structPointer_Add(ss1, field(uintptr(i)*size))
   250  		if structPointer_IsNil(structp) {
   251  			return // return the size up to this point
   252  		}
   253  
   254  		// Can the object marshal itself?
   255  		if p.isMarshaler {
   256  			m := structPointer_Interface(structp, p.stype).(Marshaler)
   257  			data, _ := m.Marshal()
   258  			n += len(p.tagcode)
   259  			n += sizeRawBytes(data)
   260  			continue
   261  		}
   262  
   263  		n0 := size_struct(p.stype, p.sprop, structp)
   264  		n1 := sizeVarint(uint64(n0)) // size of encoded length
   265  		n += n0 + n1
   266  	}
   267  	return
   268  }
   269  
   270  func (o *Buffer) enc_custom_bytes(p *Properties, base structPointer) error {
   271  	i := structPointer_InterfaceRef(base, p.field, p.ctype)
   272  	if i == nil {
   273  		return ErrNil
   274  	}
   275  	custom := i.(Marshaler)
   276  	data, err := custom.Marshal()
   277  	if err != nil {
   278  		return err
   279  	}
   280  	if data == nil {
   281  		return ErrNil
   282  	}
   283  	o.buf = append(o.buf, p.tagcode...)
   284  	o.EncodeRawBytes(data)
   285  	return nil
   286  }
   287  
   288  func size_custom_bytes(p *Properties, base structPointer) (n int) {
   289  	n += len(p.tagcode)
   290  	i := structPointer_InterfaceRef(base, p.field, p.ctype)
   291  	if i == nil {
   292  		return 0
   293  	}
   294  	custom := i.(Marshaler)
   295  	data, _ := custom.Marshal()
   296  	n += sizeRawBytes(data)
   297  	return
   298  }
   299  
   300  func (o *Buffer) enc_custom_ref_bytes(p *Properties, base structPointer) error {
   301  	custom := structPointer_InterfaceAt(base, p.field, p.ctype).(Marshaler)
   302  	data, err := custom.Marshal()
   303  	if err != nil {
   304  		return err
   305  	}
   306  	if data == nil {
   307  		return ErrNil
   308  	}
   309  	o.buf = append(o.buf, p.tagcode...)
   310  	o.EncodeRawBytes(data)
   311  	return nil
   312  }
   313  
   314  func size_custom_ref_bytes(p *Properties, base structPointer) (n int) {
   315  	n += len(p.tagcode)
   316  	i := structPointer_InterfaceAt(base, p.field, p.ctype)
   317  	if i == nil {
   318  		return 0
   319  	}
   320  	custom := i.(Marshaler)
   321  	data, _ := custom.Marshal()
   322  	n += sizeRawBytes(data)
   323  	return
   324  }
   325  
   326  func (o *Buffer) enc_custom_slice_bytes(p *Properties, base structPointer) error {
   327  	inter := structPointer_InterfaceRef(base, p.field, p.ctype)
   328  	if inter == nil {
   329  		return ErrNil
   330  	}
   331  	slice := reflect.ValueOf(inter)
   332  	l := slice.Len()
   333  	for i := 0; i < l; i++ {
   334  		v := slice.Index(i)
   335  		custom := v.Interface().(Marshaler)
   336  		data, err := custom.Marshal()
   337  		if err != nil {
   338  			return err
   339  		}
   340  		o.buf = append(o.buf, p.tagcode...)
   341  		o.EncodeRawBytes(data)
   342  	}
   343  	return nil
   344  }
   345  
   346  func size_custom_slice_bytes(p *Properties, base structPointer) (n int) {
   347  	inter := structPointer_InterfaceRef(base, p.field, p.ctype)
   348  	if inter == nil {
   349  		return 0
   350  	}
   351  	slice := reflect.ValueOf(inter)
   352  	l := slice.Len()
   353  	n += l * len(p.tagcode)
   354  	for i := 0; i < l; i++ {
   355  		v := slice.Index(i)
   356  		custom := v.Interface().(Marshaler)
   357  		data, _ := custom.Marshal()
   358  		n += sizeRawBytes(data)
   359  	}
   360  	return
   361  }