forked from apache/dubbo-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager_helper.go
More file actions
121 lines (108 loc) · 3.66 KB
/
manager_helper.go
File metadata and controls
121 lines (108 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package manager
import (
"reflect"
"github.com/apache/dubbo-admin/pkg/common/bizerror"
"github.com/apache/dubbo-admin/pkg/core/resource/model"
)
// GetByKey is a helper function of ResourceManager.GeyByKey
func GetByKey[T model.Resource](rm ReadOnlyResourceManager, rk model.ResourceKind, key string) (r T, exist bool, err error) {
resource, exist, err := rm.GetByKey(rk, key)
if err != nil || !exist {
var zero T
return zero, exist, err
}
typedResource, ok := resource.(T)
if !ok {
var zero T
return zero, false, bizerror.NewAssertionError(rk, reflect.TypeOf(typedResource).Name())
}
return typedResource, true, nil
}
// ListByIndexes is a helper function of ResourceManager.ListByIndexes
func ListByIndexes[T model.Resource](rm ReadOnlyResourceManager, rk model.ResourceKind, indexes map[string]string) ([]T, error) {
resources, err := rm.ListByIndexes(rk, indexes)
if err != nil {
return nil, err
}
typedResources := make([]T, len(resources))
for i, resource := range resources {
typedResource, ok := resource.(T)
if !ok {
return nil, bizerror.NewAssertionError(rk, reflect.TypeOf(typedResource).Name())
}
typedResources[i] = typedResource
}
return typedResources, nil
}
// PageListByIndexes is a helper function of ResourceManager.PageListByIndexes
func PageListByIndexes[T model.Resource](
rm ReadOnlyResourceManager,
rk model.ResourceKind,
indexes map[string]string,
pr model.PageReq) (*model.PageData[T], error) {
pageData, err := rm.PageListByIndexes(rk, indexes, pr)
if err != nil {
return nil, err
}
typedResources := make([]T, len(pageData.Data))
for i, resource := range pageData.Data {
typedResource, ok := resource.(T)
if !ok {
return nil, bizerror.NewAssertionError(rk, reflect.TypeOf(typedResource).Name())
}
typedResources[i] = typedResource
}
newPageData := &model.PageData[T]{
Pagination: model.Pagination{
Total: pageData.Total,
PageOffset: pageData.PageOffset,
PageSize: pageData.PageSize,
},
Data: typedResources,
}
return newPageData, nil
}
// PageSearchResourceByConditions is a helper function of ResourceManager.PageSearchResourceByConditions
func PageSearchResourceByConditions[T model.Resource](
rm ReadOnlyResourceManager,
rk model.ResourceKind,
conditions []string,
pr model.PageReq) (*model.PageData[T], error) {
pageData, err := rm.PageSearchResourceByConditions(rk, conditions, pr)
if err != nil {
return nil, err
}
typedResources := make([]T, len(pageData.Data))
for i, resource := range pageData.Data {
typedResource, ok := resource.(T)
if !ok {
return nil, bizerror.NewAssertionError(rk, reflect.TypeOf(typedResource).Name())
}
typedResources[i] = typedResource
}
newPageData := &model.PageData[T]{
Pagination: model.Pagination{
Total: pageData.Total,
PageOffset: pageData.PageOffset,
PageSize: pageData.PageSize,
},
Data: typedResources,
}
return newPageData, nil
}