-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathattribute_array.go
More file actions
103 lines (91 loc) · 2.87 KB
/
attribute_array.go
File metadata and controls
103 lines (91 loc) · 2.87 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
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 metadata
import (
"configcenter/src/common/util"
"encoding/json"
"fmt"
"math"
"github.com/tidwall/gjson"
"go.mongodb.org/mongo-driver/bson"
)
// ArrayOption len cap ,option is basic type 's option
type ArrayOption[T any] struct {
Len int `bson:"len" json:"len"`
Cap int `bson:"cap" json:"cap"`
Option T `bson:"option" json:"option"`
}
// Valid ArrayOption
func (a *ArrayOption[T]) Valid() error {
if a.Len < 0 || a.Len > a.Cap {
return fmt.Errorf("invalid array option,len:%d cap:%d", a.Len, a.Cap)
}
return nil
}
// ParseArrayOption len cap ,option is basic type 's option
func ParseArrayOption[T any](option any, handle func(v any) (T, error)) (ArrayOption[T], error) {
if option == nil {
return ArrayOption[T]{Len: math.MaxInt, Cap: math.MaxInt}, nil
}
var result ArrayOption[T]
optMap := map[string]interface{}{
"len": math.MaxInt,
"cap": math.MaxInt,
}
switch value := option.(type) {
case ArrayOption[T]:
return value, nil
case bson.M:
optMap = value
case map[string]interface{}:
optMap = value
default:
marshal, err := json.Marshal(option)
if err != nil {
return result, fmt.Errorf("invalid array option,type:%v,value:%v,err:%w",
option, option, err)
}
lenItem := gjson.GetBytes(marshal, "len")
capItem := gjson.GetBytes(marshal, "cap")
if !lenItem.Exists() || !capItem.Exists() {
return result, fmt.Errorf("invalid array option,type:%v,value:%v,err: not exist len or cap", option, option)
}
optMap["len"] = lenItem.Int()
optMap["cap"] = capItem.Int()
optMap["option"] = gjson.GetBytes(marshal, "option").Value()
}
lenn, lenOk := optMap["len"]
capp, capOk := optMap["cap"]
if !lenOk || !capOk {
return result, fmt.Errorf("invalid array option,type:%v,value:%v,err: not exist len or cap", option, option)
}
lenOpt, err := util.GetIntByInterface(lenn)
if err != nil {
return result, err
}
result.Len = lenOpt
capOpt, err := util.GetIntByInterface(capp)
if err != nil {
return result, err
}
result.Cap = capOpt
var defaultOption T
result.Option = defaultOption
if handle != nil {
t, err := handle(optMap["option"])
if err != nil {
return ArrayOption[T]{}, err
}
result.Option = t
}
return result, result.Valid()
}