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 | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 20 | "reflect" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 24 | // IsProtoMessage determines if the specified implements proto.Message type |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | func IsProtoMessage(object interface{}) bool { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 26 | var ok = false |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 27 | |
| 28 | if object != nil { |
| 29 | st := reflect.TypeOf(object) |
| 30 | _, ok = st.MethodByName("ProtoMessage") |
| 31 | } |
| 32 | return ok |
| 33 | } |
| 34 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 35 | // FindOwnerType will traverse a data structure and find the parent type of the specified object |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 36 | func FindOwnerType(obj reflect.Value, name string, depth int, found bool) reflect.Type { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 37 | prefix := "" |
| 38 | for d := 0; d < depth; d++ { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 39 | prefix += ">>" |
| 40 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 41 | k := obj.Kind() |
| 42 | switch k { |
| 43 | case reflect.Ptr: |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 44 | if found { |
| 45 | return obj.Type() |
| 46 | } |
| 47 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 48 | t := obj.Type().Elem() |
| 49 | n := reflect.New(t) |
| 50 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 51 | if rc := FindOwnerType(n.Elem(), name, depth+1, found); rc != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 52 | return rc |
| 53 | } |
| 54 | |
| 55 | case reflect.Struct: |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 56 | if found { |
| 57 | return obj.Type() |
| 58 | } |
| 59 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 60 | for i := 0; i < obj.NumField(); i++ { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 61 | v := reflect.Indirect(obj) |
| 62 | |
| 63 | json := strings.Split(v.Type().Field(i).Tag.Get("json"), ",") |
| 64 | |
| 65 | if json[0] == name { |
| 66 | return FindOwnerType(obj.Field(i), name, depth+1, true) |
| 67 | } |
| 68 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 69 | if rc := FindOwnerType(obj.Field(i), name, depth+1, found); rc != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 70 | return rc |
| 71 | } |
| 72 | } |
| 73 | case reflect.Slice: |
| 74 | s := reflect.MakeSlice(obj.Type(), 1, 1) |
| 75 | n := reflect.New(obj.Type()) |
| 76 | n.Elem().Set(s) |
| 77 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 78 | for i := 0; i < n.Elem().Len(); i++ { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 79 | if found { |
| 80 | return reflect.ValueOf(n.Elem().Index(i).Interface()).Type() |
| 81 | } |
| 82 | } |
| 83 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 84 | for i := 0; i < obj.Len(); i++ { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 85 | if found { |
| 86 | return obj.Index(i).Type() |
| 87 | } |
| 88 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 89 | if rc := FindOwnerType(obj.Index(i), name, depth+1, found); rc != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | return rc |
| 91 | } |
| 92 | } |
| 93 | default: |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 94 | //log.Debugf("%s Unhandled <%+v> ... It's a %+v\n", prefix, obj, k) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | return nil |
| 98 | } |
| 99 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 100 | // FindKeyOwner will traverse a structure to find the owner type of the specified name |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 101 | func FindKeyOwner(iface interface{}, name string, depth int) interface{} { |
| 102 | obj := reflect.ValueOf(iface) |
| 103 | k := obj.Kind() |
| 104 | switch k { |
| 105 | case reflect.Ptr: |
| 106 | t := obj.Type().Elem() |
| 107 | n := reflect.New(t) |
| 108 | |
| 109 | if rc := FindKeyOwner(n.Elem().Interface(), name, depth+1); rc != nil { |
| 110 | return rc |
| 111 | } |
| 112 | |
| 113 | case reflect.Struct: |
| 114 | for i := 0; i < obj.NumField(); i++ { |
| 115 | json := strings.Split(obj.Type().Field(i).Tag.Get("json"), ",") |
| 116 | |
| 117 | if json[0] == name { |
| 118 | return obj.Type().Field(i).Type |
| 119 | } |
| 120 | |
| 121 | if rc := FindKeyOwner(obj.Field(i).Interface(), name, depth+1); rc != nil { |
| 122 | return rc |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | case reflect.Slice: |
| 127 | s := reflect.MakeSlice(obj.Type(), 1, 1) |
| 128 | n := reflect.New(obj.Type()) |
| 129 | n.Elem().Set(s) |
| 130 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 131 | for i := 0; i < n.Elem().Len(); i++ { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 132 | if rc := FindKeyOwner(n.Elem().Index(i).Interface(), name, depth+1); rc != nil { |
| 133 | return rc |
| 134 | } |
| 135 | } |
| 136 | default: |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 137 | //log.Debugf("%s Unhandled <%+v> ... It's a %+v\n", prefix, obj, k) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return nil |
| 141 | } |
| 142 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 143 | // GetAttributeValue traverse a structure to find the value of an attribute |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 144 | // FIXME: Need to figure out if GetAttributeValue and GetAttributeStructure can become one |
| 145 | // Code is repeated in both, but outputs have a different purpose |
| 146 | // Left as-is for now to get things working |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 147 | func GetAttributeValue(data interface{}, name string, depth int) (string, reflect.Value) { |
| 148 | var attribName string |
| 149 | var attribValue reflect.Value |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 150 | obj := reflect.ValueOf(data) |
| 151 | |
| 152 | if !obj.IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 153 | return attribName, attribValue |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | k := obj.Kind() |
| 157 | switch k { |
| 158 | case reflect.Ptr: |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 159 | if obj.IsNil() { |
| 160 | return attribName, attribValue |
| 161 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 162 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 163 | if attribName, attribValue = GetAttributeValue(obj.Elem().Interface(), name, depth+1); attribValue.IsValid() { |
| 164 | return attribName, attribValue |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | case reflect.Struct: |
| 168 | for i := 0; i < obj.NumField(); i++ { |
| 169 | json := strings.Split(obj.Type().Field(i).Tag.Get("json"), ",") |
| 170 | |
| 171 | if json[0] == name { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 172 | return obj.Type().Field(i).Name, obj.Field(i) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | if obj.Field(i).IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 176 | if attribName, attribValue = GetAttributeValue(obj.Field(i).Interface(), name, depth+1); attribValue.IsValid() { |
| 177 | return attribName, attribValue |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | case reflect.Slice: |
| 183 | s := reflect.MakeSlice(obj.Type(), 1, 1) |
| 184 | n := reflect.New(obj.Type()) |
| 185 | n.Elem().Set(s) |
| 186 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 187 | for i := 0; i < obj.Len(); i++ { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 188 | if attribName, attribValue = GetAttributeValue(obj.Index(i).Interface(), name, depth+1); attribValue.IsValid() { |
| 189 | return attribName, attribValue |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | default: |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 193 | //log.Debugf("%s Unhandled <%+v> ... It's a %+v\n", prefix, obj, k) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 196 | return attribName, attribValue |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 197 | |
| 198 | } |
| 199 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 200 | // GetAttributeStructure will traverse a structure to find the data structure for the named attribute |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 201 | // FIXME: See GetAttributeValue(...) comment |
| 202 | func GetAttributeStructure(data interface{}, name string, depth int) reflect.StructField { |
| 203 | var result reflect.StructField |
| 204 | obj := reflect.ValueOf(data) |
| 205 | |
| 206 | if !obj.IsValid() { |
| 207 | return result |
| 208 | } |
| 209 | |
| 210 | k := obj.Kind() |
| 211 | switch k { |
| 212 | case reflect.Ptr: |
| 213 | t := obj.Type().Elem() |
| 214 | n := reflect.New(t) |
| 215 | |
| 216 | if rc := GetAttributeStructure(n.Elem().Interface(), name, depth+1); rc.Name != "" { |
| 217 | return rc |
| 218 | } |
| 219 | |
| 220 | case reflect.Struct: |
| 221 | for i := 0; i < obj.NumField(); i++ { |
| 222 | v := reflect.Indirect(obj) |
| 223 | json := strings.Split(obj.Type().Field(i).Tag.Get("json"), ",") |
| 224 | |
| 225 | if json[0] == name { |
| 226 | return v.Type().Field(i) |
| 227 | } |
| 228 | |
| 229 | if obj.Field(i).IsValid() { |
| 230 | if rc := GetAttributeStructure(obj.Field(i).Interface(), name, depth+1); rc.Name != "" { |
| 231 | return rc |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | case reflect.Slice: |
| 237 | s := reflect.MakeSlice(obj.Type(), 1, 1) |
| 238 | n := reflect.New(obj.Type()) |
| 239 | n.Elem().Set(s) |
| 240 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 241 | for i := 0; i < obj.Len(); i++ { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 242 | if rc := GetAttributeStructure(obj.Index(i).Interface(), name, depth+1); rc.Name != "" { |
| 243 | return rc |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | default: |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 248 | //log.Debugf("%s Unhandled <%+v> ... It's a %+v\n", prefix, obj, k) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | return result |
| 252 | |
| 253 | } |