1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2011 Google Inc. All rights reserved.
4 // http://code.google.com/p/goprotobuf/
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // Protocol buffer deep copy.
33 // TODO: MessageSet and RawMessage.
43 // Clone returns a deep copy of a protocol buffer.
44 // pb must be a pointer to a protocol buffer struct.
45 func Clone(pb interface{}) interface{} {
46 in := reflect.ValueOf(pb)
47 if in.Kind() != reflect.Ptr || in.Elem().Kind() != reflect.Struct {
51 out := reflect.New(in.Type().Elem())
52 copyStruct(out.Elem(), in.Elem())
53 return out.Interface()
56 func copyStruct(out, in reflect.Value) {
57 for i := 0; i < in.NumField(); i++ {
58 f := in.Type().Field(i)
59 if strings.HasPrefix(f.Name, "XXX_") {
62 copyAny(out.Field(i), in.Field(i))
65 if emIn, ok := in.Addr().Interface().(extendableProto); ok {
66 emOut := out.Addr().Interface().(extendableProto)
67 copyExtension(emOut.ExtensionMap(), emIn.ExtensionMap())
70 // TODO: Deal with XXX_unrecognized.
73 func copyAny(out, in reflect.Value) {
75 case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64,
76 reflect.String, reflect.Uint32, reflect.Uint64:
82 out.Set(reflect.New(in.Type().Elem()))
83 copyAny(out.Elem(), in.Elem())
89 out.Set(reflect.MakeSlice(in.Type(), n, n))
90 switch in.Type().Elem().Kind() {
91 case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64,
92 reflect.String, reflect.Uint32, reflect.Uint64, reflect.Uint8:
95 for i := 0; i < n; i++ {
96 copyAny(out.Index(i), in.Index(i))
102 // unknown type, so not a protocol buffer
103 log.Printf("proto: don't know how to copy %v", in)
107 func copyExtension(out, in map[int32]Extension) {
108 for extNum, eIn := range in {
109 eOut := Extension{desc: eIn.desc}
110 if eIn.value != nil {
111 v := reflect.New(reflect.TypeOf(eIn.value)).Elem()
112 copyAny(v, reflect.ValueOf(eIn.value))
113 eOut.value = v.Interface()
116 eOut.enc = make([]byte, len(eIn.enc))
117 copy(eOut.enc, eIn.enc)