diff --git a/.travis.yml b/.travis.yml index d4b19015..479d70ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: go go: - - 1.6 - - 1.7 - - 1.8 + - 1.6.4 + - 1.7.5 + - 1.8.1 services: - redis-server - mysql @@ -35,6 +35,8 @@ install: - go get github.com/gogo/protobuf/proto - go get -u honnef.co/go/tools/cmd/gosimple - go get -u github.com/mdempsky/unconvert + - go get -u github.com/gordonklaus/ineffassign + - go get -u github.com/golang/lint/golint before_script: - psql --version - sh -c "if [ '$ORM_DRIVER' = 'postgres' ]; then psql -c 'create database orm_test;' -U postgres; fi" @@ -51,5 +53,8 @@ script: - go test -v ./... - gosimple -ignore "$(cat .gosimpleignore)" $(go list ./... | grep -v /vendor/) - unconvert $(go list ./... | grep -v /vendor/) + - ineffassign . + - find . ! \( -path './vendor' -prune \) -type f -name '*.go' -print0 | xargs -0 gofmt -l -s + - golint ./... addons: postgresql: "9.4" diff --git a/admin.go b/admin.go index 80615957..875cd0e8 100644 --- a/admin.go +++ b/admin.go @@ -105,29 +105,12 @@ func listConf(rw http.ResponseWriter, r *http.Request) { tmpl.Execute(rw, data) case "router": - var ( - content = map[string]interface{}{ - "Fields": []string{ - "Router Pattern", - "Methods", - "Controller", - }, - } - methods = []string{} - methodsData = make(map[string]interface{}) - ) - for method, t := range BeeApp.Handlers.routers { - - resultList := new([][]string) - - printTree(resultList, t) - - methods = append(methods, method) - methodsData[method] = resultList + content := PrintTree() + content["Fields"] = []string{ + "Router Pattern", + "Methods", + "Controller", } - - content["Data"] = methodsData - content["Methods"] = methods data["Content"] = content data["Title"] = "Routers" execTpl(rw, data, routerAndFilterTpl, defaultScriptsTpl) @@ -200,6 +183,28 @@ func list(root string, p interface{}, m map[string]interface{}) { } } +// PrintTree prints all registered routers. +func PrintTree() map[string]interface{} { + var ( + content = map[string]interface{}{} + methods = []string{} + methodsData = make(map[string]interface{}) + ) + for method, t := range BeeApp.Handlers.routers { + + resultList := new([][]string) + + printTree(resultList, t) + + methods = append(methods, method) + methodsData[method] = resultList + } + + content["Data"] = methodsData + content["Methods"] = methods + return content +} + func printTree(resultList *[][]string, t *Tree) { for _, tr := range t.fixrouters { printTree(resultList, tr) diff --git a/beego.go b/beego.go index 7acf4a13..7a8db390 100644 --- a/beego.go +++ b/beego.go @@ -23,7 +23,7 @@ import ( const ( // VERSION represent beego web framework version. - VERSION = "1.8.1" + VERSION = "1.8.2" // DEV is for develop DEV = "dev" diff --git a/config/env/env.go b/config/env/env.go index a819e51a..34f094fe 100644 --- a/config/env/env.go +++ b/config/env/env.go @@ -12,6 +12,8 @@ // 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 env is used to parse environment. package env import ( diff --git a/config/ini.go b/config/ini.go index 3572b955..a681bc1b 100644 --- a/config/ini.go +++ b/config/ini.go @@ -325,7 +325,10 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) { // Get section or key comments. Fixed #1607 getCommentStr := func(section, key string) string { - comment, ok := "", false + var ( + comment string + ok bool + ) if len(key) == 0 { comment, ok = c.sectionComment[section] } else { diff --git a/context/acceptencoder.go b/context/acceptencoder.go index 350b560d..b4e2492c 100644 --- a/context/acceptencoder.go +++ b/context/acceptencoder.go @@ -39,6 +39,7 @@ var ( getMethodOnly bool ) +// InitGzip init the gzipcompress func InitGzip(minLength, compressLevel int, methods []string) { if minLength >= 0 { gzipMinLength = minLength diff --git a/grace/server.go b/grace/server.go index c71193ad..b8242335 100644 --- a/grace/server.go +++ b/grace/server.go @@ -291,7 +291,7 @@ func (srv *Server) fork() (err error) { // RegisterSignalHook registers a function to be run PreSignal or PostSignal for a given signal. func (srv *Server) RegisterSignalHook(ppFlag int, sig os.Signal, f func()) (err error) { if ppFlag != PreSignal && ppFlag != PostSignal { - err = fmt.Errorf("Invalid ppFlag argument. Must be either grace.PreSignal or grace.PostSignal.") + err = fmt.Errorf("Invalid ppFlag argument. Must be either grace.PreSignal or grace.PostSignal") return } for _, s := range hookableSignals { @@ -300,6 +300,6 @@ func (srv *Server) RegisterSignalHook(ppFlag int, sig os.Signal, f func()) (err return } } - err = fmt.Errorf("Signal '%v' is not supported.", sig) + err = fmt.Errorf("Signal '%v' is not supported", sig) return } diff --git a/hooks.go b/hooks.go index 091ecbc7..0fddc82f 100644 --- a/hooks.go +++ b/hooks.go @@ -55,9 +55,9 @@ func registerSession() error { conf.ProviderConfig = filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig) conf.DisableHTTPOnly = BConfig.WebConfig.Session.SessionDisableHTTPOnly conf.Domain = BConfig.WebConfig.Session.SessionDomain - conf.EnableSidInHttpHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader - conf.SessionNameInHttpHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader - conf.EnableSidInUrlQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery + conf.EnableSidInHTTPHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader + conf.SessionNameInHTTPHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader + conf.EnableSidInURLQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery } else { if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil { return err diff --git a/httplib/README.md b/httplib/README.md index 6a72cf7c..97df8e6b 100644 --- a/httplib/README.md +++ b/httplib/README.md @@ -32,7 +32,7 @@ The default timeout is `60` seconds, function prototype: SetTimeout(connectTimeout, readWriteTimeout time.Duration) -Exmaple: +Example: // GET httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) diff --git a/httplib/httplib.go b/httplib/httplib.go index 32edc5c4..4fd572d6 100644 --- a/httplib/httplib.go +++ b/httplib/httplib.go @@ -344,7 +344,7 @@ func (b *BeegoHTTPRequest) buildURL(paramBody string) { } // build POST/PUT/PATCH url and body - if (b.req.Method == "POST" || b.req.Method == "PUT" || b.req.Method == "PATCH") && b.req.Body == nil { + if (b.req.Method == "POST" || b.req.Method == "PUT" || b.req.Method == "PATCH" || b.req.Method == "DELETE") && b.req.Body == nil { // with files if len(b.files) > 0 { pr, pw := io.Pipe() @@ -520,9 +520,9 @@ func (b *BeegoHTTPRequest) Bytes() ([]byte, error) { return nil, err } b.body, err = ioutil.ReadAll(reader) - } else { - b.body, err = ioutil.ReadAll(resp.Body) + return b.body, err } + b.body, err = ioutil.ReadAll(resp.Body) return b.body, err } diff --git a/httplib/httplib_test.go b/httplib/httplib_test.go index 05815054..32d3e7f6 100644 --- a/httplib/httplib_test.go +++ b/httplib/httplib_test.go @@ -102,6 +102,14 @@ func TestSimpleDelete(t *testing.T) { t.Log(str) } +func TestSimpleDeleteParam(t *testing.T) { + str, err := Delete("http://httpbin.org/delete").Param("key", "val").String() + if err != nil { + t.Fatal(err) + } + t.Log(str) +} + func TestWithCookie(t *testing.T) { v := "smallfish" str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String() diff --git a/logs/alils/alils.go b/logs/alils/alils.go index 30a09243..867ff4cb 100644 --- a/logs/alils/alils.go +++ b/logs/alils/alils.go @@ -2,19 +2,23 @@ package alils import ( "encoding/json" - "github.com/astaxie/beego/logs" - "github.com/gogo/protobuf/proto" "strings" "sync" "time" + + "github.com/astaxie/beego/logs" + "github.com/gogo/protobuf/proto" ) const ( - CacheSize int = 64 + // CacheSize set the flush size + CacheSize int = 64 + // Delimiter define the topic delimiter Delimiter string = "##" ) -type AliLSConfig struct { +// Config is the Config for Ali Log +type Config struct { Project string `json:"project"` Endpoint string `json:"endpoint"` KeyID string `json:"key_id"` @@ -34,18 +38,17 @@ type aliLSWriter struct { withMap bool groupMap map[string]*LogGroup lock *sync.Mutex - AliLSConfig + Config } -// 创建提供Logger接口的日志服务 +// NewAliLS create a new Logger func NewAliLS() logs.Logger { alils := new(aliLSWriter) alils.Level = logs.LevelTrace return alils } -// 读取配置 -// 初始化必要的数据结构 +// Init parse config and init struct func (c *aliLSWriter) Init(jsonConfig string) (err error) { json.Unmarshal([]byte(jsonConfig), c) @@ -54,28 +57,26 @@ func (c *aliLSWriter) Init(jsonConfig string) (err error) { c.FlushWhen = CacheSize } - // 初始化Project prj := &LogProject{ Name: c.Project, Endpoint: c.Endpoint, - AccessKeyId: c.KeyID, + AccessKeyID: c.KeyID, AccessKeySecret: c.KeySecret, } - // 获取logstore c.store, err = prj.GetLogStore(c.LogStore) if err != nil { return err } - // 创建默认Log Group + // Create default Log Group c.group = append(c.group, &LogGroup{ Topic: proto.String(""), Source: proto.String(c.Source), Logs: make([]*Log, 0, c.FlushWhen), }) - // 创建其它Log Group + // Create other Log Group c.groupMap = make(map[string]*LogGroup) for _, topic := range c.Topics { @@ -113,7 +114,7 @@ func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error var lg *LogGroup if c.withMap { - // 解析出Topic,并匹配LogGroup + // Topic,LogGroup strs := strings.SplitN(msg, Delimiter, 2) if len(strs) == 2 { pos := strings.LastIndex(strs[0], " ") @@ -122,27 +123,24 @@ func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error lg = c.groupMap[topic] } - // 默认发到空Topic + // send to empty Topic if lg == nil { - topic = "" content = msg lg = c.group[0] } } else { - topic = "" content = msg lg = c.group[0] } - // 生成日志 - c1 := &Log_Content{ + c1 := &LogContent{ Key: proto.String("msg"), Value: proto.String(content), } l := &Log{ - Time: proto.Uint32(uint32(when.Unix())), // 填写日志时间 - Contents: []*Log_Content{ + Time: proto.Uint32(uint32(when.Unix())), + Contents: []*LogContent{ c1, }, } @@ -151,7 +149,6 @@ func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error lg.Logs = append(lg.Logs, l) c.lock.Unlock() - // 满足条件则Flush if len(lg.Logs) >= c.FlushWhen { c.flush(lg) } @@ -162,7 +159,7 @@ func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error // Flush implementing method. empty. func (c *aliLSWriter) Flush() { - // flush所有group + // flush all group for _, lg := range c.group { c.flush(lg) } @@ -176,9 +173,6 @@ func (c *aliLSWriter) flush(lg *LogGroup) { c.lock.Lock() defer c.lock.Unlock() - - // 把以上的LogGroup推送到SLS服务器, - // SLS服务器会根据该logstore的shard个数自动进行负载均衡。 err := c.store.PutLogs(lg) if err != nil { return diff --git a/logs/alils/log.pb.go b/logs/alils/log.pb.go index b60ca200..601b0d78 100755 --- a/logs/alils/log.pb.go +++ b/logs/alils/log.pb.go @@ -1,30 +1,43 @@ package alils -import "github.com/gogo/protobuf/proto" -import "fmt" -import "math" +import ( + "fmt" + "io" + "math" -// discarding unused import gogoproto "." - -import github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" - -import "io" + "github.com/gogo/protobuf/proto" + github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal var _ = fmt.Errorf var _ = math.Inf +var ( + // ErrInvalidLengthLog invalid proto + ErrInvalidLengthLog = fmt.Errorf("proto: negative length found during unmarshaling") + // ErrIntOverflowLog overflow + ErrIntOverflowLog = fmt.Errorf("proto: integer overflow") +) + +// Log define the proto Log type Log struct { - Time *uint32 `protobuf:"varint,1,req,name=Time" json:"Time,omitempty"` - Contents []*Log_Content `protobuf:"bytes,2,rep,name=Contents" json:"Contents,omitempty"` - XXX_unrecognized []byte `json:"-"` + Time *uint32 `protobuf:"varint,1,req,name=Time" json:"Time,omitempty"` + Contents []*LogContent `protobuf:"bytes,2,rep,name=Contents" json:"Contents,omitempty"` + XXXUnrecognized []byte `json:"-"` } -func (m *Log) Reset() { *m = Log{} } -func (m *Log) String() string { return proto.CompactTextString(m) } -func (*Log) ProtoMessage() {} +// Reset the Log +func (m *Log) Reset() { *m = Log{} } +// String return the Compact Log +func (m *Log) String() string { return proto.CompactTextString(m) } + +// ProtoMessage not implemented +func (*Log) ProtoMessage() {} + +// GetTime return the Log's Time func (m *Log) GetTime() uint32 { if m != nil && m.Time != nil { return *m.Time @@ -32,49 +45,65 @@ func (m *Log) GetTime() uint32 { return 0 } -func (m *Log) GetContents() []*Log_Content { +// GetContents return the Log's Contents +func (m *Log) GetContents() []*LogContent { if m != nil { return m.Contents } return nil } -type Log_Content struct { - Key *string `protobuf:"bytes,1,req,name=Key" json:"Key,omitempty"` - Value *string `protobuf:"bytes,2,req,name=Value" json:"Value,omitempty"` - XXX_unrecognized []byte `json:"-"` +// LogContent define the Log content struct +type LogContent struct { + Key *string `protobuf:"bytes,1,req,name=Key" json:"Key,omitempty"` + Value *string `protobuf:"bytes,2,req,name=Value" json:"Value,omitempty"` + XXXUnrecognized []byte `json:"-"` } -func (m *Log_Content) Reset() { *m = Log_Content{} } -func (m *Log_Content) String() string { return proto.CompactTextString(m) } -func (*Log_Content) ProtoMessage() {} +// Reset LogContent +func (m *LogContent) Reset() { *m = LogContent{} } -func (m *Log_Content) GetKey() string { +// String return the compact text +func (m *LogContent) String() string { return proto.CompactTextString(m) } + +// ProtoMessage not implemented +func (*LogContent) ProtoMessage() {} + +// GetKey return the Key +func (m *LogContent) GetKey() string { if m != nil && m.Key != nil { return *m.Key } return "" } -func (m *Log_Content) GetValue() string { +// GetValue return the Value +func (m *LogContent) GetValue() string { if m != nil && m.Value != nil { return *m.Value } return "" } +// LogGroup define the logs struct type LogGroup struct { - Logs []*Log `protobuf:"bytes,1,rep,name=Logs" json:"Logs,omitempty"` - Reserved *string `protobuf:"bytes,2,opt,name=Reserved" json:"Reserved,omitempty"` - Topic *string `protobuf:"bytes,3,opt,name=Topic" json:"Topic,omitempty"` - Source *string `protobuf:"bytes,4,opt,name=Source" json:"Source,omitempty"` - XXX_unrecognized []byte `json:"-"` + Logs []*Log `protobuf:"bytes,1,rep,name=Logs" json:"Logs,omitempty"` + Reserved *string `protobuf:"bytes,2,opt,name=Reserved" json:"Reserved,omitempty"` + Topic *string `protobuf:"bytes,3,opt,name=Topic" json:"Topic,omitempty"` + Source *string `protobuf:"bytes,4,opt,name=Source" json:"Source,omitempty"` + XXXUnrecognized []byte `json:"-"` } -func (m *LogGroup) Reset() { *m = LogGroup{} } -func (m *LogGroup) String() string { return proto.CompactTextString(m) } -func (*LogGroup) ProtoMessage() {} +// Reset LogGroup +func (m *LogGroup) Reset() { *m = LogGroup{} } +// String return the compact text +func (m *LogGroup) String() string { return proto.CompactTextString(m) } + +// ProtoMessage not implemented +func (*LogGroup) ProtoMessage() {} + +// GetLogs return the loggroup logs func (m *LogGroup) GetLogs() []*Log { if m != nil { return m.Logs @@ -82,6 +111,7 @@ func (m *LogGroup) GetLogs() []*Log { return nil } +// GetReserved return Reserved func (m *LogGroup) GetReserved() string { if m != nil && m.Reserved != nil { return *m.Reserved @@ -89,6 +119,7 @@ func (m *LogGroup) GetReserved() string { return "" } +// GetTopic return Topic func (m *LogGroup) GetTopic() string { if m != nil && m.Topic != nil { return *m.Topic @@ -96,6 +127,7 @@ func (m *LogGroup) GetTopic() string { return "" } +// GetSource return Source func (m *LogGroup) GetSource() string { if m != nil && m.Source != nil { return *m.Source @@ -103,15 +135,22 @@ func (m *LogGroup) GetSource() string { return "" } +// LogGroupList define the LogGroups type LogGroupList struct { - LogGroups []*LogGroup `protobuf:"bytes,1,rep,name=logGroups" json:"logGroups,omitempty"` - XXX_unrecognized []byte `json:"-"` + LogGroups []*LogGroup `protobuf:"bytes,1,rep,name=logGroups" json:"logGroups,omitempty"` + XXXUnrecognized []byte `json:"-"` } -func (m *LogGroupList) Reset() { *m = LogGroupList{} } -func (m *LogGroupList) String() string { return proto.CompactTextString(m) } -func (*LogGroupList) ProtoMessage() {} +// Reset LogGroupList +func (m *LogGroupList) Reset() { *m = LogGroupList{} } +// String return compact text +func (m *LogGroupList) String() string { return proto.CompactTextString(m) } + +// ProtoMessage not implemented +func (*LogGroupList) ProtoMessage() {} + +// GetLogGroups return the LogGroups func (m *LogGroupList) GetLogGroups() []*LogGroup { if m != nil { return m.LogGroups @@ -119,6 +158,7 @@ func (m *LogGroupList) GetLogGroups() []*LogGroup { return nil } +// Marshal the logs to byte slice func (m *Log) Marshal() (data []byte, err error) { size := m.Size() data = make([]byte, size) @@ -129,6 +169,7 @@ func (m *Log) Marshal() (data []byte, err error) { return data[:n], nil } +// MarshalTo data func (m *Log) MarshalTo(data []byte) (int, error) { var i int _ = i @@ -136,11 +177,10 @@ func (m *Log) MarshalTo(data []byte) (int, error) { _ = l if m.Time == nil { return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("Time") - } else { - data[i] = 0x8 - i++ - i = encodeVarintLog(data, i, uint64(*m.Time)) } + data[i] = 0x8 + i++ + i = encodeVarintLog(data, i, uint64(*m.Time)) if len(m.Contents) > 0 { for _, msg := range m.Contents { data[i] = 0x12 @@ -153,13 +193,14 @@ func (m *Log) MarshalTo(data []byte) (int, error) { i += n } } - if m.XXX_unrecognized != nil { - i += copy(data[i:], m.XXX_unrecognized) + if m.XXXUnrecognized != nil { + i += copy(data[i:], m.XXXUnrecognized) } return i, nil } -func (m *Log_Content) Marshal() (data []byte, err error) { +// Marshal LogContent +func (m *LogContent) Marshal() (data []byte, err error) { size := m.Size() data = make([]byte, size) n, err := m.MarshalTo(data) @@ -169,33 +210,34 @@ func (m *Log_Content) Marshal() (data []byte, err error) { return data[:n], nil } -func (m *Log_Content) MarshalTo(data []byte) (int, error) { +// MarshalTo logcontent to data +func (m *LogContent) MarshalTo(data []byte) (int, error) { var i int _ = i var l int _ = l if m.Key == nil { return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("Key") - } else { - data[i] = 0xa - i++ - i = encodeVarintLog(data, i, uint64(len(*m.Key))) - i += copy(data[i:], *m.Key) } + data[i] = 0xa + i++ + i = encodeVarintLog(data, i, uint64(len(*m.Key))) + i += copy(data[i:], *m.Key) + if m.Value == nil { return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("Value") - } else { - data[i] = 0x12 - i++ - i = encodeVarintLog(data, i, uint64(len(*m.Value))) - i += copy(data[i:], *m.Value) } - if m.XXX_unrecognized != nil { - i += copy(data[i:], m.XXX_unrecognized) + data[i] = 0x12 + i++ + i = encodeVarintLog(data, i, uint64(len(*m.Value))) + i += copy(data[i:], *m.Value) + if m.XXXUnrecognized != nil { + i += copy(data[i:], m.XXXUnrecognized) } return i, nil } +// Marshal LogGroup func (m *LogGroup) Marshal() (data []byte, err error) { size := m.Size() data = make([]byte, size) @@ -206,6 +248,7 @@ func (m *LogGroup) Marshal() (data []byte, err error) { return data[:n], nil } +// MarshalTo LogGroup to data func (m *LogGroup) MarshalTo(data []byte) (int, error) { var i int _ = i @@ -241,12 +284,13 @@ func (m *LogGroup) MarshalTo(data []byte) (int, error) { i = encodeVarintLog(data, i, uint64(len(*m.Source))) i += copy(data[i:], *m.Source) } - if m.XXX_unrecognized != nil { - i += copy(data[i:], m.XXX_unrecognized) + if m.XXXUnrecognized != nil { + i += copy(data[i:], m.XXXUnrecognized) } return i, nil } +// Marshal LogGroupList func (m *LogGroupList) Marshal() (data []byte, err error) { size := m.Size() data = make([]byte, size) @@ -257,6 +301,7 @@ func (m *LogGroupList) Marshal() (data []byte, err error) { return data[:n], nil } +// MarshalTo LogGroupList to data func (m *LogGroupList) MarshalTo(data []byte) (int, error) { var i int _ = i @@ -274,8 +319,8 @@ func (m *LogGroupList) MarshalTo(data []byte) (int, error) { i += n } } - if m.XXX_unrecognized != nil { - i += copy(data[i:], m.XXX_unrecognized) + if m.XXXUnrecognized != nil { + i += copy(data[i:], m.XXXUnrecognized) } return i, nil } @@ -307,6 +352,8 @@ func encodeVarintLog(data []byte, offset int, v uint64) int { data[offset] = uint8(v) return offset + 1 } + +// Size return the log's size func (m *Log) Size() (n int) { var l int _ = l @@ -319,13 +366,14 @@ func (m *Log) Size() (n int) { n += 1 + l + sovLog(uint64(l)) } } - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) + if m.XXXUnrecognized != nil { + n += len(m.XXXUnrecognized) } return n } -func (m *Log_Content) Size() (n int) { +// Size return LogContent size based on Key and Value +func (m *LogContent) Size() (n int) { var l int _ = l if m.Key != nil { @@ -336,12 +384,13 @@ func (m *Log_Content) Size() (n int) { l = len(*m.Value) n += 1 + l + sovLog(uint64(l)) } - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) + if m.XXXUnrecognized != nil { + n += len(m.XXXUnrecognized) } return n } +// Size return LogGroup size based on Logs func (m *LogGroup) Size() (n int) { var l int _ = l @@ -363,12 +412,13 @@ func (m *LogGroup) Size() (n int) { l = len(*m.Source) n += 1 + l + sovLog(uint64(l)) } - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) + if m.XXXUnrecognized != nil { + n += len(m.XXXUnrecognized) } return n } +// Size return LogGroupList size func (m *LogGroupList) Size() (n int) { var l int _ = l @@ -378,8 +428,8 @@ func (m *LogGroupList) Size() (n int) { n += 1 + l + sovLog(uint64(l)) } } - if m.XXX_unrecognized != nil { - n += len(m.XXX_unrecognized) + if m.XXXUnrecognized != nil { + n += len(m.XXXUnrecognized) } return n } @@ -397,6 +447,8 @@ func sovLog(x uint64) (n int) { func sozLog(x uint64) (n int) { return sovLog((x << 1) ^ (x >> 63)) } + +// Unmarshal data to log func (m *Log) Unmarshal(data []byte) error { var hasFields [1]uint64 l := len(data) @@ -474,7 +526,7 @@ func (m *Log) Unmarshal(data []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Contents = append(m.Contents, &Log_Content{}) + m.Contents = append(m.Contents, &LogContent{}) if err := m.Contents[len(m.Contents)-1].Unmarshal(data[iNdEx:postIndex]); err != nil { return err } @@ -491,7 +543,7 @@ func (m *Log) Unmarshal(data []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } - m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...) + m.XXXUnrecognized = append(m.XXXUnrecognized, data[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } @@ -504,7 +556,9 @@ func (m *Log) Unmarshal(data []byte) error { } return nil } -func (m *Log_Content) Unmarshal(data []byte) error { + +// Unmarshal data to LogContent +func (m *LogContent) Unmarshal(data []byte) error { var hasFields [1]uint64 l := len(data) iNdEx := 0 @@ -608,7 +662,7 @@ func (m *Log_Content) Unmarshal(data []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } - m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...) + m.XXXUnrecognized = append(m.XXXUnrecognized, data[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } @@ -624,6 +678,8 @@ func (m *Log_Content) Unmarshal(data []byte) error { } return nil } + +// Unmarshal data to LogGroup func (m *LogGroup) Unmarshal(data []byte) error { l := len(data) iNdEx := 0 @@ -786,7 +842,7 @@ func (m *LogGroup) Unmarshal(data []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } - m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...) + m.XXXUnrecognized = append(m.XXXUnrecognized, data[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } @@ -796,6 +852,8 @@ func (m *LogGroup) Unmarshal(data []byte) error { } return nil } + +// Unmarshal data to LogGroupList func (m *LogGroupList) Unmarshal(data []byte) error { l := len(data) iNdEx := 0 @@ -868,7 +926,7 @@ func (m *LogGroupList) Unmarshal(data []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } - m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...) + m.XXXUnrecognized = append(m.XXXUnrecognized, data[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } @@ -878,6 +936,7 @@ func (m *LogGroupList) Unmarshal(data []byte) error { } return nil } + func skipLog(data []byte) (n int, err error) { l := len(data) iNdEx := 0 @@ -940,7 +999,7 @@ func skipLog(data []byte) (n int, err error) { case 3: for { var innerWire uint64 - var start int = iNdEx + var start = iNdEx for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLog @@ -977,8 +1036,3 @@ func skipLog(data []byte) (n int, err error) { } panic("unreachable") } - -var ( - ErrInvalidLengthLog = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowLog = fmt.Errorf("proto: integer overflow") -) diff --git a/logs/alils/log_config.go b/logs/alils/log_config.go index 41fa0959..e8564efb 100755 --- a/logs/alils/log_config.go +++ b/logs/alils/log_config.go @@ -1,5 +1,6 @@ package alils +// InputDetail define log detail type InputDetail struct { LogType string `json:"logType"` LogPath string `json:"logPath"` @@ -14,11 +15,13 @@ type InputDetail struct { TopicFormat string `json:"topicFormat"` } +// OutputDetail define the output detail type OutputDetail struct { Endpoint string `json:"endpoint"` LogStoreName string `json:"logstoreName"` } +// LogConfig define Log Config type LogConfig struct { Name string `json:"configName"` InputType string `json:"inputType"` diff --git a/logs/alils/log_project.go b/logs/alils/log_project.go index 63ab07f8..59db8cbf 100755 --- a/logs/alils/log_project.go +++ b/logs/alils/log_project.go @@ -1,5 +1,5 @@ /* -Package sls implements the SDK(v0.5.0) of Simple Log Service(abbr. SLS). +Package alils implements the SDK(v0.5.0) of Simple Log Service(abbr. SLS). For more description about SLS, please read this article: http://gitlab.alibaba-inc.com/sls/doc. @@ -20,19 +20,20 @@ type errorMessage struct { Message string `json:"errorMessage"` } +// LogProject Define the Ali Project detail type LogProject struct { Name string // Project name Endpoint string // IP or hostname of SLS endpoint - AccessKeyId string + AccessKeyID string AccessKeySecret string } // NewLogProject creates a new SLS project. -func NewLogProject(name, endpoint, accessKeyId, accessKeySecret string) (p *LogProject, err error) { +func NewLogProject(name, endpoint, AccessKeyID, accessKeySecret string) (p *LogProject, err error) { p = &LogProject{ Name: name, Endpoint: endpoint, - AccessKeyId: accessKeyId, + AccessKeyID: AccessKeyID, AccessKeySecret: accessKeySecret, } return p, nil diff --git a/logs/alils/log_store.go b/logs/alils/log_store.go index 009e39c4..fa502736 100755 --- a/logs/alils/log_store.go +++ b/logs/alils/log_store.go @@ -12,6 +12,7 @@ import ( "github.com/gogo/protobuf/proto" ) +// LogStore Store the logs type LogStore struct { Name string `json:"logstoreName"` TTL int @@ -23,6 +24,7 @@ type LogStore struct { project *LogProject } +// Shard define the Log Shard type Shard struct { ShardID int `json:"shardID"` } @@ -116,16 +118,16 @@ func (s *LogStore) PutLogs(lg *LogGroup) (err error) { return } -// GetCursor gets log cursor of one shard specified by shardId. +// GetCursor gets log cursor of one shard specified by shardID. // The from can be in three form: a) unix timestamp in seccond, b) "begin", c) "end". // For more detail please read: http://gitlab.alibaba-inc.com/sls/doc/blob/master/api/shard.md#logstore -func (s *LogStore) GetCursor(shardId int, from string) (cursor string, err error) { +func (s *LogStore) GetCursor(shardID int, from string) (cursor string, err error) { h := map[string]string{ "x-sls-bodyrawsize": "0", } uri := fmt.Sprintf("/logstores/%v/shards/%v?type=cursor&from=%v", - s.Name, shardId, from) + s.Name, shardID, from) r, err := request(s.project, "GET", uri, h, nil) if err != nil { @@ -163,10 +165,10 @@ func (s *LogStore) GetCursor(shardId int, from string) (cursor string, err error return } -// GetLogsBytes gets logs binary data from shard specified by shardId according cursor. +// GetLogsBytes gets logs binary data from shard specified by shardID according cursor. // The logGroupMaxCount is the max number of logGroup could be returned. // The nextCursor is the next curosr can be used to read logs at next time. -func (s *LogStore) GetLogsBytes(shardId int, cursor string, +func (s *LogStore) GetLogsBytes(shardID int, cursor string, logGroupMaxCount int) (out []byte, nextCursor string, err error) { h := map[string]string{ @@ -176,7 +178,7 @@ func (s *LogStore) GetLogsBytes(shardId int, cursor string, } uri := fmt.Sprintf("/logstores/%v/shards/%v?type=logs&cursor=%v&count=%v", - s.Name, shardId, cursor, logGroupMaxCount) + s.Name, shardID, cursor, logGroupMaxCount) r, err := request(s.project, "GET", uri, h, nil) if err != nil { @@ -249,13 +251,13 @@ func LogsBytesDecode(data []byte) (gl *LogGroupList, err error) { return } -// GetLogs gets logs from shard specified by shardId according cursor. +// GetLogs gets logs from shard specified by shardID according cursor. // The logGroupMaxCount is the max number of logGroup could be returned. // The nextCursor is the next curosr can be used to read logs at next time. -func (s *LogStore) GetLogs(shardId int, cursor string, +func (s *LogStore) GetLogs(shardID int, cursor string, logGroupMaxCount int) (gl *LogGroupList, nextCursor string, err error) { - out, nextCursor, err := s.GetLogsBytes(shardId, cursor, logGroupMaxCount) + out, nextCursor, err := s.GetLogsBytes(shardID, cursor, logGroupMaxCount) if err != nil { return } diff --git a/logs/alils/machine_group.go b/logs/alils/machine_group.go index 7a0aace1..b6c69a14 100755 --- a/logs/alils/machine_group.go +++ b/logs/alils/machine_group.go @@ -8,18 +8,20 @@ import ( "net/http/httputil" ) -type MachinGroupAttribute struct { +// MachineGroupAttribute define the Attribute +type MachineGroupAttribute struct { ExternalName string `json:"externalName"` TopicName string `json:"groupTopic"` } +// MachineGroup define the machine Group type MachineGroup struct { Name string `json:"groupName"` Type string `json:"groupType"` - MachineIdType string `json:"machineIdentifyType"` - MachineIdList []string `json:"machineList"` + MachineIDType string `json:"machineIdentifyType"` + MachineIDList []string `json:"machineList"` - Attribute MachinGroupAttribute `json:"groupAttribute"` + Attribute MachineGroupAttribute `json:"groupAttribute"` CreateTime uint32 LastModifyTime uint32 @@ -27,12 +29,14 @@ type MachineGroup struct { project *LogProject } +// Machine define the Machine type Machine struct { IP string - UniqueId string `json:"machine-uniqueid"` - UserdefinedId string `json:"userdefined-id"` + UniqueID string `json:"machine-uniqueid"` + UserdefinedID string `json:"userdefined-id"` } +// MachineList define the Machine List type MachineList struct { Total int Machines []*Machine diff --git a/logs/alils/request.go b/logs/alils/request.go index 20df45b4..50d9c43c 100755 --- a/logs/alils/request.go +++ b/logs/alils/request.go @@ -33,12 +33,12 @@ func request(project *LogProject, method, uri string, headers map[string]string, } // Calc Authorization - // Authorization = "SLS :" + // Authorization = "SLS :" digest, err := signature(project, method, uri, headers) if err != nil { return } - auth := fmt.Sprintf("SLS %v:%v", project.AccessKeyId, digest) + auth := fmt.Sprintf("SLS %v:%v", project.AccessKeyID, digest) headers["Authorization"] = auth // Initialize http request diff --git a/logs/color_windows.go b/logs/color_windows.go index deee4c87..4e28f188 100644 --- a/logs/color_windows.go +++ b/logs/color_windows.go @@ -361,7 +361,7 @@ func isParameterChar(b byte) bool { } func (cw *ansiColorWriter) Write(p []byte) (int, error) { - r, nw, first, last := 0, 0, 0, 0 + var r, nw, first, last int if cw.mode != DiscardNonColorEscSeq { cw.state = outsideCsiCode cw.resetBuffer() diff --git a/logs/file.go b/logs/file.go index 32595ec4..1c2db882 100644 --- a/logs/file.go +++ b/logs/file.go @@ -170,7 +170,7 @@ func (w *fileLogWriter) initFd() error { fd := w.fileWriter fInfo, err := fd.Stat() if err != nil { - return fmt.Errorf("get stat err: %s\n", err) + return fmt.Errorf("get stat err: %s", err) } w.maxSizeCurSize = int(fInfo.Size()) w.dailyOpenTime = time.Now() @@ -259,7 +259,7 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error { } // return error if the last file checked still existed if err == nil { - return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.Filename) + return fmt.Errorf("Rotate: Cannot find free log number to rename %s", w.Filename) } // close fileWriter before rename @@ -268,6 +268,9 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error { // Rename the file to its new found name // even if occurs error,we MUST guarantee to restart new logger err = os.Rename(w.Filename, fName) + if err != nil { + goto RESTART_LOGGER + } err = os.Chmod(fName, os.FileMode(0440)) // re-start logger RESTART_LOGGER: @@ -276,13 +279,12 @@ RESTART_LOGGER: go w.deleteOldLog() if startLoggerErr != nil { - return fmt.Errorf("Rotate StartLogger: %s\n", startLoggerErr) + return fmt.Errorf("Rotate StartLogger: %s", startLoggerErr) } if err != nil { - return fmt.Errorf("Rotate: %s\n", err) + return fmt.Errorf("Rotate: %s", err) } return nil - } func (w *fileLogWriter) deleteOldLog() { diff --git a/logs/log.go b/logs/log.go index a5e0a9ea..0e97a70e 100644 --- a/logs/log.go +++ b/logs/log.go @@ -492,9 +492,9 @@ func (bl *BeeLogger) flush() { } // beeLogger references the used application logger. -var beeLogger *BeeLogger = NewLogger() +var beeLogger = NewLogger() -// GetLogger returns the default BeeLogger +// GetBeeLogger returns the default BeeLogger func GetBeeLogger() *BeeLogger { return beeLogger } @@ -534,6 +534,7 @@ func Reset() { beeLogger.Reset() } +// Async set the beelogger with Async mode and hold msglen messages func Async(msgLen ...int64) *BeeLogger { return beeLogger.Async(msgLen...) } diff --git a/logs/logger.go b/logs/logger.go index e0abfdc4..b5d7255f 100644 --- a/logs/logger.go +++ b/logs/logger.go @@ -139,6 +139,11 @@ var ( reset = string([]byte{27, 91, 48, 109}) ) +// ColorByStatus return color by http code +// 2xx return Green +// 3xx return White +// 4xx return Yellow +// 5xx return Red func ColorByStatus(cond bool, code int) string { switch { case code >= 200 && code < 300: @@ -152,6 +157,14 @@ func ColorByStatus(cond bool, code int) string { } } +// ColorByMethod return color by http code +// GET return Blue +// POST return Cyan +// PUT return Yellow +// DELETE return Red +// PATCH return Green +// HEAD return Magenta +// OPTIONS return WHITE func ColorByMethod(cond bool, method string) string { switch method { case "GET": @@ -173,10 +186,10 @@ func ColorByMethod(cond bool, method string) string { } } -// Guard Mutex to guarantee atomicity of W32Debug(string) function +// Guard Mutex to guarantee atomic of W32Debug(string) function var mu sync.Mutex -// Helper method to output colored logs in Windows terminals +// W32Debug Helper method to output colored logs in Windows terminals func W32Debug(msg string) { mu.Lock() defer mu.Unlock() diff --git a/orm/db.go b/orm/db.go index e9508ee0..2a05797a 100644 --- a/orm/db.go +++ b/orm/db.go @@ -507,10 +507,9 @@ func (d *dbBase) InsertOrUpdate(q dbQuerier, mi *modelInfo, ind reflect.Value, a case DRPostgres: if len(args) == 0 { return 0, fmt.Errorf("`%s` use InsertOrUpdate must have a conflict column", a.DriverName) - } else { - args0 = strings.ToLower(args[0]) - iouStr = fmt.Sprintf("ON CONFLICT (%s) DO UPDATE SET", args0) } + args0 = strings.ToLower(args[0]) + iouStr = fmt.Sprintf("ON CONFLICT (%s) DO UPDATE SET", args0) default: return 0, fmt.Errorf("`%s` nonsupport InsertOrUpdate in beego", a.DriverName) } @@ -1110,7 +1109,7 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition // generate sql with replacing operator string placeholders and replaced values. func (d *dbBase) GenerateOperatorSQL(mi *modelInfo, fi *fieldInfo, operator string, args []interface{}, tz *time.Location) (string, []interface{}) { - sql := "" + var sql string params := getFlatParams(fi, args, tz) if len(params) == 0 { @@ -1733,7 +1732,7 @@ func (d *dbBase) TableQuote() string { return "`" } -// replace value placeholer in parametered sql string. +// replace value placeholder in parametered sql string. func (d *dbBase) ReplaceMarks(query *string) { // default use `?` as mark, do nothing } diff --git a/orm/db_alias.go b/orm/db_alias.go index e70db9f0..c7089239 100644 --- a/orm/db_alias.go +++ b/orm/db_alias.go @@ -250,7 +250,7 @@ func RegisterDriver(driverName string, typ DriverType) error { drivers[driverName] = typ } else { if t != typ { - return fmt.Errorf("driverName `%s` db driver already registered and is other type\n", driverName) + return fmt.Errorf("driverName `%s` db driver already registered and is other type", driverName) } } return nil @@ -261,7 +261,7 @@ func SetDataBaseTZ(aliasName string, tz *time.Location) error { if al, ok := dataBaseCache.get(aliasName); ok { al.TZ = tz } else { - return fmt.Errorf("DataBase alias name `%s` not registered\n", aliasName) + return fmt.Errorf("DataBase alias name `%s` not registered", aliasName) } return nil } @@ -296,5 +296,5 @@ func GetDB(aliasNames ...string) (*sql.DB, error) { if ok { return al.DB, nil } - return nil, fmt.Errorf("DataBase of alias name `%s` not found\n", name) + return nil, fmt.Errorf("DataBase of alias name `%s` not found", name) } diff --git a/orm/db_mysql.go b/orm/db_mysql.go index 1016de2b..51185563 100644 --- a/orm/db_mysql.go +++ b/orm/db_mysql.go @@ -103,8 +103,7 @@ func (d *dbBaseMysql) IndexExists(db dbQuerier, table string, name string) bool // If no will insert // Add "`" for mysql sql building func (d *dbBaseMysql) InsertOrUpdate(q dbQuerier, mi *modelInfo, ind reflect.Value, a *alias, args ...string) (int64, error) { - - iouStr := "" + var iouStr string argsMap := map[string]string{} iouStr = "ON DUPLICATE KEY UPDATE" diff --git a/orm/orm.go b/orm/orm.go index d364e621..5db79386 100644 --- a/orm/orm.go +++ b/orm/orm.go @@ -420,7 +420,7 @@ func (o *orm) getRelQs(md interface{}, mi *modelInfo, fi *fieldInfo) *querySet { // table name can be string or struct. // e.g. QueryTable("user"), QueryTable(&user{}) or QueryTable((*User)(nil)), func (o *orm) QueryTable(ptrStructOrTableName interface{}) (qs QuerySeter) { - name := "" + var name string if table, ok := ptrStructOrTableName.(string); ok { name = snakeString(table) if mi, ok := modelCache.get(name); ok { diff --git a/orm/orm_raw.go b/orm/orm_raw.go index a968b1a1..1e86212a 100644 --- a/orm/orm_raw.go +++ b/orm/orm_raw.go @@ -671,7 +671,7 @@ func (o *rawSet) queryRowsTo(container interface{}, keyCol, valueCol string) (in ind *reflect.Value ) - typ := 0 + var typ int switch container.(type) { case *Params: typ = 1 diff --git a/orm/orm_test.go b/orm/orm_test.go index a8b4fe69..c5bfa8b9 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -135,7 +135,7 @@ func getCaller(skip int) string { if i := strings.LastIndex(funName, "."); i > -1 { funName = funName[i+1:] } - return fmt.Sprintf("%s:%d: \n%s", fn, line, strings.Join(codes, "\n")) + return fmt.Sprintf("%s:%s:%d: \n%s", fn, funName, line, strings.Join(codes, "\n")) } func throwFail(t *testing.T, err error, args ...interface{}) { @@ -1014,6 +1014,8 @@ func TestAll(t *testing.T) { var users3 []*User qs = dORM.QueryTable("user") num, err = qs.Filter("user_name", "nothing").All(&users3) + throwFailNow(t, err) + throwFailNow(t, AssertIs(num, 0)) throwFailNow(t, AssertIs(users3 == nil, false)) } @@ -1138,6 +1140,7 @@ func TestRelatedSel(t *testing.T) { } err = qs.Filter("user_name", "nobody").RelatedSel("profile").One(&user) + throwFail(t, err) throwFail(t, AssertIs(num, 1)) throwFail(t, AssertIs(user.Profile, nil)) @@ -1246,20 +1249,24 @@ func TestLoadRelated(t *testing.T) { num, err = dORM.LoadRelated(&user, "Posts", true) throwFailNow(t, err) + throwFailNow(t, AssertIs(num, 2)) throwFailNow(t, AssertIs(len(user.Posts), 2)) throwFailNow(t, AssertIs(user.Posts[0].User.UserName, "astaxie")) num, err = dORM.LoadRelated(&user, "Posts", true, 1) throwFailNow(t, err) + throwFailNow(t, AssertIs(num, 1)) throwFailNow(t, AssertIs(len(user.Posts), 1)) num, err = dORM.LoadRelated(&user, "Posts", true, 0, 0, "-Id") throwFailNow(t, err) + throwFailNow(t, AssertIs(num, 2)) throwFailNow(t, AssertIs(len(user.Posts), 2)) throwFailNow(t, AssertIs(user.Posts[0].Title, "Formatting")) num, err = dORM.LoadRelated(&user, "Posts", true, 1, 1, "Id") throwFailNow(t, err) + throwFailNow(t, AssertIs(num, 1)) throwFailNow(t, AssertIs(len(user.Posts), 1)) throwFailNow(t, AssertIs(user.Posts[0].Title, "Formatting")) @@ -1976,6 +1983,7 @@ func TestReadOrCreate(t *testing.T) { created, pk, err := dORM.ReadOrCreate(u, "UserName") throwFail(t, err) throwFail(t, AssertIs(created, true)) + throwFail(t, AssertIs(u.ID, pk)) throwFail(t, AssertIs(u.UserName, "Kyle")) throwFail(t, AssertIs(u.Email, "kylemcc@gmail.com")) throwFail(t, AssertIs(u.Password, "other_pass")) @@ -2130,13 +2138,13 @@ func TestUintPk(t *testing.T) { Name: name, } - created, pk, err := dORM.ReadOrCreate(u, "ID") + created, _, err := dORM.ReadOrCreate(u, "ID") throwFail(t, err) throwFail(t, AssertIs(created, true)) throwFail(t, AssertIs(u.Name, name)) nu := &UintPk{ID: 8} - created, pk, err = dORM.ReadOrCreate(nu, "ID") + created, pk, err := dORM.ReadOrCreate(nu, "ID") throwFail(t, err) throwFail(t, AssertIs(created, false)) throwFail(t, AssertIs(nu.ID, u.ID)) diff --git a/policy.go b/policy.go index 2b91fdcc..ab23f927 100644 --- a/policy.go +++ b/policy.go @@ -23,7 +23,7 @@ import ( // PolicyFunc defines a policy function which is invoked before the controller handler is executed. type PolicyFunc func(*context.Context) -// FindRouter Find Router info for URL +// FindPolicy Find Router info for URL func (p *ControllerRegister) FindPolicy(cont *context.Context) []PolicyFunc { var urlPath = cont.Input.URL() if !BConfig.RouterCaseSensitive { @@ -71,7 +71,7 @@ func (p *ControllerRegister) addToPolicy(method, pattern string, r ...PolicyFunc } } -// Register new policy in beego +// Policy Register new policy in beego func Policy(pattern, method string, policy ...PolicyFunc) { BeeApp.Handlers.addToPolicy(method, pattern, policy...) } diff --git a/session/couchbase/sess_couchbase.go b/session/couchbase/sess_couchbase.go index 791a6cc7..707d042c 100644 --- a/session/couchbase/sess_couchbase.go +++ b/session/couchbase/sess_couchbase.go @@ -162,7 +162,9 @@ func (cp *Provider) SessionRead(sid string) (session.Store, error) { ) err = cp.b.Get(sid, &doc) - if doc == nil { + if err != nil { + return nil, err + } else if doc == nil { kv = make(map[interface{}]interface{}) } else { kv, err = session.DecodeGob(doc) diff --git a/session/ledis/ledis_session.go b/session/ledis/ledis_session.go index 18b27708..77685d1e 100644 --- a/session/ledis/ledis_session.go +++ b/session/ledis/ledis_session.go @@ -113,13 +113,10 @@ func (lp *Provider) SessionInit(maxlifetime int64, savePath string) error { func (lp *Provider) SessionRead(sid string) (session.Store, error) { var ( kv map[interface{}]interface{} - kvs []byte err error ) - if kvs, err = c.Get([]byte(sid)); err != nil { - return nil, err - } + kvs, _ := c.Get([]byte(sid)) if len(kvs) == 0 { kv = make(map[interface{}]interface{}) diff --git a/session/redis/sess_redis.go b/session/redis/sess_redis.go index 08efa6e1..20dbe030 100644 --- a/session/redis/sess_redis.go +++ b/session/redis/sess_redis.go @@ -176,16 +176,12 @@ func (rp *Provider) SessionRead(sid string) (session.Store, error) { c := rp.poollist.Get() defer c.Close() - var ( - kv map[interface{}]interface{} - kvs string - err error - ) + var kv map[interface{}]interface{} - if kvs, err = redis.String(c.Do("GET", sid)); err != nil { + kvs, err := redis.String(c.Do("GET", sid)) + if err != redis.ErrNil { return nil, err } - if len(kvs) == 0 { kv = make(map[interface{}]interface{}) } else { diff --git a/session/sess_file.go b/session/sess_file.go index 12cf1f3f..3ca93d55 100644 --- a/session/sess_file.go +++ b/session/sess_file.go @@ -87,9 +87,16 @@ func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) { var f *os.File if err == nil { f, err = os.OpenFile(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR, 0777) + if err != nil { + SLogger.Println(err) + return + } } else if os.IsNotExist(err) { f, err = os.Create(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid)) - + if err != nil { + SLogger.Println(err) + return + } } else { return } diff --git a/session/sess_test.go b/session/sess_test.go index 60b47dd4..906abec2 100644 --- a/session/sess_test.go +++ b/session/sess_test.go @@ -74,8 +74,7 @@ func TestCookieEncodeDecode(t *testing.T) { if err != nil { t.Fatal("encodeCookie:", err) } - dst := make(map[interface{}]interface{}) - dst, err = decodeCookie(block, hashKey, securityName, str, 3600) + dst, err := decodeCookie(block, hashKey, securityName, str, 3600) if err != nil { t.Fatal("decodeCookie", err) } diff --git a/session/session.go b/session/session.go index fb4b2821..cf647521 100644 --- a/session/session.go +++ b/session/session.go @@ -81,6 +81,7 @@ func Register(name string, provide Provider) { provides[name] = provide } +// ManagerConfig define the session config type ManagerConfig struct { CookieName string `json:"cookieName"` EnableSetCookie bool `json:"enableSetCookie,omitempty"` @@ -92,9 +93,9 @@ type ManagerConfig struct { ProviderConfig string `json:"providerConfig"` Domain string `json:"domain"` SessionIDLength int64 `json:"sessionIDLength"` - EnableSidInHttpHeader bool `json:"enableSidInHttpHeader"` - SessionNameInHttpHeader string `json:"sessionNameInHttpHeader"` - EnableSidInUrlQuery bool `json:"enableSidInUrlQuery"` + EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"` + SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"` + EnableSidInURLQuery bool `json:"EnableSidInURLQuery"` } // Manager contains Provider and its configuration. @@ -125,14 +126,14 @@ func NewManager(provideName string, cf *ManagerConfig) (*Manager, error) { cf.Maxlifetime = cf.Gclifetime } - if cf.EnableSidInHttpHeader { - if cf.SessionNameInHttpHeader == "" { - panic(errors.New("SessionNameInHttpHeader is empty")) + if cf.EnableSidInHTTPHeader { + if cf.SessionNameInHTTPHeader == "" { + panic(errors.New("SessionNameInHTTPHeader is empty")) } - strMimeHeader := textproto.CanonicalMIMEHeaderKey(cf.SessionNameInHttpHeader) - if cf.SessionNameInHttpHeader != strMimeHeader { - strErrMsg := "SessionNameInHttpHeader (" + cf.SessionNameInHttpHeader + ") has the wrong format, it should be like this : " + strMimeHeader + strMimeHeader := textproto.CanonicalMIMEHeaderKey(cf.SessionNameInHTTPHeader) + if cf.SessionNameInHTTPHeader != strMimeHeader { + strErrMsg := "SessionNameInHTTPHeader (" + cf.SessionNameInHTTPHeader + ") has the wrong format, it should be like this : " + strMimeHeader panic(errors.New(strErrMsg)) } } @@ -163,7 +164,7 @@ func (manager *Manager) getSid(r *http.Request) (string, error) { cookie, errs := r.Cookie(manager.config.CookieName) if errs != nil || cookie.Value == "" { var sid string - if manager.config.EnableSidInUrlQuery { + if manager.config.EnableSidInURLQuery { errs := r.ParseForm() if errs != nil { return "", errs @@ -173,8 +174,8 @@ func (manager *Manager) getSid(r *http.Request) (string, error) { } // if not found in Cookie / param, then read it from request headers - if manager.config.EnableSidInHttpHeader && sid == "" { - sids, isFound := r.Header[manager.config.SessionNameInHttpHeader] + if manager.config.EnableSidInHTTPHeader && sid == "" { + sids, isFound := r.Header[manager.config.SessionNameInHTTPHeader] if isFound && len(sids) != 0 { return sids[0], nil } @@ -226,9 +227,9 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se } r.AddCookie(cookie) - if manager.config.EnableSidInHttpHeader { - r.Header.Set(manager.config.SessionNameInHttpHeader, sid) - w.Header().Set(manager.config.SessionNameInHttpHeader, sid) + if manager.config.EnableSidInHTTPHeader { + r.Header.Set(manager.config.SessionNameInHTTPHeader, sid) + w.Header().Set(manager.config.SessionNameInHTTPHeader, sid) } return @@ -236,9 +237,9 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se // SessionDestroy Destroy session by its id in http request cookie. func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request) { - if manager.config.EnableSidInHttpHeader { - r.Header.Del(manager.config.SessionNameInHttpHeader) - w.Header().Del(manager.config.SessionNameInHttpHeader) + if manager.config.EnableSidInHTTPHeader { + r.Header.Del(manager.config.SessionNameInHTTPHeader) + w.Header().Del(manager.config.SessionNameInHTTPHeader) } cookie, err := r.Cookie(manager.config.CookieName) @@ -306,9 +307,9 @@ func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Reque } r.AddCookie(cookie) - if manager.config.EnableSidInHttpHeader { - r.Header.Set(manager.config.SessionNameInHttpHeader, sid) - w.Header().Set(manager.config.SessionNameInHttpHeader, sid) + if manager.config.EnableSidInHTTPHeader { + r.Header.Set(manager.config.SessionNameInHTTPHeader, sid) + w.Header().Set(manager.config.SessionNameInHTTPHeader, sid) } return @@ -328,7 +329,7 @@ func (manager *Manager) sessionID() (string, error) { b := make([]byte, manager.config.SessionIDLength) n, err := rand.Read(b) if n != len(b) || err != nil { - return "", fmt.Errorf("Could not successfully read from the system CSPRNG.") + return "", fmt.Errorf("Could not successfully read from the system CSPRNG") } return hex.EncodeToString(b), nil } diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go index 03b60793..de0c6360 100644 --- a/session/ssdb/sess_ssdb.go +++ b/session/ssdb/sess_ssdb.go @@ -11,16 +11,17 @@ import ( "github.com/ssdb/gossdb/ssdb" ) -var ssdbProvider = &SsdbProvider{} +var ssdbProvider = &Provider{} -type SsdbProvider struct { +// Provider holds ssdb client and configs +type Provider struct { client *ssdb.Client host string port int maxLifetime int64 } -func (p *SsdbProvider) connectInit() error { +func (p *Provider) connectInit() error { var err error if p.host == "" || p.port == 0 { return errors.New("SessionInit First") @@ -29,7 +30,8 @@ func (p *SsdbProvider) connectInit() error { return err } -func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error { +// SessionInit init the ssdb with the config +func (p *Provider) SessionInit(maxLifetime int64, savePath string) error { p.maxLifetime = maxLifetime address := strings.Split(savePath, ":") p.host = address[0] @@ -41,7 +43,8 @@ func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error { return p.connectInit() } -func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) { +// SessionRead return a ssdb client session Store +func (p *Provider) SessionRead(sid string) (session.Store, error) { if p.client == nil { if err := p.connectInit(); err != nil { return nil, err @@ -64,7 +67,8 @@ func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) { return rs, nil } -func (p *SsdbProvider) SessionExist(sid string) bool { +// SessionExist judged whether sid is exist in session +func (p *Provider) SessionExist(sid string) bool { if p.client == nil { if err := p.connectInit(); err != nil { panic(err) @@ -80,7 +84,8 @@ func (p *SsdbProvider) SessionExist(sid string) bool { return true } -func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) { +// SessionRegenerate regenerate session with new sid and delete oldsid +func (p *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) { //conn.Do("setx", key, v, ttl) if p.client == nil { if err := p.connectInit(); err != nil { @@ -112,7 +117,8 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err return rs, nil } -func (p *SsdbProvider) SessionDestroy(sid string) error { +// SessionDestroy destroy the sid +func (p *Provider) SessionDestroy(sid string) error { if p.client == nil { if err := p.connectInit(); err != nil { return err @@ -122,13 +128,16 @@ func (p *SsdbProvider) SessionDestroy(sid string) error { return err } -func (p *SsdbProvider) SessionGC() { +// SessionGC not implemented +func (p *Provider) SessionGC() { } -func (p *SsdbProvider) SessionAll() int { +// SessionAll not implemented +func (p *Provider) SessionAll() int { return 0 } +// SessionStore holds the session information which stored in ssdb type SessionStore struct { sid string lock sync.RWMutex @@ -137,12 +146,15 @@ type SessionStore struct { client *ssdb.Client } +// Set the key and value func (s *SessionStore) Set(key, value interface{}) error { s.lock.Lock() defer s.lock.Unlock() s.values[key] = value return nil } + +// Get return the value by the key func (s *SessionStore) Get(key interface{}) interface{} { s.lock.Lock() defer s.lock.Unlock() @@ -152,30 +164,36 @@ func (s *SessionStore) Get(key interface{}) interface{} { return nil } +// Delete the key in session store func (s *SessionStore) Delete(key interface{}) error { s.lock.Lock() defer s.lock.Unlock() delete(s.values, key) return nil } + +// Flush delete all keys and values func (s *SessionStore) Flush() error { s.lock.Lock() defer s.lock.Unlock() s.values = make(map[interface{}]interface{}) return nil } + +// SessionID return the sessionID func (s *SessionStore) SessionID() string { return s.sid } +// SessionRelease Store the keyvalues into ssdb func (s *SessionStore) SessionRelease(w http.ResponseWriter) { b, err := session.EncodeGob(s.values) if err != nil { return } s.client.Do("setx", s.sid, string(b), s.maxLifetime) - } + func init() { session.Register("ssdb", ssdbProvider) } diff --git a/swagger/swagger.go b/swagger/swagger.go index e0ac5cf5..c687fb8e 100644 --- a/swagger/swagger.go +++ b/swagger/swagger.go @@ -100,7 +100,7 @@ type Parameter struct { Default interface{} `json:"default,omitempty" yaml:"default,omitempty"` } -// A limited subset of JSON-Schema's items object. It is used by parameter definitions that are not located in "body". +// ParameterItems A limited subset of JSON-Schema's items object. It is used by parameter definitions that are not located in "body". // http://swagger.io/specification/#itemsObject type ParameterItems struct { Type string `json:"type,omitempty" yaml:"type,omitempty"` diff --git a/template.go b/template.go index 104383c9..d4859cd7 100644 --- a/template.go +++ b/template.go @@ -307,8 +307,9 @@ func _getTemplate(t0 *template.Template, root string, subMods [][]string, others } //second check define for _, otherFile := range others { + var data []byte fileAbsPath := filepath.Join(root, otherFile) - data, err := ioutil.ReadFile(fileAbsPath) + data, err = ioutil.ReadFile(fileAbsPath) if err != nil { continue } @@ -364,6 +365,7 @@ func DelStaticPath(url string) *App { return BeeApp } +// AddTemplateEngine add a new templatePreProcessor which support extension func AddTemplateEngine(extension string, fn templatePreProcessor) *App { AddTemplateExt(extension) beeTemplateEngines[extension] = fn diff --git a/template_test.go b/template_test.go index 17690965..2153ef72 100644 --- a/template_test.go +++ b/template_test.go @@ -15,6 +15,7 @@ package beego import ( + "bytes" "os" "path/filepath" "testing" @@ -142,3 +143,116 @@ func TestRelativeTemplate(t *testing.T) { } os.RemoveAll(dir) } + +var add = `{{ template "layout_blog.tpl" . }} +{{ define "css" }} + +{{ end}} + + +{{ define "content" }} +

{{ .Title }}

+

This is SomeVar: {{ .SomeVar }}

+{{ end }} + +{{ define "js" }} + +{{ end}}` + +var layoutBlog = ` + + + Lin Li + + + + + {{ block "css" . }}{{ end }} + + + +
+ {{ block "content" . }}{{ end }} +
+ + + {{ block "js" . }}{{ end }} + +` + +var output = ` + + + Lin Li + + + + + + + + + + +
+ +

Hello

+

This is SomeVar: val

+ +
+ + + + + + + + + + + + +` + +func TestTemplateLayout(t *testing.T) { + dir := "_beeTmp" + files := []string{ + "add.tpl", + "layout_blog.tpl", + } + if err := os.MkdirAll(dir, 0777); err != nil { + t.Fatal(err) + } + for k, name := range files { + os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777) + if f, err := os.Create(filepath.Join(dir, name)); err != nil { + t.Fatal(err) + } else { + if k == 0 { + f.WriteString(add) + } else if k == 1 { + f.WriteString(layoutBlog) + } + f.Close() + } + } + if err := AddViewPath(dir); err != nil { + t.Fatal(err) + } + beeTemplates := beeViewPathTemplates[dir] + if len(beeTemplates) != 2 { + t.Fatalf("should be 2 but got %v", len(beeTemplates)) + } + out := bytes.NewBufferString("") + if err := beeTemplates["add.tpl"].ExecuteTemplate(out, "add.tpl", map[string]string{"Title": "Hello", "SomeVar": "val"}); err != nil { + t.Fatal(err) + } + if out.String() != output { + t.Log(out.String()) + t.Fatal("Compare failed") + } + for _, name := range files { + os.RemoveAll(filepath.Join(dir, name)) + } + os.RemoveAll(dir) +} diff --git a/templatefunc.go b/templatefunc.go index 4fdc936d..a6f9c961 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -507,9 +507,9 @@ func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id str class = fieldT.Tag.Get("class") required = false - required_field := fieldT.Tag.Get("required") - if required_field != "-" && required_field != "" { - required, _ = strconv.ParseBool(required_field) + requiredField := fieldT.Tag.Get("required") + if requiredField != "-" && requiredField != "" { + required, _ = strconv.ParseBool(requiredField) } switch len(tags) { diff --git a/templatefunc_test.go b/templatefunc_test.go index 20d9b850..9df61125 100644 --- a/templatefunc_test.go +++ b/templatefunc_test.go @@ -254,43 +254,43 @@ func TestParseFormTag(t *testing.T) { objT := reflect.TypeOf(&user{}).Elem() - label, name, fType, id, class, ignored, required := parseFormTag(objT.Field(0)) + label, name, fType, _, _, ignored, _ := parseFormTag(objT.Field(0)) if !(name == "name" && label == "年龄:" && fType == "text" && !ignored) { t.Errorf("Form Tag with name, label and type was not correctly parsed.") } - label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(1)) + label, name, fType, _, _, ignored, _ = parseFormTag(objT.Field(1)) if !(name == "NoName" && label == "年龄:" && fType == "hidden" && !ignored) { t.Errorf("Form Tag with label and type but without name was not correctly parsed.") } - label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(2)) + label, name, fType, _, _, ignored, _ = parseFormTag(objT.Field(2)) if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && !ignored) { t.Errorf("Form Tag containing only label was not correctly parsed.") } - label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(3)) + label, name, fType, id, class, ignored, _ := parseFormTag(objT.Field(3)) if !(name == "name" && label == "OnlyName: " && fType == "text" && !ignored && id == "name" && class == "form-name") { t.Errorf("Form Tag containing only name was not correctly parsed.") } - label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(4)) + _, _, _, _, _, ignored, _ = parseFormTag(objT.Field(4)) if !ignored { t.Errorf("Form Tag that should be ignored was not correctly parsed.") } - label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(5)) + _, name, _, _, _, _, required := parseFormTag(objT.Field(5)) if !(name == "name" && required) { t.Errorf("Form Tag containing only name and required was not correctly parsed.") } - label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(6)) + _, name, _, _, _, _, required = parseFormTag(objT.Field(6)) if !(name == "name" && !required) { t.Errorf("Form Tag containing only name and ignore required was not correctly parsed.") } - label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(7)) + _, name, _, _, _, _, required = parseFormTag(objT.Field(7)) if !(name == "name" && !required) { t.Errorf("Form Tag containing only name and not required was not correctly parsed.") } diff --git a/utils/file.go b/utils/file.go index db197882..6090eb17 100644 --- a/utils/file.go +++ b/utils/file.go @@ -72,7 +72,7 @@ func GrepFile(patten string, filename string) (lines []string, err error) { lines = make([]string, 0) reader := bufio.NewReader(fd) prefix := "" - isLongLine := false + var isLongLine bool for { byteLine, isPrefix, er := reader.ReadLine() if er != nil && er != io.EOF { diff --git a/utils/file_test.go b/utils/file_test.go index 86d1a700..b2644157 100644 --- a/utils/file_test.go +++ b/utils/file_test.go @@ -54,7 +54,7 @@ func TestSearchFile(t *testing.T) { _, err = SearchFile(noExistedFile, ".") if err == nil { - t.Errorf("err shouldnot be nil, got path: %s", SelfDir()) + t.Errorf("err shouldnt be nil, got path: %s", SelfDir()) } } diff --git a/validation/util_test.go b/validation/util_test.go index d7e10506..e74d50ed 100644 --- a/validation/util_test.go +++ b/validation/util_test.go @@ -42,7 +42,7 @@ func TestGetValidFuncs(t *testing.T) { } f, _ = tf.FieldByName("Tag") - if vfs, err = getValidFuncs(f); err.Error() != "doesn't exsits Maxx valid function" { + if _, err = getValidFuncs(f); err.Error() != "doesn't exsits Maxx valid function" { t.Fatal(err) }