Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 28, 2022
1 parent 7d4a757 commit 9dce175
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
5 changes: 1 addition & 4 deletions configure/yamlConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,14 @@ func (r *yamlConfig) flattening(keyPrefix string, m map[string]any) {

// 扁平化any
func (r *yamlConfig) flatteningAny(key string, v any) {
switch v.(type) {
switch subNode := v.(type) {
// 需要继续往里面遍历子节点map
case map[string]any:
subNode := v.(map[string]any)
r.data[key] = subNode
r.flattening(key, subNode)
// 需要继续往里面遍历子节点数组
case []any:
subNode := v.([]any)
r.data[key] = subNode

for subIndex := 0; subIndex < len(subNode); subIndex++ {
r.flatteningAny(key+fmt.Sprintf("[%d]", subIndex), subNode[subIndex])
}
Expand Down
1 change: 0 additions & 1 deletion exception/refuseException.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "fmt"
type RefuseException struct {
// 异常信息
Message string
r any
}

// ThrowRefuseException 抛出RefuseException异常
Expand Down
1 change: 0 additions & 1 deletion exception/webException.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type WebException struct {
// 异常信息
Message string
StatusCode int
r any
}

// ThrowWebException 抛出WebException异常
Expand Down
8 changes: 4 additions & 4 deletions stopwatch/startNew.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (sw *Watch) Start() {

// Stop 停止计时
func (sw *Watch) Stop() {
sub := time.Now().Sub(sw.startTime)
sub := time.Since(sw.startTime)
sw.lastElapsedMilliseconds += sub.Milliseconds()
sw.lastElapsedMicroseconds += sub.Microseconds()
sw.lastElapsedNanoseconds += sub.Nanoseconds()
Expand All @@ -68,23 +68,23 @@ func (sw *Watch) Stop() {
// ElapsedMilliseconds 返回当前已计时的时间(毫秒)
func (sw *Watch) ElapsedMilliseconds() int64 {
if sw.isRunning {
return time.Now().Sub(sw.startTime).Milliseconds() + sw.lastElapsedMilliseconds
return time.Since(sw.startTime).Milliseconds() + sw.lastElapsedMilliseconds
}
return sw.lastElapsedMilliseconds
}

// ElapsedMicroseconds 返回当前已计时的时间(微秒)
func (sw *Watch) ElapsedMicroseconds() int64 {
if sw.isRunning {
return time.Now().Sub(sw.startTime).Microseconds() + sw.lastElapsedMicroseconds
return time.Since(sw.startTime).Microseconds() + sw.lastElapsedMicroseconds
}
return sw.lastElapsedMicroseconds
}

// ElapsedNanoseconds 返回当前已计时的时间(纳秒)
func (sw *Watch) ElapsedNanoseconds() int64 {
if sw.isRunning {
return time.Now().Sub(sw.startTime).Nanoseconds() + sw.lastElapsedNanoseconds
return time.Since(sw.startTime).Nanoseconds() + sw.lastElapsedNanoseconds
}
return sw.lastElapsedNanoseconds
}
Expand Down
10 changes: 6 additions & 4 deletions test/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ func (t testModule) Initialize() {

func (t testModule) PostInitialize() {
lst = append(lst, 3)
fs.AddInitCallback(func() {})
fs.AddInitCallback(func() {
lst = append(lst, 4)
})
}

func (t testModule) Shutdown() {
}
func (t testModule) Shutdown() {}

func TestInitialize(t *testing.T) {
fs.Initialize[testModule]("unit test")
assert.Equal(t, 3, len(lst))
assert.Equal(t, 4, len(lst))
assert.Equal(t, 1, lst[0])
assert.Equal(t, 2, lst[1])
assert.Equal(t, 3, lst[2])
assert.Equal(t, 4, lst[3])

assert.Equal(t, 3, strings.Count(fs.AppIp, "."))
assert.Equal(t, 18, len(strconv.FormatInt(fs.AppId, 10)))
Expand Down

0 comments on commit 9dce175

Please sign in to comment.