github.com/matm/etcd@v0.3.1-0.20140328024009-5b4a473f1453/third_party/code.google.com/p/gogoprotobuf/proto/properties_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  	"fmt"
    31  	"os"
    32  	"reflect"
    33  )
    34  
    35  func (p *Properties) setCustomEncAndDec(typ reflect.Type) {
    36  	p.ctype = typ
    37  	if p.Repeated {
    38  		p.enc = (*Buffer).enc_custom_slice_bytes
    39  		p.dec = (*Buffer).dec_custom_slice_bytes
    40  		p.size = size_custom_slice_bytes
    41  	} else if typ.Kind() == reflect.Ptr {
    42  		p.enc = (*Buffer).enc_custom_bytes
    43  		p.dec = (*Buffer).dec_custom_bytes
    44  		p.size = size_custom_bytes
    45  	} else {
    46  		p.enc = (*Buffer).enc_custom_ref_bytes
    47  		p.dec = (*Buffer).dec_custom_ref_bytes
    48  		p.size = size_custom_ref_bytes
    49  	}
    50  }
    51  
    52  func (p *Properties) setNonNullableEncAndDec(typ reflect.Type) bool {
    53  	switch typ.Kind() {
    54  	case reflect.Bool:
    55  		p.enc = (*Buffer).enc_ref_bool
    56  		p.dec = (*Buffer).dec_ref_bool
    57  		p.size = size_ref_bool
    58  	case reflect.Int32, reflect.Uint32:
    59  		p.enc = (*Buffer).enc_ref_int32
    60  		p.dec = (*Buffer).dec_ref_int32
    61  		p.size = size_ref_int32
    62  	case reflect.Int64, reflect.Uint64:
    63  		p.enc = (*Buffer).enc_ref_int64
    64  		p.dec = (*Buffer).dec_ref_int64
    65  		p.size = size_ref_int64
    66  	case reflect.Float32:
    67  		p.enc = (*Buffer).enc_ref_int32 // can just treat them as bits
    68  		p.dec = (*Buffer).dec_ref_int32
    69  		p.size = size_ref_int32
    70  	case reflect.Float64:
    71  		p.enc = (*Buffer).enc_ref_int64 // can just treat them as bits
    72  		p.dec = (*Buffer).dec_ref_int64
    73  		p.size = size_ref_int64
    74  	case reflect.String:
    75  		p.dec = (*Buffer).dec_ref_string
    76  		p.enc = (*Buffer).enc_ref_string
    77  		p.size = size_ref_string
    78  	case reflect.Struct:
    79  		p.stype = typ
    80  		p.isMarshaler = isMarshaler(typ)
    81  		p.isUnmarshaler = isUnmarshaler(typ)
    82  		if p.Wire == "bytes" {
    83  			p.enc = (*Buffer).enc_ref_struct_message
    84  			p.dec = (*Buffer).dec_ref_struct_message
    85  			p.size = size_ref_struct_message
    86  		} else {
    87  			fmt.Fprintf(os.Stderr, "proto: no coders for struct %T\n", typ)
    88  		}
    89  	default:
    90  		return false
    91  	}
    92  	return true
    93  }
    94  
    95  func (p *Properties) setSliceOfNonPointerStructs(typ reflect.Type) {
    96  	t2 := typ.Elem()
    97  	p.sstype = typ
    98  	p.stype = t2
    99  	p.isMarshaler = isMarshaler(t2)
   100  	p.isUnmarshaler = isUnmarshaler(t2)
   101  	p.enc = (*Buffer).enc_slice_ref_struct_message
   102  	p.dec = (*Buffer).dec_slice_ref_struct_message
   103  	p.size = size_slice_ref_struct_message
   104  	if p.Wire != "bytes" {
   105  		fmt.Fprintf(os.Stderr, "proto: no ptr oenc for %T -> %T \n", typ, t2)
   106  	}
   107  }