github.com/defanghe/fabric@v2.1.1+incompatible/common/tools/protolator/nested.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package protolator 18 19 import ( 20 "reflect" 21 22 "github.com/golang/protobuf/proto" 23 "github.com/golang/protobuf/ptypes/timestamp" 24 ) 25 26 func nestedFrom(value interface{}, destType reflect.Type) (reflect.Value, error) { 27 tree := value.(map[string]interface{}) // Safe, already checked 28 result := reflect.New(destType.Elem()) 29 nMsg := result.Interface().(proto.Message) // Safe, already checked 30 if err := recursivelyPopulateMessageFromTree(tree, nMsg); err != nil { 31 return reflect.Value{}, err 32 } 33 return result, nil 34 } 35 36 func nestedTo(value reflect.Value) (interface{}, error) { 37 nMsg := value.Interface().(proto.Message) // Safe, already checked 38 return recursivelyCreateTreeFromMessage(nMsg) 39 } 40 41 var timestampType = reflect.TypeOf(×tamp.Timestamp{}) 42 43 type nestedFieldFactory struct{} 44 45 func (nff nestedFieldFactory) Handles(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) bool { 46 // Note, we skip recursing into the field if it is a proto native timestamp, because there is other custom marshaling this conflicts with 47 // this should probably be revisited more generally to prevent custom marshaling of 'well known messages' 48 return fieldType.Kind() == reflect.Ptr && fieldType.AssignableTo(protoMsgType) && !fieldType.AssignableTo(timestampType) 49 } 50 51 func (nff nestedFieldFactory) NewProtoField(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) (protoField, error) { 52 return &plainField{ 53 baseField: baseField{ 54 msg: msg, 55 name: fieldName, 56 fType: mapStringInterfaceType, 57 vType: fieldType, 58 value: fieldValue, 59 }, 60 populateFrom: nestedFrom, 61 populateTo: nestedTo, 62 }, nil 63 } 64 65 type nestedMapFieldFactory struct{} 66 67 func (nmff nestedMapFieldFactory) Handles(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) bool { 68 return fieldType.Kind() == reflect.Map && fieldType.Elem().AssignableTo(protoMsgType) && !fieldType.Elem().AssignableTo(timestampType) && fieldType.Key().Kind() == reflect.String 69 } 70 71 func (nmff nestedMapFieldFactory) NewProtoField(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) (protoField, error) { 72 return &mapField{ 73 baseField: baseField{ 74 msg: msg, 75 name: fieldName, 76 fType: mapStringInterfaceType, 77 vType: fieldType, 78 value: fieldValue, 79 }, 80 populateFrom: func(k string, v interface{}, dT reflect.Type) (reflect.Value, error) { 81 return nestedFrom(v, dT) 82 }, 83 populateTo: func(k string, v reflect.Value) (interface{}, error) { 84 return nestedTo(v) 85 }, 86 }, nil 87 } 88 89 type nestedSliceFieldFactory struct{} 90 91 func (nmff nestedSliceFieldFactory) Handles(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) bool { 92 return fieldType.Kind() == reflect.Slice && fieldType.Elem().AssignableTo(protoMsgType) && !fieldType.Elem().AssignableTo(timestampType) 93 } 94 95 func (nmff nestedSliceFieldFactory) NewProtoField(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) (protoField, error) { 96 return &sliceField{ 97 baseField: baseField{ 98 msg: msg, 99 name: fieldName, 100 fType: mapStringInterfaceType, 101 vType: fieldType, 102 value: fieldValue, 103 }, 104 populateFrom: func(i int, v interface{}, dT reflect.Type) (reflect.Value, error) { 105 return nestedFrom(v, dT) 106 }, 107 populateTo: func(i int, v reflect.Value) (interface{}, error) { 108 return nestedTo(v) 109 }, 110 }, nil 111 }