github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configvalues/root/standardvalues.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 config 18 19 import ( 20 "fmt" 21 "reflect" 22 23 "github.com/golang/protobuf/proto" 24 ) 25 26 type standardValues struct { 27 lookup map[string]proto.Message 28 } 29 30 // NewStandardValues accepts a structure which must contain only protobuf message 31 // types. The structure may embed other (non-pointer) structures which satisfy 32 // the same condition. NewStandard values will instantiate memory for all the proto 33 // messages and build a lookup map from structure field name to proto message instance 34 // This is a useful way to easily implement the Values interface 35 func NewStandardValues(protosStructs ...interface{}) (*standardValues, error) { 36 sv := &standardValues{ 37 lookup: make(map[string]proto.Message), 38 } 39 40 for _, protosStruct := range protosStructs { 41 if err := sv.initializeProtosStruct(reflect.ValueOf(protosStruct)); err != nil { 42 return nil, err 43 } 44 } 45 46 return sv, nil 47 } 48 49 func (sv *standardValues) ProtoMsg(key string) (proto.Message, bool) { 50 msg, ok := sv.lookup[key] 51 return msg, ok 52 } 53 54 func (sv *standardValues) initializeProtosStruct(objValue reflect.Value) error { 55 objType := objValue.Type() 56 if objType.Kind() != reflect.Ptr { 57 return fmt.Errorf("Non pointer type") 58 } 59 if objType.Elem().Kind() != reflect.Struct { 60 return fmt.Errorf("Non struct type") 61 } 62 63 numFields := objValue.Elem().NumField() 64 for i := 0; i < numFields; i++ { 65 structField := objType.Elem().Field(i) 66 fmt.Printf("Processing field: %s\n", structField.Name) 67 switch structField.Type.Kind() { 68 case reflect.Ptr: 69 fieldPtr := objValue.Elem().Field(i) 70 if !fieldPtr.CanSet() { 71 return fmt.Errorf("Cannot set structure field %s (unexported?)", structField) 72 } 73 fieldPtr.Set(reflect.New(structField.Type.Elem())) 74 default: 75 return fmt.Errorf("Bad type supplied: %s", structField.Type.Kind()) 76 } 77 78 proto, ok := objValue.Elem().Field(i).Interface().(proto.Message) 79 if !ok { 80 return fmt.Errorf("Field type %T does not implement proto.Message", objValue.Elem().Field(i)) 81 } 82 83 _, ok = sv.lookup[structField.Name] 84 if ok { 85 return fmt.Errorf("Ambiguous field name specified, multiple occurances of %s", structField.Name) 86 } 87 88 sv.lookup[structField.Name] = proto 89 } 90 91 return nil 92 }