|
| 1 | +// Licensed to Apache Software Foundation (ASF) under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Apache Software Foundation (ASF) licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package tools |
| 19 | + |
| 20 | +import "github.com/apache/skywalking-go/plugins/core/operator" |
| 21 | + |
| 22 | +type ReflectFieldFilter interface { |
| 23 | + Apply(interface{}) |
| 24 | +} |
| 25 | + |
| 26 | +// GetInstanceValueByType using reflect to get the first value of instance by type |
| 27 | +func GetInstanceValueByType(instance interface{}, filters ...ReflectFieldFilter) interface{} { |
| 28 | + if instance == nil || len(filters) == 0 { |
| 29 | + return nil |
| 30 | + } |
| 31 | + op := operator.GetOperator() |
| 32 | + if op == nil { |
| 33 | + return nil |
| 34 | + } |
| 35 | + fs := make([]interface{}, len(filters)) |
| 36 | + for i, f := range filters { |
| 37 | + fs[i] = f |
| 38 | + } |
| 39 | + return op.Tools().(operator.ToolsOperator).ReflectGetValue(instance, fs) |
| 40 | +} |
| 41 | + |
| 42 | +func WithFieldName(name string) ReflectFieldFilter { |
| 43 | + return buildFieldFilterOption(func(s ReflectFieldFilterSetter) { |
| 44 | + s.SetName(name) |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func WithInterfaceType(typeVal interface{}) ReflectFieldFilter { |
| 49 | + return buildFieldFilterOption(func(s ReflectFieldFilterSetter) { |
| 50 | + s.SetInterfaceType(typeVal) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func WithType(typeVal interface{}) ReflectFieldFilter { |
| 55 | + return buildFieldFilterOption(func(s ReflectFieldFilterSetter) { |
| 56 | + s.SetType(typeVal) |
| 57 | + }) |
| 58 | +} |
0 commit comments