github.com/lzy4123/fabric@v2.1.1+incompatible/common/tools/protolator/variably_opaque.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  )
    24  
    25  type variablyOpaqueFieldFactory struct{}
    26  
    27  func (soff variablyOpaqueFieldFactory) Handles(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) bool {
    28  	opaqueProto, ok := msg.(VariablyOpaqueFieldProto)
    29  	if !ok {
    30  		return false
    31  	}
    32  
    33  	return stringInSlice(fieldName, opaqueProto.VariablyOpaqueFields())
    34  }
    35  
    36  func (soff variablyOpaqueFieldFactory) NewProtoField(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) (protoField, error) {
    37  	opaqueProto := msg.(VariablyOpaqueFieldProto) // Type checked in Handles
    38  
    39  	return &plainField{
    40  		baseField: baseField{
    41  			msg:   msg,
    42  			name:  fieldName,
    43  			fType: mapStringInterfaceType,
    44  			vType: bytesType,
    45  			value: fieldValue,
    46  		},
    47  		populateFrom: func(v interface{}, dT reflect.Type) (reflect.Value, error) {
    48  			return opaqueFrom(func() (proto.Message, error) { return opaqueProto.VariablyOpaqueFieldProto(fieldName) }, v, dT)
    49  		},
    50  		populateTo: func(v reflect.Value) (interface{}, error) {
    51  			return opaqueTo(func() (proto.Message, error) { return opaqueProto.VariablyOpaqueFieldProto(fieldName) }, v)
    52  		},
    53  	}, nil
    54  }
    55  
    56  type variablyOpaqueMapFieldFactory struct{}
    57  
    58  func (soff variablyOpaqueMapFieldFactory) Handles(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) bool {
    59  	opaqueProto, ok := msg.(VariablyOpaqueMapFieldProto)
    60  	if !ok {
    61  		return false
    62  	}
    63  
    64  	return stringInSlice(fieldName, opaqueProto.VariablyOpaqueMapFields())
    65  }
    66  
    67  func (soff variablyOpaqueMapFieldFactory) NewProtoField(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) (protoField, error) {
    68  	opaqueProto := msg.(VariablyOpaqueMapFieldProto) // Type checked in Handles
    69  
    70  	return &mapField{
    71  		baseField: baseField{
    72  			msg:   msg,
    73  			name:  fieldName,
    74  			fType: mapStringInterfaceType,
    75  			vType: fieldType,
    76  			value: fieldValue,
    77  		},
    78  		populateFrom: func(key string, v interface{}, dT reflect.Type) (reflect.Value, error) {
    79  			return opaqueFrom(func() (proto.Message, error) {
    80  				return opaqueProto.VariablyOpaqueMapFieldProto(fieldName, key)
    81  			}, v, dT)
    82  		},
    83  		populateTo: func(key string, v reflect.Value) (interface{}, error) {
    84  			return opaqueTo(func() (proto.Message, error) {
    85  				return opaqueProto.VariablyOpaqueMapFieldProto(fieldName, key)
    86  			}, v)
    87  		},
    88  	}, nil
    89  }
    90  
    91  type variablyOpaqueSliceFieldFactory struct{}
    92  
    93  func (soff variablyOpaqueSliceFieldFactory) Handles(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) bool {
    94  	opaqueProto, ok := msg.(VariablyOpaqueSliceFieldProto)
    95  	if !ok {
    96  		return false
    97  	}
    98  
    99  	return stringInSlice(fieldName, opaqueProto.VariablyOpaqueSliceFields())
   100  }
   101  
   102  func (soff variablyOpaqueSliceFieldFactory) NewProtoField(msg proto.Message, fieldName string, fieldType reflect.Type, fieldValue reflect.Value) (protoField, error) {
   103  	opaqueProto := msg.(VariablyOpaqueSliceFieldProto) // Type checked in Handles
   104  
   105  	return &sliceField{
   106  		baseField: baseField{
   107  			msg:   msg,
   108  			name:  fieldName,
   109  			fType: mapStringInterfaceType,
   110  			vType: fieldType,
   111  			value: fieldValue,
   112  		},
   113  		populateFrom: func(index int, v interface{}, dT reflect.Type) (reflect.Value, error) {
   114  			return opaqueFrom(func() (proto.Message, error) {
   115  				return opaqueProto.VariablyOpaqueSliceFieldProto(fieldName, index)
   116  			}, v, dT)
   117  		},
   118  		populateTo: func(index int, v reflect.Value) (interface{}, error) {
   119  			return opaqueTo(func() (proto.Message, error) {
   120  				return opaqueProto.VariablyOpaqueSliceFieldProto(fieldName, index)
   121  			}, v)
   122  		},
   123  	}, nil
   124  }