From 68cb5f25162302e1c701ad0c81ae719f9426661b Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Sat, 1 Jul 2023 16:37:11 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E9=85=8D=E7=BD=AE=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E7=94=9F=E6=88=90=E7=9A=84=20Go=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=8E=B7=E5=8F=96=E6=89=80=E6=9C=89=E7=BA=BF?= =?UTF-8?q?=E4=B8=8A=E9=85=8D=E7=BD=AE=E7=9A=84=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- planner/configexport/example/EasyConfig.json | 4 +- planner/configexport/example/IndexConfig.json | 78 +++++++++---------- planner/configexport/example/config.go | 12 +++ planner/configexport/internal/template.go | 12 ++- 4 files changed, 64 insertions(+), 42 deletions(-) diff --git a/planner/configexport/example/EasyConfig.json b/planner/configexport/example/EasyConfig.json index 79f1fe2..e12622a 100644 --- a/planner/configexport/example/EasyConfig.json +++ b/planner/configexport/example/EasyConfig.json @@ -6,8 +6,8 @@ }, "Other": { "0": { - "id": 1, - "name": "张飞" + "name": "张飞", + "id": 1 }, "1": { "id": 2, diff --git a/planner/configexport/example/IndexConfig.json b/planner/configexport/example/IndexConfig.json index 7294fdd..0ac28e5 100644 --- a/planner/configexport/example/IndexConfig.json +++ b/planner/configexport/example/IndexConfig.json @@ -1,58 +1,58 @@ { "1": { "b": { - "Award": { - "0": "asd", - "1": "12" - }, - "Other": { - "0": { - "name": "张飞", - "id": 1 - }, - "1": { - "name": "刘备", - "id": 2 - } - }, "Id": 1, - "Count": "b" - } - }, - "2": { - "c": { + "Count": "b", "Award": { "0": "asd", "1": "12" }, - "Other": { - "0": { - "name": "张飞", - "id": 1 - }, - "1": { - "id": 2, - "name": "刘备" - } - }, - "Id": 2, - "Count": "c" - }, - "d": { - "Id": 2, - "Count": "d", - "Award": { - "1": "12", - "0": "asd" - }, "Other": { "0": { "id": 1, "name": "张飞" }, + "1": { + "name": "刘备", + "id": 2 + } + } + } + }, + "2": { + "c": { + "Other": { + "0": { + "id": 1, + "name": "张飞" + }, + "1": { + "name": "刘备", + "id": 2 + } + }, + "Id": 2, + "Count": "c", + "Award": { + "0": "asd", + "1": "12" + } + }, + "d": { + "Id": 2, + "Count": "d", + "Award": { + "0": "asd", + "1": "12" + }, + "Other": { "1": { "id": 2, "name": "刘备" + }, + "0": { + "id": 1, + "name": "张飞" } } } diff --git a/planner/configexport/example/config.go b/planner/configexport/example/config.go index 8c7d4c3..fae0ca4 100644 --- a/planner/configexport/example/config.go +++ b/planner/configexport/example/config.go @@ -9,6 +9,7 @@ import ( ) var json = jsonIter.ConfigCompatibleWithStandardLibrary +var full map[string]any var ( // IndexConfig 有索引 IndexConfig map[int]map[string]*IndexConfigDefine @@ -32,11 +33,16 @@ func LoadConfig(handle func(filename string, config any) error) { } +// Refresh 将加载后的配置刷新到线上 func Refresh() { + full = make(map[string]any) IndexConfig = _IndexConfig + full["IndexConfig"] = IndexConfig EasyConfig = _EasyConfig + full["EasyConfig"] = EasyConfig } +// DefaultLoad 默认提供的配置加载函数 func DefaultLoad(filepath string) { LoadConfig(func(filename string, config any) error { bytes, err := os.ReadFile(filepath) @@ -47,3 +53,9 @@ func DefaultLoad(filepath string) { return json.Unmarshal(bytes, &config) }) } + +// GetFull 获取所有配置的 map 集合 +// - 通常用于前端配置通过后端接口获取的情况 +func GetFull() map[string]any { + return full +} diff --git a/planner/configexport/internal/template.go b/planner/configexport/internal/template.go index fe1a194..4862648 100644 --- a/planner/configexport/internal/template.go +++ b/planner/configexport/internal/template.go @@ -41,6 +41,7 @@ import ( ) var json = jsonIter.ConfigCompatibleWithStandardLibrary +var full map[string]any var ( {{range $index, $config := .Configs}} @@ -66,13 +67,16 @@ func LoadConfig(handle func(filename string, config any) error) { {{end}} } +// Refresh 将加载后的配置刷新到线上 func Refresh() { + full = make(map[string]any) {{range $index, $config := .Configs}} {{$config.Name}} = _{{$config.Name}} + full["{{$config.Name}}"] = {{$config.Name}} {{end}} } - +// DefaultLoad 默认提供的配置加载函数 func DefaultLoad(filepath string) { LoadConfig(func(filename string, config any) error { bytes, err := os.ReadFile(filepath) @@ -82,6 +86,12 @@ func DefaultLoad(filepath string) { return json.Unmarshal(bytes, &config) }) } + +// GetFull 获取所有配置的 map 集合 +// - 通常用于前端配置通过后端接口获取的情况 +func GetFull() map[string]any { + return full +} ` GenerateGoDefineTemplate = `// Code generated by minotaur-config-export. DO NOT EDIT. From f22bf5bc936e6f709f43c306e38703be498acf01 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Sat, 1 Jul 2023 16:49:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?other:=20=E5=88=A0=E9=99=A4=20net=20?= =?UTF-8?q?=E5=8C=85=E4=B8=AD=E7=9A=84=E4=B8=8D=E5=90=88=E7=90=86=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/network/float.go | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 utils/network/float.go diff --git a/utils/network/float.go b/utils/network/float.go deleted file mode 100644 index 9226a92..0000000 --- a/utils/network/float.go +++ /dev/null @@ -1,17 +0,0 @@ -package network - -import ( - "fmt" - "github.com/kercylan98/minotaur/utils/generic" - "github.com/kercylan98/minotaur/utils/maths" - "strings" -) - -// FloatEnlarge 用于将浮点型放大后进行网络传输,返回放大后的值和放大的倍率 -// - 存在精度丢失问题,如1.13 -func FloatEnlarge[F generic.Float](f F) (value int64, multi int64) { - str := fmt.Sprint(f) - multi = maths.PowInt64(10, int64(len(str[strings.Index(str, ".")+1:]))) - value = int64(f * F(multi)) - return -} From d4d11f2a8d1eab2633bc9772c893011d7051706e Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Sat, 1 Jul 2023 16:50:08 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E4=B8=BA=20slice=20=E5=8C=85?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E5=A4=9A=E7=9A=84=E8=BE=85=E5=8A=A9?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/slice/slice.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/utils/slice/slice.go b/utils/slice/slice.go index 332dceb..142996e 100644 --- a/utils/slice/slice.go +++ b/utils/slice/slice.go @@ -1,5 +1,7 @@ package slice +import "math/rand" + // Del 删除特定索引的元素 func Del[V any](slice *[]V, index int) { s := *slice @@ -67,3 +69,54 @@ func NextLoop[V any](slice []V, i int) (next int, value V) { } return next, slice[next] } + +// PrevLoop 返回 i 的上一个数组成员,当 i 为 0 时从数组末尾开始 +// - 当 i 为 -1 时将返回最后一个元素 +func PrevLoop[V any](slice []V, i int) (prev int, value V) { + if i == -1 { + return len(slice) - 1, slice[len(slice)-1] + } + prev = i - 1 + if prev == -1 { + prev = len(slice) - 1 + } + return prev, slice[prev] +} + +// Reverse 反转数组 +func Reverse[V any](slice []V) { + for i := 0; i < len(slice)/2; i++ { + slice[i], slice[len(slice)-1-i] = slice[len(slice)-1-i], slice[i] + } +} + +// Shuffle 随机打乱数组 +func Shuffle[V any](slice []V) { + for i := 0; i < len(slice); i++ { + j := rand.Intn(len(slice)) + slice[i], slice[j] = slice[j], slice[i] + } +} + +// Swap 交换数组中的两个元素 +func Swap[V any](slice []V, i, j int) { + slice[i], slice[j] = slice[j], slice[i] +} + +// ToMap 将数组转换为 map +func ToMap[K comparable, V any](slice []V, key func(V) K) map[K]V { + m := make(map[K]V) + for _, v := range slice { + m[key(v)] = v + } + return m +} + +// ToSet 将数组转换为 set +func ToSet[V comparable](slice []V) map[V]struct{} { + m := make(map[V]struct{}) + for _, v := range slice { + m[v] = struct{}{} + } + return m +}