github.com/matm/etcd@v0.3.1-0.20140328024009-5b4a473f1453/third_party/code.google.com/p/gogoprotobuf/proto/decode_gogo.go (about)

     1  // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
     2  // http://code.google.com/p/gogoprotobuf/gogoproto
     3  //
     4  // Redistribution and use in source and binary forms, with or without
     5  // modification, are permitted provided that the following conditions are
     6  // met:
     7  //
     8  //     * Redistributions of source code must retain the above copyright
     9  // notice, this list of conditions and the following disclaimer.
    10  //     * Redistributions in binary form must reproduce the above
    11  // copyright notice, this list of conditions and the following disclaimer
    12  // in the documentation and/or other materials provided with the
    13  // distribution.
    14  //
    15  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    16  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    17  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    18  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    19  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    20  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    21  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    22  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    23  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    24  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    25  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    26  
    27  package proto
    28  
    29  import (
    30  	"reflect"
    31  )
    32  
    33  // Decode a reference to a bool pointer.
    34  func (o *Buffer) dec_ref_bool(p *Properties, base structPointer) error {
    35  	u, err := p.valDec(o)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	if len(o.bools) == 0 {
    40  		o.bools = make([]bool, boolPoolSize)
    41  	}
    42  	o.bools[0] = u != 0
    43  	*structPointer_RefBool(base, p.field) = o.bools[0]
    44  	o.bools = o.bools[1:]
    45  	return nil
    46  }
    47  
    48  // Decode a reference to an int32 pointer.
    49  func (o *Buffer) dec_ref_int32(p *Properties, base structPointer) error {
    50  	u, err := p.valDec(o)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	refWord32_Set(structPointer_RefWord32(base, p.field), o, uint32(u))
    55  	return nil
    56  }
    57  
    58  // Decode a reference to an int64 pointer.
    59  func (o *Buffer) dec_ref_int64(p *Properties, base structPointer) error {
    60  	u, err := p.valDec(o)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	refWord64_Set(structPointer_RefWord64(base, p.field), o, u)
    65  	return nil
    66  }
    67  
    68  // Decode a reference to a string pointer.
    69  func (o *Buffer) dec_ref_string(p *Properties, base structPointer) error {
    70  	s, err := o.DecodeStringBytes()
    71  	if err != nil {
    72  		return err
    73  	}
    74  	*structPointer_RefString(base, p.field) = s
    75  	return nil
    76  }
    77  
    78  // Decode a reference to a struct pointer.
    79  func (o *Buffer) dec_ref_struct_message(p *Properties, base structPointer) (err error) {
    80  	raw, e := o.DecodeRawBytes(false)
    81  	if e != nil {
    82  		return e
    83  	}
    84  
    85  	// If the object can unmarshal itself, let it.
    86  	if p.isUnmarshaler {
    87  		panic("not supported, since this is a pointer receiver")
    88  	}
    89  
    90  	obuf := o.buf
    91  	oi := o.index
    92  	o.buf = raw
    93  	o.index = 0
    94  
    95  	bas := structPointer_FieldPointer(base, p.field)
    96  
    97  	err = o.unmarshalType(p.stype, p.sprop, false, bas)
    98  	o.buf = obuf
    99  	o.index = oi
   100  
   101  	return err
   102  }
   103  
   104  // Decode a slice of references to struct pointers ([]struct).
   105  func (o *Buffer) dec_slice_ref_struct(p *Properties, is_group bool, base structPointer) error {
   106  	newBas := appendStructPointer(base, p.field, p.sstype)
   107  
   108  	if is_group {
   109  		panic("not supported, maybe in future, if requested.")
   110  	}
   111  
   112  	raw, err := o.DecodeRawBytes(false)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	// If the object can unmarshal itself, let it.
   118  	if p.isUnmarshaler {
   119  		panic("not supported, since this is not a pointer receiver.")
   120  	}
   121  
   122  	obuf := o.buf
   123  	oi := o.index
   124  	o.buf = raw
   125  	o.index = 0
   126  
   127  	err = o.unmarshalType(p.stype, p.sprop, is_group, newBas)
   128  
   129  	o.buf = obuf
   130  	o.index = oi
   131  
   132  	return err
   133  }
   134  
   135  // Decode a slice of references to struct pointers.
   136  func (o *Buffer) dec_slice_ref_struct_message(p *Properties, base structPointer) error {
   137  	return o.dec_slice_ref_struct(p, false, base)
   138  }
   139  
   140  func setPtrCustomType(base structPointer, f field, v interface{}) {
   141  	if v == nil {
   142  		return
   143  	}
   144  	structPointer_SetStructPointer(base, f, structPointer(reflect.ValueOf(v).Pointer()))
   145  }
   146  
   147  func setCustomType(base structPointer, f field, value interface{}) {
   148  	if value == nil {
   149  		return
   150  	}
   151  	v := reflect.ValueOf(value).Elem()
   152  	t := reflect.TypeOf(value).Elem()
   153  	kind := t.Kind()
   154  	switch kind {
   155  	case reflect.Slice:
   156  		slice := reflect.MakeSlice(t, v.Len(), v.Cap())
   157  		reflect.Copy(slice, v)
   158  		oldHeader := structPointer_GetSliceHeader(base, f)
   159  		oldHeader.Data = slice.Pointer()
   160  		oldHeader.Len = v.Len()
   161  		oldHeader.Cap = v.Cap()
   162  	default:
   163  		l := 1
   164  		size := reflect.TypeOf(value).Elem().Size()
   165  		if kind == reflect.Array {
   166  			l = reflect.TypeOf(value).Elem().Len()
   167  			size = reflect.TypeOf(value).Size()
   168  		}
   169  		total := int(size) * l
   170  		structPointer_Copy(toStructPointer(reflect.ValueOf(value)), structPointer_Add(base, f), total)
   171  	}
   172  }
   173  
   174  func (o *Buffer) dec_custom_bytes(p *Properties, base structPointer) error {
   175  	b, err := o.DecodeRawBytes(true)
   176  	if err != nil {
   177  		return err
   178  	}
   179  	i := reflect.New(p.ctype.Elem()).Interface()
   180  	custom := (i).(Unmarshaler)
   181  	if err := custom.Unmarshal(b); err != nil {
   182  		return err
   183  	}
   184  	setPtrCustomType(base, p.field, custom)
   185  	return nil
   186  }
   187  
   188  func (o *Buffer) dec_custom_ref_bytes(p *Properties, base structPointer) error {
   189  	b, err := o.DecodeRawBytes(true)
   190  	if err != nil {
   191  		return err
   192  	}
   193  	i := reflect.New(p.ctype).Interface()
   194  	custom := (i).(Unmarshaler)
   195  	if err := custom.Unmarshal(b); err != nil {
   196  		return err
   197  	}
   198  	if custom != nil {
   199  		setCustomType(base, p.field, custom)
   200  	}
   201  	return nil
   202  }
   203  
   204  // Decode a slice of bytes ([]byte) into a slice of custom types.
   205  func (o *Buffer) dec_custom_slice_bytes(p *Properties, base structPointer) error {
   206  	b, err := o.DecodeRawBytes(true)
   207  	if err != nil {
   208  		return err
   209  	}
   210  	i := reflect.New(p.ctype.Elem()).Interface()
   211  	custom := (i).(Unmarshaler)
   212  	if err := custom.Unmarshal(b); err != nil {
   213  		return err
   214  	}
   215  	newBas := appendStructPointer(base, p.field, p.ctype)
   216  
   217  	setCustomType(newBas, 0, custom)
   218  
   219  	return nil
   220  }