e4aac47fe0ec3c02c7564b2ebed138fdc5fbbcb8
[ealt-edge.git] / mep / mepserver / mp1 / arch / bus / load.go
1 /*
2  * Copyright 2020 Huawei Technologies Co., Ltd.
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 bus
18
19 import (
20         "reflect"
21         "strings"
22 )
23
24 func parseTags(tag reflect.StructTag) (string, string) {
25         fieldName := tag.Get("json")
26         if fieldName == "" {
27                 return "", ""
28         }
29         fieldNames := strings.Split(fieldName, ",")
30         secTag := "in"
31         if len(fieldNames) > 1 {
32                 secTag = fieldNames[1]
33         }
34         return fieldNames[0], secTag
35 }
36
37 func LoadObjByInd(dst interface{}, src interface{}, direction string) bool {
38         rflDst := reflect.ValueOf(dst).Elem()
39         rflSrc := reflect.ValueOf(src).Elem()
40         vType := rflDst.Type()
41         for i := 0; i < rflDst.NumField(); i++ {
42                 fieldName, secTag := parseTags(vType.Field(i).Tag)
43                 if fieldName == "" {
44                         continue
45                 }
46                 if secTag != direction {
47                         continue
48                 }
49
50                 valDst := rflDst.Field(i)
51                 srcNode := ObjReflectPath(rflSrc, rflSrc, fieldName)
52                 if srcNode.e != nil || srcNode.CurNode.Kind() == reflect.Invalid {
53                         continue
54                 }
55
56                 valSrc := srcNode.CurNode
57                 if direction == "in" {
58                         valDst.Set(valSrc)
59                 } else {
60                         valSrc.Set(valDst)
61                 }
62         }
63         return true
64 }