/
types.go
278 lines (242 loc) · 7.87 KB
/
types.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ssg Project
// Copyright (C) 2021 ALiwoto
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of the source code.
package ssg
import (
"hash"
"sync"
"time"
"github.com/AnimeKaizoku/ssg/ssg/rangeValues"
"github.com/AnimeKaizoku/ssg/ssg/shellUtils"
)
type ExpiringValue[T any] struct {
_value T
_t time.Time
}
// the StrongString used in the program for additional usage.
type StrongString struct {
_value []rune
}
type ListW[T comparable] struct {
_values []T
}
// AdvancedMap is a safe map of type TIndex to pointers of type TValue with
// extra advanced features that you can't find in safe-map types.
// obviously, because of its extra features, it's slightly slower than other
// normal safe-map types.
// this map is completely thread safe and is using internal lock when
// getting and setting variables.
type AdvancedMap[TKey comparable, TValue any] struct {
mut *sync.Mutex
values map[TKey]*TValue
// keys field is a slice of the map keys used in the map above. We put them in a slice
// so that we can get a random key by choosing a random index.
keys []TKey
// We store the index of each key, so that when we remove an item, we can
// quickly remove it from the slice above.
sliceKeyIndex map[TKey]int
// _default field is the default value this map has to return in GetValue
// method when the key is not found.
_default TValue
}
// SafeMap is a safe map of type TIndex to pointers of type TValue.
// this map is completely thread safe and is using internal lock when
// getting and setting variables.
type SafeMap[TKey comparable, TValue any] struct {
mut *sync.RWMutex
values map[TKey]*TValue
// _default field is the default value this map has to return in GetValue
// method when the key is not found.
_default TValue
// _disabled determines whether the map is disabled or not.
_disabled bool
}
type NumIdGenerator[T rangeValues.Integer] struct {
current T
mut *sync.Mutex
}
// SafeEMap is a safe map of type TIndex to pointers of type TValue.
// this map is completely thread safe and is using internal lock when
// getting and setting variables.
// the difference of SafeEMap and SafeMap is that SafeEMap is using a checker loop
// for removing the expired values from itself.
type SafeEMap[TKey comparable, TValue any] struct {
checkingEnabled bool
checkInterval time.Duration
expiration time.Duration
mut *sync.RWMutex
checkerMut *sync.Mutex
values map[TKey]*ExpiringValue[*TValue]
// keys field is a slice of the map keys used in the map above. We put them in a slice
// so that we can get a random key by choosing a random index.
keys []TKey
// We store the index of each key, so that when we remove an item, we can
// quickly remove it from the slice above.
sliceKeyIndex map[TKey]int
_default TValue
// _disabled determines whether the map is disabled or not.
_disabled bool
// onExpired is the event function that will be called when a value with the certain
// key on the map is expired. this event function will be called in a new goroutine.
onExpired func(key TKey, value TValue)
}
// EndpointResponse is the generalized form of a response from a HTTP API.
//
// T field is already a pointer in this struct, please avoid passing a pointer
//
// type, to prevent from `Result` field being a pointer to a pointer.
type EndpointResponse[T any] struct {
Success bool `json:"success"`
Result *T `json:"result"`
Error *EndpointError `json:"error"`
}
// EndpointError is the generalized form of an error from a HTTP API.
// this type should be used as a pointer in EndpointResponse.
type EndpointError struct {
ErrorCode int `json:"code"`
ErrorType int `json:"error_type"`
Message string `json:"message"`
Origin string `json:"origin"`
Date string `json:"date"`
}
type RangeInt = rangeValues.IntegerRange[int]
type RangeInt32 = rangeValues.IntegerRange[int32]
type RangeInt64 = rangeValues.IntegerRange[int64]
type RangeFloat64 = rangeValues.RangeFloat64
type Int64Container = rangeValues.IntContainer[int64]
type UInt64Container = rangeValues.IntContainer[uint64]
type Int32Container = rangeValues.IntContainer[int32]
type UInt32Container = rangeValues.IntContainer[uint32]
type Int16Container = rangeValues.IntContainer[int16]
type UInt16Container = rangeValues.IntContainer[uint16]
type Int8Container = rangeValues.IntContainer[int8]
type UInt8Container = rangeValues.IntContainer[uint8]
type ExecuteCommandResult = shellUtils.ExecuteCommandResult
//type safeList[T any] #TODO: implement safe-list
type StringUniqueIdContainer = UniqueIdContainer[string]
type Int64UniqueIdContainer = UniqueIdContainer[int64]
type UniqueIdContainer[T comparable] interface {
GetUniqueId() T
SetAsUniqueId(value T)
HasValidUniqueId() bool
}
// MetaDataProvider interface provides useful methods for getting/setting
// metadata for a data structure.
type MetaDataProvider interface {
Get(key string) (string, error)
GetInt(key string) (int, error)
GetInt8(key string) (int8, error)
GetInt16(key string) (int16, error)
GetInt32(key string) (int32, error)
GetInt64(key string) (int64, error)
GetUInt(key string) (uint, error)
GetUInt8(key string) (uint8, error)
GetUInt16(key string) (uint16, error)
GetUInt32(key string) (uint32, error)
GetUInt64(key string) (uint64, error)
GetBool(key string) (bool, error)
GetNoErr(key string) string
GetIntNoErr(key string) int
GetInt8NoErr(key string) int8
GetInt16NoErr(key string) int16
GetInt32NoErr(key string) int32
GetInt64NoErr(key string) int64
GetUIntNoErr(key string) uint
GetUInt8NoErr(key string) uint8
GetUInt16NoErr(key string) uint16
GetUInt32NoErr(key string) uint32
GetUInt64NoErr(key string) uint64
GetBoolNoErr(key string) bool
Set(key, value string)
SetInt(key string, value int)
SetInt8(key string, value int8)
SetInt16(key string, value int16)
SetInt32(key string, value int32)
SetInt64(key string, value int64)
SetUInt(key string, value uint)
SetUInt8(key string, value uint8)
SetUInt16(key string, value uint16)
SetUInt32(key string, value uint32)
SetUInt64(key string, value uint64)
SetBool(key string, value bool)
}
type GenericList[T comparable] interface {
BasicObject
Validator
Find(element T) int
Count(element T) int
Counts(element ...T) int
Contains(element T) bool
ContainsAll(elements ...T) bool
ContainsOne(elements ...T) bool
Change(index int, element T)
Exists(element T) bool
Append(elements ...T)
Add(elements ...T)
RemoveAt(index int)
RemoveOnce(element T)
RemoveAll(element ...T)
Remove(element T)
AsArray() []T
ToArray() []T
Clear()
Get(index int) T
}
type BytesObject interface {
ToBytes() ([]byte, error)
Length() int
}
type IntegerRepresent interface {
ToInt64() int64
ToUInt64() uint64
ToInt32() int32
ToUInt32() uint32
}
type BitsBlocks interface {
GetBitsSize() int
}
type BasicObject interface {
IsEmpty() bool
Length() int
}
type QString interface {
BasicObject
GetValue() string
GetIndexV(int) rune
IsEqual(QString) bool
Split(...QString) []QString
SplitN(int, ...QString) []QString
SplitFirst(qs ...QString) []QString
SplitStr(...string) []QString
SplitStrN(int, ...string) []QString
SplitStrFirst(...string) []QString
Contains(...QString) bool
ContainsStr(...string) bool
ContainsAll(...QString) bool
ContainsStrAll(...string) bool
TrimPrefix(...QString) QString
TrimPrefixStr(...string) QString
TrimSuffix(...QString) QString
TrimSuffixStr(...string) QString
Trim(qs ...QString) QString
TrimStr(...string) QString
Replace(qs, newS QString) QString
ReplaceStr(qs, newS string) QString
LockSpecial()
UnlockSpecial()
ToBool() bool
}
type Serializer interface {
Serialize() ([]byte, error)
StrSerialize() string
}
type Validator interface {
IsValid() bool
}
type SignatureContainer interface {
GetSignature() string
SetSignature(signature string) bool
SetSignatureByBytes(data []byte) bool
SetSignatureByFunc(h func() hash.Hash) bool
}