khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | desc "github.com/golang/protobuf/descriptor" |
| 21 | "github.com/golang/protobuf/proto" |
| 22 | "github.com/golang/protobuf/protoc-gen-go/descriptor" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/protos/common" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 24 | "reflect" |
| 25 | "strconv" |
| 26 | "sync" |
| 27 | ) |
| 28 | |
| 29 | type singleton struct { |
| 30 | ChildrenFieldsCache map[interface{}]map[string]*ChildType |
| 31 | } |
| 32 | |
| 33 | var instance *singleton |
| 34 | var once sync.Once |
| 35 | |
| 36 | func GetInstance() *singleton { |
| 37 | once.Do(func() { |
| 38 | instance = &singleton{} |
| 39 | }) |
| 40 | return instance |
| 41 | } |
| 42 | |
| 43 | type ChildType struct { |
| 44 | ClassModule string |
| 45 | ClassType reflect.Type |
| 46 | IsContainer bool |
| 47 | Key string |
| 48 | KeyFromStr func(s string) interface{} |
| 49 | } |
| 50 | |
| 51 | func ChildrenFields(cls interface{}) map[string]*ChildType { |
| 52 | if cls == nil { |
| 53 | return nil |
| 54 | } |
| 55 | var names map[string]*ChildType |
| 56 | var names_exist bool |
| 57 | |
| 58 | if GetInstance().ChildrenFieldsCache == nil { |
| 59 | GetInstance().ChildrenFieldsCache = make(map[interface{}]map[string]*ChildType) |
| 60 | } |
| 61 | |
| 62 | msgType := reflect.TypeOf(cls) |
| 63 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 64 | inst := GetInstance() |
| 65 | |
| 66 | if names, names_exist = inst.ChildrenFieldsCache[msgType.String()]; !names_exist { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 67 | names = make(map[string]*ChildType) |
| 68 | |
| 69 | _, md := desc.ForMessage(cls.(desc.Message)) |
| 70 | |
| 71 | // TODO: Do we need to validate MD for nil, panic or exception? |
| 72 | for _, field := range md.Field { |
| 73 | if options := field.GetOptions(); options != nil { |
| 74 | if proto.HasExtension(options, common.E_ChildNode) { |
| 75 | isContainer := *field.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED |
| 76 | meta, _ := proto.GetExtension(options, common.E_ChildNode) |
| 77 | var keyFromStr func(string) interface{} |
| 78 | |
| 79 | if meta.(*common.ChildNode).GetKey() == "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 80 | //fmt.Println("Child key is empty ... moving on") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 81 | } else { |
| 82 | parentType := FindOwnerType(reflect.ValueOf(cls), field.GetName(), 0, false) |
| 83 | keyType := FindKeyOwner(reflect.New(parentType).Elem().Interface(), meta.(*common.ChildNode).GetKey(), 0) |
| 84 | |
| 85 | switch keyType.(reflect.Type).Name() { |
| 86 | case "string": |
| 87 | keyFromStr = func(s string) interface{} { |
| 88 | return s |
| 89 | } |
| 90 | case "int32": |
| 91 | fallthrough |
| 92 | case "int64": |
| 93 | fallthrough |
| 94 | case "uint32": |
| 95 | fallthrough |
| 96 | case "uint64": |
| 97 | keyFromStr = func(s string) interface{} { |
| 98 | i, _ := strconv.Atoi(s) |
| 99 | return i |
| 100 | } |
| 101 | default: |
| 102 | fmt.Errorf("Key type not implemented - type: %s\n", keyType.(reflect.Type)) |
| 103 | } |
| 104 | |
| 105 | ct := ChildType{ |
| 106 | ClassModule: parentType.String(), |
| 107 | ClassType: parentType, |
| 108 | IsContainer: isContainer, |
| 109 | Key: meta.(*common.ChildNode).GetKey(), |
| 110 | KeyFromStr: keyFromStr, |
| 111 | } |
| 112 | |
| 113 | names[field.GetName()] = &ct |
| 114 | |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | GetInstance().ChildrenFieldsCache[msgType.String()] = names |
| 121 | } |
| 122 | |
| 123 | return names |
| 124 | } |