Merge master and develop

This commit is contained in:
Ming Deng
2021-04-05 20:55:30 +08:00
227 changed files with 8831 additions and 3174 deletions

View File

@@ -108,7 +108,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats, w io.Writer) {
if gcstats.NumGC > 0 {
lastPause := gcstats.Pause[0]
elapsed := time.Now().Sub(startTime)
elapsed := time.Since(startTime)
overhead := float64(gcstats.PauseTotal) / float64(elapsed) * 100
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
@@ -125,7 +125,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats, w io.Writer) {
utils.ToShortTimeFormat(gcstats.PauseQuantiles[99]))
} else {
// while GC has disabled
elapsed := time.Now().Sub(startTime)
elapsed := time.Since(startTime)
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
fmt.Fprintf(w, "Alloc:%s Sys:%s Alloc(Rate):%s/s\n",

View File

@@ -51,24 +51,25 @@ func TestTagAutoWireBeanFactory_AutoWire(t *testing.T) {
}
type ComplicateStruct struct {
IntValue int `default:"12"`
StrValue string `default:"hello, strValue"`
Int8Value int8 `default:"8"`
Int16Value int16 `default:"16"`
Int32Value int32 `default:"32"`
Int64Value int64 `default:"64"`
BoolValue bool `default:"true"`
Int8Value int8 `default:"8"`
Uint8Value uint8 `default:"88"`
UintValue uint `default:"13"`
Uint8Value uint8 `default:"88"`
Int16Value int16 `default:"16"`
Uint16Value uint16 `default:"1616"`
Int32Value int32 `default:"32"`
Uint32Value uint32 `default:"3232"`
IntValue int `default:"12"`
UintValue uint `default:"13"`
Int64Value int64 `default:"64"`
Uint64Value uint64 `default:"6464"`
StrValue string `default:"hello, strValue"`
Float32Value float32 `default:"32.32"`
Float64Value float64 `default:"64.64"`
BoolValue bool `default:"true"`
ignoreInt int `default:"11"`
TimeValue time.Time `default:"2018-02-03 12:13:14.000"`

86
core/berror/codes.go Normal file
View File

@@ -0,0 +1,86 @@
// Copyright 2020 beego
//
// 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 berror
import (
"fmt"
"sync"
)
// A Code is an unsigned 32-bit error code as defined in the beego spec.
type Code interface {
Code() uint32
Module() string
Desc() string
Name() string
}
var defaultCodeRegistry = &codeRegistry{
codes: make(map[uint32]*codeDefinition, 127),
}
// DefineCode defining a new Code
// Before defining a new code, please read Beego specification.
// desc could be markdown doc
func DefineCode(code uint32, module string, name string, desc string) Code {
res := &codeDefinition{
code: code,
module: module,
desc: desc,
}
defaultCodeRegistry.lock.Lock()
defer defaultCodeRegistry.lock.Unlock()
if _, ok := defaultCodeRegistry.codes[code]; ok {
panic(fmt.Sprintf("duplicate code, code %d has been registered", code))
}
defaultCodeRegistry.codes[code] = res
return res
}
type codeRegistry struct {
lock sync.RWMutex
codes map[uint32]*codeDefinition
}
func (cr *codeRegistry) Get(code uint32) (Code, bool) {
cr.lock.RLock()
defer cr.lock.RUnlock()
c, ok := cr.codes[code]
return c, ok
}
type codeDefinition struct {
code uint32
module string
desc string
name string
}
func (c *codeDefinition) Name() string {
return c.name
}
func (c *codeDefinition) Code() uint32 {
return c.code
}
func (c *codeDefinition) Module() string {
return c.module
}
func (c *codeDefinition) Desc() string {
return c.desc
}

69
core/berror/error.go Normal file
View File

@@ -0,0 +1,69 @@
// Copyright 2020 beego
//
// 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 berror
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
// code, msg
const errFmt = "ERROR-%d, %s"
// Err returns an error representing c and msg. If c is OK, returns nil.
func Error(c Code, msg string) error {
return fmt.Errorf(errFmt, c.Code(), msg)
}
// Errorf returns error
func Errorf(c Code, format string, a ...interface{}) error {
return Error(c, fmt.Sprintf(format, a...))
}
func Wrap(err error, c Code, msg string) error {
if err == nil {
return nil
}
return errors.Wrap(err, fmt.Sprintf(errFmt, c.Code(), msg))
}
func Wrapf(err error, c Code, format string, a ...interface{}) error {
return Wrap(err, c, fmt.Sprintf(format, a...))
}
// FromError is very simple. It just parse error msg and check whether code has been register
// if code not being register, return unknown
// if err.Error() is not valid beego error code, return unknown
func FromError(err error) (Code, bool) {
msg := err.Error()
codeSeg := strings.SplitN(msg, ",", 2)
if strings.HasPrefix(codeSeg[0], "ERROR-") {
codeStr := strings.SplitN(codeSeg[0], "-", 2)
if len(codeStr) < 2 {
return Unknown, false
}
codeInt, e := strconv.ParseUint(codeStr[1], 10, 32)
if e != nil {
return Unknown, false
}
if code, ok := defaultCodeRegistry.Get(uint32(codeInt)); ok {
return code, true
}
}
return Unknown, false
}

77
core/berror/error_test.go Normal file
View File

@@ -0,0 +1,77 @@
// Copyright 2020 beego
//
// 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 berror
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
var testCode1 = DefineCode(1, "unit_test", "TestError", "Hello, test code1")
var testErr = errors.New("hello, this is error")
func TestErrorf(t *testing.T) {
msg := Errorf(testCode1, "errorf %s", "aaaa")
assert.NotNil(t, msg)
assert.Equal(t, "ERROR-1, errorf aaaa", msg.Error())
}
func TestWrapf(t *testing.T) {
err := Wrapf(testErr, testCode1, "Wrapf %s", "aaaa")
assert.NotNil(t, err)
assert.True(t, errors.Is(err, testErr))
}
func TestFromError(t *testing.T) {
err := errors.New("ERROR-1, errorf aaaa")
code, ok := FromError(err)
assert.True(t, ok)
assert.Equal(t, testCode1, code)
assert.Equal(t, "unit_test", code.Module())
assert.Equal(t, "Hello, test code1", code.Desc())
err = errors.New("not beego error")
code, ok = FromError(err)
assert.False(t, ok)
assert.Equal(t, Unknown, code)
err = errors.New("ERROR-2, not register")
code, ok = FromError(err)
assert.False(t, ok)
assert.Equal(t, Unknown, code)
err = errors.New("ERROR-aaa, invalid code")
code, ok = FromError(err)
assert.False(t, ok)
assert.Equal(t, Unknown, code)
err = errors.New("aaaaaaaaaaaaaa")
code, ok = FromError(err)
assert.False(t, ok)
assert.Equal(t, Unknown, code)
err = errors.New("ERROR-2-3, invalid error")
code, ok = FromError(err)
assert.False(t, ok)
assert.Equal(t, Unknown, code)
err = errors.New("ERROR, invalid error")
code, ok = FromError(err)
assert.False(t, ok)
assert.Equal(t, Unknown, code)
}

View File

@@ -0,0 +1,51 @@
// Copyright 2021 beego
//
// 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 berror
import (
"fmt"
)
// pre define code
// Unknown indicates got some error which is not defined
var Unknown = DefineCode(5000001, "error", "Unknown", fmt.Sprintf(`
Unknown error code. Usually you will see this code in three cases:
1. You forget to define Code or function DefineCode not being executed;
2. This is not Beego's error but you call FromError();
3. Beego got unexpected error and don't know how to handle it, and then return Unknown error
A common practice to DefineCode looks like:
%s
In this way, you may forget to import this package, and got Unknown error.
Sometimes, you believe you got Beego error, but actually you don't, and then you call FromError(err)
`, goCodeBlock(`
import your_package
func init() {
DefineCode(5100100, "your_module", "detail")
// ...
}
`)))
func goCodeBlock(code string) string {
return codeBlock("go", code)
}
func codeBlock(lan string, code string) string {
return fmt.Sprintf("```%s\n%s\n```", lan, code)
}

View File

@@ -14,7 +14,7 @@
// Package config is used to parse config.
// Usage:
// import "github.com/beego/beego/v2/config"
// import "github.com/beego/beego/v2/core/config"
// Examples.
//
// cnf, err := config.NewConfig("ini", "config.conf")

View File

@@ -20,8 +20,8 @@
//
// Usage:
// import(
// _ "github.com/beego/beego/v2/config/xml"
// "github.com/beego/beego/v2/config"
// _ "github.com/beego/beego/v2/core/config/xml"
// "github.com/beego/beego/v2/core/config"
// )
//
// cnf, err := config.NewConfig("xml", "config.xml")

View File

@@ -20,8 +20,8 @@
//
// Usage:
// import(
// _ "github.com/beego/beego/v2/config/yaml"
// "github.com/beego/beego/v2/config"
// _ "github.com/beego/beego/v2/core/config/yaml"
// "github.com/beego/beego/v2/core/config"
// )
//
// cnf, err := config.NewConfig("yaml", "config.yaml")

View File

@@ -4,7 +4,7 @@ logs is a Go logs manager. It can use many logs adapters. The repo is inspired b
## How to install?
go get github.com/beego/beego/v2/logs
go get github.com/beego/beego/v2/core/logs
## What adapters are supported?
@@ -16,7 +16,7 @@ First you must import it
```golang
import (
"github.com/beego/beego/v2/logs"
"github.com/beego/beego/v2/core/logs"
)
```

View File

@@ -29,7 +29,7 @@ func NewES() logs.Logger {
// please import this package
// usually means that you can import this package in your main package
// for example, anonymous:
// import _ "github.com/beego/beego/v2/logs/es"
// import _ "github.com/beego/beego/v2/core/logs/es"
type esLogger struct {
*elasticsearch.Client
DSN string `json:"dsn"`

View File

@@ -33,6 +33,11 @@ import (
// Writes messages by lines limit, file size limit, or time frequency.
type fileLogWriter struct {
sync.RWMutex // write log order by order and atomic incr maxLinesCurLines and maxSizeCurSize
Rotate bool `json:"rotate"`
Daily bool `json:"daily"`
Hourly bool `json:"hourly"`
// The opened file
Filename string `json:"filename"`
fileWriter *os.File
@@ -49,19 +54,15 @@ type fileLogWriter struct {
maxSizeCurSize int
// Rotate daily
Daily bool `json:"daily"`
MaxDays int64 `json:"maxdays"`
dailyOpenDate int
dailyOpenTime time.Time
// Rotate hourly
Hourly bool `json:"hourly"`
MaxHours int64 `json:"maxhours"`
hourlyOpenDate int
hourlyOpenTime time.Time
Rotate bool `json:"rotate"`
Level int `json:"level"`
Perm string `json:"perm"`

View File

@@ -15,7 +15,7 @@
// Package logs provide a general log interface
// Usage:
//
// import "github.com/beego/beego/v2/logs"
// import "github.com/beego/beego/v2/core/logs"
//
// log := NewLogger(10000)
// log.SetLogger("console", "")
@@ -112,17 +112,17 @@ func Register(name string, log newLoggerFunc) {
// Can contain several providers and log message into all providers.
type BeeLogger struct {
lock sync.Mutex
level int
init bool
enableFuncCallDepth bool
loggerFuncCallDepth int
enableFullFilePath bool
asynchronous bool
wg sync.WaitGroup
level int
loggerFuncCallDepth int
prefix string
msgChanLen int64
msgChan chan *LogMsg
signalChan chan string
wg sync.WaitGroup
outputs []*nameLogger
globalFormatter string
}

View File

@@ -7,18 +7,18 @@ validation is a form validation for a data validation and error collecting using
Install:
go get github.com/beego/beego/v2/validation
go get github.com/beego/beego/v2/core/validation
Test:
go test github.com/beego/beego/v2/validation
go test github.com/beego/beego/v2/core/validation
## Example
Direct Use:
import (
"github.com/beego/beego/v2/validation"
"github.com/beego/beego/v2/core/validation"
"log"
)
@@ -49,7 +49,7 @@ Direct Use:
Struct Tag Use:
import (
"github.com/beego/beego/v2/validation"
"github.com/beego/beego/v2/core/validation"
)
// validation function follow with "valid" tag
@@ -81,7 +81,7 @@ Struct Tag Use:
Use custom function:
import (
"github.com/beego/beego/v2/validation"
"github.com/beego/beego/v2/core/validation"
)
type user struct {

View File

@@ -15,7 +15,7 @@
// Package validation for validations
//
// import (
// "github.com/beego/beego/v2/validation"
// "github.com/beego/beego/v2/core/validation"
// "log"
// )
//
@@ -121,7 +121,7 @@ func (v *Validation) Clear() {
v.ErrorsMap = nil
}
// HasErrors Has ValidationError nor not.
// HasErrors Has ValidationError or not.
func (v *Validation) HasErrors() bool {
return len(v.Errors) > 0
}
@@ -158,7 +158,7 @@ func (v *Validation) Max(obj interface{}, max int, key string) *Result {
return v.apply(Max{max, key}, obj)
}
// Range Test that the obj is between mni and max if obj's type is int
// Range Test that the obj is between min and max if obj's type is int
func (v *Validation) Range(obj interface{}, min, max int, key string) *Result {
return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, obj)
}