-
Notifications
You must be signed in to change notification settings - Fork 31
/
stub.go
74 lines (65 loc) · 1.7 KB
/
stub.go
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
/**
* @license
* Copyright 2020 Dynatrace LLC
*
* Licensed 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 api
import (
"sort"
)
type Stub struct {
ID string `json:"id"`
Name string `json:"name"`
EntityID string `json:"entityId,omitempty"`
Value any `json:"-"`
LegacyID *string `json:"legacyID,omitempty"`
ParentID *string `json:"parentID,omitempty"`
}
type Stubs []*Stub
func (me Stubs) Sort() Stubs {
stubs := append(Stubs{}, me...)
sort.Slice(stubs, func(i, j int) bool {
a := stubs[i]
b := stubs[j]
if a.Name < b.Name {
return true
}
return a.ID < b.ID
})
return stubs
}
func (me *Stubs) ToStubs() Stubs {
res := []*Stub{}
for _, stub := range *me {
if len(stub.ID) == 0 && len(stub.EntityID) != 0 {
stub.ID = stub.EntityID
}
res = append(res, &Stub{ID: stub.ID, Name: stub.Name, Value: stub.Value, EntityID: stub.EntityID, LegacyID: stub.LegacyID, ParentID: stub.ParentID})
}
return res
}
type StubList struct {
Values []*Stub `json:"values"`
}
func (me *StubList) ToStubs() Stubs {
for _, stub := range me.Values {
if len(stub.ID) == 0 && len(stub.EntityID) != 0 {
stub.ID = stub.EntityID
}
}
return me.Values
}
type RecordStubs interface {
ToStubs() Stubs
}