diff --git a/.gitignore b/.gitignore index 8c89938..cd08347 100644 --- a/.gitignore +++ b/.gitignore @@ -25,5 +25,4 @@ _testmain.go dockyard conf/runtime.conf -Godeps/_workspace/* log/* diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 3978ca7..c3c158f 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -12,13 +12,13 @@ }, { "ImportPath": "github.com/astaxie/beego/config", - "Comment": "v1.4.3-4-gc4aa33f", - "Rev": "c4aa33fb1b7ee01fa7dcc9bb29bbdf408b47fa72" + "Comment": "v1.4.3-6-gaf71289", + "Rev": "af71289c25f64e35b688376d5115dbf8b93d87ab" }, { "ImportPath": "github.com/astaxie/beego/logs", - "Comment": "v1.4.3-4-gc4aa33f", - "Rev": "c4aa33fb1b7ee01fa7dcc9bb29bbdf408b47fa72" + "Comment": "v1.4.3-6-gaf71289", + "Rev": "af71289c25f64e35b688376d5115dbf8b93d87ab" }, { "ImportPath": "github.com/codegangsta/cli", diff --git a/Godeps/_workspace/.gitignore b/Godeps/_workspace/.gitignore new file mode 100644 index 0000000..f037d68 --- /dev/null +++ b/Godeps/_workspace/.gitignore @@ -0,0 +1,2 @@ +/pkg +/bin diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/.gitignore b/Godeps/_workspace/src/github.com/Unknwon/com/.gitignore new file mode 100644 index 0000000..0da157f --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test +.idea + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.iml diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/.travis.yml b/Godeps/_workspace/src/github.com/Unknwon/com/.travis.yml new file mode 100644 index 0000000..882eb2d --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/.travis.yml @@ -0,0 +1,13 @@ +language: go + +go: + - 1.2 + - 1.3 + - 1.4 + - tip + +install: go get -v -t + +notifications: + email: + - u@gogs.io \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/LICENSE b/Godeps/_workspace/src/github.com/Unknwon/com/LICENSE new file mode 100644 index 0000000..8405e89 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/LICENSE @@ -0,0 +1,191 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets "[]" replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same "printed page" as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/README.md b/Godeps/_workspace/src/github.com/Unknwon/com/README.md new file mode 100644 index 0000000..8d821ab --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/README.md @@ -0,0 +1,20 @@ +Common Functions +================ + +[![Build Status](https://travis-ci.org/Unknwon/com.svg)](https://travis-ci.org/Unknwon/com) [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/Unknwon/com) + +This is an open source project for commonly used functions for the Go programming language. + +This package need >= **go 1.2** + +Code Convention: based on [Go Code Convention](https://github.com/Unknwon/go-code-convention). + +## Contribute + +Your contribute is welcome, but you have to check following steps after you added some functions and commit them: + +1. Make sure you wrote user-friendly comments for **all functions** . +2. Make sure you wrote test cases with any possible condition for **all functions** in file `*_test.go`. +3. Make sure you wrote benchmarks for **all functions** in file `*_test.go`. +4. Make sure you wrote useful examples for **all functions** in file `example_test.go`. +5. Make sure you ran `go test` and got **PASS** . diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/cmd.go b/Godeps/_workspace/src/github.com/Unknwon/com/cmd.go new file mode 100644 index 0000000..dc7086d --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/cmd.go @@ -0,0 +1,161 @@ +// +build go1.2 + +// Copyright 2013 com authors +// +// 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 com is an open source project for commonly used functions for the Go programming language. +package com + +import ( + "bytes" + "fmt" + "os/exec" + "runtime" + "strings" +) + +// ExecCmdDirBytes executes system command in given directory +// and return stdout, stderr in bytes type, along with possible error. +func ExecCmdDirBytes(dir, cmdName string, args ...string) ([]byte, []byte, error) { + bufOut := new(bytes.Buffer) + bufErr := new(bytes.Buffer) + + cmd := exec.Command(cmdName, args...) + cmd.Dir = dir + cmd.Stdout = bufOut + cmd.Stderr = bufErr + + err := cmd.Run() + return bufOut.Bytes(), bufErr.Bytes(), err +} + +// ExecCmdBytes executes system command +// and return stdout, stderr in bytes type, along with possible error. +func ExecCmdBytes(cmdName string, args ...string) ([]byte, []byte, error) { + return ExecCmdDirBytes("", cmdName, args...) +} + +// ExecCmdDir executes system command in given directory +// and return stdout, stderr in string type, along with possible error. +func ExecCmdDir(dir, cmdName string, args ...string) (string, string, error) { + bufOut, bufErr, err := ExecCmdDirBytes(dir, cmdName, args...) + return string(bufOut), string(bufErr), err +} + +// ExecCmd executes system command +// and return stdout, stderr in string type, along with possible error. +func ExecCmd(cmdName string, args ...string) (string, string, error) { + return ExecCmdDir("", cmdName, args...) +} + +// _________ .__ .____ +// \_ ___ \ ____ | | ___________ | | ____ ____ +// / \ \/ / _ \| | / _ \_ __ \ | | / _ \ / ___\ +// \ \___( <_> ) |_( <_> ) | \/ | |__( <_> ) /_/ > +// \______ /\____/|____/\____/|__| |_______ \____/\___ / +// \/ \/ /_____/ + +// Color number constants. +const ( + Gray = uint8(iota + 90) + Red + Green + Yellow + Blue + Magenta + //NRed = uint8(31) // Normal + EndColor = "\033[0m" +) + +// getColorLevel returns colored level string by given level. +func getColorLevel(level string) string { + level = strings.ToUpper(level) + switch level { + case "TRAC": + return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level) + case "ERRO": + return fmt.Sprintf("\033[%dm%s\033[0m", Red, level) + case "WARN": + return fmt.Sprintf("\033[%dm%s\033[0m", Magenta, level) + case "SUCC": + return fmt.Sprintf("\033[%dm%s\033[0m", Green, level) + default: + return level + } +} + +// ColorLogS colors log and return colored content. +// Log format: [ error ]. +// Level: TRAC -> blue; ERRO -> red; WARN -> Magenta; SUCC -> green; others -> default. +// Content: default; path: yellow; error -> red. +// Level has to be surrounded by "[" and "]". +// Highlights have to be surrounded by "# " and " #"(space), "#" will be deleted. +// Paths have to be surrounded by "( " and " )"(space). +// Errors have to be surrounded by "[ " and " ]"(space). +// Note: it hasn't support windows yet, contribute is welcome. +func ColorLogS(format string, a ...interface{}) string { + log := fmt.Sprintf(format, a...) + + var clog string + + if runtime.GOOS != "windows" { + // Level. + i := strings.Index(log, "]") + if log[0] == '[' && i > -1 { + clog += "[" + getColorLevel(log[1:i]) + "]" + } + + log = log[i+1:] + + // Error. + log = strings.Replace(log, "[ ", fmt.Sprintf("[\033[%dm", Red), -1) + log = strings.Replace(log, " ]", EndColor+"]", -1) + + // Path. + log = strings.Replace(log, "( ", fmt.Sprintf("(\033[%dm", Yellow), -1) + log = strings.Replace(log, " )", EndColor+")", -1) + + // Highlights. + log = strings.Replace(log, "# ", fmt.Sprintf("\033[%dm", Gray), -1) + log = strings.Replace(log, " #", EndColor, -1) + + } else { + // Level. + i := strings.Index(log, "]") + if log[0] == '[' && i > -1 { + clog += "[" + log[1:i] + "]" + } + + log = log[i+1:] + + // Error. + log = strings.Replace(log, "[ ", "[", -1) + log = strings.Replace(log, " ]", "]", -1) + + // Path. + log = strings.Replace(log, "( ", "(", -1) + log = strings.Replace(log, " )", ")", -1) + + // Highlights. + log = strings.Replace(log, "# ", "", -1) + log = strings.Replace(log, " #", "", -1) + } + return clog + log +} + +// ColorLog prints colored log to stdout. +// See color rules in function 'ColorLogS'. +func ColorLog(format string, a ...interface{}) { + fmt.Print(ColorLogS(format, a...)) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/cmd_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/cmd_test.go new file mode 100644 index 0000000..97ad78f --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/cmd_test.go @@ -0,0 +1,140 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "fmt" + "runtime" + "strings" + "testing" +) + +func TestColorLogS(t *testing.T) { + if runtime.GOOS != "windows" { + // Trace + path. + cls := ColorLogS("[TRAC] Trace level test with path( %s )", "/path/to/somethere") + clsR := fmt.Sprintf( + "[\033[%dmTRAC%s] Trace level test with path(\033[%dm%s%s)", + Blue, EndColor, Yellow, "/path/to/somethere", EndColor) + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + + // Error + error. + cls = ColorLogS("[ERRO] Error level test with error[ %s ]", "test error") + clsR = fmt.Sprintf( + "[\033[%dmERRO%s] Error level test with error[\033[%dm%s%s]", + Red, EndColor, Red, "test error", EndColor) + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + + // Warning + highlight. + cls = ColorLogS("[WARN] Warnning level test with highlight # %s #", "special offer!") + clsR = fmt.Sprintf( + "[\033[%dmWARN%s] Warnning level test with highlight \033[%dm%s%s", + Magenta, EndColor, Gray, "special offer!", EndColor) + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + + // Success. + cls = ColorLogS("[SUCC] Success level test") + clsR = fmt.Sprintf( + "[\033[%dmSUCC%s] Success level test", + Green, EndColor) + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + + // Default. + cls = ColorLogS("[INFO] Default level test") + clsR = fmt.Sprintf( + "[INFO] Default level test") + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + } else { + // Trace + path. + cls := ColorLogS("[TRAC] Trace level test with path( %s )", "/path/to/somethere") + clsR := fmt.Sprintf( + "[TRAC] Trace level test with path(%s)", + "/path/to/somethere") + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + + // Error + error. + cls = ColorLogS("[ERRO] Error level test with error[ %s ]", "test error") + clsR = fmt.Sprintf( + "[ERRO] Error level test with error[%s]", + "test error") + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + + // Warning + highlight. + cls = ColorLogS("[WARN] Warnning level test with highlight # %s #", "special offer!") + clsR = fmt.Sprintf( + "[WARN] Warnning level test with highlight %s", + "special offer!") + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + + // Success. + cls = ColorLogS("[SUCC] Success level test") + clsR = fmt.Sprintf( + "[SUCC] Success level test") + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + + // Default. + cls = ColorLogS("[INFO] Default level test") + clsR = fmt.Sprintf( + "[INFO] Default level test") + if cls != clsR { + t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) + } + } +} + +func TestExecCmd(t *testing.T) { + stdout, stderr, err := ExecCmd("go", "help", "get") + if err != nil { + t.Errorf("ExecCmd:\n Expect => %v\n Got => %v\n", nil, err) + } else if len(stderr) != 0 { + t.Errorf("ExecCmd:\n Expect => %s\n Got => %s\n", "", stderr) + } else if !strings.HasPrefix(stdout, "usage: go get") { + t.Errorf("ExecCmd:\n Expect => %s\n Got => %s\n", "usage: go get", stdout) + } +} + +func BenchmarkColorLogS(b *testing.B) { + log := fmt.Sprintf( + "[WARN] This is a tesing log that should be colored, path( %s ),"+ + " highlight # %s #, error [ %s ].", + "path to somewhere", "highlighted content", "tesing error") + for i := 0; i < b.N; i++ { + ColorLogS(log) + } +} + +func BenchmarkExecCmd(b *testing.B) { + for i := 0; i < b.N; i++ { + ExecCmd("go", "help", "get") + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/convert.go b/Godeps/_workspace/src/github.com/Unknwon/com/convert.go new file mode 100644 index 0000000..9c3af84 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/convert.go @@ -0,0 +1,157 @@ +// Copyright 2014 com authors +// +// 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 com + +import ( + "fmt" + "strconv" +) + +// Convert string to specify type. +type StrTo string + +func (f StrTo) Exist() bool { + return string(f) != string(0x1E) +} + +func (f StrTo) Uint8() (uint8, error) { + v, err := strconv.ParseUint(f.String(), 10, 8) + return uint8(v), err +} + +func (f StrTo) Int() (int, error) { + v, err := strconv.ParseInt(f.String(), 10, 32) + return int(v), err +} + +func (f StrTo) Int64() (int64, error) { + v, err := strconv.ParseInt(f.String(), 10, 64) + return int64(v), err +} + +func (f StrTo) MustUint8() uint8 { + v, _ := f.Uint8() + return v +} + +func (f StrTo) MustInt() int { + v, _ := f.Int() + return v +} + +func (f StrTo) MustInt64() int64 { + v, _ := f.Int64() + return v +} + +func (f StrTo) String() string { + if f.Exist() { + return string(f) + } + return "" +} + +// Convert any type to string. +func ToStr(value interface{}, args ...int) (s string) { + switch v := value.(type) { + case bool: + s = strconv.FormatBool(v) + case float32: + s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32)) + case float64: + s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64)) + case int: + s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10)) + case int8: + s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10)) + case int16: + s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10)) + case int32: + s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10)) + case int64: + s = strconv.FormatInt(v, argInt(args).Get(0, 10)) + case uint: + s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10)) + case uint8: + s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10)) + case uint16: + s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10)) + case uint32: + s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10)) + case uint64: + s = strconv.FormatUint(v, argInt(args).Get(0, 10)) + case string: + s = v + case []byte: + s = string(v) + default: + s = fmt.Sprintf("%v", v) + } + return s +} + +type argInt []int + +func (a argInt) Get(i int, args ...int) (r int) { + if i >= 0 && i < len(a) { + r = a[i] + } else if len(args) > 0 { + r = args[0] + } + return +} + +// HexStr2int converts hex format string to decimal number. +func HexStr2int(hexStr string) (int, error) { + num := 0 + length := len(hexStr) + for i := 0; i < length; i++ { + char := hexStr[length-i-1] + factor := -1 + + switch { + case char >= '0' && char <= '9': + factor = int(char) - '0' + case char >= 'a' && char <= 'f': + factor = int(char) - 'a' + 10 + default: + return -1, fmt.Errorf("invalid hex: %s", string(char)) + } + + num += factor * PowInt(16, i) + } + return num, nil +} + +// Int2HexStr converts decimal number to hex format string. +func Int2HexStr(num int) (hex string) { + if num == 0 { + return "0" + } + + for num > 0 { + r := num % 16 + + c := "?" + if r >= 0 && r <= 9 { + c = string(r + '0') + } else { + c = string(r + 'a' - 10) + } + hex = c + hex + num = num / 16 + } + return hex +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/convert_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/convert_test.go new file mode 100644 index 0000000..e97e27f --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/convert_test.go @@ -0,0 +1,56 @@ +// Copyright 2014 com authors +// +// 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 com + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestHexStr2int(t *testing.T) { + Convey("Convert hex format string to decimal", t, func() { + hexDecs := map[string]int{ + "1": 1, + "002": 2, + "011": 17, + "0a1": 161, + "35e": 862, + } + + for hex, dec := range hexDecs { + val, err := HexStr2int(hex) + So(err, ShouldBeNil) + So(val, ShouldEqual, dec) + } + }) +} + +func TestInt2HexStr(t *testing.T) { + Convey("Convert decimal to hex format string", t, func() { + decHexs := map[int]string{ + 1: "1", + 2: "2", + 17: "11", + 161: "a1", + 862: "35e", + } + + for dec, hex := range decHexs { + val := Int2HexStr(dec) + So(val, ShouldEqual, hex) + } + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/dir.go b/Godeps/_workspace/src/github.com/Unknwon/com/dir.go new file mode 100644 index 0000000..c126d79 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/dir.go @@ -0,0 +1,173 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "errors" + "fmt" + "os" + "path" + "strings" +) + +// IsDir returns true if given path is a directory, +// or returns false when it's a file or does not exist. +func IsDir(dir string) bool { + f, e := os.Stat(dir) + if e != nil { + return false + } + return f.IsDir() +} + +func statDir(dirPath, recPath string, includeDir, isDirOnly bool) ([]string, error) { + dir, err := os.Open(dirPath) + if err != nil { + return nil, err + } + defer dir.Close() + + fis, err := dir.Readdir(0) + if err != nil { + return nil, err + } + + statList := make([]string, 0) + for _, fi := range fis { + if strings.Contains(fi.Name(), ".DS_Store") { + continue + } + + relPath := path.Join(recPath, fi.Name()) + curPath := path.Join(dirPath, fi.Name()) + if fi.IsDir() { + if includeDir { + statList = append(statList, relPath+"/") + } + s, err := statDir(curPath, relPath, includeDir, isDirOnly) + if err != nil { + return nil, err + } + statList = append(statList, s...) + } else if !isDirOnly { + statList = append(statList, relPath) + } + } + return statList, nil +} + +// StatDir gathers information of given directory by depth-first. +// It returns slice of file list and includes subdirectories if enabled; +// it returns error and nil slice when error occurs in underlying functions, +// or given path is not a directory or does not exist. +// +// Slice does not include given path itself. +// If subdirectories is enabled, they will have suffix '/'. +func StatDir(rootPath string, includeDir ...bool) ([]string, error) { + if !IsDir(rootPath) { + return nil, errors.New("not a directory or does not exist: " + rootPath) + } + + isIncludeDir := false + if len(includeDir) >= 1 { + isIncludeDir = includeDir[0] + } + return statDir(rootPath, "", isIncludeDir, false) +} + +// GetAllSubDirs returns all subdirectories of given root path. +// Slice does not include given path itself. +func GetAllSubDirs(rootPath string) ([]string, error) { + if !IsDir(rootPath) { + return nil, errors.New("not a directory or does not exist: " + rootPath) + } + return statDir(rootPath, "", true, true) +} + +// GetFileListBySuffix returns an ordered list of file paths. +// It recognize if given path is a file, and don't do recursive find. +func GetFileListBySuffix(dirPath, suffix string) ([]string, error) { + if !IsExist(dirPath) { + return nil, fmt.Errorf("given path does not exist: %s", dirPath) + } else if IsFile(dirPath) { + return []string{dirPath}, nil + } + + // Given path is a directory. + dir, err := os.Open(dirPath) + if err != nil { + return nil, err + } + + fis, err := dir.Readdir(0) + if err != nil { + return nil, err + } + + files := make([]string, 0, len(fis)) + for _, fi := range fis { + if strings.HasSuffix(fi.Name(), suffix) { + files = append(files, path.Join(dirPath, fi.Name())) + } + } + + return files, nil +} + +// CopyDir copy files recursively from source to target directory. +// +// The filter accepts a function that process the path info. +// and should return true for need to filter. +// +// It returns error when error occurs in underlying functions. +func CopyDir(srcPath, destPath string, filters ...func(filePath string) bool) error { + // Check if target directory exists. + if IsExist(destPath) { + return errors.New("file or directory alreay exists: " + destPath) + } + + err := os.MkdirAll(destPath, os.ModePerm) + if err != nil { + return err + } + + // Gather directory info. + infos, err := StatDir(srcPath, true) + if err != nil { + return err + } + + var filter func(filePath string) bool + if len(filters) > 0 { + filter = filters[0] + } + + for _, info := range infos { + if filter != nil && filter(info) { + continue + } + + curPath := path.Join(destPath, info) + if strings.HasSuffix(info, "/") { + err = os.MkdirAll(curPath, os.ModePerm) + } else { + err = Copy(path.Join(srcPath, info), curPath) + } + if err != nil { + return err + } + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/dir_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/dir_test.go new file mode 100644 index 0000000..5d9a92b --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/dir_test.go @@ -0,0 +1,56 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "os" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestIsDir(t *testing.T) { + Convey("Check if given path is a directory", t, func() { + Convey("Pass a file name", func() { + So(IsDir("file.go"), ShouldEqual, false) + }) + Convey("Pass a directory name", func() { + So(IsDir("testdata"), ShouldEqual, true) + }) + Convey("Pass a invalid path", func() { + So(IsDir("foo"), ShouldEqual, false) + }) + }) +} + +func TestCopyDir(t *testing.T) { + Convey("Items of two slices should be same", t, func() { + _, err := StatDir("testdata", true) + So(err, ShouldEqual, nil) + + err = CopyDir("testdata", "testdata2") + So(err, ShouldEqual, nil) + + _, err = StatDir("testdata2", true) + os.RemoveAll("testdata2") + So(err, ShouldEqual, nil) + }) +} + +func BenchmarkIsDir(b *testing.B) { + for i := 0; i < b.N; i++ { + IsDir("file.go") + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/example_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/example_test.go new file mode 100644 index 0000000..bc1930b --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/example_test.go @@ -0,0 +1,299 @@ +// Copyright 2013 com authors +// +// 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 com_test + +import ( + "fmt" + "io/ioutil" + "net/http" + + "github.com/Unknwon/com" +) + +// ------------------------------ +// cmd.go +// ------------------------------ + +func ExampleColorLogS() { + coloredLog := com.ColorLogS(fmt.Sprintf( + "[WARN] This is a tesing log that should be colored, path( %s ),"+ + " highlight # %s #, error [ %s ].", + "path to somewhere", "highlighted content", "tesing error")) + fmt.Println(coloredLog) +} + +func ExampleColorLog() { + com.ColorLog(fmt.Sprintf( + "[WARN] This is a tesing log that should be colored, path( %s ),"+ + " highlight # %s #, error [ %s ].", + "path to somewhere", "highlighted content", "tesing error")) +} + +func ExampleExecCmd() { + stdout, stderr, err := com.ExecCmd("go", "help", "get") + fmt.Println(stdout, stderr, err) +} + +// ------------- END ------------ + +// ------------------------------ +// html.go +// ------------------------------ + +func ExampleHtml2JS() { + htm := "
Click me
\n\r" + js := string(com.Html2JS([]byte(htm))) + fmt.Println(js) + // Output:
Click me
\n +} + +// ------------- END ------------ + +// ------------------------------ +// path.go +// ------------------------------ + +func ExampleGetGOPATHs() { + gps := com.GetGOPATHs() + fmt.Println(gps) +} + +func ExampleGetSrcPath() { + srcPath, err := com.GetSrcPath("github.com/Unknwon/com") + if err != nil { + fmt.Println(err) + return + } + fmt.Println(srcPath) +} + +func ExampleHomeDir() { + hd, err := com.HomeDir() + fmt.Println(hd, err) +} + +// ------------- END ------------ + +// ------------------------------ +// file.go +// ------------------------------ + +func ExampleIsFile() { + if com.IsFile("file.go") { + fmt.Println("file.go exists") + return + } + fmt.Println("file.go is not a file or does not exist") +} + +func ExampleIsExist() { + if com.IsExist("file.go") { + fmt.Println("file.go exists") + return + } + fmt.Println("file.go does not exist") +} + +// ------------- END ------------ + +// ------------------------------ +// dir.go +// ------------------------------ + +func ExampleIsDir() { + if com.IsDir("files") { + fmt.Println("directory 'files' exists") + return + } + fmt.Println("'files' is not a directory or does not exist") +} + +// ------------- END ------------ + +// ------------------------------ +// string.go +// ------------------------------ + +func ExampleIsLetter() { + fmt.Println(com.IsLetter('1')) + fmt.Println(com.IsLetter('[')) + fmt.Println(com.IsLetter('a')) + fmt.Println(com.IsLetter('Z')) + // Output: + // false + // false + // true + // true +} + +func ExampleExpand() { + match := map[string]string{ + "domain": "gowalker.org", + "subdomain": "github.com", + } + s := "http://{domain}/{subdomain}/{0}/{1}" + fmt.Println(com.Expand(s, match, "Unknwon", "gowalker")) + // Output: http://gowalker.org/github.com/Unknwon/gowalker +} + +// ------------- END ------------ + +// ------------------------------ +// http.go +// ------------------------------ + +func ExampleHttpGet() ([]byte, error) { + rc, err := com.HttpGet(&http.Client{}, "http://gowalker.org", nil) + if err != nil { + return nil, err + } + p, err := ioutil.ReadAll(rc) + rc.Close() + return p, err +} + +func ExampleHttpGetBytes() ([]byte, error) { + p, err := com.HttpGetBytes(&http.Client{}, "http://gowalker.org", nil) + return p, err +} + +func ExampleHttpGetJSON() interface{} { + j := com.HttpGetJSON(&http.Client{}, "http://gowalker.org", nil) + return j +} + +type rawFile struct { + name string + rawURL string + data []byte +} + +func (rf *rawFile) Name() string { + return rf.name +} + +func (rf *rawFile) RawUrl() string { + return rf.rawURL +} + +func (rf *rawFile) Data() []byte { + return rf.data +} + +func (rf *rawFile) SetData(p []byte) { + rf.data = p +} + +func ExampleFetchFiles() { + // Code that should be outside of your function body. + // type rawFile struct { + // name string + // rawURL string + // data []byte + // } + + // func (rf *rawFile) Name() string { + // return rf.name + // } + + // func (rf *rawFile) RawUrl() string { + // return rf.rawURL + // } + + // func (rf *rawFile) Data() []byte { + // return rf.data + // } + + // func (rf *rawFile) SetData(p []byte) { + // rf.data = p + // } + + files := []com.RawFile{ + &rawFile{rawURL: "http://example.com"}, + &rawFile{rawURL: "http://example.com/foo"}, + } + err := com.FetchFiles(&http.Client{}, files, nil) + fmt.Println(err, len(files[0].Data()), len(files[1].Data())) +} + +func ExampleFetchFilesCurl() { + // Code that should be outside of your function body. + // type rawFile struct { + // name string + // rawURL string + // data []byte + // } + + // func (rf *rawFile) Name() string { + // return rf.name + // } + + // func (rf *rawFile) RawUrl() string { + // return rf.rawURL + // } + + // func (rf *rawFile) Data() []byte { + // return rf.data + // } + + // func (rf *rawFile) SetData(p []byte) { + // rf.data = p + // } + + files := []com.RawFile{ + &rawFile{rawURL: "http://example.com"}, + &rawFile{rawURL: "http://example.com/foo"}, + } + err := com.FetchFilesCurl(files) + fmt.Println(err, len(files[0].Data()), len(files[1].Data())) +} + +// ------------- END ------------ + +// ------------------------------ +// regex.go +// ------------------------------ + +func ExampleIsEmail() { + fmt.Println(com.IsEmail("test@example.com")) + fmt.Println(com.IsEmail("@example.com")) + // Output: + // true + // false +} + +func ExampleIsUrl() { + fmt.Println(com.IsUrl("http://example.com")) + fmt.Println(com.IsUrl("http//example.com")) + // Output: + // true + // false +} + +// ------------- END ------------ + +// ------------------------------ +// slice.go +// ------------------------------ + +func ExampleAppendStr() { + s := []string{"a"} + s = com.AppendStr(s, "a") + s = com.AppendStr(s, "b") + fmt.Println(s) + // Output: [a b] +} + +// ------------- END ------------ diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/file.go b/Godeps/_workspace/src/github.com/Unknwon/com/file.go new file mode 100644 index 0000000..b51502c --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/file.go @@ -0,0 +1,145 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "fmt" + "io" + "io/ioutil" + "math" + "os" + "path" +) + +// Storage unit constants. +const ( + Byte = 1 + KByte = Byte * 1024 + MByte = KByte * 1024 + GByte = MByte * 1024 + TByte = GByte * 1024 + PByte = TByte * 1024 + EByte = PByte * 1024 +) + +func logn(n, b float64) float64 { + return math.Log(n) / math.Log(b) +} + +func humanateBytes(s uint64, base float64, sizes []string) string { + if s < 10 { + return fmt.Sprintf("%dB", s) + } + e := math.Floor(logn(float64(s), base)) + suffix := sizes[int(e)] + val := float64(s) / math.Pow(base, math.Floor(e)) + f := "%.0f" + if val < 10 { + f = "%.1f" + } + + return fmt.Sprintf(f+"%s", val, suffix) +} + +// HumaneFileSize calculates the file size and generate user-friendly string. +func HumaneFileSize(s uint64) string { + sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} + return humanateBytes(s, 1024, sizes) +} + +// FileMTime returns file modified time and possible error. +func FileMTime(file string) (int64, error) { + f, err := os.Stat(file) + if err != nil { + return 0, err + } + return f.ModTime().Unix(), nil +} + +// FileSize returns file size in bytes and possible error. +func FileSize(file string) (int64, error) { + f, err := os.Stat(file) + if err != nil { + return 0, err + } + return f.Size(), nil +} + +// Copy copies file from source to target path. +func Copy(src, dest string) error { + // Gather file information to set back later. + si, err := os.Lstat(src) + if err != nil { + return err + } + + // Handle symbolic link. + if si.Mode()&os.ModeSymlink != 0 { + target, err := os.Readlink(src) + if err != nil { + return err + } + // NOTE: os.Chmod and os.Chtimes don't recoganize symbolic link, + // which will lead "no such file or directory" error. + return os.Symlink(target, dest) + } + + sr, err := os.Open(src) + if err != nil { + return err + } + defer sr.Close() + + dw, err := os.Create(dest) + if err != nil { + return err + } + defer dw.Close() + + if _, err = io.Copy(dw, sr); err != nil { + return err + } + + // Set back file information. + if err = os.Chtimes(dest, si.ModTime(), si.ModTime()); err != nil { + return err + } + return os.Chmod(dest, si.Mode()) +} + +// WriteFile writes data to a file named by filename. +// If the file does not exist, WriteFile creates it +// and its upper level paths. +func WriteFile(filename string, data []byte) error { + os.MkdirAll(path.Dir(filename), os.ModePerm) + return ioutil.WriteFile(filename, data, 0655) +} + +// IsFile returns true if given path is a file, +// or returns false when it's a directory or does not exist. +func IsFile(filePath string) bool { + f, e := os.Stat(filePath) + if e != nil { + return false + } + return !f.IsDir() +} + +// IsExist checks whether a file or directory exists. +// It returns false when the file or directory does not exist. +func IsExist(path string) bool { + _, err := os.Stat(path) + return err == nil || os.IsExist(err) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/file_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/file_test.go new file mode 100644 index 0000000..4ff00dc --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/file_test.go @@ -0,0 +1,61 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestIsFile(t *testing.T) { + if !IsFile("file.go") { + t.Errorf("IsExist:\n Expect => %v\n Got => %v\n", true, false) + } + + if IsFile("testdata") { + t.Errorf("IsExist:\n Expect => %v\n Got => %v\n", false, true) + } + + if IsFile("files.go") { + t.Errorf("IsExist:\n Expect => %v\n Got => %v\n", false, true) + } +} + +func TestIsExist(t *testing.T) { + Convey("Check if file or directory exists", t, func() { + Convey("Pass a file name that exists", func() { + So(IsExist("file.go"), ShouldEqual, true) + }) + Convey("Pass a directory name that exists", func() { + So(IsExist("testdata"), ShouldEqual, true) + }) + Convey("Pass a directory name that does not exist", func() { + So(IsExist(".hg"), ShouldEqual, false) + }) + }) +} + +func BenchmarkIsFile(b *testing.B) { + for i := 0; i < b.N; i++ { + IsFile("file.go") + } +} + +func BenchmarkIsExist(b *testing.B) { + for i := 0; i < b.N; i++ { + IsExist("file.go") + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/html.go b/Godeps/_workspace/src/github.com/Unknwon/com/html.go new file mode 100644 index 0000000..762d94b --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/html.go @@ -0,0 +1,60 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "html" + "regexp" + "strings" +) + +// Html2JS converts []byte type of HTML content into JS format. +func Html2JS(data []byte) []byte { + s := string(data) + s = strings.Replace(s, `\`, `\\`, -1) + s = strings.Replace(s, "\n", `\n`, -1) + s = strings.Replace(s, "\r", "", -1) + s = strings.Replace(s, "\"", `\"`, -1) + s = strings.Replace(s, "", "<table>", -1) + return []byte(s) +} + +// encode html chars to string +func HtmlEncode(str string) string { + return html.EscapeString(str) +} + +// decode string to html chars +func HtmlDecode(str string) string { + return html.UnescapeString(str) +} + +// strip tags in html string +func StripTags(src string) string { + //去除style,script,html tag + re := regexp.MustCompile(`(?s)<(?:style|script)[^<>]*>.*?|]*>|`) + src = re.ReplaceAllString(src, "") + + //trim all spaces(2+) into \n + re = regexp.MustCompile(`\s{2,}`) + src = re.ReplaceAllString(src, "\n") + + return strings.TrimSpace(src) +} + +// change \n to
+func Nl2br(str string) string { + return strings.Replace(str, "\n", "
", -1) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/html_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/html_test.go new file mode 100644 index 0000000..2c9f404 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/html_test.go @@ -0,0 +1,35 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "testing" +) + +func TestHtml2JS(t *testing.T) { + htm := "
Click me
\n\r" + js := string(Html2JS([]byte(htm))) + jsR := `
Click me
\n` + if js != jsR { + t.Errorf("Html2JS:\n Expect => %s\n Got => %s\n", jsR, js) + } +} + +func BenchmarkHtml2JS(b *testing.B) { + htm := "
Click me
\n\r" + for i := 0; i < b.N; i++ { + Html2JS([]byte(htm)) + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/http.go b/Godeps/_workspace/src/github.com/Unknwon/com/http.go new file mode 100644 index 0000000..3415059 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/http.go @@ -0,0 +1,201 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "io/ioutil" + "net/http" + "os" + "path" +) + +type NotFoundError struct { + Message string +} + +func (e NotFoundError) Error() string { + return e.Message +} + +type RemoteError struct { + Host string + Err error +} + +func (e *RemoteError) Error() string { + return e.Err.Error() +} + +var UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1541.0 Safari/537.36" + +// HttpCall makes HTTP method call. +func HttpCall(client *http.Client, method, url string, header http.Header, body io.Reader) (io.ReadCloser, error) { + req, err := http.NewRequest(method, url, body) + if err != nil { + return nil, err + } + req.Header.Set("User-Agent", UserAgent) + for k, vs := range header { + req.Header[k] = vs + } + resp, err := client.Do(req) + if err != nil { + return nil, err + } + if resp.StatusCode == 200 { + return resp.Body, nil + } + resp.Body.Close() + if resp.StatusCode == 404 { // 403 can be rate limit error. || resp.StatusCode == 403 { + err = fmt.Errorf("resource not found: %s", url) + } else { + err = fmt.Errorf("%s %s -> %d", method, url, resp.StatusCode) + } + return nil, err +} + +// HttpGet gets the specified resource. +// ErrNotFound is returned if the server responds with status 404. +func HttpGet(client *http.Client, url string, header http.Header) (io.ReadCloser, error) { + return HttpCall(client, "GET", url, header, nil) +} + +// HttpPost posts the specified resource. +// ErrNotFound is returned if the server responds with status 404. +func HttpPost(client *http.Client, url string, header http.Header, body []byte) (io.ReadCloser, error) { + return HttpCall(client, "POST", url, header, bytes.NewBuffer(body)) +} + +// HttpGetToFile gets the specified resource and writes to file. +// ErrNotFound is returned if the server responds with status 404. +func HttpGetToFile(client *http.Client, url string, header http.Header, fileName string) error { + rc, err := HttpGet(client, url, header) + if err != nil { + return err + } + defer rc.Close() + + os.MkdirAll(path.Dir(fileName), os.ModePerm) + f, err := os.Create(fileName) + if err != nil { + return err + } + defer f.Close() + _, err = io.Copy(f, rc) + return err +} + +// HttpGetBytes gets the specified resource. ErrNotFound is returned if the server +// responds with status 404. +func HttpGetBytes(client *http.Client, url string, header http.Header) ([]byte, error) { + rc, err := HttpGet(client, url, header) + if err != nil { + return nil, err + } + defer rc.Close() + return ioutil.ReadAll(rc) +} + +// HttpGetJSON gets the specified resource and mapping to struct. +// ErrNotFound is returned if the server responds with status 404. +func HttpGetJSON(client *http.Client, url string, v interface{}) error { + rc, err := HttpGet(client, url, nil) + if err != nil { + return err + } + defer rc.Close() + err = json.NewDecoder(rc).Decode(v) + if _, ok := err.(*json.SyntaxError); ok { + return fmt.Errorf("JSON syntax error at %s", url) + } + return nil +} + +// HttpPostJSON posts the specified resource with struct values, +// and maps results to struct. +// ErrNotFound is returned if the server responds with status 404. +func HttpPostJSON(client *http.Client, url string, body, v interface{}) error { + data, err := json.Marshal(body) + if err != nil { + return err + } + rc, err := HttpPost(client, url, http.Header{"content-type": []string{"application/json"}}, data) + if err != nil { + return err + } + defer rc.Close() + err = json.NewDecoder(rc).Decode(v) + if _, ok := err.(*json.SyntaxError); ok { + return fmt.Errorf("JSON syntax error at %s", url) + } + return nil +} + +// A RawFile describes a file that can be downloaded. +type RawFile interface { + Name() string + RawUrl() string + Data() []byte + SetData([]byte) +} + +// FetchFiles fetches files specified by the rawURL field in parallel. +func FetchFiles(client *http.Client, files []RawFile, header http.Header) error { + ch := make(chan error, len(files)) + for i := range files { + go func(i int) { + p, err := HttpGetBytes(client, files[i].RawUrl(), nil) + if err != nil { + ch <- err + return + } + files[i].SetData(p) + ch <- nil + }(i) + } + for _ = range files { + if err := <-ch; err != nil { + return err + } + } + return nil +} + +// FetchFiles uses command `curl` to fetch files specified by the rawURL field in parallel. +func FetchFilesCurl(files []RawFile, curlOptions ...string) error { + ch := make(chan error, len(files)) + for i := range files { + go func(i int) { + stdout, _, err := ExecCmd("curl", append(curlOptions, files[i].RawUrl())...) + if err != nil { + ch <- err + return + } + + files[i].SetData([]byte(stdout)) + ch <- nil + }(i) + } + for _ = range files { + if err := <-ch; err != nil { + return err + } + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/http_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/http_test.go new file mode 100644 index 0000000..902b660 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/http_test.go @@ -0,0 +1,111 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "io/ioutil" + "net/http" + "strings" + "testing" +) + +var examplePrefix = ` + + + Example Domain +` + +func TestHttpGet(t *testing.T) { + // 200. + rc, err := HttpGet(&http.Client{}, "http://example.com", nil) + if err != nil { + t.Fatalf("HttpGet:\n Expect => %v\n Got => %s\n", nil, err) + } + p, err := ioutil.ReadAll(rc) + if err != nil { + t.Errorf("HttpGet:\n Expect => %v\n Got => %s\n", nil, err) + } + s := string(p) + if !strings.HasPrefix(s, examplePrefix) { + t.Errorf("HttpGet:\n Expect => %s\n Got => %s\n", examplePrefix, s) + } +} + +func TestHttpGetBytes(t *testing.T) { + p, err := HttpGetBytes(&http.Client{}, "http://example.com", nil) + if err != nil { + t.Errorf("HttpGetBytes:\n Expect => %v\n Got => %s\n", nil, err) + } + s := string(p) + if !strings.HasPrefix(s, examplePrefix) { + t.Errorf("HttpGet:\n Expect => %s\n Got => %s\n", examplePrefix, s) + } +} + +func TestHttpGetJSON(t *testing.T) { + +} + +type rawFile struct { + name string + rawURL string + data []byte +} + +func (rf *rawFile) Name() string { + return rf.name +} + +func (rf *rawFile) RawUrl() string { + return rf.rawURL +} + +func (rf *rawFile) Data() []byte { + return rf.data +} + +func (rf *rawFile) SetData(p []byte) { + rf.data = p +} + +func TestFetchFiles(t *testing.T) { + files := []RawFile{ + &rawFile{rawURL: "http://example.com"}, + &rawFile{rawURL: "http://example.com"}, + } + err := FetchFiles(&http.Client{}, files, nil) + if err != nil { + t.Errorf("FetchFiles:\n Expect => %v\n Got => %s\n", nil, err) + } else if len(files[0].Data()) != 1270 { + t.Errorf("FetchFiles:\n Expect => %d\n Got => %d\n", 1270, len(files[0].Data())) + } else if len(files[1].Data()) != 1270 { + t.Errorf("FetchFiles:\n Expect => %d\n Got => %d\n", 1270, len(files[1].Data())) + } +} + +func TestFetchFilesCurl(t *testing.T) { + files := []RawFile{ + &rawFile{rawURL: "http://example.com"}, + &rawFile{rawURL: "http://example.com"}, + } + err := FetchFilesCurl(files) + if err != nil { + t.Errorf("FetchFilesCurl:\n Expect => %v\n Got => %s\n", nil, err) + } else if len(files[0].Data()) != 1270 { + t.Errorf("FetchFilesCurl:\n Expect => %d\n Got => %d\n", 1270, len(files[0].Data())) + } else if len(files[1].Data()) != 1270 { + t.Errorf("FetchFilesCurl:\n Expect => %d\n Got => %d\n", 1270, len(files[1].Data())) + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/math.go b/Godeps/_workspace/src/github.com/Unknwon/com/math.go new file mode 100644 index 0000000..99c56b6 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/math.go @@ -0,0 +1,29 @@ +// Copyright 2014 com authors +// +// 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 com + +// PowInt is int type of math.Pow function. +func PowInt(x int, y int) int { + if y <= 0 { + return 1 + } else { + if y % 2 == 0 { + sqrt := PowInt(x, y/2) + return sqrt * sqrt + } else { + return PowInt(x, y-1) * x + } + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/math_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/math_test.go new file mode 100644 index 0000000..587eb19 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/math_test.go @@ -0,0 +1,44 @@ +// Copyright 2015 com authors +// +// 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 com + +import ( + "math" + "math/rand" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Pow(t *testing.T) { + Convey("Power int", t, func() { + for x := 0; x < 10; x++ { + for y := 0; y < 8; y++ { + result := PowInt(x, y) + result_float := math.Pow(float64(x), float64(y)) + So(result, ShouldEqual, int(result_float)) + } + } + }) +} + +func BenchmarkPow(b *testing.B) { + x := rand.Intn(100) + y := rand.Intn(6) + b.ResetTimer() + for n := 0; n < b.N; n++ { + PowInt(x, y) + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/path.go b/Godeps/_workspace/src/github.com/Unknwon/com/path.go new file mode 100644 index 0000000..a501c85 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/path.go @@ -0,0 +1,80 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "errors" + "os" + "path/filepath" + "runtime" + "strings" +) + +// GetGOPATHs returns all paths in GOPATH variable. +func GetGOPATHs() []string { + gopath := os.Getenv("GOPATH") + var paths []string + if runtime.GOOS == "windows" { + gopath = strings.Replace(gopath, "\\", "/", -1) + paths = strings.Split(gopath, ";") + } else { + paths = strings.Split(gopath, ":") + } + return paths +} + +// GetSrcPath returns app. source code path. +// It only works when you have src. folder in GOPATH, +// it returns error not able to locate source folder path. +func GetSrcPath(importPath string) (appPath string, err error) { + paths := GetGOPATHs() + for _, p := range paths { + if IsExist(p + "/src/" + importPath + "/") { + appPath = p + "/src/" + importPath + "/" + break + } + } + + if len(appPath) == 0 { + return "", errors.New("Unable to locate source folder path") + } + + appPath = filepath.Dir(appPath) + "/" + if runtime.GOOS == "windows" { + // Replace all '\' to '/'. + appPath = strings.Replace(appPath, "\\", "/", -1) + } + + return appPath, nil +} + +// HomeDir returns path of '~'(in Linux) on Windows, +// it returns error when the variable does not exist. +func HomeDir() (home string, err error) { + if runtime.GOOS == "windows" { + home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + if home == "" { + home = os.Getenv("USERPROFILE") + } + } else { + home = os.Getenv("HOME") + } + + if len(home) == 0 { + return "", errors.New("Cannot specify home directory because it's empty") + } + + return home, nil +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/path_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/path_test.go new file mode 100644 index 0000000..da5488d --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/path_test.go @@ -0,0 +1,67 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "os" + "runtime" + "testing" +) + +func TestGetGOPATHs(t *testing.T) { + var gpsR []string + + if runtime.GOOS != "windows" { + gpsR = []string{"path/to/gopath1", "path/to/gopath2", "path/to/gopath3"} + os.Setenv("GOPATH", "path/to/gopath1:path/to/gopath2:path/to/gopath3") + } else { + gpsR = []string{"path/to/gopath1", "path/to/gopath2", "path/to/gopath3"} + os.Setenv("GOPATH", "path\\to\\gopath1;path\\to\\gopath2;path\\to\\gopath3") + } + + gps := GetGOPATHs() + if !CompareSliceStr(gps, gpsR) { + t.Errorf("GetGOPATHs:\n Expect => %s\n Got => %s\n", gpsR, gps) + } +} + +func TestGetSrcPath(t *testing.T) { + +} + +func TestHomeDir(t *testing.T) { + _, err := HomeDir() + if err != nil { + t.Errorf("HomeDir:\n Expect => %v\n Got => %s\n", nil, err) + } +} + +func BenchmarkGetGOPATHs(b *testing.B) { + for i := 0; i < b.N; i++ { + GetGOPATHs() + } +} + +func BenchmarkGetSrcPath(b *testing.B) { + for i := 0; i < b.N; i++ { + GetSrcPath("github.com/Unknwon/com") + } +} + +func BenchmarkHomeDir(b *testing.B) { + for i := 0; i < b.N; i++ { + HomeDir() + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/regex.go b/Godeps/_workspace/src/github.com/Unknwon/com/regex.go new file mode 100644 index 0000000..765bfc4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/regex.go @@ -0,0 +1,56 @@ +// Copyright 2013 com authors +// +// 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 com + +import "regexp" + +const ( + regex_email_pattern = `(?i)[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}` + regex_strict_email_pattern = `(?i)[A-Z0-9!#$%&'*+/=?^_{|}~-]+` + + `(?:\.[A-Z0-9!#$%&'*+/=?^_{|}~-]+)*` + + `@(?:[A-Z0-9](?:[A-Z0-9-]*[A-Z0-9])?\.)+` + + `[A-Z0-9](?:[A-Z0-9-]*[A-Z0-9])?` + regex_url_pattern = `(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?` +) + +var ( + regex_email *regexp.Regexp + regex_strict_email *regexp.Regexp + regex_url *regexp.Regexp +) + +func init() { + regex_email = regexp.MustCompile(regex_email_pattern) + regex_strict_email = regexp.MustCompile(regex_strict_email_pattern) + regex_url = regexp.MustCompile(regex_url_pattern) +} + +// validate string is an email address, if not return false +// basically validation can match 99% cases +func IsEmail(email string) bool { + return regex_email.MatchString(email) +} + +// validate string is an email address, if not return false +// this validation omits RFC 2822 +func IsEmailRFC(email string) bool { + return regex_strict_email.MatchString(email) +} + +// validate string is a url link, if not return false +// simple validation can match 99% cases +func IsUrl(url string) bool { + return regex_url.MatchString(url) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/regex_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/regex_test.go new file mode 100644 index 0000000..b027bd1 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/regex_test.go @@ -0,0 +1,70 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "testing" +) + +func TestIsEmail(t *testing.T) { + emails := map[string]bool{ + `test@example.com`: true, + `single-character@b.org`: true, + `uncommon_address@test.museum`: true, + `local@sld.UPPER`: true, + `@missing.org`: false, + `missing@.com`: false, + `missing@qq.`: false, + `wrong-ip@127.1.1.1.26`: false, + } + for e, r := range emails { + b := IsEmail(e) + if b != r { + t.Errorf("IsEmail:\n Expect => %v\n Got => %v\n", r, b) + } + } +} + +func TestIsUrl(t *testing.T) { + urls := map[string]bool{ + "http://www.example.com": true, + "http://example.com": true, + "http://example.com?user=test&password=test": true, + "http://example.com?user=test#login": true, + "ftp://example.com": true, + "https://example.com": true, + "htp://example.com": false, + "http//example.com": false, + "http://example": true, + } + for u, r := range urls { + b := IsUrl(u) + if b != r { + t.Errorf("IsUrl:\n Expect => %v\n Got => %v\n", r, b) + } + } +} + +func BenchmarkIsEmail(b *testing.B) { + for i := 0; i < b.N; i++ { + IsEmail("test@example.com") + } +} + +func BenchmarkIsUrl(b *testing.B) { + for i := 0; i < b.N; i++ { + IsEmail("http://example.com") + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/slice.go b/Godeps/_workspace/src/github.com/Unknwon/com/slice.go new file mode 100644 index 0000000..27801a4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/slice.go @@ -0,0 +1,87 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "strings" +) + +// AppendStr appends string to slice with no duplicates. +func AppendStr(strs []string, str string) []string { + for _, s := range strs { + if s == str { + return strs + } + } + return append(strs, str) +} + +// CompareSliceStr compares two 'string' type slices. +// It returns true if elements and order are both the same. +func CompareSliceStr(s1, s2 []string) bool { + if len(s1) != len(s2) { + return false + } + + for i := range s1 { + if s1[i] != s2[i] { + return false + } + } + + return true +} + +// CompareSliceStr compares two 'string' type slices. +// It returns true if elements are the same, and ignores the order. +func CompareSliceStrU(s1, s2 []string) bool { + if len(s1) != len(s2) { + return false + } + + for i := range s1 { + for j := len(s2) - 1; j >= 0; j-- { + if s1[i] == s2[j] { + s2 = append(s2[:j], s2[j+1:]...) + break + } + } + } + if len(s2) > 0 { + return false + } + return true +} + +// IsSliceContainsStr returns true if the string exists in given slice, ignore case. +func IsSliceContainsStr(sl []string, str string) bool { + str = strings.ToLower(str) + for _, s := range sl { + if strings.ToLower(s) == str { + return true + } + } + return false +} + +// IsSliceContainsInt64 returns true if the int64 exists in given slice. +func IsSliceContainsInt64(sl []int64, i int64) bool { + for _, s := range sl { + if s == i { + return true + } + } + return false +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/slice_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/slice_test.go new file mode 100644 index 0000000..6a94156 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/slice_test.go @@ -0,0 +1,99 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "fmt" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestAppendStr(t *testing.T) { + Convey("Append a string to a slice with no duplicates", t, func() { + s := []string{"a"} + + Convey("Append a string that does not exist in slice", func() { + s = AppendStr(s, "b") + So(len(s), ShouldEqual, 2) + }) + + Convey("Append a string that does exist in slice", func() { + s = AppendStr(s, "b") + So(len(s), ShouldEqual, 2) + }) + }) +} + +func TestCompareSliceStr(t *testing.T) { + Convey("Compares two 'string' type slices with elements and order", t, func() { + Convey("Compare two slices that do have same elements and order", func() { + So(CompareSliceStr( + []string{"1", "2", "3"}, []string{"1", "2", "3"}), ShouldBeTrue) + }) + + Convey("Compare two slices that do have same elements but does not have same order", func() { + So(!CompareSliceStr( + []string{"2", "1", "3"}, []string{"1", "2", "3"}), ShouldBeTrue) + }) + + Convey("Compare two slices that have different number of elements", func() { + So(!CompareSliceStr( + []string{"2", "1"}, []string{"1", "2", "3"}), ShouldBeTrue) + }) + }) +} + +func TestCompareSliceStrU(t *testing.T) { + Convey("Compare two 'string' type slices with elements and ignore the order", t, func() { + Convey("Compare two slices that do have same elements and order", func() { + So(CompareSliceStrU( + []string{"1", "2", "3"}, []string{"1", "2", "3"}), ShouldBeTrue) + }) + + Convey("Compare two slices that do have same elements but does not have same order", func() { + So(CompareSliceStrU( + []string{"2", "1", "3"}, []string{"1", "2", "3"}), ShouldBeTrue) + }) + + Convey("Compare two slices that have different number of elements", func() { + So(!CompareSliceStrU( + []string{"2", "1"}, []string{"1", "2", "3"}), ShouldBeTrue) + }) + }) +} + +func BenchmarkAppendStr(b *testing.B) { + s := []string{"a"} + for i := 0; i < b.N; i++ { + s = AppendStr(s, fmt.Sprint(b.N%3)) + } +} + +func BenchmarkCompareSliceStr(b *testing.B) { + s1 := []string{"1", "2", "3"} + s2 := []string{"1", "2", "3"} + for i := 0; i < b.N; i++ { + CompareSliceStr(s1, s2) + } +} + +func BenchmarkCompareSliceStrU(b *testing.B) { + s1 := []string{"1", "4", "2", "3"} + s2 := []string{"1", "2", "3", "4"} + for i := 0; i < b.N; i++ { + CompareSliceStrU(s1, s2) + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/string.go b/Godeps/_workspace/src/github.com/Unknwon/com/string.go new file mode 100644 index 0000000..4c79820 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/string.go @@ -0,0 +1,243 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "bytes" + "crypto/aes" + "crypto/cipher" + "crypto/rand" + "encoding/base64" + "errors" + "io" + r "math/rand" + "strconv" + "strings" + "time" + "unicode" + "unicode/utf8" +) + +// AESEncrypt encrypts text and given key with AES. +func AESEncrypt(key, text []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + b := base64.StdEncoding.EncodeToString(text) + ciphertext := make([]byte, aes.BlockSize+len(b)) + iv := ciphertext[:aes.BlockSize] + if _, err := io.ReadFull(rand.Reader, iv); err != nil { + return nil, err + } + cfb := cipher.NewCFBEncrypter(block, iv) + cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b)) + return ciphertext, nil +} + +// AESDecrypt decrypts text and given key with AES. +func AESDecrypt(key, text []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + if len(text) < aes.BlockSize { + return nil, errors.New("ciphertext too short") + } + iv := text[:aes.BlockSize] + text = text[aes.BlockSize:] + cfb := cipher.NewCFBDecrypter(block, iv) + cfb.XORKeyStream(text, text) + data, err := base64.StdEncoding.DecodeString(string(text)) + if err != nil { + return nil, err + } + return data, nil +} + +// IsLetter returns true if the 'l' is an English letter. +func IsLetter(l uint8) bool { + n := (l | 0x20) - 'a' + if n >= 0 && n < 26 { + return true + } + return false +} + +// Expand replaces {k} in template with match[k] or subs[atoi(k)] if k is not in match. +func Expand(template string, match map[string]string, subs ...string) string { + var p []byte + var i int + for { + i = strings.Index(template, "{") + if i < 0 { + break + } + p = append(p, template[:i]...) + template = template[i+1:] + i = strings.Index(template, "}") + if s, ok := match[template[:i]]; ok { + p = append(p, s...) + } else { + j, _ := strconv.Atoi(template[:i]) + if j >= len(subs) { + p = append(p, []byte("Missing")...) + } else { + p = append(p, subs[j]...) + } + } + template = template[i+1:] + } + p = append(p, template...) + return string(p) +} + +// Reverse s string, support unicode +func Reverse(s string) string { + n := len(s) + runes := make([]rune, n) + for _, rune := range s { + n-- + runes[n] = rune + } + return string(runes[n:]) +} + +// RandomCreateBytes generate random []byte by specify chars. +func RandomCreateBytes(n int, alphabets ...byte) []byte { + const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + var bytes = make([]byte, n) + var randby bool + if num, err := rand.Read(bytes); num != n || err != nil { + r.Seed(time.Now().UnixNano()) + randby = true + } + for i, b := range bytes { + if len(alphabets) == 0 { + if randby { + bytes[i] = alphanum[r.Intn(len(alphanum))] + } else { + bytes[i] = alphanum[b%byte(len(alphanum))] + } + } else { + if randby { + bytes[i] = alphabets[r.Intn(len(alphabets))] + } else { + bytes[i] = alphabets[b%byte(len(alphabets))] + } + } + } + return bytes +} + +// ToSnakeCase can convert all upper case characters in a string to +// underscore format. +// +// Some samples. +// "FirstName" => "first_name" +// "HTTPServer" => "http_server" +// "NoHTTPS" => "no_https" +// "GO_PATH" => "go_path" +// "GO PATH" => "go_path" // space is converted to underscore. +// "GO-PATH" => "go_path" // hyphen is converted to underscore. +// +// From https://github.com/huandu/xstrings +func ToSnakeCase(str string) string { + if len(str) == 0 { + return "" + } + + buf := &bytes.Buffer{} + var prev, r0, r1 rune + var size int + + r0 = '_' + + for len(str) > 0 { + prev = r0 + r0, size = utf8.DecodeRuneInString(str) + str = str[size:] + + switch { + case r0 == utf8.RuneError: + buf.WriteByte(byte(str[0])) + + case unicode.IsUpper(r0): + if prev != '_' { + buf.WriteRune('_') + } + + buf.WriteRune(unicode.ToLower(r0)) + + if len(str) == 0 { + break + } + + r0, size = utf8.DecodeRuneInString(str) + str = str[size:] + + if !unicode.IsUpper(r0) { + buf.WriteRune(r0) + break + } + + // find next non-upper-case character and insert `_` properly. + // it's designed to convert `HTTPServer` to `http_server`. + // if there are more than 2 adjacent upper case characters in a word, + // treat them as an abbreviation plus a normal word. + for len(str) > 0 { + r1 = r0 + r0, size = utf8.DecodeRuneInString(str) + str = str[size:] + + if r0 == utf8.RuneError { + buf.WriteRune(unicode.ToLower(r1)) + buf.WriteByte(byte(str[0])) + break + } + + if !unicode.IsUpper(r0) { + if r0 == '_' || r0 == ' ' || r0 == '-' { + r0 = '_' + + buf.WriteRune(unicode.ToLower(r1)) + } else { + buf.WriteRune('_') + buf.WriteRune(unicode.ToLower(r1)) + buf.WriteRune(r0) + } + + break + } + + buf.WriteRune(unicode.ToLower(r1)) + } + + if len(str) == 0 || r0 == '_' { + buf.WriteRune(unicode.ToLower(r0)) + break + } + + default: + if r0 == ' ' || r0 == '-' { + r0 = '_' + } + + buf.WriteRune(r0) + } + } + + return buf.String() +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/string_test.go b/Godeps/_workspace/src/github.com/Unknwon/com/string_test.go new file mode 100644 index 0000000..1148978 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/string_test.go @@ -0,0 +1,108 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestIsLetter(t *testing.T) { + if IsLetter('1') { + t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", false, true) + } + + if IsLetter('[') { + t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", false, true) + } + + if !IsLetter('a') { + t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", true, false) + } + + if !IsLetter('Z') { + t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", true, false) + } +} + +func TestExpand(t *testing.T) { + match := map[string]string{ + "domain": "gowalker.org", + "subdomain": "github.com", + } + s := "http://{domain}/{subdomain}/{0}/{1}" + sR := "http://gowalker.org/github.com/Unknwon/gowalker" + if Expand(s, match, "Unknwon", "gowalker") != sR { + t.Errorf("Expand:\n Expect => %s\n Got => %s\n", sR, s) + } +} + +func TestReverse(t *testing.T) { + if Reverse("abcdefg") != "gfedcba" { + t.Errorf("Reverse:\n Except => %s\n Got =>%s\n", "gfedcba", Reverse("abcdefg")) + } + if Reverse("上善若水厚德载物") != "物载德厚水若善上" { + t.Errorf("Reverse:\n Except => %s\n Got =>%s\n", "物载德厚水若善上", Reverse("上善若水厚德载物")) + } +} + +func Test_ToSnakeCase(t *testing.T) { + cases := map[string]string{ + "HTTPServer": "http_server", + "_camelCase": "_camel_case", + "NoHTTPS": "no_https", + "Wi_thF": "wi_th_f", + "_AnotherTES_TCaseP": "_another_tes_t_case_p", + "ALL": "all", + "_HELLO_WORLD_": "_hello_world_", + "HELLO_WORLD": "hello_world", + "HELLO____WORLD": "hello____world", + "TW": "tw", + "_C": "_c", + + " sentence case ": "__sentence_case__", + " Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case", + } + Convey("Convert string into snake case", t, func() { + for old, new := range cases { + So(ToSnakeCase(old), ShouldEqual, new) + } + }) +} + +func BenchmarkIsLetter(b *testing.B) { + for i := 0; i < b.N; i++ { + IsLetter('a') + } +} + +func BenchmarkExpand(b *testing.B) { + match := map[string]string{ + "domain": "gowalker.org", + "subdomain": "github.com", + } + s := "http://{domain}/{subdomain}/{0}/{1}" + for i := 0; i < b.N; i++ { + Expand(s, match, "Unknwon", "gowalker") + } +} + +func BenchmarkReverse(b *testing.B) { + s := "abscef中文" + for i := 0; i < b.N; i++ { + Reverse(s) + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/SaveFile.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/SaveFile.txt new file mode 100644 index 0000000..0547fc8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/SaveFile.txt @@ -0,0 +1 @@ +TestSaveFile \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/SaveFileS.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/SaveFileS.txt new file mode 100644 index 0000000..dfc3325 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/SaveFileS.txt @@ -0,0 +1 @@ +TestSaveFileS \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/sample_file.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/sample_file.txt new file mode 100644 index 0000000..e69de29 diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/SaveFile.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/SaveFile.txt new file mode 100644 index 0000000..0547fc8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/SaveFile.txt @@ -0,0 +1 @@ +TestSaveFile \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/SaveFileS.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/SaveFileS.txt new file mode 100644 index 0000000..dfc3325 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/SaveFileS.txt @@ -0,0 +1 @@ +TestSaveFileS \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/sample_file.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/sample_file.txt new file mode 100644 index 0000000..e69de29 diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/secondLevel/SaveFile.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/secondLevel/SaveFile.txt new file mode 100644 index 0000000..0547fc8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/secondLevel/SaveFile.txt @@ -0,0 +1 @@ +TestSaveFile \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/secondLevel/SaveFileS.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/secondLevel/SaveFileS.txt new file mode 100644 index 0000000..dfc3325 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/secondLevel/SaveFileS.txt @@ -0,0 +1 @@ +TestSaveFileS \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/secondLevel/sample_file.txt b/Godeps/_workspace/src/github.com/Unknwon/com/testdata/statDir/secondLevel/sample_file.txt new file mode 100644 index 0000000..e69de29 diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/time.go b/Godeps/_workspace/src/github.com/Unknwon/com/time.go new file mode 100644 index 0000000..dd1cdbc --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/time.go @@ -0,0 +1,115 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "fmt" + "strconv" + "strings" + "time" +) + +// Format unix time int64 to string +func Date(ti int64, format string) string { + t := time.Unix(int64(ti), 0) + return DateT(t, format) +} + +// Format unix time string to string +func DateS(ts string, format string) string { + i, _ := strconv.ParseInt(ts, 10, 64) + return Date(i, format) +} + +// Format time.Time struct to string +// MM - month - 01 +// M - month - 1, single bit +// DD - day - 02 +// D - day 2 +// YYYY - year - 2006 +// YY - year - 06 +// HH - 24 hours - 03 +// H - 24 hours - 3 +// hh - 12 hours - 03 +// h - 12 hours - 3 +// mm - minute - 04 +// m - minute - 4 +// ss - second - 05 +// s - second = 5 +func DateT(t time.Time, format string) string { + res := strings.Replace(format, "MM", t.Format("01"), -1) + res = strings.Replace(res, "M", t.Format("1"), -1) + res = strings.Replace(res, "DD", t.Format("02"), -1) + res = strings.Replace(res, "D", t.Format("2"), -1) + res = strings.Replace(res, "YYYY", t.Format("2006"), -1) + res = strings.Replace(res, "YY", t.Format("06"), -1) + res = strings.Replace(res, "HH", fmt.Sprintf("%02d", t.Hour()), -1) + res = strings.Replace(res, "H", fmt.Sprintf("%d", t.Hour()), -1) + res = strings.Replace(res, "hh", t.Format("03"), -1) + res = strings.Replace(res, "h", t.Format("3"), -1) + res = strings.Replace(res, "mm", t.Format("04"), -1) + res = strings.Replace(res, "m", t.Format("4"), -1) + res = strings.Replace(res, "ss", t.Format("05"), -1) + res = strings.Replace(res, "s", t.Format("5"), -1) + return res +} + +// DateFormat pattern rules. +var datePatterns = []string{ + // year + "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 + "y", "06", //A two digit representation of a year Examples: 99 or 03 + + // month + "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 + "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 + "M", "Jan", // A short textual representation of a month, three letters Jan through Dec + "F", "January", // A full textual representation of a month, such as January or March January through December + + // day + "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 + "j", "2", // Day of the month without leading zeros 1 to 31 + + // week + "D", "Mon", // A textual representation of a day, three letters Mon through Sun + "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday + + // time + "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 + "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 + "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 + "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 + + "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm + "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM + + "i", "04", // Minutes with leading zeros 00 to 59 + "s", "05", // Seconds, with leading zeros 00 through 59 + + // time zone + "T", "MST", + "P", "-07:00", + "O", "-0700", + + // RFC 2822 + "r", time.RFC1123Z, +} + +// Parse Date use PHP time format. +func DateParse(dateString, format string) (time.Time, error) { + replacer := strings.NewReplacer(datePatterns...) + format = replacer.Replace(format) + return time.ParseInLocation(format, dateString, time.Local) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/com/url.go b/Godeps/_workspace/src/github.com/Unknwon/com/url.go new file mode 100644 index 0000000..b0b7c0e --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/com/url.go @@ -0,0 +1,41 @@ +// Copyright 2013 com authors +// +// 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 com + +import ( + "encoding/base64" + "net/url" +) + +// url encode string, is + not %20 +func UrlEncode(str string) string { + return url.QueryEscape(str) +} + +// url decode string +func UrlDecode(str string) (string, error) { + return url.QueryUnescape(str) +} + +// base64 encode +func Base64Encode(str string) string { + return base64.StdEncoding.EncodeToString([]byte(str)) +} + +// base64 decode +func Base64Decode(str string) (string, error) { + s, e := base64.StdEncoding.DecodeString(str) + return string(s), e +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/.gitignore b/Godeps/_workspace/src/github.com/Unknwon/macaron/.gitignore new file mode 100644 index 0000000..57ee3ec --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/.gitignore @@ -0,0 +1,2 @@ +macaron.sublime-project +macaron.sublime-workspace \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/LICENSE b/Godeps/_workspace/src/github.com/Unknwon/macaron/LICENSE new file mode 100644 index 0000000..37ec93a --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/LICENSE @@ -0,0 +1,191 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets "[]" replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same "printed page" as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/README.md b/Godeps/_workspace/src/github.com/Unknwon/macaron/README.md new file mode 100644 index 0000000..8b20162 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/README.md @@ -0,0 +1,94 @@ +Macaron [![Build Status](https://drone.io/github.com/Unknwon/macaron/status.png)](https://drone.io/github.com/Unknwon/macaron/latest) [![](http://gocover.io/_badge/github.com/Unknwon/macaron)](http://gocover.io/github.com/Unknwon/macaron) +======================= + +![Macaron Logo](https://raw.githubusercontent.com/Unknwon/macaron/master/macaronlogo.png) + +Package macaron is a high productive and modular design web framework in Go. + +##### Current version: 0.5.4 + +## Getting Started + +To install Macaron: + + go get github.com/Unknwon/macaron + +The very basic usage of Macaron: + +```go +package main + +import "github.com/Unknwon/macaron" + +func main() { + m := macaron.Classic() + m.Get("/", func() string { + return "Hello world!" + }) + m.Run() +} +``` + +## Features + +- Powerful routing with suburl. +- Flexible routes combinations. +- Unlimited nested group routers. +- Directly integrate with existing services. +- Dynamically change template files at runtime. +- Allow to use in-memory template and static files. +- Easy to plugin/unplugin features with modular design. +- Handy dependency injection powered by [inject](https://github.com/codegangsta/inject). +- Better router layer and less reflection make faster speed. + +## Middlewares + +Middlewares allow you easily plugin/unplugin features for your Macaron applications. + +There are already many [middlewares](https://github.com/macaron-contrib) to simplify your work: + +- gzip - Gzip compression to all requests +- render - Go template engine +- static - Serves static files +- [binding](https://github.com/macaron-contrib/binding) - Request data binding and validation +- [i18n](https://github.com/macaron-contrib/i18n) - Internationalization and Localization +- [cache](https://github.com/macaron-contrib/cache) - Cache manager +- [session](https://github.com/macaron-contrib/session) - Session manager +- [csrf](https://github.com/macaron-contrib/csrf) - Generates and validates csrf tokens +- [captcha](https://github.com/macaron-contrib/captcha) - Captcha service +- [pongo2](https://github.com/macaron-contrib/pongo2) - Pongo2 template engine support +- [sockets](https://github.com/macaron-contrib/sockets) - WebSockets channels binding +- [bindata](https://github.com/macaron-contrib/bindata) - Embed binary data as static and template files +- [toolbox](https://github.com/macaron-contrib/toolbox) - Health check, pprof, profile and statistic services +- [oauth2](https://github.com/macaron-contrib/oauth2) - OAuth 2.0 backend +- [switcher](https://github.com/macaron-contrib/switcher) - Multiple-site support +- [method](https://github.com/macaron-contrib/method) - HTTP method override +- [permissions2](https://github.com/xyproto/permissions2) - Cookies, users and permissions +- [renders](https://github.com/macaron-contrib/renders) - Beego-like render engine(Macaron has built-in template engine, this is another option) + +## Use Cases + +- [Gogs](https://github.com/gogits/gogs): Go Git Service +- [Gogs Web](https://github.com/gogits/gogsweb): Gogs official website +- [Go Walker](https://gowalker.org): Go online API documentation +- [Switch](https://github.com/gpmgo/switch): Gopm registry +- [YouGam](http://yougam.com): Online Forum +- [Car Girl](http://qcnl.gzsy.com/): Online campaign +- [Critical Stack Intel](https://intel.criticalstack.com/): A 100% free intel marketplace from Critical Stack, Inc. + +## Getting Help + +- [API Reference](https://gowalker.org/github.com/Unknwon/macaron) +- [Documentation](http://macaron.gogs.io) +- [FAQs](http://macaron.gogs.io/docs/faqs) +- [![Join the chat at https://gitter.im/Unknwon/macaron](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Unknwon/macaron?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +## Credits + +- Basic design of [Martini](https://github.com/go-martini/martini). +- Router layer of [beego](https://github.com/astaxie/beego). +- Logo is modified by [@insionng](https://github.com/insionng) based on [Tribal Dragon](http://xtremeyamazaki.deviantart.com/art/Tribal-Dragon-27005087). + +## License + +This project is under Apache v2 License. See the [LICENSE](LICENSE) file for the full license text. diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/context.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/context.go new file mode 100644 index 0000000..abf7ac9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/context.go @@ -0,0 +1,478 @@ +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "crypto/md5" + "encoding/hex" + "html/template" + "io" + "io/ioutil" + "mime/multipart" + "net/http" + "net/url" + "os" + "path" + "path/filepath" + "reflect" + "strconv" + "strings" + "time" + + "github.com/Unknwon/com" + + "github.com/Unknwon/macaron/inject" +) + +// Locale reprents a localization interface. +type Locale interface { + Language() string + Tr(string, ...interface{}) string +} + +// RequestBody represents a request body. +type RequestBody struct { + reader io.ReadCloser +} + +// Bytes reads and returns content of request body in bytes. +func (rb *RequestBody) Bytes() ([]byte, error) { + return ioutil.ReadAll(rb.reader) +} + +// String reads and returns content of request body in string. +func (rb *RequestBody) String() (string, error) { + data, err := rb.Bytes() + return string(data), err +} + +// ReadCloser returns a ReadCloser for request body. +func (rb *RequestBody) ReadCloser() io.ReadCloser { + return rb.reader +} + +// Request represents an HTTP request received by a server or to be sent by a client. +type Request struct { + *http.Request +} + +func (r *Request) Body() *RequestBody { + return &RequestBody{r.Request.Body} +} + +// Context represents the runtime context of current request of Macaron instance. +// It is the integration of most frequently used middlewares and helper methods. +type Context struct { + inject.Injector + handlers []Handler + action Handler + index int + + *Router + Req Request + Resp ResponseWriter + params Params + Render // Not nil only if you use macaran.Render middleware. + Locale + Data map[string]interface{} +} + +func (c *Context) handler() Handler { + if c.index < len(c.handlers) { + return c.handlers[c.index] + } + if c.index == len(c.handlers) { + return c.action + } + panic("invalid index for context handler") +} + +func (c *Context) Next() { + c.index += 1 + c.run() +} + +func (c *Context) Written() bool { + return c.Resp.Written() +} + +func (c *Context) run() { + for c.index <= len(c.handlers) { + vals, err := c.Invoke(c.handler()) + if err != nil { + panic(err) + } + c.index += 1 + + // if the handler returned something, write it to the http response + if len(vals) > 0 { + ev := c.GetVal(reflect.TypeOf(ReturnHandler(nil))) + handleReturn := ev.Interface().(ReturnHandler) + handleReturn(c, vals) + } + + if c.Written() { + return + } + } +} + +// RemoteAddr returns more real IP address. +func (ctx *Context) RemoteAddr() string { + addr := ctx.Req.Header.Get("X-Real-IP") + if len(addr) == 0 { + addr = ctx.Req.Header.Get("X-Forwarded-For") + if addr == "" { + addr = ctx.Req.RemoteAddr + if i := strings.LastIndex(addr, ":"); i > -1 { + addr = addr[:i] + } + } + } + return addr +} + +func (ctx *Context) renderHTML(status int, setName, tplName string, data ...interface{}) { + if ctx.Render == nil { + panic("renderer middleware hasn't been registered") + } + if len(data) <= 0 { + ctx.Render.HTMLSet(status, setName, tplName, ctx.Data) + } else if len(data) == 1 { + ctx.Render.HTMLSet(status, setName, tplName, data[0]) + } else { + ctx.Render.HTMLSet(status, setName, tplName, data[0], data[1].(HTMLOptions)) + } +} + +// HTML calls Render.HTML but allows less arguments. +func (ctx *Context) HTML(status int, name string, data ...interface{}) { + ctx.renderHTML(status, _DEFAULT_TPL_SET_NAME, name, data...) +} + +// HTML calls Render.HTMLSet but allows less arguments. +func (ctx *Context) HTMLSet(status int, setName, tplName string, data ...interface{}) { + ctx.renderHTML(status, setName, tplName, data...) +} + +func (ctx *Context) Redirect(location string, status ...int) { + code := http.StatusFound + if len(status) == 1 { + code = status[0] + } + + http.Redirect(ctx.Resp, ctx.Req.Request, location, code) +} + +// Query querys form parameter. +func (ctx *Context) Query(name string) string { + if ctx.Req.Form == nil { + ctx.Req.ParseForm() + } + return ctx.Req.Form.Get(name) +} + +// QueryTrim querys and trims spaces form parameter. +func (ctx *Context) QueryTrim(name string) string { + return strings.TrimSpace(ctx.Query(name)) +} + +// QueryStrings returns a list of results by given query name. +func (ctx *Context) QueryStrings(name string) []string { + if ctx.Req.Form == nil { + ctx.Req.ParseForm() + } + + vals, ok := ctx.Req.Form[name] + if !ok { + return []string{} + } + return vals +} + +// QueryEscape returns escapred query result. +func (ctx *Context) QueryEscape(name string) string { + return template.HTMLEscapeString(ctx.Query(name)) +} + +// QueryInt returns query result in int type. +func (ctx *Context) QueryInt(name string) int { + return com.StrTo(ctx.Query(name)).MustInt() +} + +// QueryInt64 returns query result in int64 type. +func (ctx *Context) QueryInt64(name string) int64 { + return com.StrTo(ctx.Query(name)).MustInt64() +} + +// QueryFloat64 returns query result in float64 type. +func (ctx *Context) QueryFloat64(name string) float64 { + v, _ := strconv.ParseFloat(ctx.Query(name), 64) + return v +} + +// Params returns value of given param name. +// e.g. ctx.Params(":uid") or ctx.Params("uid") +func (ctx *Context) Params(name string) string { + if len(name) == 0 { + return "" + } + if name[0] != '*' && name[0] != ':' { + name = ":" + name + } + return ctx.params[name] +} + +// SetParams sets value of param with given name. +func (ctx *Context) SetParams(name, val string) { + if !strings.HasPrefix(name, ":") { + name = ":" + name + } + ctx.params[name] = val +} + +// ParamsEscape returns escapred params result. +// e.g. ctx.ParamsEscape(":uname") +func (ctx *Context) ParamsEscape(name string) string { + return template.HTMLEscapeString(ctx.Params(name)) +} + +// ParamsInt returns params result in int type. +// e.g. ctx.ParamsInt(":uid") +func (ctx *Context) ParamsInt(name string) int { + return com.StrTo(ctx.Params(name)).MustInt() +} + +// ParamsInt64 returns params result in int64 type. +// e.g. ctx.ParamsInt64(":uid") +func (ctx *Context) ParamsInt64(name string) int64 { + return com.StrTo(ctx.Params(name)).MustInt64() +} + +// ParamsFloat64 returns params result in int64 type. +// e.g. ctx.ParamsFloat64(":uid") +func (ctx *Context) ParamsFloat64(name string) float64 { + v, _ := strconv.ParseFloat(ctx.Params(name), 64) + return v +} + +// GetFile returns information about user upload file by given form field name. +func (ctx *Context) GetFile(name string) (multipart.File, *multipart.FileHeader, error) { + return ctx.Req.FormFile(name) +} + +// SetCookie sets given cookie value to response header. +// FIXME: IE support? http://golanghome.com/post/620#reply2 +func (ctx *Context) SetCookie(name string, value string, others ...interface{}) { + cookie := http.Cookie{} + cookie.Name = name + cookie.Value = url.QueryEscape(value) + + if len(others) > 0 { + switch v := others[0].(type) { + case int: + cookie.MaxAge = v + case int64: + cookie.MaxAge = int(v) + case int32: + cookie.MaxAge = int(v) + } + } + + cookie.Path = "/" + if len(others) > 1 { + if v, ok := others[1].(string); ok && len(v) > 0 { + cookie.Path = v + } + } + + if len(others) > 2 { + if v, ok := others[2].(string); ok && len(v) > 0 { + cookie.Domain = v + } + } + + if len(others) > 3 { + switch v := others[3].(type) { + case bool: + cookie.Secure = v + default: + if others[3] != nil { + cookie.Secure = true + } + } + } + + if len(others) > 4 { + if v, ok := others[4].(bool); ok && v { + cookie.HttpOnly = true + } + } + + ctx.Resp.Header().Add("Set-Cookie", cookie.String()) +} + +// GetCookie returns given cookie value from request header. +func (ctx *Context) GetCookie(name string) string { + cookie, err := ctx.Req.Cookie(name) + if err != nil { + return "" + } + val, _ := url.QueryUnescape(cookie.Value) + return val +} + +// GetCookieInt returns cookie result in int type. +func (ctx *Context) GetCookieInt(name string) int { + return com.StrTo(ctx.GetCookie(name)).MustInt() +} + +// GetCookieInt64 returns cookie result in int64 type. +func (ctx *Context) GetCookieInt64(name string) int64 { + return com.StrTo(ctx.GetCookie(name)).MustInt64() +} + +// GetCookieFloat64 returns cookie result in float64 type. +func (ctx *Context) GetCookieFloat64(name string) float64 { + v, _ := strconv.ParseFloat(ctx.GetCookie(name), 64) + return v +} + +var defaultCookieSecret string + +// SetDefaultCookieSecret sets global default secure cookie secret. +func (m *Macaron) SetDefaultCookieSecret(secret string) { + defaultCookieSecret = secret +} + +// SetSecureCookie sets given cookie value to response header with default secret string. +func (ctx *Context) SetSecureCookie(name, value string, others ...interface{}) { + ctx.SetSuperSecureCookie(defaultCookieSecret, name, value, others...) +} + +// GetSecureCookie returns given cookie value from request header with default secret string. +func (ctx *Context) GetSecureCookie(key string) (string, bool) { + return ctx.GetSuperSecureCookie(defaultCookieSecret, key) +} + +// SetSuperSecureCookie sets given cookie value to response header with secret string. +func (ctx *Context) SetSuperSecureCookie(secret, name, value string, others ...interface{}) { + m := md5.Sum([]byte(secret)) + secret = hex.EncodeToString(m[:]) + text, err := com.AESEncrypt([]byte(secret), []byte(value)) + if err != nil { + panic("error encrypting cookie: " + err.Error()) + } + ctx.SetCookie(name, hex.EncodeToString(text), others...) +} + +// GetSuperSecureCookie returns given cookie value from request header with secret string. +func (ctx *Context) GetSuperSecureCookie(secret, key string) (string, bool) { + val := ctx.GetCookie(key) + if val == "" { + return "", false + } + + data, err := hex.DecodeString(val) + if err != nil { + return "", false + } + + m := md5.Sum([]byte(secret)) + secret = hex.EncodeToString(m[:]) + text, err := com.AESDecrypt([]byte(secret), data) + return string(text), err == nil +} + +func (ctx *Context) setRawContentHeader() { + ctx.Resp.Header().Set("Content-Description", "Raw content") + ctx.Resp.Header().Set("Content-Type", "text/plain") + ctx.Resp.Header().Set("Expires", "0") + ctx.Resp.Header().Set("Cache-Control", "must-revalidate") + ctx.Resp.Header().Set("Pragma", "public") +} + +// ServeContent serves given content to response. +func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) { + modtime := time.Now() + for _, p := range params { + switch v := p.(type) { + case time.Time: + modtime = v + } + } + + ctx.setRawContentHeader() + http.ServeContent(ctx.Resp, ctx.Req.Request, name, modtime, r) +} + +// ServeFileContent serves given file as content to response. +func (ctx *Context) ServeFileContent(file string, names ...string) { + var name string + if len(names) > 0 { + name = names[0] + } else { + name = path.Base(file) + } + + f, err := os.Open(file) + if err != nil { + if Env == PROD { + http.Error(ctx.Resp, "Internal Server Error", 500) + } else { + http.Error(ctx.Resp, err.Error(), 500) + } + return + } + defer f.Close() + + ctx.setRawContentHeader() + http.ServeContent(ctx.Resp, ctx.Req.Request, name, time.Now(), f) +} + +// ServeFile serves given file to response. +func (ctx *Context) ServeFile(file string, names ...string) { + var name string + if len(names) > 0 { + name = names[0] + } else { + name = path.Base(file) + } + ctx.Resp.Header().Set("Content-Description", "File Transfer") + ctx.Resp.Header().Set("Content-Type", "application/octet-stream") + ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+name) + ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary") + ctx.Resp.Header().Set("Expires", "0") + ctx.Resp.Header().Set("Cache-Control", "must-revalidate") + ctx.Resp.Header().Set("Pragma", "public") + http.ServeFile(ctx.Resp, ctx.Req.Request, file) +} + +// ChangeStaticPath changes static path from old to new one. +func (ctx *Context) ChangeStaticPath(oldPath, newPath string) { + if !filepath.IsAbs(oldPath) { + oldPath = filepath.Join(Root, oldPath) + } + dir := statics.Get(oldPath) + if dir != nil { + statics.Delete(oldPath) + + if !filepath.IsAbs(newPath) { + newPath = filepath.Join(Root, newPath) + } + *dir = http.Dir(newPath) + statics.Set(dir) + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/context_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/context_test.go new file mode 100644 index 0000000..c4b4752 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/context_test.go @@ -0,0 +1,370 @@ +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + "time" + + "github.com/Unknwon/com" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Context(t *testing.T) { + Convey("Do advanced encapsulation operations", t, func() { + m := Classic() + m.Use(Renderers(RenderOptions{ + Directory: "fixtures/basic", + }, "fixtures/basic2")) + + Convey("Get request body", func() { + m.Get("/body1", func(ctx *Context) { + data, err := ioutil.ReadAll(ctx.Req.Body().ReadCloser()) + So(err, ShouldBeNil) + So(string(data), ShouldEqual, "This is my request body") + }) + m.Get("/body2", func(ctx *Context) { + data, err := ctx.Req.Body().Bytes() + So(err, ShouldBeNil) + So(string(data), ShouldEqual, "This is my request body") + }) + m.Get("/body3", func(ctx *Context) { + data, err := ctx.Req.Body().String() + So(err, ShouldBeNil) + So(data, ShouldEqual, "This is my request body") + }) + + for i := 1; i <= 3; i++ { + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/body"+com.ToStr(i), nil) + req.Body = ioutil.NopCloser(bytes.NewBufferString("This is my request body")) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + } + }) + + Convey("Get remote IP address", func() { + m.Get("/remoteaddr", func(ctx *Context) string { + return ctx.RemoteAddr() + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/remoteaddr", nil) + req.RemoteAddr = "127.0.0.1:3333" + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "127.0.0.1") + }) + + Convey("Render HTML", func() { + + Convey("Normal HTML", func() { + m.Get("/html", func(ctx *Context) { + ctx.HTML(304, "hello", "Unknwon") // 304 for logger test. + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/html", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "

Hello Unknwon

") + }) + + Convey("HTML template set", func() { + m.Get("/html2", func(ctx *Context) { + ctx.Data["Name"] = "Unknwon" + ctx.HTMLSet(200, "basic2", "hello2") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/html2", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "

Hello Unknwon

") + }) + + Convey("With layout", func() { + m.Get("/layout", func(ctx *Context) { + ctx.HTML(200, "hello", "Unknwon", HTMLOptions{"layout"}) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/layout", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "head

Hello Unknwon

foot") + }) + }) + + Convey("Parse from and query", func() { + m.Get("/query", func(ctx *Context) string { + var buf bytes.Buffer + buf.WriteString(ctx.QueryTrim("name") + " ") + buf.WriteString(ctx.QueryEscape("name") + " ") + buf.WriteString(com.ToStr(ctx.QueryInt("int")) + " ") + buf.WriteString(com.ToStr(ctx.QueryInt64("int64")) + " ") + buf.WriteString(com.ToStr(ctx.QueryFloat64("float64")) + " ") + return buf.String() + }) + m.Get("/query2", func(ctx *Context) string { + var buf bytes.Buffer + buf.WriteString(strings.Join(ctx.QueryStrings("list"), ",") + " ") + buf.WriteString(strings.Join(ctx.QueryStrings("404"), ",") + " ") + return buf.String() + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/query?name=Unknwon&int=12&int64=123&float64=1.25", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "Unknwon Unknwon 12 123 1.25 ") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/query2?list=item1&list=item2", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "item1,item2 ") + }) + + Convey("URL parameter", func() { + m.Get("/:name/:int/:int64/:float64", func(ctx *Context) string { + var buf bytes.Buffer + ctx.SetParams("name", ctx.Params("name")) + buf.WriteString(ctx.Params("")) + buf.WriteString(ctx.Params(":name") + " ") + buf.WriteString(ctx.ParamsEscape(":name") + " ") + buf.WriteString(com.ToStr(ctx.ParamsInt(":int")) + " ") + buf.WriteString(com.ToStr(ctx.ParamsInt64(":int64")) + " ") + buf.WriteString(com.ToStr(ctx.ParamsFloat64(":float64")) + " ") + return buf.String() + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/user/1/13/1.24", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "user user 1 13 1.24 ") + }) + + Convey("Get file", func() { + m.Get("/getfile", func(ctx *Context) { + ctx.GetFile("hi") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/getfile", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) + + Convey("Set and get cookie", func() { + m.Get("/set", func(ctx *Context) { + ctx.SetCookie("user", "Unknwon", 1, "/", "localhost", true, true) + ctx.SetCookie("user", "Unknwon", int32(1), "/", "localhost", 1) + ctx.SetCookie("user", "Unknwon", int64(1)) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/set", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Header().Get("Set-Cookie"), ShouldEqual, "user=Unknwon; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure") + + m.Get("/get", func(ctx *Context) string { + ctx.GetCookie("404") + So(ctx.GetCookieInt("uid"), ShouldEqual, 1) + So(ctx.GetCookieInt64("uid"), ShouldEqual, 1) + So(ctx.GetCookieFloat64("balance"), ShouldEqual, 1.25) + return ctx.GetCookie("user") + }) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "user=Unknwon; uid=1; balance=1.25") + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "Unknwon") + }) + + Convey("Set and get secure cookie", func() { + m.SetDefaultCookieSecret("macaron") + m.Get("/set", func(ctx *Context) { + ctx.SetSecureCookie("user", "Unknwon", 1) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/set", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + cookie := resp.Header().Get("Set-Cookie") + + m.Get("/get", func(ctx *Context) string { + name, ok := ctx.GetSecureCookie("user") + So(ok, ShouldBeTrue) + return name + }) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "Unknwon") + }) + + Convey("Serve files", func() { + m.Get("/file", func(ctx *Context) { + ctx.ServeFile("fixtures/custom_funcs/index.tmpl") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/file", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "{{ myCustomFunc }}") + + m.Get("/file2", func(ctx *Context) { + ctx.ServeFile("fixtures/custom_funcs/index.tmpl", "ok.tmpl") + }) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/file2", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "{{ myCustomFunc }}") + }) + + Convey("Serve file content", func() { + m.Get("/file", func(ctx *Context) { + ctx.ServeFileContent("fixtures/custom_funcs/index.tmpl") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/file", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "{{ myCustomFunc }}") + + m.Get("/file2", func(ctx *Context) { + ctx.ServeFileContent("fixtures/custom_funcs/index.tmpl", "ok.tmpl") + }) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/file2", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "{{ myCustomFunc }}") + + m.Get("/file3", func(ctx *Context) { + ctx.ServeFileContent("404.tmpl") + }) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/file3", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "open 404.tmpl: no such file or directory\n") + So(resp.Code, ShouldEqual, 500) + }) + + Convey("Serve content", func() { + m.Get("/content", func(ctx *Context) { + ctx.ServeContent("content1", bytes.NewReader([]byte("Hello world!"))) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/content", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "Hello world!") + + m.Get("/content2", func(ctx *Context) { + ctx.ServeContent("content1", bytes.NewReader([]byte("Hello world!")), time.Now()) + }) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/content2", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "Hello world!") + }) + }) +} + +func Test_Context_Render(t *testing.T) { + Convey("Invalid render", t, func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + + m := New() + m.Get("/", func(ctx *Context) { + ctx.HTML(200, "hey") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) +} + +func Test_Context_Redirect(t *testing.T) { + Convey("Context with default redirect", t, func() { + url, err := url.Parse("http://localhost/path/one") + So(err, ShouldBeNil) + resp := httptest.NewRecorder() + req := http.Request{ + Method: "GET", + URL: url, + } + ctx := &Context{ + Req: Request{&req}, + Resp: NewResponseWriter(resp), + Data: make(map[string]interface{}), + } + ctx.Redirect("two") + + So(resp.Code, ShouldEqual, http.StatusFound) + So(resp.HeaderMap["Location"][0], ShouldEqual, "/path/two") + }) + + Convey("Context with custom redirect", t, func() { + url, err := url.Parse("http://localhost/path/one") + So(err, ShouldBeNil) + resp := httptest.NewRecorder() + req := http.Request{ + Method: "GET", + URL: url, + } + ctx := &Context{ + Req: Request{&req}, + Resp: NewResponseWriter(resp), + Data: make(map[string]interface{}), + } + ctx.Redirect("two", 307) + + So(resp.Code, ShouldEqual, http.StatusTemporaryRedirect) + So(resp.HeaderMap["Location"][0], ShouldEqual, "/path/two") + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/admin/index.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/admin/index.tmpl new file mode 100644 index 0000000..b6a910c --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/admin/index.tmpl @@ -0,0 +1 @@ +

Admin {{.}}

\ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/another_layout.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/another_layout.tmpl new file mode 100644 index 0000000..66f698e --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/another_layout.tmpl @@ -0,0 +1 @@ +another head{{ yield }}another foot \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/content.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/content.tmpl new file mode 100644 index 0000000..0dbb973 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/content.tmpl @@ -0,0 +1 @@ +

{{ . }}

\ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/current_layout.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/current_layout.tmpl new file mode 100644 index 0000000..5076a58 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/current_layout.tmpl @@ -0,0 +1 @@ +{{ current }} head{{ yield }}{{ current }} foot \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/delims.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/delims.tmpl new file mode 100644 index 0000000..c6fc9c0 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/delims.tmpl @@ -0,0 +1 @@ +

Hello {[{.}]}

\ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/hello.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/hello.tmpl new file mode 100644 index 0000000..85a52cb --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/hello.tmpl @@ -0,0 +1 @@ +

Hello {{.}}

\ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/hypertext.html b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/hypertext.html new file mode 100644 index 0000000..3a82e2a --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/hypertext.html @@ -0,0 +1 @@ +Hypertext! \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/layout.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/layout.tmpl new file mode 100644 index 0000000..7c2c4ea --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic/layout.tmpl @@ -0,0 +1 @@ +head{{ yield }}foot \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic2/hello.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic2/hello.tmpl new file mode 100644 index 0000000..118e16c --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic2/hello.tmpl @@ -0,0 +1 @@ +

What's up, {{.}}

\ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic2/hello2.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic2/hello2.tmpl new file mode 100644 index 0000000..45122e3 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/basic2/hello2.tmpl @@ -0,0 +1 @@ +

Hello {{.Name}}

\ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/custom_funcs/index.tmpl b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/custom_funcs/index.tmpl new file mode 100644 index 0000000..936b6e9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/fixtures/custom_funcs/index.tmpl @@ -0,0 +1 @@ +{{ myCustomFunc }} \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/gzip.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/gzip.go new file mode 100644 index 0000000..2e935f3 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/gzip.go @@ -0,0 +1,81 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "bufio" + "compress/gzip" + "fmt" + "net" + "net/http" + "strings" +) + +const ( + HeaderAcceptEncoding = "Accept-Encoding" + HeaderContentEncoding = "Content-Encoding" + HeaderContentLength = "Content-Length" + HeaderContentType = "Content-Type" + HeaderVary = "Vary" +) + +// Gziper returns a Handler that adds gzip compression to all requests. +// Make sure to include the Gzip middleware above other middleware +// that alter the response body (like the render middleware). +func Gziper() Handler { + return func(ctx *Context) { + if !strings.Contains(ctx.Req.Header.Get(HeaderAcceptEncoding), "gzip") { + return + } + + headers := ctx.Resp.Header() + headers.Set(HeaderContentEncoding, "gzip") + headers.Set(HeaderVary, HeaderAcceptEncoding) + + gz := gzip.NewWriter(ctx.Resp) + defer gz.Close() + + gzw := gzipResponseWriter{gz, ctx.Resp} + ctx.Resp = gzw + ctx.MapTo(gzw, (*http.ResponseWriter)(nil)) + + ctx.Next() + + // delete content length after we know we have been written to + gzw.Header().Del("Content-Length") + } +} + +type gzipResponseWriter struct { + w *gzip.Writer + ResponseWriter +} + +func (grw gzipResponseWriter) Write(p []byte) (int, error) { + if len(grw.Header().Get(HeaderContentType)) == 0 { + grw.Header().Set(HeaderContentType, http.DetectContentType(p)) + } + + return grw.w.Write(p) +} + +func (grw gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { + hijacker, ok := grw.ResponseWriter.(http.Hijacker) + if !ok { + return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface") + } + return hijacker.Hijack() +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/gzip_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/gzip_test.go new file mode 100644 index 0000000..565eed6 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/gzip_test.go @@ -0,0 +1,65 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Gzip(t *testing.T) { + Convey("Gzip response content", t, func() { + before := false + + m := New() + m.Use(Gziper()) + m.Use(func(r http.ResponseWriter) { + r.(ResponseWriter).Before(func(rw ResponseWriter) { + before = true + }) + }) + m.Get("/", func() string { return "hello wolrd!" }) + + // Not yet gzip. + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + _, ok := resp.HeaderMap[HeaderContentEncoding] + So(ok, ShouldBeFalse) + + ce := resp.Header().Get(HeaderContentEncoding) + So(strings.EqualFold(ce, "gzip"), ShouldBeFalse) + + // Gzip now. + resp = httptest.NewRecorder() + req.Header.Set(HeaderAcceptEncoding, "gzip") + m.ServeHTTP(resp, req) + + _, ok = resp.HeaderMap[HeaderContentEncoding] + So(ok, ShouldBeTrue) + + ce = resp.Header().Get(HeaderContentEncoding) + So(strings.EqualFold(ce, "gzip"), ShouldBeTrue) + + So(before, ShouldBeTrue) + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/README.md b/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/README.md new file mode 100644 index 0000000..1721ab4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/README.md @@ -0,0 +1,4 @@ +inject +====== + +Dependency injection for go diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject.go new file mode 100644 index 0000000..87cbe22 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject.go @@ -0,0 +1,187 @@ +// Package inject provides utilities for mapping and injecting dependencies in various ways. +package inject + +import ( + "fmt" + "reflect" +) + +// Injector represents an interface for mapping and injecting dependencies into structs +// and function arguments. +type Injector interface { + Applicator + Invoker + TypeMapper + // SetParent sets the parent of the injector. If the injector cannot find a + // dependency in its Type map it will check its parent before returning an + // error. + SetParent(Injector) +} + +// Applicator represents an interface for mapping dependencies to a struct. +type Applicator interface { + // Maps dependencies in the Type map to each field in the struct + // that is tagged with 'inject'. Returns an error if the injection + // fails. + Apply(interface{}) error +} + +// Invoker represents an interface for calling functions via reflection. +type Invoker interface { + // Invoke attempts to call the interface{} provided as a function, + // providing dependencies for function arguments based on Type. Returns + // a slice of reflect.Value representing the returned values of the function. + // Returns an error if the injection fails. + Invoke(interface{}) ([]reflect.Value, error) +} + +// TypeMapper represents an interface for mapping interface{} values based on type. +type TypeMapper interface { + // Maps the interface{} value based on its immediate type from reflect.TypeOf. + Map(interface{}) TypeMapper + // Maps the interface{} value based on the pointer of an Interface provided. + // This is really only useful for mapping a value as an interface, as interfaces + // cannot at this time be referenced directly without a pointer. + MapTo(interface{}, interface{}) TypeMapper + // Provides a possibility to directly insert a mapping based on type and value. + // This makes it possible to directly map type arguments not possible to instantiate + // with reflect like unidirectional channels. + Set(reflect.Type, reflect.Value) TypeMapper + // Returns the Value that is mapped to the current type. Returns a zeroed Value if + // the Type has not been mapped. + GetVal(reflect.Type) reflect.Value +} + +type injector struct { + values map[reflect.Type]reflect.Value + parent Injector +} + +// InterfaceOf dereferences a pointer to an Interface type. +// It panics if value is not an pointer to an interface. +func InterfaceOf(value interface{}) reflect.Type { + t := reflect.TypeOf(value) + + for t.Kind() == reflect.Ptr { + t = t.Elem() + } + + if t.Kind() != reflect.Interface { + panic("Called inject.InterfaceOf with a value that is not a pointer to an interface. (*MyInterface)(nil)") + } + + return t +} + +// New returns a new Injector. +func New() Injector { + return &injector{ + values: make(map[reflect.Type]reflect.Value), + } +} + +// Invoke attempts to call the interface{} provided as a function, +// providing dependencies for function arguments based on Type. +// Returns a slice of reflect.Value representing the returned values of the function. +// Returns an error if the injection fails. +// It panics if f is not a function +func (inj *injector) Invoke(f interface{}) ([]reflect.Value, error) { + t := reflect.TypeOf(f) + + var in = make([]reflect.Value, t.NumIn()) //Panic if t is not kind of Func + for i := 0; i < t.NumIn(); i++ { + argType := t.In(i) + val := inj.GetVal(argType) + if !val.IsValid() { + return nil, fmt.Errorf("Value not found for type %v", argType) + } + + in[i] = val + } + + return reflect.ValueOf(f).Call(in), nil +} + +// Maps dependencies in the Type map to each field in the struct +// that is tagged with 'inject'. +// Returns an error if the injection fails. +func (inj *injector) Apply(val interface{}) error { + v := reflect.ValueOf(val) + + for v.Kind() == reflect.Ptr { + v = v.Elem() + } + + if v.Kind() != reflect.Struct { + return nil // Should not panic here ? + } + + t := v.Type() + + for i := 0; i < v.NumField(); i++ { + f := v.Field(i) + structField := t.Field(i) + if f.CanSet() && (structField.Tag == "inject" || structField.Tag.Get("inject") != "") { + ft := f.Type() + v := inj.GetVal(ft) + if !v.IsValid() { + return fmt.Errorf("Value not found for type %v", ft) + } + + f.Set(v) + } + + } + + return nil +} + +// Maps the concrete value of val to its dynamic type using reflect.TypeOf, +// It returns the TypeMapper registered in. +func (i *injector) Map(val interface{}) TypeMapper { + i.values[reflect.TypeOf(val)] = reflect.ValueOf(val) + return i +} + +func (i *injector) MapTo(val interface{}, ifacePtr interface{}) TypeMapper { + i.values[InterfaceOf(ifacePtr)] = reflect.ValueOf(val) + return i +} + +// Maps the given reflect.Type to the given reflect.Value and returns +// the Typemapper the mapping has been registered in. +func (i *injector) Set(typ reflect.Type, val reflect.Value) TypeMapper { + i.values[typ] = val + return i +} + +func (i *injector) GetVal(t reflect.Type) reflect.Value { + val := i.values[t] + + if val.IsValid() { + return val + } + + // no concrete types found, try to find implementors + // if t is an interface + if t.Kind() == reflect.Interface { + for k, v := range i.values { + if k.Implements(t) { + val = v + break + } + } + } + + // Still no type found, try to look it up on the parent + if !val.IsValid() && i.parent != nil { + val = i.parent.GetVal(t) + } + + return val + +} + +func (i *injector) SetParent(parent Injector) { + i.parent = parent +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject.goconvey b/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject.goconvey new file mode 100644 index 0000000..8485e98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject.goconvey @@ -0,0 +1 @@ +ignore \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject_test.go new file mode 100644 index 0000000..748572a --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/inject/inject_test.go @@ -0,0 +1,174 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 inject_test + +import ( + "fmt" + "reflect" + "testing" + + "github.com/Unknwon/macaron/inject" +) + +type SpecialString interface { +} + +type TestStruct struct { + Dep1 string `inject:"t" json:"-"` + Dep2 SpecialString `inject` + Dep3 string +} + +type Greeter struct { + Name string +} + +func (g *Greeter) String() string { + return "Hello, My name is" + g.Name +} + +/* Test Helpers */ +func expect(t *testing.T, a interface{}, b interface{}) { + if a != b { + t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) + } +} + +func refute(t *testing.T, a interface{}, b interface{}) { + if a == b { + t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) + } +} + +func Test_InjectorInvoke(t *testing.T) { + injector := inject.New() + expect(t, injector == nil, false) + + dep := "some dependency" + injector.Map(dep) + dep2 := "another dep" + injector.MapTo(dep2, (*SpecialString)(nil)) + dep3 := make(chan *SpecialString) + dep4 := make(chan *SpecialString) + typRecv := reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(dep3).Elem()) + typSend := reflect.ChanOf(reflect.SendDir, reflect.TypeOf(dep4).Elem()) + injector.Set(typRecv, reflect.ValueOf(dep3)) + injector.Set(typSend, reflect.ValueOf(dep4)) + + _, err := injector.Invoke(func(d1 string, d2 SpecialString, d3 <-chan *SpecialString, d4 chan<- *SpecialString) { + expect(t, d1, dep) + expect(t, d2, dep2) + expect(t, reflect.TypeOf(d3).Elem(), reflect.TypeOf(dep3).Elem()) + expect(t, reflect.TypeOf(d4).Elem(), reflect.TypeOf(dep4).Elem()) + expect(t, reflect.TypeOf(d3).ChanDir(), reflect.RecvDir) + expect(t, reflect.TypeOf(d4).ChanDir(), reflect.SendDir) + }) + + expect(t, err, nil) +} + +func Test_InjectorInvokeReturnValues(t *testing.T) { + injector := inject.New() + expect(t, injector == nil, false) + + dep := "some dependency" + injector.Map(dep) + dep2 := "another dep" + injector.MapTo(dep2, (*SpecialString)(nil)) + + result, err := injector.Invoke(func(d1 string, d2 SpecialString) string { + expect(t, d1, dep) + expect(t, d2, dep2) + return "Hello world" + }) + + expect(t, result[0].String(), "Hello world") + expect(t, err, nil) +} + +func Test_InjectorApply(t *testing.T) { + injector := inject.New() + + injector.Map("a dep").MapTo("another dep", (*SpecialString)(nil)) + + s := TestStruct{} + err := injector.Apply(&s) + expect(t, err, nil) + + expect(t, s.Dep1, "a dep") + expect(t, s.Dep2, "another dep") +} + +func Test_InterfaceOf(t *testing.T) { + iType := inject.InterfaceOf((*SpecialString)(nil)) + expect(t, iType.Kind(), reflect.Interface) + + iType = inject.InterfaceOf((**SpecialString)(nil)) + expect(t, iType.Kind(), reflect.Interface) + + // Expecting nil + defer func() { + rec := recover() + refute(t, rec, nil) + }() + iType = inject.InterfaceOf((*testing.T)(nil)) +} + +func Test_InjectorSet(t *testing.T) { + injector := inject.New() + typ := reflect.TypeOf("string") + typSend := reflect.ChanOf(reflect.SendDir, typ) + typRecv := reflect.ChanOf(reflect.RecvDir, typ) + + // instantiating unidirectional channels is not possible using reflect + // http://golang.org/src/pkg/reflect/value.go?s=60463:60504#L2064 + chanRecv := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, typ), 0) + chanSend := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, typ), 0) + + injector.Set(typSend, chanSend) + injector.Set(typRecv, chanRecv) + + expect(t, injector.GetVal(typSend).IsValid(), true) + expect(t, injector.GetVal(typRecv).IsValid(), true) + expect(t, injector.GetVal(chanSend.Type()).IsValid(), false) +} + +func Test_InjectorGet(t *testing.T) { + injector := inject.New() + + injector.Map("some dependency") + + expect(t, injector.GetVal(reflect.TypeOf("string")).IsValid(), true) + expect(t, injector.GetVal(reflect.TypeOf(11)).IsValid(), false) +} + +func Test_InjectorSetParent(t *testing.T) { + injector := inject.New() + injector.MapTo("another dep", (*SpecialString)(nil)) + + injector2 := inject.New() + injector2.SetParent(injector) + + expect(t, injector2.GetVal(inject.InterfaceOf((*SpecialString)(nil))).IsValid(), true) +} + +func TestInjectImplementors(t *testing.T) { + injector := inject.New() + g := &Greeter{"Jeremy"} + injector.Map(g) + + expect(t, injector.GetVal(inject.InterfaceOf((*fmt.Stringer)(nil))).IsValid(), true) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/logger.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/logger.go new file mode 100644 index 0000000..6e0b25c --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/logger.go @@ -0,0 +1,61 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "fmt" + "log" + "net/http" + "runtime" + "time" +) + +var ColorLog = true + +func init() { + ColorLog = runtime.GOOS != "windows" +} + +// Logger returns a middleware handler that logs the request as it goes in and the response as it goes out. +func Logger() Handler { + return func(ctx *Context, log *log.Logger) { + start := time.Now() + + log.Printf("Started %s %s for %s", ctx.Req.Method, ctx.Req.RequestURI, ctx.RemoteAddr()) + + rw := ctx.Resp.(ResponseWriter) + ctx.Next() + + content := fmt.Sprintf("Completed %s %v %s in %v", ctx.Req.RequestURI, rw.Status(), http.StatusText(rw.Status()), time.Since(start)) + if ColorLog { + switch rw.Status() { + case 200, 201, 202: + content = fmt.Sprintf("\033[1;32m%s\033[0m", content) + case 301, 302: + content = fmt.Sprintf("\033[1;37m%s\033[0m", content) + case 304: + content = fmt.Sprintf("\033[1;33m%s\033[0m", content) + case 401, 403: + content = fmt.Sprintf("\033[4;31m%s\033[0m", content) + case 404: + content = fmt.Sprintf("\033[1;31m%s\033[0m", content) + case 500: + content = fmt.Sprintf("\033[1;36m%s\033[0m", content) + } + } + log.Println(content) + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/logger_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/logger_test.go new file mode 100644 index 0000000..c81f702 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/logger_test.go @@ -0,0 +1,67 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "bytes" + "log" + "net/http" + "net/http/httptest" + "testing" + + "github.com/Unknwon/com" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Logger(t *testing.T) { + Convey("Global logger", t, func() { + buf := bytes.NewBufferString("") + m := New() + m.Map(log.New(buf, "[Macaron] ", 0)) + m.Use(Logger()) + m.Use(func(res http.ResponseWriter) { + res.WriteHeader(http.StatusNotFound) + }) + m.Get("/", func() {}) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldEqual, http.StatusNotFound) + So(len(buf.String()), ShouldBeGreaterThan, 0) + }) + + if ColorLog { + Convey("Color console output", t, func() { + m := Classic() + m.Get("/:code:int", func(ctx *Context) (int, string) { + return ctx.ParamsInt(":code"), "" + }) + + // Just for testing if logger would capture. + codes := []int{200, 201, 202, 301, 302, 304, 401, 403, 404, 500} + for _, code := range codes { + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/"+com.ToStr(code), nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldEqual, code) + } + }) + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/macaron.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/macaron.go new file mode 100644 index 0000000..adbe9e3 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/macaron.go @@ -0,0 +1,273 @@ +// Copyright 2014 Unknwon +// +// 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 macaron is a high productive and modular design web framework in Go. +package macaron + +import ( + "io" + "log" + "net/http" + "os" + "reflect" + "strings" + + "github.com/Unknwon/com" + "gopkg.in/ini.v1" + + "github.com/Unknwon/macaron/inject" +) + +const _VERSION = "0.5.4.0318" + +func Version() string { + return _VERSION +} + +// Handler can be any callable function. +// Macaron attempts to inject services into the handler's argument list, +// and panics if an argument could not be fullfilled via dependency injection. +type Handler interface{} + +// validateHandler makes sure a handler is a callable function, +// and panics if it is not. +func validateHandler(h Handler) { + if reflect.TypeOf(h).Kind() != reflect.Func { + panic("Macaron handler must be a callable function") + } +} + +// validateHandlers makes sure handlers are callable functions, +// and panics if any of them is not. +func validateHandlers(handlers []Handler) { + for _, h := range handlers { + validateHandler(h) + } +} + +// Macaron represents the top level web application. +// inject.Injector methods can be invoked to map services on a global level. +type Macaron struct { + inject.Injector + befores []BeforeHandler + handlers []Handler + action Handler + + urlPrefix string // For suburl support. + *Router + + logger *log.Logger +} + +// NewWithLogger creates a bare bones Macaron instance. +// Use this method if you want to have full control over the middleware that is used. +// You can specify logger output writer with this function. +func NewWithLogger(out io.Writer) *Macaron { + m := &Macaron{ + Injector: inject.New(), + action: func() {}, + Router: NewRouter(), + logger: log.New(out, "[Macaron] ", 0), + } + m.Router.m = m + m.Map(m.logger) + m.Map(defaultReturnHandler()) + m.notFound = func(resp http.ResponseWriter, req *http.Request) { + c := m.createContext(resp, req) + c.handlers = append(c.handlers, http.NotFound) + c.run() + } + return m +} + +// New creates a bare bones Macaron instance. +// Use this method if you want to have full control over the middleware that is used. +func New() *Macaron { + return NewWithLogger(os.Stdout) +} + +// Classic creates a classic Macaron with some basic default middleware: +// mocaron.Logger, mocaron.Recovery and mocaron.Static. +func Classic() *Macaron { + m := New() + m.Use(Logger()) + m.Use(Recovery()) + m.Use(Static("public")) + return m +} + +// Handlers sets the entire middleware stack with the given Handlers. +// This will clear any current middleware handlers, +// and panics if any of the handlers is not a callable function +func (m *Macaron) Handlers(handlers ...Handler) { + m.handlers = make([]Handler, 0) + for _, handler := range handlers { + m.Use(handler) + } +} + +// Action sets the handler that will be called after all the middleware has been invoked. +// This is set to macaron.Router in a macaron.Classic(). +func (m *Macaron) Action(handler Handler) { + validateHandler(handler) + m.action = handler +} + +// BeforeHandler represents a handler executes at beginning of every request. +// Macaron stops future process when it returns true. +type BeforeHandler func(rw http.ResponseWriter, req *http.Request) bool + +func (m *Macaron) Before(handler BeforeHandler) { + m.befores = append(m.befores, handler) +} + +// Use adds a middleware Handler to the stack, +// and panics if the handler is not a callable func. +// Middleware Handlers are invoked in the order that they are added. +func (m *Macaron) Use(handler Handler) { + validateHandler(handler) + m.handlers = append(m.handlers, handler) +} + +func (m *Macaron) createContext(rw http.ResponseWriter, req *http.Request) *Context { + c := &Context{ + Injector: inject.New(), + handlers: m.handlers, + action: m.action, + index: 0, + Router: m.Router, + Req: Request{req}, + Resp: NewResponseWriter(rw), + Data: make(map[string]interface{}), + } + c.SetParent(m) + c.Map(c) + c.MapTo(c.Resp, (*http.ResponseWriter)(nil)) + c.Map(req) + return c +} + +// ServeHTTP is the HTTP Entry point for a Macaron instance. +// Useful if you want to control your own HTTP server. +// Be aware that none of middleware will run without registering any router. +func (m *Macaron) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + req.URL.Path = strings.TrimPrefix(req.URL.Path, m.urlPrefix) + for _, h := range m.befores { + if h(rw, req) { + return + } + } + m.Router.ServeHTTP(rw, req) +} + +func GetDefaultListenInfo() (string, int) { + host := os.Getenv("HOST") + if len(host) == 0 { + host = "0.0.0.0" + } + port := com.StrTo(os.Getenv("PORT")).MustInt() + if port == 0 { + port = 4000 + } + return host, port +} + +// Run the http server. Listening on os.GetEnv("PORT") or 4000 by default. +func (m *Macaron) Run(args ...interface{}) { + host, port := GetDefaultListenInfo() + if len(args) == 1 { + switch arg := args[0].(type) { + case string: + host = arg + case int: + port = arg + } + } else if len(args) >= 2 { + if arg, ok := args[0].(string); ok { + host = arg + } + if arg, ok := args[1].(int); ok { + port = arg + } + } + + addr := host + ":" + com.ToStr(port) + logger := m.Injector.GetVal(reflect.TypeOf(m.logger)).Interface().(*log.Logger) + logger.Printf("listening on %s (%s)\n", addr, Env) + logger.Fatalln(http.ListenAndServe(addr, m)) +} + +// SetURLPrefix sets URL prefix of router layer, so that it support suburl. +func (m *Macaron) SetURLPrefix(prefix string) { + m.urlPrefix = prefix +} + +// ____ ____ .__ ___. .__ +// \ \ / /____ _______|__|____ \_ |__ | | ____ ______ +// \ Y /\__ \\_ __ \ \__ \ | __ \| | _/ __ \ / ___/ +// \ / / __ \| | \/ |/ __ \| \_\ \ |_\ ___/ \___ \ +// \___/ (____ /__| |__(____ /___ /____/\___ >____ > +// \/ \/ \/ \/ \/ + +const ( + DEV = "development" + PROD = "production" + TEST = "test" +) + +var ( + // Env is the environment that Macaron is executing in. + // The MACARON_ENV is read on initialization to set this variable. + Env = DEV + + // Path of work directory. + Root string + + // Flash applies to current request. + FlashNow bool + + // Configuration convention object. + cfg *ini.File +) + +func setENV(e string) { + if len(e) > 0 { + Env = e + } +} + +func init() { + setENV(os.Getenv("MACARON_ENV")) + + var err error + Root, err = os.Getwd() + if err != nil { + panic("error getting work directory: " + err.Error()) + } +} + +// SetConfig sets data sources for configuration. +func SetConfig(source interface{}, others ...interface{}) (_ *ini.File, err error) { + cfg, err = ini.Load(source, others...) + return Config(), err +} + +// Config returns configuration convention object. +// It returns an empty object if there is no one available. +func Config() *ini.File { + if cfg == nil { + return ini.Empty() + } + return cfg +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/macaron_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/macaron_test.go new file mode 100644 index 0000000..35cc8f8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/macaron_test.go @@ -0,0 +1,218 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Version(t *testing.T) { + Convey("Get version", t, func() { + So(Version(), ShouldEqual, _VERSION) + }) +} + +func Test_New(t *testing.T) { + Convey("Initialize a new instance", t, func() { + So(New(), ShouldNotBeNil) + }) + + Convey("Just test that Run doesn't bomb", t, func() { + go New().Run() + time.Sleep(1 * time.Second) + os.Setenv("PORT", "4001") + go New().Run("0.0.0.0") + go New().Run(4002) + go New().Run("0.0.0.0", 4003) + }) +} + +func Test_Macaron_Before(t *testing.T) { + Convey("Register before handlers", t, func() { + m := New() + m.Before(func(rw http.ResponseWriter, req *http.Request) bool { + return false + }) + m.Before(func(rw http.ResponseWriter, req *http.Request) bool { + return true + }) + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) +} + +func Test_Macaron_ServeHTTP(t *testing.T) { + Convey("Serve HTTP requests", t, func() { + result := "" + m := New() + m.Use(func(c *Context) { + result += "foo" + c.Next() + result += "ban" + }) + m.Use(func(c *Context) { + result += "bar" + c.Next() + result += "baz" + }) + m.Get("/", func() {}) + m.Action(func(res http.ResponseWriter, req *http.Request) { + result += "bat" + res.WriteHeader(http.StatusBadRequest) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(result, ShouldEqual, "foobarbatbazban") + So(resp.Code, ShouldEqual, http.StatusBadRequest) + }) +} + +func Test_Macaron_Handlers(t *testing.T) { + Convey("Add custom handlers", t, func() { + result := "" + batman := func(c *Context) { + result += "batman!" + } + + m := New() + m.Use(func(c *Context) { + result += "foo" + c.Next() + result += "ban" + }) + m.Handlers( + batman, + batman, + batman, + ) + + Convey("Add not callable function", func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + m.Use("shit") + }) + + m.Get("/", func() {}) + m.Action(func(res http.ResponseWriter, req *http.Request) { + result += "bat" + res.WriteHeader(http.StatusBadRequest) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(result, ShouldEqual, "batman!batman!batman!bat") + So(resp.Code, ShouldEqual, http.StatusBadRequest) + }) +} + +func Test_Macaron_EarlyWrite(t *testing.T) { + Convey("Write early content to response", t, func() { + result := "" + m := New() + m.Use(func(res http.ResponseWriter) { + result += "foobar" + res.Write([]byte("Hello world")) + }) + m.Use(func() { + result += "bat" + }) + m.Get("/", func() {}) + m.Action(func(res http.ResponseWriter) { + result += "baz" + res.WriteHeader(http.StatusBadRequest) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(result, ShouldEqual, "foobar") + So(resp.Code, ShouldEqual, http.StatusOK) + }) +} + +func Test_Macaron_Written(t *testing.T) { + Convey("Written sign", t, func() { + resp := httptest.NewRecorder() + m := New() + m.Handlers(func(res http.ResponseWriter) { + res.WriteHeader(http.StatusOK) + }) + + ctx := m.createContext(resp, &http.Request{Method: "GET"}) + So(ctx.Written(), ShouldBeFalse) + + ctx.run() + So(ctx.Written(), ShouldBeTrue) + }) +} + +func Test_Macaron_Basic_NoRace(t *testing.T) { + Convey("Make sure no race between requests", t, func() { + m := New() + handlers := []Handler{func() {}, func() {}} + // Ensure append will not realloc to trigger the race condition + m.handlers = handlers[:1] + m.Get("/", func() {}) + req, _ := http.NewRequest("GET", "/", nil) + for i := 0; i < 2; i++ { + go func() { + resp := httptest.NewRecorder() + m.ServeHTTP(resp, req) + }() + } + }) +} + +func Test_SetENV(t *testing.T) { + Convey("Get and save environment variable", t, func() { + tests := []struct { + in string + out string + }{ + {"", "development"}, + {"not_development", "not_development"}, + } + + for _, test := range tests { + setENV(test.in) + So(Env, ShouldEqual, test.out) + } + }) +} + +func Test_Config(t *testing.T) { + Convey("Set and get configuration object", t, func() { + So(Config(), ShouldNotBeNil) + cfg, err := SetConfig([]byte("")) + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/macaronlogo.png b/Godeps/_workspace/src/github.com/Unknwon/macaron/macaronlogo.png new file mode 100644 index 0000000..3997597 Binary files /dev/null and b/Godeps/_workspace/src/github.com/Unknwon/macaron/macaronlogo.png differ diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/recovery.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/recovery.go new file mode 100644 index 0000000..6ff1659 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/recovery.go @@ -0,0 +1,163 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "bytes" + "fmt" + "io/ioutil" + "log" + "net/http" + "runtime" + + "github.com/Unknwon/macaron/inject" +) + +const ( + panicHtml = ` +PANIC: %s + + + +

PANIC

+
%s
+
%s
+ +` +) + +var ( + dunno = []byte("???") + centerDot = []byte("·") + dot = []byte(".") + slash = []byte("/") +) + +// stack returns a nicely formated stack frame, skipping skip frames +func stack(skip int) []byte { + buf := new(bytes.Buffer) // the returned data + // As we loop, we open files and read them. These variables record the currently + // loaded file. + var lines [][]byte + var lastFile string + for i := skip; ; i++ { // Skip the expected number of frames + pc, file, line, ok := runtime.Caller(i) + if !ok { + break + } + // Print this much at least. If we can't find the source, it won't show. + fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc) + if file != lastFile { + data, err := ioutil.ReadFile(file) + if err != nil { + continue + } + lines = bytes.Split(data, []byte{'\n'}) + lastFile = file + } + fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line)) + } + return buf.Bytes() +} + +// source returns a space-trimmed slice of the n'th line. +func source(lines [][]byte, n int) []byte { + n-- // in stack trace, lines are 1-indexed but our array is 0-indexed + if n < 0 || n >= len(lines) { + return dunno + } + return bytes.TrimSpace(lines[n]) +} + +// function returns, if possible, the name of the function containing the PC. +func function(pc uintptr) []byte { + fn := runtime.FuncForPC(pc) + if fn == nil { + return dunno + } + name := []byte(fn.Name()) + // The name includes the path name to the package, which is unnecessary + // since the file name is already included. Plus, it has center dots. + // That is, we see + // runtime/debug.*T·ptrmethod + // and want + // *T.ptrmethod + // Also the package path might contains dot (e.g. code.google.com/...), + // so first eliminate the path prefix + if lastslash := bytes.LastIndex(name, slash); lastslash >= 0 { + name = name[lastslash+1:] + } + if period := bytes.Index(name, dot); period >= 0 { + name = name[period+1:] + } + name = bytes.Replace(name, centerDot, dot, -1) + return name +} + +// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. +// While Martini is in development mode, Recovery will also output the panic as HTML. +func Recovery() Handler { + return func(c *Context, log *log.Logger) { + defer func() { + if err := recover(); err != nil { + stack := stack(3) + log.Printf("PANIC: %s\n%s", err, stack) + + // Lookup the current responsewriter + val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil))) + res := val.Interface().(http.ResponseWriter) + + // respond with panic message while in development mode + var body []byte + if Env == DEV { + res.Header().Set("Content-Type", "text/html") + body = []byte(fmt.Sprintf(panicHtml, err, err, stack)) + } + + res.WriteHeader(http.StatusInternalServerError) + if nil != body { + res.Write(body) + } + } + }() + + c.Next() + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/recovery_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/recovery_test.go new file mode 100644 index 0000000..dc9b341 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/recovery_test.go @@ -0,0 +1,74 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "bytes" + "log" + "net/http" + "net/http/httptest" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Recovery(t *testing.T) { + Convey("Recovery from panic", t, func() { + buf := bytes.NewBufferString("") + setENV(DEV) + + m := New() + m.Map(log.New(buf, "[Macaron] ", 0)) + m.Use(func(res http.ResponseWriter, req *http.Request) { + res.Header().Set("Content-Type", "unpredictable") + }) + m.Use(Recovery()) + m.Use(func(res http.ResponseWriter, req *http.Request) { + panic("here is a panic!") + }) + m.Get("/", func() {}) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldEqual, http.StatusInternalServerError) + So(resp.HeaderMap.Get("Content-Type"), ShouldEqual, "text/html") + So(buf.String(), ShouldNotBeEmpty) + }) + + Convey("Revocery panic to another response writer", t, func() { + resp := httptest.NewRecorder() + resp2 := httptest.NewRecorder() + setENV(DEV) + + m := New() + m.Use(Recovery()) + m.Use(func(c *Context) { + c.MapTo(resp2, (*http.ResponseWriter)(nil)) + panic("here is a panic!") + }) + m.Get("/", func() {}) + + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp2.Code, ShouldEqual, http.StatusInternalServerError) + So(resp2.HeaderMap.Get("Content-Type"), ShouldEqual, "text/html") + So(resp2.Body.Len(), ShouldBeGreaterThan, 0) + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/render.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/render.go new file mode 100644 index 0000000..b0558c9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/render.go @@ -0,0 +1,624 @@ +// Copyright 2013 Martini Authors +// Copyright 2013 oxtoacart +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "bytes" + "encoding/json" + "encoding/xml" + "fmt" + "html/template" + "io" + "io/ioutil" + "net/http" + "os" + "path" + "path/filepath" + "strings" + "sync" + "time" + + "github.com/Unknwon/com" +) + +// BufferPool implements a pool of bytes.Buffers in the form of a bounded channel. +type BufferPool struct { + c chan *bytes.Buffer +} + +// NewBufferPool creates a new BufferPool bounded to the given size. +func NewBufferPool(size int) (bp *BufferPool) { + return &BufferPool{ + c: make(chan *bytes.Buffer, size), + } +} + +// Get gets a Buffer from the BufferPool, or creates a new one if none are available +// in the pool. +func (bp *BufferPool) Get() (b *bytes.Buffer) { + select { + case b = <-bp.c: + // reuse existing buffer + default: + // create new buffer + b = bytes.NewBuffer([]byte{}) + } + return +} + +// Put returns the given Buffer to the BufferPool. +func (bp *BufferPool) Put(b *bytes.Buffer) { + b.Reset() + bp.c <- b +} + +const ( + ContentType = "Content-Type" + ContentLength = "Content-Length" + ContentBinary = "application/octet-stream" + ContentJSON = "application/json" + ContentHTML = "text/html" + CONTENT_PLAIN = "text/plain" + ContentXHTML = "application/xhtml+xml" + ContentXML = "text/xml" + defaultCharset = "UTF-8" +) + +var ( + // Provides a temporary buffer to execute templates into and catch errors. + bufpool = NewBufferPool(64) + + // Included helper functions for use when rendering html + helperFuncs = template.FuncMap{ + "yield": func() (string, error) { + return "", fmt.Errorf("yield called with no layout defined") + }, + "current": func() (string, error) { + return "", nil + }, + } +) + +type ( + // TemplateFile represents a interface of template file that has name and can be read. + TemplateFile interface { + Name() string + Data() []byte + Ext() string + } + // TemplateFileSystem represents a interface of template file system that able to list all files. + TemplateFileSystem interface { + ListFiles() []TemplateFile + } + + // Delims represents a set of Left and Right delimiters for HTML template rendering + Delims struct { + // Left delimiter, defaults to {{ + Left string + // Right delimiter, defaults to }} + Right string + } + + // RenderOptions represents a struct for specifying configuration options for the Render middleware. + RenderOptions struct { + // Directory to load templates. Default is "templates". + Directory string + // Layout template name. Will not render a layout if "". Default is to "". + Layout string + // Extensions to parse template files from. Defaults are [".tmpl", ".html"]. + Extensions []string + // Funcs is a slice of FuncMaps to apply to the template upon compilation. This is useful for helper functions. Default is []. + Funcs []template.FuncMap + // Delims sets the action delimiters to the specified strings in the Delims struct. + Delims Delims + // Appends the given charset to the Content-Type header. Default is "UTF-8". + Charset string + // Outputs human readable JSON. + IndentJSON bool + // Outputs human readable XML. + IndentXML bool + // Prefixes the JSON output with the given bytes. + PrefixJSON []byte + // Prefixes the XML output with the given bytes. + PrefixXML []byte + // Allows changing of output to XHTML instead of HTML. Default is "text/html" + HTMLContentType string + // TemplateFileSystem is the interface for supporting any implmentation of template file system. + TemplateFileSystem + } + + // HTMLOptions is a struct for overriding some rendering Options for specific HTML call + HTMLOptions struct { + // Layout template name. Overrides Options.Layout. + Layout string + } + + Render interface { + http.ResponseWriter + RW() http.ResponseWriter + + JSON(int, interface{}) + JSONString(interface{}) (string, error) + RawData(int, []byte) + RenderData(int, []byte) + HTML(int, string, interface{}, ...HTMLOptions) + HTMLSet(int, string, string, interface{}, ...HTMLOptions) + HTMLSetString(string, string, interface{}, ...HTMLOptions) (string, error) + HTMLString(string, interface{}, ...HTMLOptions) (string, error) + HTMLSetBytes(string, string, interface{}, ...HTMLOptions) ([]byte, error) + HTMLBytes(string, interface{}, ...HTMLOptions) ([]byte, error) + XML(int, interface{}) + Error(int, ...string) + Status(int) + SetTemplatePath(string, string) + HasTemplateSet(string) bool + } +) + +// TplFile implements TemplateFile interface. +type TplFile struct { + name string + data []byte + ext string +} + +// NewTplFile cerates new template file with given name and data. +func NewTplFile(name string, data []byte, ext string) *TplFile { + return &TplFile{name, data, ext} +} + +func (f *TplFile) Name() string { + return f.name +} + +func (f *TplFile) Data() []byte { + return f.data +} + +func (f *TplFile) Ext() string { + return f.ext +} + +// TplFileSystem implements TemplateFileSystem interface. +type TplFileSystem struct { + files []TemplateFile +} + +// NewTemplateFileSystem creates new template file system with given options. +func NewTemplateFileSystem(opt RenderOptions, omitData bool) TplFileSystem { + fs := TplFileSystem{} + fs.files = make([]TemplateFile, 0, 10) + + if err := filepath.Walk(opt.Directory, func(path string, info os.FileInfo, err error) error { + r, err := filepath.Rel(opt.Directory, path) + if err != nil { + return err + } + + ext := GetExt(r) + + for _, extension := range opt.Extensions { + if ext == extension { + var data []byte + if !omitData { + data, err = ioutil.ReadFile(path) + if err != nil { + return err + } + } + + name := filepath.ToSlash((r[0 : len(r)-len(ext)])) + fs.files = append(fs.files, NewTplFile(name, data, ext)) + break + } + } + + return nil + }); err != nil { + panic("NewTemplateFileSystem: " + err.Error()) + } + + return fs +} + +func (fs TplFileSystem) ListFiles() []TemplateFile { + return fs.files +} + +func PrepareCharset(charset string) string { + if len(charset) != 0 { + return "; charset=" + charset + } + + return "; charset=" + defaultCharset +} + +func GetExt(s string) string { + index := strings.Index(s, ".") + if index == -1 { + return "" + } + return s[index:] +} + +func compile(opt RenderOptions) *template.Template { + dir := opt.Directory + t := template.New(dir) + t.Delims(opt.Delims.Left, opt.Delims.Right) + // Parse an initial template in case we don't have any. + template.Must(t.Parse("Macaron")) + + if opt.TemplateFileSystem == nil { + opt.TemplateFileSystem = NewTemplateFileSystem(opt, false) + } + + for _, f := range opt.TemplateFileSystem.ListFiles() { + tmpl := t.New(f.Name()) + for _, funcs := range opt.Funcs { + tmpl.Funcs(funcs) + } + // Bomb out if parse fails. We don't want any silent server starts. + template.Must(tmpl.Funcs(helperFuncs).Parse(string(f.Data()))) + } + + return t +} + +const ( + _DEFAULT_TPL_SET_NAME = "DEFAULT" +) + +// templateSet represents a template set of type *template.Template. +type templateSet struct { + lock sync.RWMutex + sets map[string]*template.Template + dirs map[string]string +} + +func newTemplateSet() *templateSet { + return &templateSet{ + sets: make(map[string]*template.Template), + dirs: make(map[string]string), + } +} + +func (ts *templateSet) Set(name string, opt *RenderOptions) *template.Template { + t := compile(*opt) + + ts.lock.Lock() + defer ts.lock.Unlock() + + ts.sets[name] = t + ts.dirs[name] = opt.Directory + return t +} + +func (ts *templateSet) Get(name string) *template.Template { + ts.lock.RLock() + defer ts.lock.RUnlock() + + return ts.sets[name] +} + +func (ts *templateSet) GetDir(name string) string { + ts.lock.RLock() + defer ts.lock.RUnlock() + + return ts.dirs[name] +} + +func prepareOptions(options []RenderOptions) RenderOptions { + var opt RenderOptions + if len(options) > 0 { + opt = options[0] + } + + // Defaults. + if len(opt.Directory) == 0 { + opt.Directory = "templates" + } + if len(opt.Extensions) == 0 { + opt.Extensions = []string{".tmpl", ".html"} + } + if len(opt.HTMLContentType) == 0 { + opt.HTMLContentType = ContentHTML + } + + return opt +} + +func ParseTplSet(tplSet string) (tplName string, tplDir string) { + tplSet = strings.TrimSpace(tplSet) + if len(tplSet) == 0 { + panic("empty template set argument") + } + infos := strings.Split(tplSet, ":") + if len(infos) == 1 { + tplDir = infos[0] + tplName = path.Base(tplDir) + } else { + tplName = infos[0] + tplDir = infos[1] + } + + if !com.IsDir(tplDir) { + panic("template set path does not exist or is not a directory") + } + return tplName, tplDir +} + +func renderHandler(opt RenderOptions, tplSets []string) Handler { + cs := PrepareCharset(opt.Charset) + ts := newTemplateSet() + ts.Set(_DEFAULT_TPL_SET_NAME, &opt) + + var tmpOpt RenderOptions + for _, tplSet := range tplSets { + tplName, tplDir := ParseTplSet(tplSet) + tmpOpt = opt + tmpOpt.Directory = tplDir + ts.Set(tplName, &tmpOpt) + } + + return func(ctx *Context) { + r := &TplRender{ + ResponseWriter: ctx.Resp, + templateSet: ts, + Opt: &opt, + CompiledCharset: cs, + } + ctx.Data["TmplLoadTimes"] = func() string { + if r.startTime.IsZero() { + return "" + } + return fmt.Sprint(time.Since(r.startTime).Nanoseconds()/1e6) + "ms" + } + + ctx.Render = r + ctx.MapTo(r, (*Render)(nil)) + } +} + +// Renderer is a Middleware that maps a macaron.Render service into the Macaron handler chain. +// An single variadic macaron.RenderOptions struct can be optionally provided to configure +// HTML rendering. The default directory for templates is "templates" and the default +// file extension is ".tmpl" and ".html". +// +// If MACARON_ENV is set to "" or "development" then templates will be recompiled on every request. For more performance, set the +// MACARON_ENV environment variable to "production". +func Renderer(options ...RenderOptions) Handler { + return renderHandler(prepareOptions(options), []string{}) +} + +func Renderers(options RenderOptions, tplSets ...string) Handler { + return renderHandler(prepareOptions([]RenderOptions{options}), tplSets) +} + +type TplRender struct { + http.ResponseWriter + *templateSet + Opt *RenderOptions + CompiledCharset string + + startTime time.Time +} + +func (r *TplRender) RW() http.ResponseWriter { + return r.ResponseWriter +} + +func (r *TplRender) JSON(status int, v interface{}) { + var ( + result []byte + err error + ) + if r.Opt.IndentJSON { + result, err = json.MarshalIndent(v, "", " ") + } else { + result, err = json.Marshal(v) + } + if err != nil { + http.Error(r, err.Error(), 500) + return + } + + // json rendered fine, write out the result + r.Header().Set(ContentType, ContentJSON+r.CompiledCharset) + r.WriteHeader(status) + if len(r.Opt.PrefixJSON) > 0 { + r.Write(r.Opt.PrefixJSON) + } + r.Write(result) +} + +func (r *TplRender) JSONString(v interface{}) (string, error) { + var result []byte + var err error + if r.Opt.IndentJSON { + result, err = json.MarshalIndent(v, "", " ") + } else { + result, err = json.Marshal(v) + } + if err != nil { + return "", err + } + return string(result), nil +} + +func (r *TplRender) XML(status int, v interface{}) { + var result []byte + var err error + if r.Opt.IndentXML { + result, err = xml.MarshalIndent(v, "", " ") + } else { + result, err = xml.Marshal(v) + } + if err != nil { + http.Error(r, err.Error(), 500) + return + } + + // XML rendered fine, write out the result + r.Header().Set(ContentType, ContentXML+r.CompiledCharset) + r.WriteHeader(status) + if len(r.Opt.PrefixXML) > 0 { + r.Write(r.Opt.PrefixXML) + } + r.Write(result) +} + +func (r *TplRender) data(status int, contentType string, v []byte) { + if r.Header().Get(ContentType) == "" { + r.Header().Set(ContentType, contentType) + } + r.WriteHeader(status) + r.Write(v) +} + +func (r *TplRender) RawData(status int, v []byte) { + r.data(status, ContentBinary, v) +} + +func (r *TplRender) RenderData(status int, v []byte) { + r.data(status, CONTENT_PLAIN, v) +} + +func (r *TplRender) execute(t *template.Template, name string, data interface{}) (*bytes.Buffer, error) { + buf := bufpool.Get() + return buf, t.ExecuteTemplate(buf, name, data) +} + +func (r *TplRender) addYield(t *template.Template, tplName string, data interface{}) { + funcs := template.FuncMap{ + "yield": func() (template.HTML, error) { + buf, err := r.execute(t, tplName, data) + // return safe html here since we are rendering our own template + return template.HTML(buf.String()), err + }, + "current": func() (string, error) { + return tplName, nil + }, + } + t.Funcs(funcs) +} + +func (r *TplRender) renderBytes(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) (*bytes.Buffer, error) { + t := r.templateSet.Get(setName) + if Env == DEV { + opt := *r.Opt + opt.Directory = r.templateSet.GetDir(setName) + t = r.templateSet.Set(setName, &opt) + } + if t == nil { + return nil, fmt.Errorf("html/template: template \"%s\" is undefined", tplName) + } + + opt := r.prepareHTMLOptions(htmlOpt) + + if len(opt.Layout) > 0 { + r.addYield(t, tplName, data) + tplName = opt.Layout + } + + out, err := r.execute(t, tplName, data) + if err != nil { + return nil, err + } + + return out, nil +} + +func (r *TplRender) renderHTML(status int, setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) { + r.startTime = time.Now() + + out, err := r.renderBytes(setName, tplName, data, htmlOpt...) + if err != nil { + http.Error(r, err.Error(), http.StatusInternalServerError) + return + } + + r.Header().Set(ContentType, r.Opt.HTMLContentType+r.CompiledCharset) + r.WriteHeader(status) + + io.Copy(r, out) + bufpool.Put(out) +} + +func (r *TplRender) HTML(status int, name string, data interface{}, htmlOpt ...HTMLOptions) { + r.renderHTML(status, _DEFAULT_TPL_SET_NAME, name, data, htmlOpt...) +} + +func (r *TplRender) HTMLSet(status int, setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) { + r.renderHTML(status, setName, tplName, data, htmlOpt...) +} + +func (r *TplRender) HTMLSetBytes(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error) { + out, err := r.renderBytes(setName, tplName, data, htmlOpt...) + if err != nil { + return []byte(""), err + } + return out.Bytes(), nil +} + +func (r *TplRender) HTMLBytes(name string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error) { + return r.HTMLSetBytes(_DEFAULT_TPL_SET_NAME, name, data, htmlOpt...) +} + +func (r *TplRender) HTMLSetString(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) (string, error) { + p, err := r.HTMLSetBytes(setName, tplName, data, htmlOpt...) + return string(p), err +} + +func (r *TplRender) HTMLString(name string, data interface{}, htmlOpt ...HTMLOptions) (string, error) { + p, err := r.HTMLBytes(name, data, htmlOpt...) + return string(p), err +} + +// Error writes the given HTTP status to the current ResponseWriter +func (r *TplRender) Error(status int, message ...string) { + r.WriteHeader(status) + if len(message) > 0 { + r.Write([]byte(message[0])) + } +} + +func (r *TplRender) Status(status int) { + r.WriteHeader(status) +} + +func (r *TplRender) prepareHTMLOptions(htmlOpt []HTMLOptions) HTMLOptions { + if len(htmlOpt) > 0 { + return htmlOpt[0] + } + + return HTMLOptions{ + Layout: r.Opt.Layout, + } +} + +func (r *TplRender) SetTemplatePath(setName, dir string) { + if len(setName) == 0 { + setName = _DEFAULT_TPL_SET_NAME + } + opt := *r.Opt + opt.Directory = dir + r.templateSet.Set(setName, &opt) +} + +func (r *TplRender) HasTemplateSet(name string) bool { + return r.templateSet.Get(name) != nil +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/render_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/render_test.go new file mode 100644 index 0000000..1631873 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/render_test.go @@ -0,0 +1,581 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "encoding/xml" + "html/template" + "net/http" + "net/http/httptest" + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +type Greeting struct { + One string `json:"one"` + Two string `json:"two"` +} + +type GreetingXML struct { + XMLName xml.Name `xml:"greeting"` + One string `xml:"one,attr"` + Two string `xml:"two,attr"` +} + +func Test_Render_JSON(t *testing.T) { + Convey("Render JSON", t, func() { + m := Classic() + m.Use(Renderer()) + m.Get("/foobar", func(r Render) { + r.JSON(300, Greeting{"hello", "world"}) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusMultipleChoices) + So(resp.Header().Get(ContentType), ShouldEqual, ContentJSON+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, `{"one":"hello","two":"world"}`) + }) + + Convey("Render JSON with prefix", t, func() { + m := Classic() + prefix := ")]}',\n" + m.Use(Renderer(RenderOptions{ + PrefixJSON: []byte(prefix), + })) + m.Get("/foobar", func(r Render) { + r.JSON(300, Greeting{"hello", "world"}) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusMultipleChoices) + So(resp.Header().Get(ContentType), ShouldEqual, ContentJSON+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, prefix+`{"one":"hello","two":"world"}`) + }) + + Convey("Render Indented JSON", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + IndentJSON: true, + })) + m.Get("/foobar", func(r Render) { + r.JSON(300, Greeting{"hello", "world"}) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusMultipleChoices) + So(resp.Header().Get(ContentType), ShouldEqual, ContentJSON+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, `{ + "one": "hello", + "two": "world" +}`) + }) + + Convey("Render JSON and return string", t, func() { + m := Classic() + m.Use(Renderer()) + m.Get("/foobar", func(r Render) { + result, err := r.JSONString(Greeting{"hello", "world"}) + So(err, ShouldBeNil) + So(result, ShouldEqual, `{"one":"hello","two":"world"}`) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) + + Convey("Render with charset JSON", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Charset: "foobar", + })) + m.Get("/foobar", func(r Render) { + r.JSON(300, Greeting{"hello", "world"}) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusMultipleChoices) + So(resp.Header().Get(ContentType), ShouldEqual, ContentJSON+"; charset=foobar") + So(resp.Body.String(), ShouldEqual, `{"one":"hello","two":"world"}`) + }) +} + +func Test_Render_XML(t *testing.T) { + Convey("Render XML", t, func() { + m := Classic() + m.Use(Renderer()) + m.Get("/foobar", func(r Render) { + r.XML(300, GreetingXML{One: "hello", Two: "world"}) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusMultipleChoices) + So(resp.Header().Get(ContentType), ShouldEqual, ContentXML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, ``) + }) + + Convey("Render XML with prefix", t, func() { + m := Classic() + prefix := ")]}',\n" + m.Use(Renderer(RenderOptions{ + PrefixXML: []byte(prefix), + })) + m.Get("/foobar", func(r Render) { + r.XML(300, GreetingXML{One: "hello", Two: "world"}) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusMultipleChoices) + So(resp.Header().Get(ContentType), ShouldEqual, ContentXML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, prefix+``) + }) + + Convey("Render Indented XML", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + IndentXML: true, + })) + m.Get("/foobar", func(r Render) { + r.XML(300, GreetingXML{One: "hello", Two: "world"}) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusMultipleChoices) + So(resp.Header().Get(ContentType), ShouldEqual, ContentXML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, ``) + }) +} + +func Test_Render_HTML(t *testing.T) { + Convey("Render HTML", t, func() { + m := Classic() + m.Use(Renderers(RenderOptions{ + Directory: "fixtures/basic", + }, "fixtures/basic2")) + m.Get("/foobar", func(r Render) { + r.HTML(200, "hello", "jeremy") + r.SetTemplatePath("", "fixtures/basic2") + }) + m.Get("/foobar2", func(r Render) { + if r.HasTemplateSet("basic2") { + r.HTMLSet(200, "basic2", "hello", "jeremy") + } + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentHTML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, "

Hello jeremy

") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/foobar2", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentHTML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, "

What's up, jeremy

") + + Convey("Change render templates path", func() { + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentHTML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, "

What's up, jeremy

") + }) + }) + + Convey("Render HTML and return string", t, func() { + m := Classic() + m.Use(Renderers(RenderOptions{ + Directory: "fixtures/basic", + }, "basic2:fixtures/basic2")) + m.Get("/foobar", func(r Render) { + result, err := r.HTMLString("hello", "jeremy") + So(err, ShouldBeNil) + So(result, ShouldEqual, "

Hello jeremy

") + }) + m.Get("/foobar2", func(r Render) { + result, err := r.HTMLSetString("basic2", "hello", "jeremy") + So(err, ShouldBeNil) + So(result, ShouldEqual, "

What's up, jeremy

") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/foobar2", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) + + Convey("Render with nested HTML", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/basic", + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "admin/index", "jeremy") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentHTML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, "

Admin jeremy

") + }) + + Convey("Render bad HTML", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/basic", + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "nope", nil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusInternalServerError) + So(resp.Body.String(), ShouldEqual, "html/template: \"nope\" is undefined\n") + }) + + Convey("Invalid template set", t, func() { + Convey("Empty template set argument", func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + m := Classic() + m.Use(Renderers(RenderOptions{ + Directory: "fixtures/basic", + }, "")) + }) + + Convey("Bad template set path", func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + m := Classic() + m.Use(Renderers(RenderOptions{ + Directory: "fixtures/basic", + }, "404")) + }) + }) +} + +func Test_Render_XHTML(t *testing.T) { + Convey("Render XHTML", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/basic", + HTMLContentType: ContentXHTML, + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "hello", "jeremy") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentXHTML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, "

Hello jeremy

") + }) +} + +func Test_Render_Extensions(t *testing.T) { + Convey("Render with extensions", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/basic", + Extensions: []string{".tmpl", ".html"}, + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "hypertext", nil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentHTML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, "Hypertext!") + }) +} + +func Test_Render_Funcs(t *testing.T) { + Convey("Render with functions", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/custom_funcs", + Funcs: []template.FuncMap{ + { + "myCustomFunc": func() string { + return "My custom function" + }, + }, + }, + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "index", "jeremy") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Body.String(), ShouldEqual, "My custom function") + }) +} + +func Test_Render_Layout(t *testing.T) { + Convey("Render with layout", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/basic", + Layout: "layout", + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "content", "jeremy") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Body.String(), ShouldEqual, "head

jeremy

foot") + }) + + Convey("Render with current layout", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/basic", + Layout: "current_layout", + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "content", "jeremy") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Body.String(), ShouldEqual, "content head

jeremy

content foot") + }) + + Convey("Render with override layout", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/basic", + Layout: "layout", + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "content", "jeremy", HTMLOptions{ + Layout: "another_layout", + }) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentHTML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, "another head

jeremy

another foot") + }) +} + +func Test_Render_Delimiters(t *testing.T) { + Convey("Render with delimiters", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Delims: Delims{"{[{", "}]}"}, + Directory: "fixtures/basic", + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "delims", "jeremy") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentHTML+"; charset=UTF-8") + So(resp.Body.String(), ShouldEqual, "

Hello jeremy

") + }) +} + +func Test_Render_BinaryData(t *testing.T) { + Convey("Render binary data", t, func() { + m := Classic() + m.Use(Renderer()) + m.Get("/foobar", func(r Render) { + r.RawData(200, []byte("hello there")) + }) + m.Get("/foobar2", func(r Render) { + r.RenderData(200, []byte("hello there")) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, ContentBinary) + So(resp.Body.String(), ShouldEqual, "hello there") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/foobar2", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, CONTENT_PLAIN) + So(resp.Body.String(), ShouldEqual, "hello there") + }) + + Convey("Render binary data with mime type", t, func() { + m := Classic() + m.Use(Renderer()) + m.Get("/foobar", func(r Render) { + r.RW().Header().Set(ContentType, "image/jpeg") + r.RawData(200, []byte("..jpeg data..")) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/foobar", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get(ContentType), ShouldEqual, "image/jpeg") + So(resp.Body.String(), ShouldEqual, "..jpeg data..") + }) +} + +func Test_Render_Status(t *testing.T) { + Convey("Render with status 204", t, func() { + resp := httptest.NewRecorder() + r := TplRender{resp, newTemplateSet(), &RenderOptions{}, "", time.Now()} + r.Status(204) + So(resp.Code, ShouldEqual, http.StatusNoContent) + }) + + Convey("Render with status 404", t, func() { + resp := httptest.NewRecorder() + r := TplRender{resp, newTemplateSet(), &RenderOptions{}, "", time.Now()} + r.Error(404) + So(resp.Code, ShouldEqual, http.StatusNotFound) + }) + + Convey("Render with status 500", t, func() { + resp := httptest.NewRecorder() + r := TplRender{resp, newTemplateSet(), &RenderOptions{}, "", time.Now()} + r.Error(500) + So(resp.Code, ShouldEqual, http.StatusInternalServerError) + }) +} + +func Test_Render_NoRace(t *testing.T) { + Convey("Make sure render has no race", t, func() { + m := Classic() + m.Use(Renderer(RenderOptions{ + Directory: "fixtures/basic", + })) + m.Get("/foobar", func(r Render) { + r.HTML(200, "hello", "world") + }) + + done := make(chan bool) + doreq := func() { + resp := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/foobar", nil) + m.ServeHTTP(resp, req) + done <- true + } + // Run two requests to check there is no race condition + go doreq() + go doreq() + <-done + <-done + }) +} + +func Test_GetExt(t *testing.T) { + Convey("Get extension", t, func() { + So(GetExt("test"), ShouldBeBlank) + So(GetExt("test.tmpl"), ShouldEqual, ".tmpl") + So(GetExt("test.go.tmpl"), ShouldEqual, ".go.tmpl") + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/response_writer.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/response_writer.go new file mode 100644 index 0000000..ab54f56 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/response_writer.go @@ -0,0 +1,111 @@ +// Copyright 2013 Martini Authors +// +// 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 macaron + +import ( + "bufio" + "fmt" + "net" + "net/http" +) + +// ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about +// the response. It is recommended that middleware handlers use this construct to wrap a responsewriter +// if the functionality calls for it. +type ResponseWriter interface { + http.ResponseWriter + http.Flusher + // Status returns the status code of the response or 0 if the response has not been written. + Status() int + // Written returns whether or not the ResponseWriter has been written. + Written() bool + // Size returns the size of the response body. + Size() int + // Before allows for a function to be called before the ResponseWriter has been written to. This is + // useful for setting headers or any other operations that must happen before a response has been written. + Before(BeforeFunc) +} + +// BeforeFunc is a function that is called before the ResponseWriter has been written to. +type BeforeFunc func(ResponseWriter) + +// NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter +func NewResponseWriter(rw http.ResponseWriter) ResponseWriter { + return &responseWriter{rw, 0, 0, nil} +} + +type responseWriter struct { + http.ResponseWriter + status int + size int + beforeFuncs []BeforeFunc +} + +func (rw *responseWriter) WriteHeader(s int) { + rw.callBefore() + rw.ResponseWriter.WriteHeader(s) + rw.status = s +} + +func (rw *responseWriter) Write(b []byte) (int, error) { + if !rw.Written() { + // The status will be StatusOK if WriteHeader has not been called yet + rw.WriteHeader(http.StatusOK) + } + size, err := rw.ResponseWriter.Write(b) + rw.size += size + return size, err +} + +func (rw *responseWriter) Status() int { + return rw.status +} + +func (rw *responseWriter) Size() int { + return rw.size +} + +func (rw *responseWriter) Written() bool { + return rw.status != 0 +} + +func (rw *responseWriter) Before(before BeforeFunc) { + rw.beforeFuncs = append(rw.beforeFuncs, before) +} + +func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { + hijacker, ok := rw.ResponseWriter.(http.Hijacker) + if !ok { + return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface") + } + return hijacker.Hijack() +} + +func (rw *responseWriter) CloseNotify() <-chan bool { + return rw.ResponseWriter.(http.CloseNotifier).CloseNotify() +} + +func (rw *responseWriter) callBefore() { + for i := len(rw.beforeFuncs) - 1; i >= 0; i-- { + rw.beforeFuncs[i](rw) + } +} + +func (rw *responseWriter) Flush() { + flusher, ok := rw.ResponseWriter.(http.Flusher) + if ok { + flusher.Flush() + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/response_writer_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/response_writer_test.go new file mode 100644 index 0000000..322396b --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/response_writer_test.go @@ -0,0 +1,188 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "bufio" + "io" + "net" + "net/http" + "net/http/httptest" + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +type closeNotifyingRecorder struct { + *httptest.ResponseRecorder + closed chan bool +} + +func newCloseNotifyingRecorder() *closeNotifyingRecorder { + return &closeNotifyingRecorder{ + httptest.NewRecorder(), + make(chan bool, 1), + } +} + +func (c *closeNotifyingRecorder) close() { + c.closed <- true +} + +func (c *closeNotifyingRecorder) CloseNotify() <-chan bool { + return c.closed +} + +type hijackableResponse struct { + Hijacked bool +} + +func newHijackableResponse() *hijackableResponse { + return &hijackableResponse{} +} + +func (h *hijackableResponse) Header() http.Header { return nil } +func (h *hijackableResponse) Write(buf []byte) (int, error) { return 0, nil } +func (h *hijackableResponse) WriteHeader(code int) {} +func (h *hijackableResponse) Flush() {} +func (h *hijackableResponse) Hijack() (net.Conn, *bufio.ReadWriter, error) { + h.Hijacked = true + return nil, nil, nil +} + +func Test_ResponseWriter(t *testing.T) { + Convey("Write string to response writer", t, func() { + resp := httptest.NewRecorder() + rw := NewResponseWriter(resp) + rw.Write([]byte("Hello world")) + + So(resp.Code, ShouldEqual, rw.Status()) + So(resp.Body.String(), ShouldEqual, "Hello world") + So(rw.Status(), ShouldEqual, http.StatusOK) + So(rw.Size(), ShouldEqual, 11) + So(rw.Written(), ShouldBeTrue) + }) + + Convey("Write strings to response writer", t, func() { + resp := httptest.NewRecorder() + rw := NewResponseWriter(resp) + rw.Write([]byte("Hello world")) + rw.Write([]byte("foo bar bat baz")) + + So(resp.Code, ShouldEqual, rw.Status()) + So(resp.Body.String(), ShouldEqual, "Hello worldfoo bar bat baz") + So(rw.Status(), ShouldEqual, http.StatusOK) + So(rw.Size(), ShouldEqual, 26) + So(rw.Written(), ShouldBeTrue) + }) + + Convey("Write header to response writer", t, func() { + resp := httptest.NewRecorder() + rw := NewResponseWriter(resp) + rw.WriteHeader(http.StatusNotFound) + + So(resp.Code, ShouldEqual, rw.Status()) + So(resp.Body.String(), ShouldBeBlank) + So(rw.Status(), ShouldEqual, http.StatusNotFound) + So(rw.Size(), ShouldEqual, 0) + }) + + Convey("Write before response write", t, func() { + result := "" + resp := httptest.NewRecorder() + rw := NewResponseWriter(resp) + rw.Before(func(ResponseWriter) { + result += "foo" + }) + rw.Before(func(ResponseWriter) { + result += "bar" + }) + rw.WriteHeader(http.StatusNotFound) + + So(resp.Code, ShouldEqual, rw.Status()) + So(resp.Body.String(), ShouldBeBlank) + So(rw.Status(), ShouldEqual, http.StatusNotFound) + So(rw.Size(), ShouldEqual, 0) + So(result, ShouldEqual, "barfoo") + }) + + Convey("Response writer with Hijack", t, func() { + hijackable := newHijackableResponse() + rw := NewResponseWriter(hijackable) + hijacker, ok := rw.(http.Hijacker) + So(ok, ShouldBeTrue) + _, _, err := hijacker.Hijack() + So(err, ShouldBeNil) + So(hijackable.Hijacked, ShouldBeTrue) + }) + + Convey("Response writer with bad Hijack", t, func() { + hijackable := new(http.ResponseWriter) + rw := NewResponseWriter(*hijackable) + hijacker, ok := rw.(http.Hijacker) + So(ok, ShouldBeTrue) + _, _, err := hijacker.Hijack() + So(err, ShouldNotBeNil) + }) + + Convey("Response writer with close notify", t, func() { + resp := newCloseNotifyingRecorder() + rw := NewResponseWriter(resp) + closed := false + notifier := rw.(http.CloseNotifier).CloseNotify() + resp.close() + select { + case <-notifier: + closed = true + case <-time.After(time.Second): + } + So(closed, ShouldBeTrue) + }) + + Convey("Response writer with flusher", t, func() { + resp := httptest.NewRecorder() + rw := NewResponseWriter(resp) + _, ok := rw.(http.Flusher) + So(ok, ShouldBeTrue) + }) + + Convey("Response writer with flusher handler", t, func() { + m := Classic() + m.Get("/events", func(w http.ResponseWriter, r *http.Request) { + f, ok := w.(http.Flusher) + So(ok, ShouldBeTrue) + + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("Connection", "keep-alive") + + for i := 0; i < 2; i++ { + time.Sleep(10 * time.Millisecond) + io.WriteString(w, "data: Hello\n\n") + f.Flush() + } + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/events", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Body.String(), ShouldEqual, "data: Hello\n\ndata: Hello\n\n") + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/return_handler.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/return_handler.go new file mode 100644 index 0000000..ea1e044 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/return_handler.go @@ -0,0 +1,59 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "net/http" + "reflect" + + "github.com/Unknwon/macaron/inject" +) + +// ReturnHandler is a service that Martini provides that is called +// when a route handler returns something. The ReturnHandler is +// responsible for writing to the ResponseWriter based on the values +// that are passed into this function. +type ReturnHandler func(*Context, []reflect.Value) + +func canDeref(val reflect.Value) bool { + return val.Kind() == reflect.Interface || val.Kind() == reflect.Ptr +} + +func isByteSlice(val reflect.Value) bool { + return val.Kind() == reflect.Slice && val.Type().Elem().Kind() == reflect.Uint8 +} + +func defaultReturnHandler() ReturnHandler { + return func(ctx *Context, vals []reflect.Value) { + rv := ctx.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil))) + res := rv.Interface().(http.ResponseWriter) + var respVal reflect.Value + if len(vals) > 1 && vals[0].Kind() == reflect.Int { + res.WriteHeader(int(vals[0].Int())) + respVal = vals[1] + } else if len(vals) > 0 { + respVal = vals[0] + } + if canDeref(respVal) { + respVal = respVal.Elem() + } + if isByteSlice(respVal) { + res.Write(respVal.Bytes()) + } else { + res.Write([]byte(respVal.String())) + } + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/return_handler_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/return_handler_test.go new file mode 100644 index 0000000..02325b2 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/return_handler_test.go @@ -0,0 +1,69 @@ +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "net/http" + "net/http/httptest" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Return_Handler(t *testing.T) { + Convey("Return with status and body", t, func() { + m := Classic() + m.Get("/", func() (int, string) { + return 418, "i'm a teapot" + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusTeapot) + So(resp.Body.String(), ShouldEqual, "i'm a teapot") + }) + + Convey("Return with pointer", t, func() { + m := Classic() + m.Get("/", func() *string { + str := "hello world" + return &str + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Body.String(), ShouldEqual, "hello world") + }) + + Convey("Return with byte slice", t, func() { + m := Classic() + m.Get("/", func() []byte { + return []byte("hello world") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Body.String(), ShouldEqual, "hello world") + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/router.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/router.go new file mode 100644 index 0000000..d2b3945 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/router.go @@ -0,0 +1,300 @@ +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "net/http" + "strings" + "sync" + + "github.com/Unknwon/com" +) + +var ( + // Known HTTP methods. + _HTTP_METHODS = map[string]bool{ + "GET": true, + "POST": true, + "PUT": true, + "DELETE": true, + "PATCH": true, + "OPTIONS": true, + "HEAD": true, + } +) + +// routeMap represents a thread-safe map for route tree. +type routeMap struct { + lock sync.RWMutex + routes map[string]map[string]bool +} + +// NewRouteMap initializes and returns a new routeMap. +func NewRouteMap() *routeMap { + rm := &routeMap{ + routes: make(map[string]map[string]bool), + } + for m := range _HTTP_METHODS { + rm.routes[m] = make(map[string]bool) + } + return rm +} + +// isExist returns true if a route has been registered. +func (rm *routeMap) isExist(method, pattern string) bool { + rm.lock.RLock() + defer rm.lock.RUnlock() + + return rm.routes[method][pattern] +} + +// add adds new route to route tree map. +func (rm *routeMap) add(method, pattern string) { + rm.lock.Lock() + defer rm.lock.Unlock() + + rm.routes[method][pattern] = true +} + +type group struct { + pattern string + handlers []Handler +} + +// Router represents a Macaron router layer. +type Router struct { + m *Macaron + routers map[string]*Tree + *routeMap + + groups []group + notFound http.HandlerFunc +} + +func NewRouter() *Router { + return &Router{ + routers: make(map[string]*Tree), + routeMap: NewRouteMap(), + } +} + +type Params map[string]string + +// Handle is a function that can be registered to a route to handle HTTP requests. +// Like http.HandlerFunc, but has a third parameter for the values of wildcards (variables). +type Handle func(http.ResponseWriter, *http.Request, Params) + +// handle adds new route to the router tree. +func (r *Router) handle(method, pattern string, handle Handle) { + method = strings.ToUpper(method) + + // Prevent duplicate routes. + if r.isExist(method, pattern) { + return + } + + // Validate HTTP methods. + if !_HTTP_METHODS[method] && method != "*" { + panic("unknown HTTP method: " + method) + } + + // Generate methods need register. + methods := make(map[string]bool) + if method == "*" { + for m := range _HTTP_METHODS { + methods[m] = true + } + } else { + methods[method] = true + } + + // Add to router tree. + for m := range methods { + if t, ok := r.routers[m]; ok { + t.AddRouter(pattern, handle) + } else { + t := NewTree() + t.AddRouter(pattern, handle) + r.routers[m] = t + } + r.add(m, pattern) + } +} + +// Handle registers a new request handle with the given pattern, method and handlers. +func (r *Router) Handle(method string, pattern string, handlers []Handler) { + if len(r.groups) > 0 { + groupPattern := "" + h := make([]Handler, 0) + for _, g := range r.groups { + groupPattern += g.pattern + h = append(h, g.handlers...) + } + + pattern = groupPattern + pattern + h = append(h, handlers...) + handlers = h + } + validateHandlers(handlers) + + r.handle(method, pattern, func(resp http.ResponseWriter, req *http.Request, params Params) { + c := r.m.createContext(resp, req) + c.params = params + c.handlers = make([]Handler, 0, len(r.m.handlers)+len(handlers)) + c.handlers = append(c.handlers, r.m.handlers...) + c.handlers = append(c.handlers, handlers...) + c.run() + }) +} + +func (r *Router) Group(pattern string, fn func(), h ...Handler) { + r.groups = append(r.groups, group{pattern, h}) + fn() + r.groups = r.groups[:len(r.groups)-1] +} + +// Get is a shortcut for r.Handle("GET", pattern, handlers) +func (r *Router) Get(pattern string, h ...Handler) { + r.Handle("GET", pattern, h) +} + +// Patch is a shortcut for r.Handle("PATCH", pattern, handlers) +func (r *Router) Patch(pattern string, h ...Handler) { + r.Handle("PATCH", pattern, h) +} + +// Post is a shortcut for r.Handle("POST", pattern, handlers) +func (r *Router) Post(pattern string, h ...Handler) { + r.Handle("POST", pattern, h) +} + +// Put is a shortcut for r.Handle("PUT", pattern, handlers) +func (r *Router) Put(pattern string, h ...Handler) { + r.Handle("PUT", pattern, h) +} + +// Delete is a shortcut for r.Handle("DELETE", pattern, handlers) +func (r *Router) Delete(pattern string, h ...Handler) { + r.Handle("DELETE", pattern, h) +} + +// Options is a shortcut for r.Handle("OPTIONS", pattern, handlers) +func (r *Router) Options(pattern string, h ...Handler) { + r.Handle("OPTIONS", pattern, h) +} + +// Head is a shortcut for r.Handle("HEAD", pattern, handlers) +func (r *Router) Head(pattern string, h ...Handler) { + r.Handle("HEAD", pattern, h) +} + +// Any is a shortcut for r.Handle("*", pattern, handlers) +func (r *Router) Any(pattern string, h ...Handler) { + r.Handle("*", pattern, h) +} + +// Route is a shortcut for same handlers but different HTTP methods. +// +// Example: +// m.Route("/", "GET,POST", h) +func (r *Router) Route(pattern, methods string, h ...Handler) { + for _, m := range strings.Split(methods, ",") { + r.Handle(strings.TrimSpace(m), pattern, h) + } +} + +// Combo returns a combo router. +func (r *Router) Combo(pattern string, h ...Handler) *ComboRouter { + return &ComboRouter{r, pattern, h, map[string]bool{}} +} + +// Configurable http.HandlerFunc which is called when no matching route is +// found. If it is not set, http.NotFound is used. +// Be sure to set 404 response code in your handler. +func (r *Router) NotFound(handlers ...Handler) { + r.notFound = func(rw http.ResponseWriter, req *http.Request) { + c := r.m.createContext(rw, req) + c.handlers = append(r.m.handlers, handlers...) + c.run() + } +} + +func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + if t, ok := r.routers[req.Method]; ok { + h, p := t.Match(req.URL.Path) + if h != nil { + if splat, ok := p[":splat"]; ok { + p["*"] = p[":splat"] // Better name. + splatlist := strings.Split(splat, "/") + for k, v := range splatlist { + p[com.ToStr(k)] = v + } + } + h(rw, req, p) + return + } + } + + r.notFound(rw, req) +} + +// ComboRouter represents a combo router. +type ComboRouter struct { + router *Router + pattern string + handlers []Handler + methods map[string]bool // Registered methods. +} + +func (cr *ComboRouter) checkMethod(name string) { + if cr.methods[name] { + panic("method '" + name + "' has already been registered") + } + cr.methods[name] = true +} + +func (cr *ComboRouter) route(fn func(string, ...Handler), method string, h ...Handler) *ComboRouter { + cr.checkMethod(method) + fn(cr.pattern, append(cr.handlers, h...)...) + return cr +} + +func (cr *ComboRouter) Get(h ...Handler) *ComboRouter { + return cr.route(cr.router.Get, "GET", h...) +} + +func (cr *ComboRouter) Patch(h ...Handler) *ComboRouter { + return cr.route(cr.router.Patch, "PATCH", h...) +} + +func (cr *ComboRouter) Post(h ...Handler) *ComboRouter { + return cr.route(cr.router.Post, "POST", h...) +} + +func (cr *ComboRouter) Put(h ...Handler) *ComboRouter { + return cr.route(cr.router.Put, "PUT", h...) +} + +func (cr *ComboRouter) Delete(h ...Handler) *ComboRouter { + return cr.route(cr.router.Delete, "DELETE", h...) +} + +func (cr *ComboRouter) Options(h ...Handler) *ComboRouter { + return cr.route(cr.router.Options, "OPTIONS", h...) +} + +func (cr *ComboRouter) Head(h ...Handler) *ComboRouter { + return cr.route(cr.router.Head, "HEAD", h...) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/router_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/router_test.go new file mode 100644 index 0000000..f404496 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/router_test.go @@ -0,0 +1,199 @@ +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "net/http" + "net/http/httptest" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Router_Handle(t *testing.T) { + Convey("Register all HTTP methods routes", t, func() { + m := Classic() + m.Get("/get", func() string { + return "GET" + }) + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "GET") + + m.Patch("/patch", func() string { + return "PATCH" + }) + resp = httptest.NewRecorder() + req, err = http.NewRequest("PATCH", "/patch", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "PATCH") + + m.Post("/post", func() string { + return "POST" + }) + resp = httptest.NewRecorder() + req, err = http.NewRequest("POST", "/post", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "POST") + + m.Put("/put", func() string { + return "PUT" + }) + resp = httptest.NewRecorder() + req, err = http.NewRequest("PUT", "/put", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "PUT") + + m.Delete("/delete", func() string { + return "DELETE" + }) + resp = httptest.NewRecorder() + req, err = http.NewRequest("DELETE", "/delete", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "DELETE") + + m.Options("/options", func() string { + return "OPTIONS" + }) + resp = httptest.NewRecorder() + req, err = http.NewRequest("OPTIONS", "/options", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "OPTIONS") + + m.Head("/head", func() string { + return "HEAD" + }) + resp = httptest.NewRecorder() + req, err = http.NewRequest("HEAD", "/head", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "HEAD") + + m.Any("/any", func() string { + return "ANY" + }) + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/any", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "ANY") + + m.Route("/route", "GET,POST", func() string { + return "ROUTE" + }) + resp = httptest.NewRecorder() + req, err = http.NewRequest("POST", "/route", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "ROUTE") + }) + + Convey("Register all HTTP methods routes with combo", t, func() { + m := Classic() + m.SetURLPrefix("/prefix") + m.Use(Renderer()) + m.Combo("/", func(ctx *Context) { + ctx.Data["prefix"] = "Prefix_" + }). + Get(func(ctx *Context) string { return ctx.Data["prefix"].(string) + "GET" }). + Patch(func(ctx *Context) string { return ctx.Data["prefix"].(string) + "PATCH" }). + Post(func(ctx *Context) string { return ctx.Data["prefix"].(string) + "POST" }). + Put(func(ctx *Context) string { return ctx.Data["prefix"].(string) + "PUT" }). + Delete(func(ctx *Context) string { return ctx.Data["prefix"].(string) + "DELETE" }). + Options(func(ctx *Context) string { return ctx.Data["prefix"].(string) + "OPTIONS" }). + Head(func(ctx *Context) string { return ctx.Data["prefix"].(string) + "HEAD" }) + + for name := range _HTTP_METHODS { + resp := httptest.NewRecorder() + req, err := http.NewRequest(name, "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "Prefix_"+name) + } + + defer func() { + So(recover(), ShouldNotBeNil) + }() + m.Combo("/").Get(func() {}).Get(nil) + }) + + Convey("Register duplicated routes", t, func() { + r := NewRouter() + r.Get("/") + r.Get("/") + }) + + Convey("Register invalid HTTP method", t, func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + r := NewRouter() + r.Handle("404", "/", nil) + }) +} + +func Test_Router_Group(t *testing.T) { + Convey("Register route group", t, func() { + m := Classic() + m.Group("/api", func() { + m.Group("/v1", func() { + m.Get("/list", func() string { + return "Well done!" + }) + }) + }) + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/api/v1/list", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "Well done!") + }) +} + +func Test_Router_NotFound(t *testing.T) { + Convey("Custom not found handler", t, func() { + m := Classic() + m.Get("/", func() {}) + m.NotFound(func() string { + return "Custom not found" + }) + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/404", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "Custom not found") + }) +} + +func Test_Router_splat(t *testing.T) { + Convey("Register router with glob", t, func() { + m := Classic() + m.Get("/*", func(ctx *Context) string { + return ctx.Params("*") + }) + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/hahaha", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Body.String(), ShouldEqual, "hahaha") + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/static.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/static.go new file mode 100644 index 0000000..69d523d --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/static.go @@ -0,0 +1,205 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "log" + "net/http" + "path" + "path/filepath" + "strings" + "sync" +) + +// StaticOptions is a struct for specifying configuration options for the macaron.Static middleware. +type StaticOptions struct { + // Prefix is the optional prefix used to serve the static directory content + Prefix string + // SkipLogging will disable [Static] log messages when a static file is served. + SkipLogging bool + // IndexFile defines which file to serve as index if it exists. + IndexFile string + // Expires defines which user-defined function to use for producing a HTTP Expires Header + // https://developers.google.com/speed/docs/insights/LeverageBrowserCaching + Expires func() string + // FileSystem is the interface for supporting any implmentation of file system. + FileSystem http.FileSystem +} + +// FIXME: to be deleted. +type staticMap struct { + lock sync.RWMutex + data map[string]*http.Dir +} + +func (sm *staticMap) Set(dir *http.Dir) { + sm.lock.Lock() + defer sm.lock.Unlock() + + sm.data[string(*dir)] = dir +} + +func (sm *staticMap) Get(name string) *http.Dir { + sm.lock.RLock() + defer sm.lock.RUnlock() + + return sm.data[name] +} + +func (sm *staticMap) Delete(name string) { + sm.lock.Lock() + defer sm.lock.Unlock() + + delete(sm.data, name) +} + +var statics = staticMap{sync.RWMutex{}, map[string]*http.Dir{}} + +// staticFileSystem implements http.FileSystem interface. +type staticFileSystem struct { + dir *http.Dir +} + +func newStaticFileSystem(directory string) staticFileSystem { + if !filepath.IsAbs(directory) { + directory = filepath.Join(Root, directory) + } + dir := http.Dir(directory) + statics.Set(&dir) + return staticFileSystem{&dir} +} + +func (fs staticFileSystem) Open(name string) (http.File, error) { + return fs.dir.Open(name) +} + +func prepareStaticOption(dir string, opt StaticOptions) StaticOptions { + // Defaults + if len(opt.IndexFile) == 0 { + opt.IndexFile = "index.html" + } + // Normalize the prefix if provided + if opt.Prefix != "" { + // Ensure we have a leading '/' + if opt.Prefix[0] != '/' { + opt.Prefix = "/" + opt.Prefix + } + // Remove any trailing '/' + opt.Prefix = strings.TrimRight(opt.Prefix, "/") + } + if opt.FileSystem == nil { + opt.FileSystem = newStaticFileSystem(dir) + } + return opt +} + +func prepareStaticOptions(dir string, options []StaticOptions) StaticOptions { + var opt StaticOptions + if len(options) > 0 { + opt = options[0] + } + return prepareStaticOption(dir, opt) +} + +func staticHandler(ctx *Context, log *log.Logger, opt StaticOptions) bool { + if ctx.Req.Method != "GET" && ctx.Req.Method != "HEAD" { + return false + } + + file := ctx.Req.URL.Path + // if we have a prefix, filter requests by stripping the prefix + if opt.Prefix != "" { + if !strings.HasPrefix(file, opt.Prefix) { + return false + } + file = file[len(opt.Prefix):] + if file != "" && file[0] != '/' { + return false + } + } + + f, err := opt.FileSystem.Open(file) + if err != nil { + return false + } + defer f.Close() + + fi, err := f.Stat() + if err != nil { + return true // File exists but fail to open. + } + + // Try to serve index file + if fi.IsDir() { + // Redirect if missing trailing slash. + if !strings.HasSuffix(ctx.Req.URL.Path, "/") { + http.Redirect(ctx.Resp, ctx.Req.Request, ctx.Req.URL.Path+"/", http.StatusFound) + return true + } + + file = path.Join(file, opt.IndexFile) + f, err = opt.FileSystem.Open(file) + if err != nil { + return false // Discard error. + } + defer f.Close() + + fi, err = f.Stat() + if err != nil || fi.IsDir() { + return true + } + } + + if !opt.SkipLogging { + log.Println("[Static] Serving " + file) + } + + // Add an Expires header to the static content + if opt.Expires != nil { + ctx.Resp.Header().Set("Expires", opt.Expires()) + } + + http.ServeContent(ctx.Resp, ctx.Req.Request, file, fi.ModTime(), f) + return true +} + +// Static returns a middleware handler that serves static files in the given directory. +func Static(directory string, staticOpt ...StaticOptions) Handler { + opt := prepareStaticOptions(directory, staticOpt) + + return func(ctx *Context, log *log.Logger) { + staticHandler(ctx, log, opt) + } +} + +// Statics registers multiple static middleware handlers all at once. +func Statics(opt StaticOptions, dirs ...string) Handler { + if len(dirs) == 0 { + panic("no static directory is given") + } + opts := make([]StaticOptions, len(dirs)) + for i := range dirs { + opts[i] = prepareStaticOption(dirs[i], opt) + } + + return func(ctx *Context, log *log.Logger) { + for i := range opts { + if staticHandler(ctx, log, opts[i]) { + return + } + } + } +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/static_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/static_test.go new file mode 100644 index 0000000..ecef352 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/static_test.go @@ -0,0 +1,246 @@ +// Copyright 2013 Martini Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "path" + "strings" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +var currentRoot, _ = os.Getwd() + +func Test_Static(t *testing.T) { + Convey("Serve static files", t, func() { + m := New() + m.Use(Static("./")) + + resp := httptest.NewRecorder() + resp.Body = new(bytes.Buffer) + req, err := http.NewRequest("GET", "http://localhost:4000/macaron.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get("Expires"), ShouldBeBlank) + So(resp.Body.Len(), ShouldBeGreaterThan, 0) + + Convey("Change static path", func() { + m.Get("/", func(ctx *Context) { + ctx.ChangeStaticPath("./", "inject") + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + resp = httptest.NewRecorder() + resp.Body = new(bytes.Buffer) + req, err = http.NewRequest("GET", "http://localhost:4000/inject.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get("Expires"), ShouldBeBlank) + So(resp.Body.Len(), ShouldBeGreaterThan, 0) + }) + }) + + Convey("Serve static files with local path", t, func() { + Root = os.TempDir() + f, err := ioutil.TempFile(Root, "static_content") + So(err, ShouldBeNil) + f.WriteString("Expected Content") + f.Close() + + m := New() + m.Use(Static(".")) + + resp := httptest.NewRecorder() + resp.Body = new(bytes.Buffer) + req, err := http.NewRequest("GET", "http://localhost:4000/"+path.Base(strings.Replace(f.Name(), "\\", "/", -1)), nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Header().Get("Expires"), ShouldBeBlank) + So(resp.Body.String(), ShouldEqual, "Expected Content") + }) + + Convey("Serve static files with head", t, func() { + m := New() + m.Use(Static(currentRoot)) + + resp := httptest.NewRecorder() + resp.Body = new(bytes.Buffer) + req, err := http.NewRequest("HEAD", "http://localhost:4000/macaron.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldEqual, http.StatusOK) + So(resp.Body.Len(), ShouldEqual, 0) + }) + + Convey("Serve static files as post", t, func() { + m := New() + m.Use(Static(currentRoot)) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("POST", "http://localhost:4000/macaron.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldEqual, http.StatusNotFound) + }) + + Convey("Serve static files with bad directory", t, func() { + m := Classic() + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/macaron.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + So(resp.Code, ShouldNotEqual, http.StatusOK) + }) +} + +func Test_Static_Options(t *testing.T) { + Convey("Serve static files with options logging", t, func() { + var buf bytes.Buffer + m := NewWithLogger(&buf) + opt := StaticOptions{} + m.Use(Static(currentRoot, opt)) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/macaron.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(buf.String(), ShouldEqual, "[Macaron] [Static] Serving /macaron.go\n") + + // Not disable logging. + m.Handlers() + buf.Reset() + opt.SkipLogging = true + m.Use(Static(currentRoot, opt)) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(buf.Len(), ShouldEqual, 0) + }) + + Convey("Serve static files with options serve index", t, func() { + var buf bytes.Buffer + m := NewWithLogger(&buf) + opt := StaticOptions{IndexFile: "macaron.go"} + m.Use(Static(currentRoot, opt)) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(buf.String(), ShouldEqual, "[Macaron] [Static] Serving /macaron.go\n") + }) + + Convey("Serve static files with options prefix", t, func() { + var buf bytes.Buffer + m := NewWithLogger(&buf) + opt := StaticOptions{Prefix: "public"} + m.Use(Static(currentRoot, opt)) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/public/macaron.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(buf.String(), ShouldEqual, "[Macaron] [Static] Serving /macaron.go\n") + }) + + Convey("Serve static files with options expires", t, func() { + var buf bytes.Buffer + m := NewWithLogger(&buf) + opt := StaticOptions{Expires: func() string { return "46" }} + m.Use(Static(currentRoot, opt)) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/macaron.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Header().Get("Expires"), ShouldEqual, "46") + }) +} + +func Test_Static_Redirect(t *testing.T) { + Convey("Serve static files with redirect", t, func() { + m := New() + m.Use(Static(currentRoot, StaticOptions{Prefix: "/public"})) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/public", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusFound) + So(resp.Header().Get("Location"), ShouldEqual, "/public/") + }) +} + +func Test_Statics(t *testing.T) { + Convey("Serve multiple static routers", t, func() { + Convey("Register empty directory", func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + + m := New() + m.Use(Statics(StaticOptions{})) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) + + Convey("Serve normally", func() { + var buf bytes.Buffer + m := NewWithLogger(&buf) + m.Use(Statics(StaticOptions{}, currentRoot, currentRoot+"/inject")) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "http://localhost:4000/macaron.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(buf.String(), ShouldEqual, "[Macaron] [Static] Serving /macaron.go\n") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "http://localhost:4000/inject/inject.go", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + So(resp.Code, ShouldEqual, http.StatusOK) + So(buf.String(), ShouldEndWith, "[Macaron] [Static] Serving /inject/inject.go\n") + }) + }) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/tree.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/tree.go new file mode 100644 index 0000000..7bde5ad --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/tree.go @@ -0,0 +1,421 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 macaron + +// NOTE: last sync 0c93364 on Dec 19, 2014. + +import ( + "path" + "regexp" + "strings" + + "github.com/Unknwon/com" +) + +type leafInfo struct { + // Names of wildcards that lead to this leaf. + // eg, ["id" "name"] for the wildcard ":id" and ":name". + wildcards []string + // Not nil if the leaf is regexp. + regexps *regexp.Regexp + handle Handle +} + +func (leaf *leafInfo) match(wildcardValues []string) (ok bool, params Params) { + if leaf.regexps == nil { + if len(wildcardValues) == 0 && len(leaf.wildcards) > 0 { + if com.IsSliceContainsStr(leaf.wildcards, ":") { + params = make(map[string]string) + j := 0 + for _, v := range leaf.wildcards { + if v == ":" { + continue + } + params[v] = "" + j += 1 + } + return true, params + } + return false, nil + } else if len(wildcardValues) == 0 { + return true, nil // Static path. + } + + // Match * + if len(leaf.wildcards) == 1 && leaf.wildcards[0] == ":splat" { + params = make(map[string]string) + params[":splat"] = path.Join(wildcardValues...) + return true, params + } + + // Match *.* + if len(leaf.wildcards) == 3 && leaf.wildcards[0] == "." { + params = make(map[string]string) + lastone := wildcardValues[len(wildcardValues)-1] + strs := strings.SplitN(lastone, ".", 2) + if len(strs) == 2 { + params[":ext"] = strs[1] + } else { + params[":ext"] = "" + } + params[":path"] = path.Join(wildcardValues[:len(wildcardValues)-1]...) + "/" + strs[0] + return true, params + } + + // Match :id + params = make(map[string]string) + j := 0 + for _, v := range leaf.wildcards { + if v == ":" { + continue + } + if v == "." { + lastone := wildcardValues[len(wildcardValues)-1] + strs := strings.SplitN(lastone, ".", 2) + if len(strs) == 2 { + params[":ext"] = strs[1] + } else { + params[":ext"] = "" + } + if len(wildcardValues[j:]) == 1 { + params[":path"] = strs[0] + } else { + params[":path"] = path.Join(wildcardValues[j:]...) + "/" + strs[0] + } + return true, params + } + if len(wildcardValues) <= j { + return false, nil + } + params[v] = wildcardValues[j] + j++ + } + if len(params) != len(wildcardValues) { + return false, nil + } + return true, params + } + + if !leaf.regexps.MatchString(path.Join(wildcardValues...)) { + return false, nil + } + params = make(map[string]string) + matches := leaf.regexps.FindStringSubmatch(path.Join(wildcardValues...)) + for i, match := range matches[1:] { + params[leaf.wildcards[i]] = match + } + return true, params +} + +// Tree represents a router tree for Macaron instance. +type Tree struct { + fixroutes map[string]*Tree + wildcard *Tree + leaves []*leafInfo +} + +// NewTree initializes and returns a router tree. +func NewTree() *Tree { + return &Tree{ + fixroutes: make(map[string]*Tree), + } +} + +// splitPath splites patthen into parts. +// +// Examples: +// "/" -> [] +// "/admin" -> ["admin"] +// "/admin/" -> ["admin"] +// "/admin/users" -> ["admin", "users"] +func splitPath(pattern string) []string { + if len(pattern) == 0 { + return []string{} + } + + elements := strings.Split(pattern, "/") + if elements[0] == "" { + elements = elements[1:] + } + if elements[len(elements)-1] == "" { + elements = elements[:len(elements)-1] + } + return elements +} + +// AddRouter adds a new route to router tree. +func (t *Tree) AddRouter(pattern string, handle Handle) { + t.addSegments(splitPath(pattern), handle, nil, "") +} + +// splitSegment splits segment into parts. +// +// Examples: +// "admin" -> false, nil, "" +// ":id" -> true, [:id], "" +// "?:id" -> true, [: :id], "" : meaning can empty +// ":id:int" -> true, [:id], ([0-9]+) +// ":name:string" -> true, [:name], ([\w]+) +// ":id([0-9]+)" -> true, [:id], ([0-9]+) +// ":id([0-9]+)_:name" -> true, [:id :name], ([0-9]+)_(.+) +// "cms_:id_:page.html" -> true, [:id :page], cms_(.+)_(.+).html +// "*" -> true, [:splat], "" +// "*.*" -> true,[. :path :ext], "" . meaning separator +func splitSegment(key string) (bool, []string, string) { + if strings.HasPrefix(key, "*") { + if key == "*.*" { + return true, []string{".", ":path", ":ext"}, "" + } else { + return true, []string{":splat"}, "" + } + } + if strings.ContainsAny(key, ":") { + var paramsNum int + var out []rune + var start bool + var startexp bool + var param []rune + var expt []rune + var skipnum int + params := []string{} + reg := regexp.MustCompile(`[a-zA-Z0-9]+`) + for i, v := range key { + if skipnum > 0 { + skipnum -= 1 + continue + } + if start { + //:id:int and :name:string + if v == ':' { + if len(key) >= i+4 { + if key[i+1:i+4] == "int" { + out = append(out, []rune("([0-9]+)")...) + params = append(params, ":"+string(param)) + start = false + startexp = false + skipnum = 3 + param = make([]rune, 0) + paramsNum += 1 + continue + } + } + if len(key) >= i+7 { + if key[i+1:i+7] == "string" { + out = append(out, []rune(`([\w]+)`)...) + params = append(params, ":"+string(param)) + paramsNum += 1 + start = false + startexp = false + skipnum = 6 + param = make([]rune, 0) + continue + } + } + } + // params only support a-zA-Z0-9 + if reg.MatchString(string(v)) { + param = append(param, v) + continue + } + if v != '(' { + out = append(out, []rune(`(.+)`)...) + params = append(params, ":"+string(param)) + param = make([]rune, 0) + paramsNum += 1 + start = false + startexp = false + } + } + if startexp { + if v != ')' { + expt = append(expt, v) + continue + } + } + if v == ':' { + param = make([]rune, 0) + start = true + } else if v == '(' { + startexp = true + start = false + params = append(params, ":"+string(param)) + paramsNum += 1 + expt = make([]rune, 0) + expt = append(expt, '(') + } else if v == ')' { + startexp = false + expt = append(expt, ')') + out = append(out, expt...) + param = make([]rune, 0) + } else if v == '?' { + params = append(params, ":") + } else { + out = append(out, v) + } + } + if len(param) > 0 { + if paramsNum > 0 { + out = append(out, []rune(`(.+)`)...) + } + params = append(params, ":"+string(param)) + } + return true, params, string(out) + } else { + return false, nil, "" + } +} + +// addSegments add segments to the router tree. +func (t *Tree) addSegments(segments []string, handle Handle, wildcards []string, reg string) { + // Fixed root route. + if len(segments) == 0 { + if reg != "" { + filterCards := make([]string, 0, len(wildcards)) + for _, v := range wildcards { + if v == ":" || v == "." { + continue + } + filterCards = append(filterCards, v) + } + t.leaves = append(t.leaves, &leafInfo{ + handle: handle, + wildcards: filterCards, + regexps: regexp.MustCompile("^" + reg + "$"), + }) + } else { + t.leaves = append(t.leaves, &leafInfo{ + handle: handle, + wildcards: wildcards, + }) + } + return + } + + seg := segments[0] + iswild, params, regexpStr := splitSegment(seg) + //for the router /login/*/access match /login/2009/11/access + if !iswild && com.IsSliceContainsStr(wildcards, ":splat") { + iswild = true + regexpStr = seg + } + if seg == "*" && len(wildcards) > 0 && reg == "" { + iswild = true + regexpStr = "(.+)" + } + if iswild { + if t.wildcard == nil { + t.wildcard = NewTree() + } + if regexpStr != "" { + if reg == "" { + rr := "" + for _, w := range wildcards { + if w == "." || w == ":" { + continue + } + if w == ":splat" { + rr = rr + "(.+)/" + } else { + rr = rr + "([^/]+)/" + } + } + regexpStr = rr + regexpStr + } else { + regexpStr = "/" + regexpStr + } + } else if reg != "" { + if seg == "*.*" { + regexpStr = "/([^.]+).(.+)" + } else { + for _, w := range params { + if w == "." || w == ":" { + continue + } + regexpStr = "/([^/]+)" + regexpStr + } + } + } + t.wildcard.addSegments(segments[1:], handle, append(wildcards, params...), reg+regexpStr) + } else { + subTree, ok := t.fixroutes[seg] + if !ok { + subTree = NewTree() + t.fixroutes[seg] = subTree + } + subTree.addSegments(segments[1:], handle, wildcards, reg) + } +} + +func (t *Tree) match(segments []string, wildcardValues []string) (handle Handle, params Params) { + // Handle leaf nodes. + if len(segments) == 0 { + for _, l := range t.leaves { + if ok, pa := l.match(wildcardValues); ok { + return l.handle, pa + } + } + if t.wildcard != nil { + for _, l := range t.wildcard.leaves { + if ok, pa := l.match(wildcardValues); ok { + return l.handle, pa + } + } + + } + return nil, nil + } + + seg, segs := segments[0], segments[1:] + + subTree, ok := t.fixroutes[seg] + if ok { + handle, params = subTree.match(segs, wildcardValues) + } else if len(segs) == 0 { //.json .xml + if subindex := strings.LastIndex(seg, "."); subindex != -1 { + subTree, ok = t.fixroutes[seg[:subindex]] + if ok { + handle, params = subTree.match(segs, wildcardValues) + if handle != nil { + if params == nil { + params = make(map[string]string) + } + params[":ext"] = seg[subindex+1:] + return handle, params + } + } + } + } + if handle == nil && t.wildcard != nil { + handle, params = t.wildcard.match(segs, append(wildcardValues, seg)) + } + if handle == nil { + for _, l := range t.leaves { + if ok, pa := l.match(append(wildcardValues, segments...)); ok { + return l.handle, pa + } + } + } + return handle, params +} + +// Match returns Handle and params if any route is matched. +func (t *Tree) Match(pattern string) (Handle, Params) { + if len(pattern) == 0 || pattern[0] != '/' { + return nil, nil + } + + return t.match(splitPath(pattern), nil) +} diff --git a/Godeps/_workspace/src/github.com/Unknwon/macaron/tree_test.go b/Godeps/_workspace/src/github.com/Unknwon/macaron/tree_test.go new file mode 100644 index 0000000..c814416 --- /dev/null +++ b/Godeps/_workspace/src/github.com/Unknwon/macaron/tree_test.go @@ -0,0 +1,112 @@ +// Copyright 2014 Unknwon +// +// 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 macaron + +import ( + // "net/http" + "strings" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_splitSegment(t *testing.T) { + type result struct { + Ok bool + Parts []string + Regex string + } + cases := map[string]result{ + "admin": result{false, nil, ""}, + ":id": result{true, []string{":id"}, ""}, + "?:id": result{true, []string{":", ":id"}, ""}, + ":id:int": result{true, []string{":id"}, "([0-9]+)"}, + ":name:string": result{true, []string{":name"}, `([\w]+)`}, + ":id([0-9]+)": result{true, []string{":id"}, "([0-9]+)"}, + ":id([0-9]+)_:name": result{true, []string{":id", ":name"}, "([0-9]+)_(.+)"}, + "cms_:id_:page.html": result{true, []string{":id", ":page"}, "cms_(.+)_(.+).html"}, + "*": result{true, []string{":splat"}, ""}, + "*.*": result{true, []string{".", ":path", ":ext"}, ""}, + } + Convey("Splits segment into parts", t, func() { + for key, result := range cases { + ok, parts, regex := splitSegment(key) + So(ok, ShouldEqual, result.Ok) + if result.Parts == nil { + So(parts, ShouldBeNil) + } else { + So(parts, ShouldNotBeNil) + So(strings.Join(parts, " "), ShouldEqual, strings.Join(result.Parts, " ")) + } + So(regex, ShouldEqual, result.Regex) + } + }) +} + +func Test_Tree_Match(t *testing.T) { + type result struct { + pattern string + reqUrl string + params map[string]string + } + + cases := []result{ + {"/:id", "/123", map[string]string{":id": "123"}}, + {"/hello/?:id", "/hello", map[string]string{":id": ""}}, + {"/", "/", nil}, + {"", "", nil}, + {"/customer/login", "/customer/login", nil}, + {"/customer/login", "/customer/login.json", map[string]string{":ext": "json"}}, + {"/*", "/customer/123", map[string]string{":splat": "customer/123"}}, + {"/*", "/customer/2009/12/11", map[string]string{":splat": "customer/2009/12/11"}}, + {"/aa/*/bb", "/aa/2009/bb", map[string]string{":splat": "2009"}}, + {"/cc/*/dd", "/cc/2009/11/dd", map[string]string{":splat": "2009/11"}}, + {"/ee/:year/*/ff", "/ee/2009/11/ff", map[string]string{":year": "2009", ":splat": "11"}}, + {"/thumbnail/:size/uploads/*", "/thumbnail/100x100/uploads/items/2014/04/20/dPRCdChkUd651t1Hvs18.jpg", + map[string]string{":size": "100x100", ":splat": "items/2014/04/20/dPRCdChkUd651t1Hvs18.jpg"}}, + {"/*.*", "/nice/api.json", map[string]string{":path": "nice/api", ":ext": "json"}}, + {"/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}, + {"/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}, + {"/dl/:width:int/:height:int/*.*", "/dl/48/48/05ac66d9bda00a3acf948c43e306fc9a.jpg", + map[string]string{":width": "48", ":height": "48", ":ext": "jpg", ":path": "05ac66d9bda00a3acf948c43e306fc9a"}}, + {"/v1/shop/:id:int", "/v1/shop/123", map[string]string{":id": "123"}}, + {"/:year:int/:month:int/:id/:endid", "/1111/111/aaa/aaa", map[string]string{":year": "1111", ":month": "111", ":id": "aaa", ":endid": "aaa"}}, + {"/v1/shop/:id/:name", "/v1/shop/123/nike", map[string]string{":id": "123", ":name": "nike"}}, + {"/v1/shop/:id/account", "/v1/shop/123/account", map[string]string{":id": "123"}}, + {"/v1/shop/:name:string", "/v1/shop/nike", map[string]string{":name": "nike"}}, + {"/v1/shop/:id([0-9]+)", "/v1/shop//123", map[string]string{":id": "123"}}, + {"/v1/shop/:id([0-9]+)_:name", "/v1/shop/123_nike", map[string]string{":id": "123", ":name": "nike"}}, + {"/v1/shop/:id(.+)_cms.html", "/v1/shop/123_cms.html", map[string]string{":id": "123"}}, + {"/v1/shop/cms_:id(.+)_:page(.+).html", "/v1/shop/cms_123_1.html", map[string]string{":id": "123", ":page": "1"}}, + {"/v1/:v/cms/aaa_:id(.+)_:page(.+).html", "/v1/2/cms/aaa_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}}, + {"/v1/:v/cms_:id(.+)_:page(.+).html", "/v1/2/cms_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}}, + {"/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", "/v1/2_cms/ttt_123_1.html", map[string]string{":v": "2", ":id": "123", ":page": "1"}}, + } + + Convey("Match routers in tree", t, func() { + for _, c := range cases { + t := NewTree() + t.AddRouter(c.pattern, nil) + _, params := t.Match(c.reqUrl) + if params != nil { + for k, v := range c.params { + vv, ok := params[k] + So(ok, ShouldBeTrue) + So(vv, ShouldEqual, v) + } + } + } + }) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/config.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/config.go new file mode 100644 index 0000000..8d9261b --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/config.go @@ -0,0 +1,107 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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. + +// Usage: +// import( +// "github.com/astaxie/beego/config" +// ) +// +// cnf, err := config.NewConfig("ini", "config.conf") +// +// cnf APIS: +// +// cnf.Set(key, val string) error +// cnf.String(key string) string +// cnf.Strings(key string) []string +// cnf.Int(key string) (int, error) +// cnf.Int64(key string) (int64, error) +// cnf.Bool(key string) (bool, error) +// cnf.Float(key string) (float64, error) +// cnf.DefaultString(key string, defaultval string) string +// cnf.DefaultStrings(key string, defaultval []string) []string +// cnf.DefaultInt(key string, defaultval int) int +// cnf.DefaultInt64(key string, defaultval int64) int64 +// cnf.DefaultBool(key string, defaultval bool) bool +// cnf.DefaultFloat(key string, defaultval float64) float64 +// cnf.DIY(key string) (interface{}, error) +// cnf.GetSection(section string) (map[string]string, error) +// cnf.SaveConfigFile(filename string) error +// +// more docs http://beego.me/docs/module/config.md +package config + +import ( + "fmt" +) + +// ConfigContainer defines how to get and set value from configuration raw data. +type ConfigContainer interface { + Set(key, val string) error // support section::key type in given key when using ini type. + String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. + Strings(key string) []string //get string slice + Int(key string) (int, error) + Int64(key string) (int64, error) + Bool(key string) (bool, error) + Float(key string) (float64, error) + DefaultString(key string, defaultval string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. + DefaultStrings(key string, defaultval []string) []string //get string slice + DefaultInt(key string, defaultval int) int + DefaultInt64(key string, defaultval int64) int64 + DefaultBool(key string, defaultval bool) bool + DefaultFloat(key string, defaultval float64) float64 + DIY(key string) (interface{}, error) + GetSection(section string) (map[string]string, error) + SaveConfigFile(filename string) error +} + +// Config is the adapter interface for parsing config file to get raw data to ConfigContainer. +type Config interface { + Parse(key string) (ConfigContainer, error) + ParseData(data []byte) (ConfigContainer, error) +} + +var adapters = make(map[string]Config) + +// Register makes a config adapter available by the adapter name. +// If Register is called twice with the same name or if driver is nil, +// it panics. +func Register(name string, adapter Config) { + if adapter == nil { + panic("config: Register adapter is nil") + } + if _, ok := adapters[name]; ok { + panic("config: Register called twice for adapter " + name) + } + adapters[name] = adapter +} + +// adapterName is ini/json/xml/yaml. +// filename is the config file path. +func NewConfig(adapterName, fileaname string) (ConfigContainer, error) { + adapter, ok := adapters[adapterName] + if !ok { + return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName) + } + return adapter.Parse(fileaname) +} + +// adapterName is ini/json/xml/yaml. +// data is the config data. +func NewConfigData(adapterName string, data []byte) (ConfigContainer, error) { + adapter, ok := adapters[adapterName] + if !ok { + return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName) + } + return adapter.ParseData(data) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/fake.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/fake.go new file mode 100644 index 0000000..54588e5 --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/fake.go @@ -0,0 +1,129 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 config + +import ( + "errors" + "strconv" + "strings" +) + +type fakeConfigContainer struct { + data map[string]string +} + +func (c *fakeConfigContainer) getData(key string) string { + return c.data[strings.ToLower(key)] +} + +func (c *fakeConfigContainer) Set(key, val string) error { + c.data[strings.ToLower(key)] = val + return nil +} + +func (c *fakeConfigContainer) String(key string) string { + return c.getData(key) +} + +func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string { + if v := c.getData(key); v == "" { + return defaultval + } else { + return v + } +} + +func (c *fakeConfigContainer) Strings(key string) []string { + return strings.Split(c.getData(key), ";") +} + +func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string { + if v := c.Strings(key); len(v) == 0 { + return defaultval + } else { + return v + } +} + +func (c *fakeConfigContainer) Int(key string) (int, error) { + return strconv.Atoi(c.getData(key)) +} + +func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int { + if v, err := c.Int(key); err != nil { + return defaultval + } else { + return v + } +} + +func (c *fakeConfigContainer) Int64(key string) (int64, error) { + return strconv.ParseInt(c.getData(key), 10, 64) +} + +func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 { + if v, err := c.Int64(key); err != nil { + return defaultval + } else { + return v + } +} + +func (c *fakeConfigContainer) Bool(key string) (bool, error) { + return strconv.ParseBool(c.getData(key)) +} + +func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool { + if v, err := c.Bool(key); err != nil { + return defaultval + } else { + return v + } +} + +func (c *fakeConfigContainer) Float(key string) (float64, error) { + return strconv.ParseFloat(c.getData(key), 64) +} + +func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 { + if v, err := c.Float(key); err != nil { + return defaultval + } else { + return v + } +} + +func (c *fakeConfigContainer) DIY(key string) (interface{}, error) { + if v, ok := c.data[strings.ToLower(key)]; ok { + return v, nil + } + return nil, errors.New("key not find") +} + +func (c *fakeConfigContainer) GetSection(section string) (map[string]string, error) { + return nil, errors.New("not implement in the fakeConfigContainer") +} + +func (c *fakeConfigContainer) SaveConfigFile(filename string) error { + return errors.New("not implement in the fakeConfigContainer") +} + +var _ ConfigContainer = new(fakeConfigContainer) + +func NewFakeConfig() ConfigContainer { + return &fakeConfigContainer{ + data: make(map[string]string), + } +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/ini.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/ini.go new file mode 100644 index 0000000..837c9ff --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/ini.go @@ -0,0 +1,413 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 config + +import ( + "bufio" + "bytes" + "errors" + "fmt" + "io" + "io/ioutil" + "os" + "path" + "strconv" + "strings" + "sync" + "time" + "unicode" +) + +var ( + DEFAULT_SECTION = "default" // default section means if some ini items not in a section, make them in default section, + bNumComment = []byte{'#'} // number signal + bSemComment = []byte{';'} // semicolon signal + bEmpty = []byte{} + bEqual = []byte{'='} // equal signal + bDQuote = []byte{'"'} // quote signal + sectionStart = []byte{'['} // section start signal + sectionEnd = []byte{']'} // section end signal + lineBreak = "\n" +) + +// IniConfig implements Config to parse ini file. +type IniConfig struct { +} + +// ParseFile creates a new Config and parses the file configuration from the named file. +func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { + return ini.parseFile(name) +} + +func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) { + file, err := os.Open(name) + if err != nil { + return nil, err + } + + cfg := &IniConfigContainer{ + file.Name(), + make(map[string]map[string]string), + make(map[string]string), + make(map[string]string), + sync.RWMutex{}, + } + cfg.Lock() + defer cfg.Unlock() + defer file.Close() + + var comment bytes.Buffer + buf := bufio.NewReader(file) + // check the BOM + head, err := buf.Peek(3) + if err == nil && head[0] == 239 && head[1] == 187 && head[2] == 191 { + for i := 1; i <= 3; i++ { + buf.ReadByte() + } + } + section := DEFAULT_SECTION + for { + line, _, err := buf.ReadLine() + if err == io.EOF { + break + } + if bytes.Equal(line, bEmpty) { + continue + } + line = bytes.TrimSpace(line) + + var bComment []byte + switch { + case bytes.HasPrefix(line, bNumComment): + bComment = bNumComment + case bytes.HasPrefix(line, bSemComment): + bComment = bSemComment + } + if bComment != nil { + line = bytes.TrimLeft(line, string(bComment)) + line = bytes.TrimLeftFunc(line, unicode.IsSpace) + comment.Write(line) + comment.WriteByte('\n') + continue + } + + if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) { + section = strings.ToLower(string(line[1 : len(line)-1])) // section name case insensitive + if comment.Len() > 0 { + cfg.sectionComment[section] = comment.String() + comment.Reset() + } + if _, ok := cfg.data[section]; !ok { + cfg.data[section] = make(map[string]string) + } + continue + } + + if _, ok := cfg.data[section]; !ok { + cfg.data[section] = make(map[string]string) + } + keyValue := bytes.SplitN(line, bEqual, 2) + + key := string(bytes.TrimSpace(keyValue[0])) // key name case insensitive + key = strings.ToLower(key) + + // handle include "other.conf" + if len(keyValue) == 1 && strings.HasPrefix(key, "include") { + includefiles := strings.Fields(key) + if includefiles[0] == "include" && len(includefiles) == 2 { + otherfile := strings.Trim(includefiles[1], "\"") + if !path.IsAbs(otherfile) { + otherfile = path.Join(path.Dir(name), otherfile) + } + i, err := ini.parseFile(otherfile) + if err != nil { + return nil, err + } + for sec, dt := range i.data { + if _, ok := cfg.data[sec]; !ok { + cfg.data[sec] = make(map[string]string) + } + for k, v := range dt { + cfg.data[sec][k] = v + } + } + for sec, comm := range i.sectionComment { + cfg.sectionComment[sec] = comm + } + for k, comm := range i.keyComment { + cfg.keyComment[k] = comm + } + continue + } + } + + if len(keyValue) != 2 { + return nil, errors.New("read the content error: \"" + string(line) + "\", should key = val") + } + val := bytes.TrimSpace(keyValue[1]) + if bytes.HasPrefix(val, bDQuote) { + val = bytes.Trim(val, `"`) + } + + cfg.data[section][key] = string(val) + if comment.Len() > 0 { + cfg.keyComment[section+"."+key] = comment.String() + comment.Reset() + } + + } + return cfg, nil +} + +func (ini *IniConfig) ParseData(data []byte) (ConfigContainer, error) { + // Save memory data to temporary file + tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) + os.MkdirAll(path.Dir(tmpName), os.ModePerm) + if err := ioutil.WriteFile(tmpName, data, 0655); err != nil { + return nil, err + } + return ini.Parse(tmpName) +} + +// A Config represents the ini configuration. +// When set and get value, support key as section:name type. +type IniConfigContainer struct { + filename string + data map[string]map[string]string // section=> key:val + sectionComment map[string]string // section : comment + keyComment map[string]string // id: []{comment, key...}; id 1 is for main comment. + sync.RWMutex +} + +// Bool returns the boolean value for a given key. +func (c *IniConfigContainer) Bool(key string) (bool, error) { + return strconv.ParseBool(c.getdata(key)) +} + +// DefaultBool returns the boolean value for a given key. +// if err != nil return defaltval +func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool { + if v, err := c.Bool(key); err != nil { + return defaultval + } else { + return v + } +} + +// Int returns the integer value for a given key. +func (c *IniConfigContainer) Int(key string) (int, error) { + return strconv.Atoi(c.getdata(key)) +} + +// DefaultInt returns the integer value for a given key. +// if err != nil return defaltval +func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int { + if v, err := c.Int(key); err != nil { + return defaultval + } else { + return v + } +} + +// Int64 returns the int64 value for a given key. +func (c *IniConfigContainer) Int64(key string) (int64, error) { + return strconv.ParseInt(c.getdata(key), 10, 64) +} + +// DefaultInt64 returns the int64 value for a given key. +// if err != nil return defaltval +func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 { + if v, err := c.Int64(key); err != nil { + return defaultval + } else { + return v + } +} + +// Float returns the float value for a given key. +func (c *IniConfigContainer) Float(key string) (float64, error) { + return strconv.ParseFloat(c.getdata(key), 64) +} + +// DefaultFloat returns the float64 value for a given key. +// if err != nil return defaltval +func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 { + if v, err := c.Float(key); err != nil { + return defaultval + } else { + return v + } +} + +// String returns the string value for a given key. +func (c *IniConfigContainer) String(key string) string { + return c.getdata(key) +} + +// DefaultString returns the string value for a given key. +// if err != nil return defaltval +func (c *IniConfigContainer) DefaultString(key string, defaultval string) string { + if v := c.String(key); v == "" { + return defaultval + } else { + return v + } +} + +// Strings returns the []string value for a given key. +func (c *IniConfigContainer) Strings(key string) []string { + return strings.Split(c.String(key), ";") +} + +// DefaultStrings returns the []string value for a given key. +// if err != nil return defaltval +func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string { + if v := c.Strings(key); len(v) == 0 { + return defaultval + } else { + return v + } +} + +// GetSection returns map for the given section +func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) { + if v, ok := c.data[section]; ok { + return v, nil + } else { + return nil, errors.New("not exist setction") + } +} + +// SaveConfigFile save the config into file +func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) { + // Write configuration file by filename. + f, err := os.Create(filename) + if err != nil { + return err + } + defer f.Close() + + buf := bytes.NewBuffer(nil) + for section, dt := range c.data { + // Write section comments. + if v, ok := c.sectionComment[section]; ok { + if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil { + return err + } + } + + if section != DEFAULT_SECTION { + // Write section name. + if _, err = buf.WriteString(string(sectionStart) + section + string(sectionEnd) + lineBreak); err != nil { + return err + } + } + + for key, val := range dt { + if key != " " { + // Write key comments. + if v, ok := c.keyComment[key]; ok { + if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil { + return err + } + } + + // Write key and value. + if _, err = buf.WriteString(key + string(bEqual) + val + lineBreak); err != nil { + return err + } + } + } + + // Put a line between sections. + if _, err = buf.WriteString(lineBreak); err != nil { + return err + } + } + + if _, err = buf.WriteTo(f); err != nil { + return err + } + return nil +} + +// WriteValue writes a new value for key. +// if write to one section, the key need be "section::key". +// if the section is not existed, it panics. +func (c *IniConfigContainer) Set(key, value string) error { + c.Lock() + defer c.Unlock() + if len(key) == 0 { + return errors.New("key is empty") + } + + var ( + section, k string + sectionKey []string = strings.Split(key, "::") + ) + + if len(sectionKey) >= 2 { + section = sectionKey[0] + k = sectionKey[1] + } else { + section = DEFAULT_SECTION + k = sectionKey[0] + } + + if _, ok := c.data[section]; !ok { + c.data[section] = make(map[string]string) + } + c.data[section][k] = value + return nil +} + +// DIY returns the raw value by a given key. +func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { + if v, ok := c.data[strings.ToLower(key)]; ok { + return v, nil + } + return v, errors.New("key not find") +} + +// section.key or key +func (c *IniConfigContainer) getdata(key string) string { + if len(key) == 0 { + return "" + } + c.RLock() + defer c.RUnlock() + + var ( + section, k string + sectionKey []string = strings.Split(strings.ToLower(key), "::") + ) + if len(sectionKey) >= 2 { + section = sectionKey[0] + k = sectionKey[1] + } else { + section = DEFAULT_SECTION + k = sectionKey[0] + } + if v, ok := c.data[section]; ok { + if vv, ok := v[k]; ok { + return vv + } + } + return "" +} + +func init() { + Register("ini", &IniConfig{}) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/ini_test.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/ini_test.go new file mode 100644 index 0000000..7599ab8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/ini_test.go @@ -0,0 +1,103 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 config + +import ( + "os" + "testing" +) + +var inicontext = ` +;comment one +#comment two +appname = beeapi +httpport = 8080 +mysqlport = 3600 +PI = 3.1415976 +runmode = "dev" +autorender = false +copyrequestbody = true +[demo] +key1="asta" +key2 = "xie" +CaseInsensitive = true +peers = one;two;three +` + +func TestIni(t *testing.T) { + f, err := os.Create("testini.conf") + if err != nil { + t.Fatal(err) + } + _, err = f.WriteString(inicontext) + if err != nil { + f.Close() + t.Fatal(err) + } + f.Close() + defer os.Remove("testini.conf") + iniconf, err := NewConfig("ini", "testini.conf") + if err != nil { + t.Fatal(err) + } + if iniconf.String("appname") != "beeapi" { + t.Fatal("appname not equal to beeapi") + } + if port, err := iniconf.Int("httpport"); err != nil || port != 8080 { + t.Error(port) + t.Fatal(err) + } + if port, err := iniconf.Int64("mysqlport"); err != nil || port != 3600 { + t.Error(port) + t.Fatal(err) + } + if pi, err := iniconf.Float("PI"); err != nil || pi != 3.1415976 { + t.Error(pi) + t.Fatal(err) + } + if iniconf.String("runmode") != "dev" { + t.Fatal("runmode not equal to dev") + } + if v, err := iniconf.Bool("autorender"); err != nil || v != false { + t.Error(v) + t.Fatal(err) + } + if v, err := iniconf.Bool("copyrequestbody"); err != nil || v != true { + t.Error(v) + t.Fatal(err) + } + if err = iniconf.Set("name", "astaxie"); err != nil { + t.Fatal(err) + } + if iniconf.String("name") != "astaxie" { + t.Fatal("get name error") + } + if iniconf.String("demo::key1") != "asta" { + t.Fatal("get demo.key1 error") + } + if iniconf.String("demo::key2") != "xie" { + t.Fatal("get demo.key2 error") + } + if v, err := iniconf.Bool("demo::caseinsensitive"); err != nil || v != true { + t.Fatal("get demo.caseinsensitive error") + } + + if data := iniconf.Strings("demo::peers"); len(data) != 3 { + t.Fatal("get strings error", data) + } else if data[0] != "one" { + t.Fatal("get first params error not equat to one") + } + +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/json.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/json.go new file mode 100644 index 0000000..e2b5379 --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/json.go @@ -0,0 +1,265 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 config + +import ( + "encoding/json" + "errors" + "io/ioutil" + "os" + "strings" + "sync" +) + +// JsonConfig is a json config parser and implements Config interface. +type JsonConfig struct { +} + +// Parse returns a ConfigContainer with parsed json config map. +func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { + file, err := os.Open(filename) + if err != nil { + return nil, err + } + defer file.Close() + content, err := ioutil.ReadAll(file) + if err != nil { + return nil, err + } + + return js.ParseData(content) +} + +// ParseData returns a ConfigContainer with json string +func (js *JsonConfig) ParseData(data []byte) (ConfigContainer, error) { + x := &JsonConfigContainer{ + data: make(map[string]interface{}), + } + err := json.Unmarshal(data, &x.data) + if err != nil { + var wrappingArray []interface{} + err2 := json.Unmarshal(data, &wrappingArray) + if err2 != nil { + return nil, err + } + x.data["rootArray"] = wrappingArray + } + return x, nil +} + +// A Config represents the json configuration. +// Only when get value, support key as section:name type. +type JsonConfigContainer struct { + data map[string]interface{} + sync.RWMutex +} + +// Bool returns the boolean value for a given key. +func (c *JsonConfigContainer) Bool(key string) (bool, error) { + val := c.getData(key) + if val != nil { + if v, ok := val.(bool); ok { + return v, nil + } + return false, errors.New("not bool value") + } + return false, errors.New("not exist key:" + key) +} + +// DefaultBool return the bool value if has no error +// otherwise return the defaultval +func (c *JsonConfigContainer) DefaultBool(key string, defaultval bool) bool { + if v, err := c.Bool(key); err == nil { + return v + } + return defaultval +} + +// Int returns the integer value for a given key. +func (c *JsonConfigContainer) Int(key string) (int, error) { + val := c.getData(key) + if val != nil { + if v, ok := val.(float64); ok { + return int(v), nil + } + return 0, errors.New("not int value") + } + return 0, errors.New("not exist key:" + key) +} + +// DefaultInt returns the integer value for a given key. +// if err != nil return defaltval +func (c *JsonConfigContainer) DefaultInt(key string, defaultval int) int { + if v, err := c.Int(key); err == nil { + return v + } + return defaultval +} + +// Int64 returns the int64 value for a given key. +func (c *JsonConfigContainer) Int64(key string) (int64, error) { + val := c.getData(key) + if val != nil { + if v, ok := val.(float64); ok { + return int64(v), nil + } + return 0, errors.New("not int64 value") + } + return 0, errors.New("not exist key:" + key) +} + +// DefaultInt64 returns the int64 value for a given key. +// if err != nil return defaltval +func (c *JsonConfigContainer) DefaultInt64(key string, defaultval int64) int64 { + if v, err := c.Int64(key); err == nil { + return v + } + return defaultval +} + +// Float returns the float value for a given key. +func (c *JsonConfigContainer) Float(key string) (float64, error) { + val := c.getData(key) + if val != nil { + if v, ok := val.(float64); ok { + return v, nil + } + return 0.0, errors.New("not float64 value") + } + return 0.0, errors.New("not exist key:" + key) +} + +// DefaultFloat returns the float64 value for a given key. +// if err != nil return defaltval +func (c *JsonConfigContainer) DefaultFloat(key string, defaultval float64) float64 { + if v, err := c.Float(key); err == nil { + return v + } + return defaultval +} + +// String returns the string value for a given key. +func (c *JsonConfigContainer) String(key string) string { + val := c.getData(key) + if val != nil { + if v, ok := val.(string); ok { + return v + } + } + return "" +} + +// DefaultString returns the string value for a given key. +// if err != nil return defaltval +func (c *JsonConfigContainer) DefaultString(key string, defaultval string) string { + // TODO FIXME should not use "" to replace non existance + if v := c.String(key); v != "" { + return v + } + return defaultval +} + +// Strings returns the []string value for a given key. +func (c *JsonConfigContainer) Strings(key string) []string { + stringVal := c.String(key) + if stringVal == "" { + return []string{} + } + return strings.Split(c.String(key), ";") +} + +// DefaultStrings returns the []string value for a given key. +// if err != nil return defaltval +func (c *JsonConfigContainer) DefaultStrings(key string, defaultval []string) []string { + if v := c.Strings(key); len(v) > 0 { + return v + } + return defaultval +} + +// GetSection returns map for the given section +func (c *JsonConfigContainer) GetSection(section string) (map[string]string, error) { + if v, ok := c.data[section]; ok { + return v.(map[string]string), nil + } + return nil, errors.New("nonexist section " + section) +} + +// SaveConfigFile save the config into file +func (c *JsonConfigContainer) SaveConfigFile(filename string) (err error) { + // Write configuration file by filename. + f, err := os.Create(filename) + if err != nil { + return err + } + defer f.Close() + b, err := json.MarshalIndent(c.data, "", " ") + if err != nil { + return err + } + _, err = f.Write(b) + return err +} + +// Set writes a new value for key. +func (c *JsonConfigContainer) Set(key, val string) error { + c.Lock() + defer c.Unlock() + c.data[key] = val + return nil +} + +// DIY returns the raw value by a given key. +func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { + val := c.getData(key) + if val != nil { + return val, nil + } + return nil, errors.New("not exist key") +} + +// section.key or key +func (c *JsonConfigContainer) getData(key string) interface{} { + if len(key) == 0 { + return nil + } + + c.RLock() + defer c.RUnlock() + + sectionKeys := strings.Split(key, "::") + if len(sectionKeys) >= 2 { + curValue, ok := c.data[sectionKeys[0]] + if !ok { + return nil + } + for _, key := range sectionKeys[1:] { + if v, ok := curValue.(map[string]interface{}); ok { + if curValue, ok = v[key]; !ok { + return nil + } + } + } + return curValue + } + if v, ok := c.data[key]; ok { + return v + } + return nil +} + +func init() { + Register("json", &JsonConfig{}) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/json_test.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/json_test.go new file mode 100644 index 0000000..5aedae3 --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/json_test.go @@ -0,0 +1,193 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 config + +import ( + "os" + "testing" +) + +var jsoncontext = `{ +"appname": "beeapi", +"testnames": "foo;bar", +"httpport": 8080, +"mysqlport": 3600, +"PI": 3.1415976, +"runmode": "dev", +"autorender": false, +"copyrequestbody": true, +"database": { + "host": "host", + "port": "port", + "database": "database", + "username": "username", + "password": "password", + "conns":{ + "maxconnection":12, + "autoconnect":true, + "connectioninfo":"info" + } + } +}` + +var jsoncontextwitharray = `[ + { + "url": "user", + "serviceAPI": "http://www.test.com/user" + }, + { + "url": "employee", + "serviceAPI": "http://www.test.com/employee" + } +]` + +func TestJsonStartsWithArray(t *testing.T) { + f, err := os.Create("testjsonWithArray.conf") + if err != nil { + t.Fatal(err) + } + _, err = f.WriteString(jsoncontextwitharray) + if err != nil { + f.Close() + t.Fatal(err) + } + f.Close() + defer os.Remove("testjsonWithArray.conf") + jsonconf, err := NewConfig("json", "testjsonWithArray.conf") + if err != nil { + t.Fatal(err) + } + rootArray, err := jsonconf.DIY("rootArray") + if err != nil { + t.Error("array does not exist as element") + } + rootArrayCasted := rootArray.([]interface{}) + if rootArrayCasted == nil { + t.Error("array from root is nil") + } else { + elem := rootArrayCasted[0].(map[string]interface{}) + if elem["url"] != "user" || elem["serviceAPI"] != "http://www.test.com/user" { + t.Error("array[0] values are not valid") + } + + elem2 := rootArrayCasted[1].(map[string]interface{}) + if elem2["url"] != "employee" || elem2["serviceAPI"] != "http://www.test.com/employee" { + t.Error("array[1] values are not valid") + } + } +} + +func TestJson(t *testing.T) { + f, err := os.Create("testjson.conf") + if err != nil { + t.Fatal(err) + } + _, err = f.WriteString(jsoncontext) + if err != nil { + f.Close() + t.Fatal(err) + } + f.Close() + defer os.Remove("testjson.conf") + jsonconf, err := NewConfig("json", "testjson.conf") + if err != nil { + t.Fatal(err) + } + if jsonconf.String("appname") != "beeapi" { + t.Fatal("appname not equal to beeapi") + } + if port, err := jsonconf.Int("httpport"); err != nil || port != 8080 { + t.Error(port) + t.Fatal(err) + } + if port, err := jsonconf.Int64("mysqlport"); err != nil || port != 3600 { + t.Error(port) + t.Fatal(err) + } + if pi, err := jsonconf.Float("PI"); err != nil || pi != 3.1415976 { + t.Error(pi) + t.Fatal(err) + } + if jsonconf.String("runmode") != "dev" { + t.Fatal("runmode not equal to dev") + } + if v := jsonconf.Strings("unknown"); len(v) > 0 { + t.Fatal("unknown strings, the length should be 0") + } + if v := jsonconf.Strings("testnames"); len(v) != 2 { + t.Fatal("testnames length should be 2") + } + if v, err := jsonconf.Bool("autorender"); err != nil || v != false { + t.Error(v) + t.Fatal(err) + } + if v, err := jsonconf.Bool("copyrequestbody"); err != nil || v != true { + t.Error(v) + t.Fatal(err) + } + if err = jsonconf.Set("name", "astaxie"); err != nil { + t.Fatal(err) + } + if jsonconf.String("name") != "astaxie" { + t.Fatal("get name error") + } + if jsonconf.String("database::host") != "host" { + t.Fatal("get database::host error") + } + if jsonconf.String("database::conns::connectioninfo") != "info" { + t.Fatal("get database::conns::connectioninfo error") + } + if maxconnection, err := jsonconf.Int("database::conns::maxconnection"); err != nil || maxconnection != 12 { + t.Fatal("get database::conns::maxconnection error") + } + if db, err := jsonconf.DIY("database"); err != nil { + t.Fatal(err) + } else if m, ok := db.(map[string]interface{}); !ok { + t.Log(db) + t.Fatal("db not map[string]interface{}") + } else { + if m["host"].(string) != "host" { + t.Fatal("get host err") + } + } + + if _, err := jsonconf.Int("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an Int") + } + + if _, err := jsonconf.Int64("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an Int64") + } + + if _, err := jsonconf.Float("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting a Float") + } + + if _, err := jsonconf.DIY("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an interface{}") + } + + if val := jsonconf.String("unknown"); val != "" { + t.Error("unknown keys should return an empty string when expecting a String") + } + + if _, err := jsonconf.Bool("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting a Bool") + } + + if !jsonconf.DefaultBool("unknow", true) { + t.Error("unknown keys with default value wrong") + } +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/xml/xml.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/xml/xml.go new file mode 100644 index 0000000..a1d9fcd --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/xml/xml.go @@ -0,0 +1,228 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 xml for config provider +// +// depend on github.com/beego/x2j +// +// go install github.com/beego/x2j +// +// Usage: +// import( +// _ "github.com/astaxie/beego/config/xml" +// "github.com/astaxie/beego/config" +// ) +// +// cnf, err := config.NewConfig("xml", "config.xml") +// +// more docs http://beego.me/docs/module/config.md +package xml + +import ( + "encoding/xml" + "errors" + "fmt" + "io/ioutil" + "os" + "path" + "strconv" + "strings" + "sync" + "time" + + "github.com/astaxie/beego/config" + "github.com/beego/x2j" +) + +// XmlConfig is a xml config parser and implements Config interface. +// xml configurations should be included in tag. +// only support key/value pair as value as each item. +type XMLConfig struct{} + +// Parse returns a ConfigContainer with parsed xml config map. +func (xc *XMLConfig) Parse(filename string) (config.ConfigContainer, error) { + file, err := os.Open(filename) + if err != nil { + return nil, err + } + defer file.Close() + + x := &XMLConfigContainer{data: make(map[string]interface{})} + content, err := ioutil.ReadAll(file) + if err != nil { + return nil, err + } + + d, err := x2j.DocToMap(string(content)) + if err != nil { + return nil, err + } + + x.data = d["config"].(map[string]interface{}) + return x, nil +} + +func (x *XMLConfig) ParseData(data []byte) (config.ConfigContainer, error) { + // Save memory data to temporary file + tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) + os.MkdirAll(path.Dir(tmpName), os.ModePerm) + if err := ioutil.WriteFile(tmpName, data, 0655); err != nil { + return nil, err + } + return x.Parse(tmpName) +} + +// A Config represents the xml configuration. +type XMLConfigContainer struct { + data map[string]interface{} + sync.Mutex +} + +// Bool returns the boolean value for a given key. +func (c *XMLConfigContainer) Bool(key string) (bool, error) { + return strconv.ParseBool(c.data[key].(string)) +} + +// DefaultBool return the bool value if has no error +// otherwise return the defaultval +func (c *XMLConfigContainer) DefaultBool(key string, defaultval bool) bool { + if v, err := c.Bool(key); err != nil { + return defaultval + } else { + return v + } +} + +// Int returns the integer value for a given key. +func (c *XMLConfigContainer) Int(key string) (int, error) { + return strconv.Atoi(c.data[key].(string)) +} + +// DefaultInt returns the integer value for a given key. +// if err != nil return defaltval +func (c *XMLConfigContainer) DefaultInt(key string, defaultval int) int { + if v, err := c.Int(key); err != nil { + return defaultval + } else { + return v + } +} + +// Int64 returns the int64 value for a given key. +func (c *XMLConfigContainer) Int64(key string) (int64, error) { + return strconv.ParseInt(c.data[key].(string), 10, 64) +} + +// DefaultInt64 returns the int64 value for a given key. +// if err != nil return defaltval +func (c *XMLConfigContainer) DefaultInt64(key string, defaultval int64) int64 { + if v, err := c.Int64(key); err != nil { + return defaultval + } else { + return v + } +} + +// Float returns the float value for a given key. +func (c *XMLConfigContainer) Float(key string) (float64, error) { + return strconv.ParseFloat(c.data[key].(string), 64) +} + +// DefaultFloat returns the float64 value for a given key. +// if err != nil return defaltval +func (c *XMLConfigContainer) DefaultFloat(key string, defaultval float64) float64 { + if v, err := c.Float(key); err != nil { + return defaultval + } else { + return v + } +} + +// String returns the string value for a given key. +func (c *XMLConfigContainer) String(key string) string { + if v, ok := c.data[key].(string); ok { + return v + } + return "" +} + +// DefaultString returns the string value for a given key. +// if err != nil return defaltval +func (c *XMLConfigContainer) DefaultString(key string, defaultval string) string { + if v := c.String(key); v == "" { + return defaultval + } else { + return v + } +} + +// Strings returns the []string value for a given key. +func (c *XMLConfigContainer) Strings(key string) []string { + return strings.Split(c.String(key), ";") +} + +// DefaultStrings returns the []string value for a given key. +// if err != nil return defaltval +func (c *XMLConfigContainer) DefaultStrings(key string, defaultval []string) []string { + if v := c.Strings(key); len(v) == 0 { + return defaultval + } else { + return v + } +} + +// GetSection returns map for the given section +func (c *XMLConfigContainer) GetSection(section string) (map[string]string, error) { + if v, ok := c.data[section]; ok { + return v.(map[string]string), nil + } else { + return nil, errors.New("not exist setction") + } +} + +// SaveConfigFile save the config into file +func (c *XMLConfigContainer) SaveConfigFile(filename string) (err error) { + // Write configuration file by filename. + f, err := os.Create(filename) + if err != nil { + return err + } + defer f.Close() + b, err := xml.MarshalIndent(c.data, " ", " ") + if err != nil { + return err + } + _, err = f.Write(b) + return err +} + +// WriteValue writes a new value for key. +func (c *XMLConfigContainer) Set(key, val string) error { + c.Lock() + defer c.Unlock() + c.data[key] = val + return nil +} + +// DIY returns the raw value by a given key. +func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) { + if v, ok := c.data[key]; ok { + return v, nil + } + return nil, errors.New("not exist key") +} + +func init() { + config.Register("xml", &XMLConfig{}) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/xml/xml_test.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/xml/xml_test.go new file mode 100644 index 0000000..fa3c17f --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/xml/xml_test.go @@ -0,0 +1,85 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 xml + +import ( + "os" + "testing" + + "github.com/astaxie/beego/config" +) + +//xml parse should incluce in tags +var xmlcontext = ` + +beeapi +8080 +3600 +3.1415976 +dev +false +true + +` + +func TestXML(t *testing.T) { + f, err := os.Create("testxml.conf") + if err != nil { + t.Fatal(err) + } + _, err = f.WriteString(xmlcontext) + if err != nil { + f.Close() + t.Fatal(err) + } + f.Close() + defer os.Remove("testxml.conf") + xmlconf, err := config.NewConfig("xml", "testxml.conf") + if err != nil { + t.Fatal(err) + } + if xmlconf.String("appname") != "beeapi" { + t.Fatal("appname not equal to beeapi") + } + if port, err := xmlconf.Int("httpport"); err != nil || port != 8080 { + t.Error(port) + t.Fatal(err) + } + if port, err := xmlconf.Int64("mysqlport"); err != nil || port != 3600 { + t.Error(port) + t.Fatal(err) + } + if pi, err := xmlconf.Float("PI"); err != nil || pi != 3.1415976 { + t.Error(pi) + t.Fatal(err) + } + if xmlconf.String("runmode") != "dev" { + t.Fatal("runmode not equal to dev") + } + if v, err := xmlconf.Bool("autorender"); err != nil || v != false { + t.Error(v) + t.Fatal(err) + } + if v, err := xmlconf.Bool("copyrequestbody"); err != nil || v != true { + t.Error(v) + t.Fatal(err) + } + if err = xmlconf.Set("name", "astaxie"); err != nil { + t.Fatal(err) + } + if xmlconf.String("name") != "astaxie" { + t.Fatal("get name error") + } +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/yaml/yaml.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/yaml/yaml.go new file mode 100644 index 0000000..c5be44a --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/yaml/yaml.go @@ -0,0 +1,265 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 yaml for config provider +// +// depend on github.com/beego/goyaml2 +// +// go install github.com/beego/goyaml2 +// +// Usage: +// import( +// _ "github.com/astaxie/beego/config/yaml" +// "github.com/astaxie/beego/config" +// ) +// +// cnf, err := config.NewConfig("yaml", "config.yaml") +// +// more docs http://beego.me/docs/module/config.md +package yaml + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "log" + "os" + "path" + "strings" + "sync" + "time" + + "github.com/astaxie/beego/config" + "github.com/beego/goyaml2" +) + +// YAMLConfig is a yaml config parser and implements Config interface. +type YAMLConfig struct{} + +// Parse returns a ConfigContainer with parsed yaml config map. +func (yaml *YAMLConfig) Parse(filename string) (y config.ConfigContainer, err error) { + cnf, err := ReadYmlReader(filename) + if err != nil { + return + } + y = &YAMLConfigContainer{ + data: cnf, + } + return +} + +func (yaml *YAMLConfig) ParseData(data []byte) (config.ConfigContainer, error) { + // Save memory data to temporary file + tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) + os.MkdirAll(path.Dir(tmpName), os.ModePerm) + if err := ioutil.WriteFile(tmpName, data, 0655); err != nil { + return nil, err + } + return yaml.Parse(tmpName) +} + +// Read yaml file to map. +// if json like, use json package, unless goyaml2 package. +func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { + f, err := os.Open(path) + if err != nil { + return + } + defer f.Close() + + buf, err := ioutil.ReadAll(f) + if err != nil || len(buf) < 3 { + return + } + + if string(buf[0:1]) == "{" { + log.Println("Look like a Json, try json umarshal") + err = json.Unmarshal(buf, &cnf) + if err == nil { + log.Println("It is Json Map") + return + } + } + + data, err := goyaml2.Read(bytes.NewBuffer(buf)) + if err != nil { + log.Println("Goyaml2 ERR>", string(buf), err) + return + } + + if data == nil { + log.Println("Goyaml2 output nil? Pls report bug\n" + string(buf)) + return + } + cnf, ok := data.(map[string]interface{}) + if !ok { + log.Println("Not a Map? >> ", string(buf), data) + cnf = nil + } + return +} + +// A Config represents the yaml configuration. +type YAMLConfigContainer struct { + data map[string]interface{} + sync.Mutex +} + +// Bool returns the boolean value for a given key. +func (c *YAMLConfigContainer) Bool(key string) (bool, error) { + if v, ok := c.data[key].(bool); ok { + return v, nil + } + return false, errors.New("not bool value") +} + +// DefaultBool return the bool value if has no error +// otherwise return the defaultval +func (c *YAMLConfigContainer) DefaultBool(key string, defaultval bool) bool { + if v, err := c.Bool(key); err != nil { + return defaultval + } else { + return v + } +} + +// Int returns the integer value for a given key. +func (c *YAMLConfigContainer) Int(key string) (int, error) { + if v, ok := c.data[key].(int64); ok { + return int(v), nil + } + return 0, errors.New("not int value") +} + +// DefaultInt returns the integer value for a given key. +// if err != nil return defaltval +func (c *YAMLConfigContainer) DefaultInt(key string, defaultval int) int { + if v, err := c.Int(key); err != nil { + return defaultval + } else { + return v + } +} + +// Int64 returns the int64 value for a given key. +func (c *YAMLConfigContainer) Int64(key string) (int64, error) { + if v, ok := c.data[key].(int64); ok { + return v, nil + } + return 0, errors.New("not bool value") +} + +// DefaultInt64 returns the int64 value for a given key. +// if err != nil return defaltval +func (c *YAMLConfigContainer) DefaultInt64(key string, defaultval int64) int64 { + if v, err := c.Int64(key); err != nil { + return defaultval + } else { + return v + } +} + +// Float returns the float value for a given key. +func (c *YAMLConfigContainer) Float(key string) (float64, error) { + if v, ok := c.data[key].(float64); ok { + return v, nil + } + return 0.0, errors.New("not float64 value") +} + +// DefaultFloat returns the float64 value for a given key. +// if err != nil return defaltval +func (c *YAMLConfigContainer) DefaultFloat(key string, defaultval float64) float64 { + if v, err := c.Float(key); err != nil { + return defaultval + } else { + return v + } +} + +// String returns the string value for a given key. +func (c *YAMLConfigContainer) String(key string) string { + if v, ok := c.data[key].(string); ok { + return v + } + return "" +} + +// DefaultString returns the string value for a given key. +// if err != nil return defaltval +func (c *YAMLConfigContainer) DefaultString(key string, defaultval string) string { + if v := c.String(key); v == "" { + return defaultval + } else { + return v + } +} + +// Strings returns the []string value for a given key. +func (c *YAMLConfigContainer) Strings(key string) []string { + return strings.Split(c.String(key), ";") +} + +// DefaultStrings returns the []string value for a given key. +// if err != nil return defaltval +func (c *YAMLConfigContainer) DefaultStrings(key string, defaultval []string) []string { + if v := c.Strings(key); len(v) == 0 { + return defaultval + } else { + return v + } +} + +// GetSection returns map for the given section +func (c *YAMLConfigContainer) GetSection(section string) (map[string]string, error) { + if v, ok := c.data[section]; ok { + return v.(map[string]string), nil + } else { + return nil, errors.New("not exist setction") + } +} + +// SaveConfigFile save the config into file +func (c *YAMLConfigContainer) SaveConfigFile(filename string) (err error) { + // Write configuration file by filename. + f, err := os.Create(filename) + if err != nil { + return err + } + defer f.Close() + err = goyaml2.Write(f, c.data) + return err +} + +// WriteValue writes a new value for key. +func (c *YAMLConfigContainer) Set(key, val string) error { + c.Lock() + defer c.Unlock() + c.data[key] = val + return nil +} + +// DIY returns the raw value by a given key. +func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) { + if v, ok := c.data[key]; ok { + return v, nil + } + return nil, errors.New("not exist key") +} + +func init() { + config.Register("yaml", &YAMLConfig{}) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/config/yaml/yaml_test.go b/Godeps/_workspace/src/github.com/astaxie/beego/config/yaml/yaml_test.go new file mode 100644 index 0000000..19ecdca --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/config/yaml/yaml_test.go @@ -0,0 +1,82 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 yaml + +import ( + "os" + "testing" + + "github.com/astaxie/beego/config" +) + +var yamlcontext = ` +"appname": beeapi +"httpport": 8080 +"mysqlport": 3600 +"PI": 3.1415976 +"runmode": dev +"autorender": false +"copyrequestbody": true +` + +func TestYaml(t *testing.T) { + f, err := os.Create("testyaml.conf") + if err != nil { + t.Fatal(err) + } + _, err = f.WriteString(yamlcontext) + if err != nil { + f.Close() + t.Fatal(err) + } + f.Close() + defer os.Remove("testyaml.conf") + yamlconf, err := config.NewConfig("yaml", "testyaml.conf") + if err != nil { + t.Fatal(err) + } + if yamlconf.String("appname") != "beeapi" { + t.Fatal("appname not equal to beeapi") + } + if port, err := yamlconf.Int("httpport"); err != nil || port != 8080 { + t.Error(port) + t.Fatal(err) + } + if port, err := yamlconf.Int64("mysqlport"); err != nil || port != 3600 { + t.Error(port) + t.Fatal(err) + } + if pi, err := yamlconf.Float("PI"); err != nil || pi != 3.1415976 { + t.Error(pi) + t.Fatal(err) + } + if yamlconf.String("runmode") != "dev" { + t.Fatal("runmode not equal to dev") + } + if v, err := yamlconf.Bool("autorender"); err != nil || v != false { + t.Error(v) + t.Fatal(err) + } + if v, err := yamlconf.Bool("copyrequestbody"); err != nil || v != true { + t.Error(v) + t.Fatal(err) + } + if err = yamlconf.Set("name", "astaxie"); err != nil { + t.Fatal(err) + } + if yamlconf.String("name") != "astaxie" { + t.Fatal("get name error") + } +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/README.md b/Godeps/_workspace/src/github.com/astaxie/beego/logs/README.md new file mode 100644 index 0000000..57d7abc --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/README.md @@ -0,0 +1,63 @@ +## logs +logs is a Go logs manager. It can use many logs adapters. The repo is inspired by `database/sql` . + + +## How to install? + + go get github.com/astaxie/beego/logs + + +## What adapters are supported? + +As of now this logs support console, file,smtp and conn. + + +## How to use it? + +First you must import it + + import ( + "github.com/astaxie/beego/logs" + ) + +Then init a Log (example with console adapter) + + log := NewLogger(10000) + log.SetLogger("console", "") + +> the first params stand for how many channel + +Use it like this: + + log.Trace("trace") + log.Info("info") + log.Warn("warning") + log.Debug("debug") + log.Critical("critical") + + +## File adapter + +Configure file adapter like this: + + log := NewLogger(10000) + log.SetLogger("file", `{"filename":"test.log"}`) + + +## Conn adapter + +Configure like this: + + log := NewLogger(1000) + log.SetLogger("conn", `{"net":"tcp","addr":":7020"}`) + log.Info("info") + + +## Smtp adapter + +Configure like this: + + log := NewLogger(10000) + log.SetLogger("smtp", `{"username":"beegotest@gmail.com","password":"xxxxxxxx","host":"smtp.gmail.com:587","sendTos":["xiemengjun@gmail.com"]}`) + log.Critical("sendmail critical") + time.Sleep(time.Second * 30) diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/conn.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/conn.go new file mode 100644 index 0000000..2240eec --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/conn.go @@ -0,0 +1,116 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 logs + +import ( + "encoding/json" + "io" + "log" + "net" +) + +// ConnWriter implements LoggerInterface. +// it writes messages in keep-live tcp connection. +type ConnWriter struct { + lg *log.Logger + innerWriter io.WriteCloser + ReconnectOnMsg bool `json:"reconnectOnMsg"` + Reconnect bool `json:"reconnect"` + Net string `json:"net"` + Addr string `json:"addr"` + Level int `json:"level"` +} + +// create new ConnWrite returning as LoggerInterface. +func NewConn() LoggerInterface { + conn := new(ConnWriter) + conn.Level = LevelTrace + return conn +} + +// init connection writer with json config. +// json config only need key "level". +func (c *ConnWriter) Init(jsonconfig string) error { + return json.Unmarshal([]byte(jsonconfig), c) +} + +// write message in connection. +// if connection is down, try to re-connect. +func (c *ConnWriter) WriteMsg(msg string, level int) error { + if level > c.Level { + return nil + } + if c.neddedConnectOnMsg() { + err := c.connect() + if err != nil { + return err + } + } + + if c.ReconnectOnMsg { + defer c.innerWriter.Close() + } + c.lg.Println(msg) + return nil +} + +// implementing method. empty. +func (c *ConnWriter) Flush() { + +} + +// destroy connection writer and close tcp listener. +func (c *ConnWriter) Destroy() { + if c.innerWriter != nil { + c.innerWriter.Close() + } +} + +func (c *ConnWriter) connect() error { + if c.innerWriter != nil { + c.innerWriter.Close() + c.innerWriter = nil + } + + conn, err := net.Dial(c.Net, c.Addr) + if err != nil { + return err + } + + if tcpConn, ok := conn.(*net.TCPConn); ok { + tcpConn.SetKeepAlive(true) + } + + c.innerWriter = conn + c.lg = log.New(conn, "", log.Ldate|log.Ltime) + return nil +} + +func (c *ConnWriter) neddedConnectOnMsg() bool { + if c.Reconnect { + c.Reconnect = false + return true + } + + if c.innerWriter == nil { + return true + } + + return c.ReconnectOnMsg +} + +func init() { + Register("conn", NewConn) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/conn_test.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/conn_test.go new file mode 100644 index 0000000..747fb89 --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/conn_test.go @@ -0,0 +1,25 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 logs + +import ( + "testing" +) + +func TestConn(t *testing.T) { + log := NewLogger(1000) + log.SetLogger("conn", `{"net":"tcp","addr":":7020"}`) + log.Informational("informational") +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/console.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/console.go new file mode 100644 index 0000000..ce7ecd5 --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/console.go @@ -0,0 +1,95 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 logs + +import ( + "encoding/json" + "log" + "os" + "runtime" +) + +type Brush func(string) string + +func NewBrush(color string) Brush { + pre := "\033[" + reset := "\033[0m" + return func(text string) string { + return pre + color + "m" + text + reset + } +} + +var colors = []Brush{ + NewBrush("1;37"), // Emergency white + NewBrush("1;36"), // Alert cyan + NewBrush("1;35"), // Critical magenta + NewBrush("1;31"), // Error red + NewBrush("1;33"), // Warning yellow + NewBrush("1;32"), // Notice green + NewBrush("1;34"), // Informational blue + NewBrush("1;34"), // Debug blue +} + +// ConsoleWriter implements LoggerInterface and writes messages to terminal. +type ConsoleWriter struct { + lg *log.Logger + Level int `json:"level"` +} + +// create ConsoleWriter returning as LoggerInterface. +func NewConsole() LoggerInterface { + cw := &ConsoleWriter{ + lg: log.New(os.Stdout, "", log.Ldate|log.Ltime), + Level: LevelDebug, + } + return cw +} + +// init console logger. +// jsonconfig like '{"level":LevelTrace}'. +func (c *ConsoleWriter) Init(jsonconfig string) error { + if len(jsonconfig) == 0 { + return nil + } + return json.Unmarshal([]byte(jsonconfig), c) +} + +// write message in console. +func (c *ConsoleWriter) WriteMsg(msg string, level int) error { + if level > c.Level { + return nil + } + if goos := runtime.GOOS; goos == "windows" { + c.lg.Println(msg) + return nil + } + c.lg.Println(colors[level](msg)) + + return nil +} + +// implementing method. empty. +func (c *ConsoleWriter) Destroy() { + +} + +// implementing method. empty. +func (c *ConsoleWriter) Flush() { + +} + +func init() { + Register("console", NewConsole) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/console_test.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/console_test.go new file mode 100644 index 0000000..2fad724 --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/console_test.go @@ -0,0 +1,53 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 logs + +import ( + "testing" +) + +// Try each log level in decreasing order of priority. +func testConsoleCalls(bl *BeeLogger) { + bl.Emergency("emergency") + bl.Alert("alert") + bl.Critical("critical") + bl.Error("error") + bl.Warning("warning") + bl.Notice("notice") + bl.Informational("informational") + bl.Debug("debug") +} + +// Test console logging by visually comparing the lines being output with and +// without a log level specification. +func TestConsole(t *testing.T) { + log1 := NewLogger(10000) + log1.EnableFuncCallDepth(true) + log1.SetLogger("console", "") + testConsoleCalls(log1) + + log2 := NewLogger(100) + log2.SetLogger("console", `{"level":3}`) + testConsoleCalls(log2) +} + +func BenchmarkConsole(b *testing.B) { + log := NewLogger(10000) + log.EnableFuncCallDepth(true) + log.SetLogger("console", "") + for i := 0; i < b.N; i++ { + log.Debug("debug") + } +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/file.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/file.go new file mode 100644 index 0000000..2d3449c --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/file.go @@ -0,0 +1,283 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 logs + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "log" + "os" + "path/filepath" + "strings" + "sync" + "time" +) + +// FileLogWriter implements LoggerInterface. +// It writes messages by lines limit, file size limit, or time frequency. +type FileLogWriter struct { + *log.Logger + mw *MuxWriter + // The opened file + Filename string `json:"filename"` + + Maxlines int `json:"maxlines"` + maxlines_curlines int + + // Rotate at size + Maxsize int `json:"maxsize"` + maxsize_cursize int + + // Rotate daily + Daily bool `json:"daily"` + Maxdays int64 `json:"maxdays"` + daily_opendate int + + Rotate bool `json:"rotate"` + + startLock sync.Mutex // Only one log can write to the file + + Level int `json:"level"` +} + +// an *os.File writer with locker. +type MuxWriter struct { + sync.Mutex + fd *os.File +} + +// write to os.File. +func (l *MuxWriter) Write(b []byte) (int, error) { + l.Lock() + defer l.Unlock() + return l.fd.Write(b) +} + +// set os.File in writer. +func (l *MuxWriter) SetFd(fd *os.File) { + if l.fd != nil { + l.fd.Close() + } + l.fd = fd +} + +// create a FileLogWriter returning as LoggerInterface. +func NewFileWriter() LoggerInterface { + w := &FileLogWriter{ + Filename: "", + Maxlines: 1000000, + Maxsize: 1 << 28, //256 MB + Daily: true, + Maxdays: 7, + Rotate: true, + Level: LevelTrace, + } + // use MuxWriter instead direct use os.File for lock write when rotate + w.mw = new(MuxWriter) + // set MuxWriter as Logger's io.Writer + w.Logger = log.New(w.mw, "", log.Ldate|log.Ltime) + return w +} + +// Init file logger with json config. +// jsonconfig like: +// { +// "filename":"logs/beego.log", +// "maxlines":10000, +// "maxsize":1<<30, +// "daily":true, +// "maxdays":15, +// "rotate":true +// } +func (w *FileLogWriter) Init(jsonconfig string) error { + err := json.Unmarshal([]byte(jsonconfig), w) + if err != nil { + return err + } + if len(w.Filename) == 0 { + return errors.New("jsonconfig must have filename") + } + err = w.startLogger() + return err +} + +// start file logger. create log file and set to locker-inside file writer. +func (w *FileLogWriter) startLogger() error { + fd, err := w.createLogFile() + if err != nil { + return err + } + w.mw.SetFd(fd) + return w.initFd() +} + +func (w *FileLogWriter) docheck(size int) { + w.startLock.Lock() + defer w.startLock.Unlock() + if w.Rotate && ((w.Maxlines > 0 && w.maxlines_curlines >= w.Maxlines) || + (w.Maxsize > 0 && w.maxsize_cursize >= w.Maxsize) || + (w.Daily && time.Now().Day() != w.daily_opendate)) { + if err := w.DoRotate(); err != nil { + fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err) + return + } + } + w.maxlines_curlines++ + w.maxsize_cursize += size +} + +// write logger message into file. +func (w *FileLogWriter) WriteMsg(msg string, level int) error { + if level > w.Level { + return nil + } + n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] " + w.docheck(n) + w.Logger.Println(msg) + return nil +} + +func (w *FileLogWriter) createLogFile() (*os.File, error) { + // Open the log file + fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660) + return fd, err +} + +func (w *FileLogWriter) initFd() error { + fd := w.mw.fd + finfo, err := fd.Stat() + if err != nil { + return fmt.Errorf("get stat err: %s\n", err) + } + w.maxsize_cursize = int(finfo.Size()) + w.daily_opendate = time.Now().Day() + w.maxlines_curlines = 0 + if finfo.Size() > 0 { + count, err := w.lines() + if err != nil { + return err + } + w.maxlines_curlines = count + } + return nil +} + +func (w *FileLogWriter) lines() (int, error) { + fd, err := os.Open(w.Filename) + if err != nil { + return 0, err + } + defer fd.Close() + + buf := make([]byte, 32768) // 32k + count := 0 + lineSep := []byte{'\n'} + + for { + c, err := fd.Read(buf) + if err != nil && err != io.EOF { + return count, err + } + + count += bytes.Count(buf[:c], lineSep) + + if err == io.EOF { + break + } + } + + return count, nil +} + +// DoRotate means it need to write file in new file. +// new file name like xx.log.2013-01-01.2 +func (w *FileLogWriter) DoRotate() error { + _, err := os.Lstat(w.Filename) + if err == nil { // file exists + // Find the next available number + num := 1 + fname := "" + for ; err == nil && num <= 999; num++ { + fname = w.Filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num) + _, err = os.Lstat(fname) + } + // 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) + } + + // block Logger's io.Writer + w.mw.Lock() + defer w.mw.Unlock() + + fd := w.mw.fd + fd.Close() + + // close fd before rename + // Rename the file to its newfound home + err = os.Rename(w.Filename, fname) + if err != nil { + return fmt.Errorf("Rotate: %s\n", err) + } + + // re-start logger + err = w.startLogger() + if err != nil { + return fmt.Errorf("Rotate StartLogger: %s\n", err) + } + + go w.deleteOldLog() + } + + return nil +} + +func (w *FileLogWriter) deleteOldLog() { + dir := filepath.Dir(w.Filename) + filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) { + defer func() { + if r := recover(); r != nil { + returnErr = fmt.Errorf("Unable to delete old log '%s', error: %+v", path, r) + fmt.Println(returnErr) + } + }() + + if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) { + if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) { + os.Remove(path) + } + } + return + }) +} + +// destroy file logger, close file writer. +func (w *FileLogWriter) Destroy() { + w.mw.fd.Close() +} + +// flush file logger. +// there are no buffering messages in file logger in memory. +// flush file means sync file from disk. +func (w *FileLogWriter) Flush() { + w.mw.fd.Sync() +} + +func init() { + Register("file", NewFileWriter) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/file_test.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/file_test.go new file mode 100644 index 0000000..c71e9bb --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/file_test.go @@ -0,0 +1,133 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 logs + +import ( + "bufio" + "fmt" + "os" + "strconv" + "testing" + "time" +) + +func TestFile(t *testing.T) { + log := NewLogger(10000) + log.SetLogger("file", `{"filename":"test.log"}`) + log.Debug("debug") + log.Informational("info") + log.Notice("notice") + log.Warning("warning") + log.Error("error") + log.Alert("alert") + log.Critical("critical") + log.Emergency("emergency") + time.Sleep(time.Second * 4) + f, err := os.Open("test.log") + if err != nil { + t.Fatal(err) + } + b := bufio.NewReader(f) + linenum := 0 + for { + line, _, err := b.ReadLine() + if err != nil { + break + } + if len(line) > 0 { + linenum++ + } + } + var expected = LevelDebug + 1 + if linenum != expected { + t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines") + } + os.Remove("test.log") +} + +func TestFile2(t *testing.T) { + log := NewLogger(10000) + log.SetLogger("file", fmt.Sprintf(`{"filename":"test2.log","level":%d}`, LevelError)) + log.Debug("debug") + log.Info("info") + log.Notice("notice") + log.Warning("warning") + log.Error("error") + log.Alert("alert") + log.Critical("critical") + log.Emergency("emergency") + time.Sleep(time.Second * 4) + f, err := os.Open("test2.log") + if err != nil { + t.Fatal(err) + } + b := bufio.NewReader(f) + linenum := 0 + for { + line, _, err := b.ReadLine() + if err != nil { + break + } + if len(line) > 0 { + linenum++ + } + } + var expected = LevelError + 1 + if linenum != expected { + t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines") + } + os.Remove("test2.log") +} + +func TestFileRotate(t *testing.T) { + log := NewLogger(10000) + log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) + log.Debug("debug") + log.Info("info") + log.Notice("notice") + log.Warning("warning") + log.Error("error") + log.Alert("alert") + log.Critical("critical") + log.Emergency("emergency") + time.Sleep(time.Second * 4) + rotatename := "test3.log" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1) + b, err := exists(rotatename) + if !b || err != nil { + t.Fatal("rotate not generated") + } + os.Remove(rotatename) + os.Remove("test3.log") +} + +func exists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} + +func BenchmarkFile(b *testing.B) { + log := NewLogger(100000) + log.SetLogger("file", `{"filename":"test4.log"}`) + for i := 0; i < b.N; i++ { + log.Debug("debug") + } + os.Remove("test4.log") +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/log.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/log.go new file mode 100644 index 0000000..32e0187 --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/log.go @@ -0,0 +1,303 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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. + +// Usage: +// +// import "github.com/astaxie/beego/logs" +// +// log := NewLogger(10000) +// log.SetLogger("console", "") +// +// > the first params stand for how many channel +// +// Use it like this: +// +// log.Trace("trace") +// log.Info("info") +// log.Warn("warning") +// log.Debug("debug") +// log.Critical("critical") +// +// more docs http://beego.me/docs/module/logs.md +package logs + +import ( + "fmt" + "path" + "runtime" + "sync" +) + +// RFC5424 log message levels. +const ( + LevelEmergency = iota + LevelAlert + LevelCritical + LevelError + LevelWarning + LevelNotice + LevelInformational + LevelDebug +) + +// Legacy loglevel constants to ensure backwards compatibility. +// +// Deprecated: will be removed in 1.5.0. +const ( + LevelInfo = LevelInformational + LevelTrace = LevelDebug + LevelWarn = LevelWarning +) + +type loggerType func() LoggerInterface + +// LoggerInterface defines the behavior of a log provider. +type LoggerInterface interface { + Init(config string) error + WriteMsg(msg string, level int) error + Destroy() + Flush() +} + +var adapters = make(map[string]loggerType) + +// Register makes a log provide available by the provided name. +// If Register is called twice with the same name or if driver is nil, +// it panics. +func Register(name string, log loggerType) { + if log == nil { + panic("logs: Register provide is nil") + } + if _, dup := adapters[name]; dup { + panic("logs: Register called twice for provider " + name) + } + adapters[name] = log +} + +// BeeLogger is default logger in beego application. +// it can contain several providers and log message into all providers. +type BeeLogger struct { + lock sync.Mutex + level int + enableFuncCallDepth bool + loggerFuncCallDepth int + msg chan *logMsg + outputs map[string]LoggerInterface +} + +type logMsg struct { + level int + msg string +} + +// NewLogger returns a new BeeLogger. +// channellen means the number of messages in chan. +// if the buffering chan is full, logger adapters write to file or other way. +func NewLogger(channellen int64) *BeeLogger { + bl := new(BeeLogger) + bl.level = LevelDebug + bl.loggerFuncCallDepth = 2 + bl.msg = make(chan *logMsg, channellen) + bl.outputs = make(map[string]LoggerInterface) + //bl.SetLogger("console", "") // default output to console + go bl.startLogger() + return bl +} + +// SetLogger provides a given logger adapter into BeeLogger with config string. +// config need to be correct JSON as string: {"interval":360}. +func (bl *BeeLogger) SetLogger(adaptername string, config string) error { + bl.lock.Lock() + defer bl.lock.Unlock() + if log, ok := adapters[adaptername]; ok { + lg := log() + err := lg.Init(config) + bl.outputs[adaptername] = lg + if err != nil { + fmt.Println("logs.BeeLogger.SetLogger: " + err.Error()) + return err + } + } else { + return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adaptername) + } + return nil +} + +// remove a logger adapter in BeeLogger. +func (bl *BeeLogger) DelLogger(adaptername string) error { + bl.lock.Lock() + defer bl.lock.Unlock() + if lg, ok := bl.outputs[adaptername]; ok { + lg.Destroy() + delete(bl.outputs, adaptername) + return nil + } else { + return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adaptername) + } +} + +func (bl *BeeLogger) writerMsg(loglevel int, msg string) error { + if loglevel > bl.level { + return nil + } + lm := new(logMsg) + lm.level = loglevel + if bl.enableFuncCallDepth { + _, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth) + if _, filename := path.Split(file); filename == "log.go" && (line == 97 || line == 83) { + _, file, line, ok = runtime.Caller(bl.loggerFuncCallDepth + 1) + } + if ok { + _, filename := path.Split(file) + lm.msg = fmt.Sprintf("[%s:%d] %s", filename, line, msg) + } else { + lm.msg = msg + } + } else { + lm.msg = msg + } + bl.msg <- lm + return nil +} + +// Set log message level. +// +// If message level (such as LevelDebug) is higher than logger level (such as LevelWarning), +// log providers will not even be sent the message. +func (bl *BeeLogger) SetLevel(l int) { + bl.level = l +} + +// set log funcCallDepth +func (bl *BeeLogger) SetLogFuncCallDepth(d int) { + bl.loggerFuncCallDepth = d +} + +// enable log funcCallDepth +func (bl *BeeLogger) EnableFuncCallDepth(b bool) { + bl.enableFuncCallDepth = b +} + +// start logger chan reading. +// when chan is not empty, write logs. +func (bl *BeeLogger) startLogger() { + for { + select { + case bm := <-bl.msg: + for _, l := range bl.outputs { + err := l.WriteMsg(bm.msg, bm.level) + if err != nil { + fmt.Println("ERROR, unable to WriteMsg:", err) + } + } + } + } +} + +// Log EMERGENCY level message. +func (bl *BeeLogger) Emergency(format string, v ...interface{}) { + msg := fmt.Sprintf("[M] "+format, v...) + bl.writerMsg(LevelEmergency, msg) +} + +// Log ALERT level message. +func (bl *BeeLogger) Alert(format string, v ...interface{}) { + msg := fmt.Sprintf("[A] "+format, v...) + bl.writerMsg(LevelAlert, msg) +} + +// Log CRITICAL level message. +func (bl *BeeLogger) Critical(format string, v ...interface{}) { + msg := fmt.Sprintf("[C] "+format, v...) + bl.writerMsg(LevelCritical, msg) +} + +// Log ERROR level message. +func (bl *BeeLogger) Error(format string, v ...interface{}) { + msg := fmt.Sprintf("[E] "+format, v...) + bl.writerMsg(LevelError, msg) +} + +// Log WARNING level message. +func (bl *BeeLogger) Warning(format string, v ...interface{}) { + msg := fmt.Sprintf("[W] "+format, v...) + bl.writerMsg(LevelWarning, msg) +} + +// Log NOTICE level message. +func (bl *BeeLogger) Notice(format string, v ...interface{}) { + msg := fmt.Sprintf("[N] "+format, v...) + bl.writerMsg(LevelNotice, msg) +} + +// Log INFORMATIONAL level message. +func (bl *BeeLogger) Informational(format string, v ...interface{}) { + msg := fmt.Sprintf("[I] "+format, v...) + bl.writerMsg(LevelInformational, msg) +} + +// Log DEBUG level message. +func (bl *BeeLogger) Debug(format string, v ...interface{}) { + msg := fmt.Sprintf("[D] "+format, v...) + bl.writerMsg(LevelDebug, msg) +} + +// Log WARN level message. +// +// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0. +func (bl *BeeLogger) Warn(format string, v ...interface{}) { + bl.Warning(format, v...) +} + +// Log INFO level message. +// +// Deprecated: compatibility alias for Informational(), Will be removed in 1.5.0. +func (bl *BeeLogger) Info(format string, v ...interface{}) { + bl.Informational(format, v...) +} + +// Log TRACE level message. +// +// Deprecated: compatibility alias for Debug(), Will be removed in 1.5.0. +func (bl *BeeLogger) Trace(format string, v ...interface{}) { + bl.Debug(format, v...) +} + +// flush all chan data. +func (bl *BeeLogger) Flush() { + for _, l := range bl.outputs { + l.Flush() + } +} + +// close logger, flush all chan data and destroy all adapters in BeeLogger. +func (bl *BeeLogger) Close() { + for { + if len(bl.msg) > 0 { + bm := <-bl.msg + for _, l := range bl.outputs { + err := l.WriteMsg(bm.msg, bm.level) + if err != nil { + fmt.Println("ERROR, unable to WriteMsg (while closing logger):", err) + } + } + continue + } + break + } + for _, l := range bl.outputs { + l.Flush() + l.Destroy() + } +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/smtp.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/smtp.go new file mode 100644 index 0000000..95123eb --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/smtp.go @@ -0,0 +1,165 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 logs + +import ( + "crypto/tls" + "encoding/json" + "fmt" + "net" + "net/smtp" + "strings" + "time" +) + +const ( +// no usage +// subjectPhrase = "Diagnostic message from server" +) + +// smtpWriter implements LoggerInterface and is used to send emails via given SMTP-server. +type SmtpWriter struct { + Username string `json:"Username"` + Password string `json:"password"` + Host string `json:"Host"` + Subject string `json:"subject"` + FromAddress string `json:"fromAddress"` + RecipientAddresses []string `json:"sendTos"` + Level int `json:"level"` +} + +// create smtp writer. +func NewSmtpWriter() LoggerInterface { + return &SmtpWriter{Level: LevelTrace} +} + +// init smtp writer with json config. +// config like: +// { +// "Username":"example@gmail.com", +// "password:"password", +// "host":"smtp.gmail.com:465", +// "subject":"email title", +// "fromAddress":"from@example.com", +// "sendTos":["email1","email2"], +// "level":LevelError +// } +func (s *SmtpWriter) Init(jsonconfig string) error { + err := json.Unmarshal([]byte(jsonconfig), s) + if err != nil { + return err + } + return nil +} + +func (s *SmtpWriter) GetSmtpAuth(host string) smtp.Auth { + if len(strings.Trim(s.Username, " ")) == 0 && len(strings.Trim(s.Password, " ")) == 0 { + return nil + } + return smtp.PlainAuth( + "", + s.Username, + s.Password, + host, + ) +} + +func (s *SmtpWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAddress string, recipients []string, msgContent []byte) error { + client, err := smtp.Dial(hostAddressWithPort) + if err != nil { + return err + } + + host, _, _ := net.SplitHostPort(hostAddressWithPort) + tlsConn := &tls.Config{ + InsecureSkipVerify: true, + ServerName: host, + } + if err = client.StartTLS(tlsConn); err != nil { + return err + } + + if auth != nil { + if err = client.Auth(auth); err != nil { + return err + } + } + + if err = client.Mail(fromAddress); err != nil { + return err + } + + for _, rec := range recipients { + if err = client.Rcpt(rec); err != nil { + return err + } + } + + w, err := client.Data() + if err != nil { + return err + } + _, err = w.Write([]byte(msgContent)) + if err != nil { + return err + } + + err = w.Close() + if err != nil { + return err + } + + err = client.Quit() + if err != nil { + return err + } + + return nil +} + +// write message in smtp writer. +// it will send an email with subject and only this message. +func (s *SmtpWriter) WriteMsg(msg string, level int) error { + if level > s.Level { + return nil + } + + hp := strings.Split(s.Host, ":") + + // Set up authentication information. + auth := s.GetSmtpAuth(hp[0]) + + // Connect to the server, authenticate, set the sender and recipient, + // and send the email all in one step. + content_type := "Content-Type: text/plain" + "; charset=UTF-8" + mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress + + ">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg) + + return s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg) +} + +// implementing method. empty. +func (s *SmtpWriter) Flush() { + return +} + +// implementing method. empty. +func (s *SmtpWriter) Destroy() { + return +} + +func init() { + Register("smtp", NewSmtpWriter) +} diff --git a/Godeps/_workspace/src/github.com/astaxie/beego/logs/smtp_test.go b/Godeps/_workspace/src/github.com/astaxie/beego/logs/smtp_test.go new file mode 100644 index 0000000..28e762d --- /dev/null +++ b/Godeps/_workspace/src/github.com/astaxie/beego/logs/smtp_test.go @@ -0,0 +1,27 @@ +// Copyright 2014 beego Author. All Rights Reserved. +// +// 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 logs + +import ( + "testing" + "time" +) + +func TestSmtp(t *testing.T) { + log := NewLogger(10000) + log.SetLogger("smtp", `{"username":"beegotest@gmail.com","password":"xxxxxxxx","host":"smtp.gmail.com:587","sendTos":["xiemengjun@gmail.com"]}`) + log.Critical("sendmail critical") + time.Sleep(time.Second * 30) +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/.travis.yml b/Godeps/_workspace/src/github.com/codegangsta/cli/.travis.yml new file mode 100644 index 0000000..baf46ab --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/.travis.yml @@ -0,0 +1,6 @@ +language: go +go: 1.1 + +script: +- go vet ./... +- go test -v ./... diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/LICENSE b/Godeps/_workspace/src/github.com/codegangsta/cli/LICENSE new file mode 100644 index 0000000..5515ccf --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/LICENSE @@ -0,0 +1,21 @@ +Copyright (C) 2013 Jeremy Saenz +All Rights Reserved. + +MIT LICENSE + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/README.md b/Godeps/_workspace/src/github.com/codegangsta/cli/README.md new file mode 100644 index 0000000..cd980fd --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/README.md @@ -0,0 +1,306 @@ +[![Build Status](https://travis-ci.org/codegangsta/cli.png?branch=master)](https://travis-ci.org/codegangsta/cli) + +# cli.go +cli.go is simple, fast, and fun package for building command line apps in Go. The goal is to enable developers to write fast and distributable command line applications in an expressive way. + +You can view the API docs here: +http://godoc.org/github.com/codegangsta/cli + +## Overview +Command line apps are usually so tiny that there is absolutely no reason why your code should *not* be self-documenting. Things like generating help text and parsing command flags/options should not hinder productivity when writing a command line app. + +**This is where cli.go comes into play.** cli.go makes command line programming fun, organized, and expressive! + +## Installation +Make sure you have a working Go environment (go 1.1+ is *required*). [See the install instructions](http://golang.org/doc/install.html). + +To install `cli.go`, simply run: +``` +$ go get github.com/codegangsta/cli +``` + +Make sure your `PATH` includes to the `$GOPATH/bin` directory so your commands can be easily used: +``` +export PATH=$PATH:$GOPATH/bin +``` + +## Getting Started +One of the philosophies behind cli.go is that an API should be playful and full of discovery. So a cli.go app can be as little as one line of code in `main()`. + +``` go +package main + +import ( + "os" + "github.com/codegangsta/cli" +) + +func main() { + cli.NewApp().Run(os.Args) +} +``` + +This app will run and show help text, but is not very useful. Let's give an action to execute and some help documentation: + +``` go +package main + +import ( + "os" + "github.com/codegangsta/cli" +) + +func main() { + app := cli.NewApp() + app.Name = "boom" + app.Usage = "make an explosive entrance" + app.Action = func(c *cli.Context) { + println("boom! I say!") + } + + app.Run(os.Args) +} +``` + +Running this already gives you a ton of functionality, plus support for things like subcommands and flags, which are covered below. + +## Example + +Being a programmer can be a lonely job. Thankfully by the power of automation that is not the case! Let's create a greeter app to fend off our demons of loneliness! + +Start by creating a directory named `greet`, and within it, add a file, `greet.go` with the following code in it: + +``` go +package main + +import ( + "os" + "github.com/codegangsta/cli" +) + +func main() { + app := cli.NewApp() + app.Name = "greet" + app.Usage = "fight the loneliness!" + app.Action = func(c *cli.Context) { + println("Hello friend!") + } + + app.Run(os.Args) +} +``` + +Install our command to the `$GOPATH/bin` directory: + +``` +$ go install +``` + +Finally run our new command: + +``` +$ greet +Hello friend! +``` + +cli.go also generates some bitchass help text: +``` +$ greet help +NAME: + greet - fight the loneliness! + +USAGE: + greet [global options] command [command options] [arguments...] + +VERSION: + 0.0.0 + +COMMANDS: + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS + --version Shows version information +``` + +### Arguments +You can lookup arguments by calling the `Args` function on `cli.Context`. + +``` go +... +app.Action = func(c *cli.Context) { + println("Hello", c.Args()[0]) +} +... +``` + +### Flags +Setting and querying flags is simple. +``` go +... +app.Flags = []cli.Flag { + cli.StringFlag{ + Name: "lang", + Value: "english", + Usage: "language for the greeting", + }, +} +app.Action = func(c *cli.Context) { + name := "someone" + if len(c.Args()) > 0 { + name = c.Args()[0] + } + if c.String("lang") == "spanish" { + println("Hola", name) + } else { + println("Hello", name) + } +} +... +``` + +#### Alternate Names + +You can set alternate (or short) names for flags by providing a comma-delimited list for the `Name`. e.g. + +``` go +app.Flags = []cli.Flag { + cli.StringFlag{ + Name: "lang, l", + Value: "english", + Usage: "language for the greeting", + }, +} +``` + +That flag can then be set with `--lang spanish` or `-l spanish`. Note that giving two different forms of the same flag in the same command invocation is an error. + +#### Values from the Environment + +You can also have the default value set from the environment via `EnvVar`. e.g. + +``` go +app.Flags = []cli.Flag { + cli.StringFlag{ + Name: "lang, l", + Value: "english", + Usage: "language for the greeting", + EnvVar: "APP_LANG", + }, +} +``` + +The `EnvVar` may also be given as a comma-delimited "cascade", where the first environment variable that resolves is used as the default. + +``` go +app.Flags = []cli.Flag { + cli.StringFlag{ + Name: "lang, l", + Value: "english", + Usage: "language for the greeting", + EnvVar: "LEGACY_COMPAT_LANG,APP_LANG,LANG", + }, +} +``` + +### Subcommands + +Subcommands can be defined for a more git-like command line app. +```go +... +app.Commands = []cli.Command{ + { + Name: "add", + Aliases: []string{"a"}, + Usage: "add a task to the list", + Action: func(c *cli.Context) { + println("added task: ", c.Args().First()) + }, + }, + { + Name: "complete", + Aliases: []string{"c"}, + Usage: "complete a task on the list", + Action: func(c *cli.Context) { + println("completed task: ", c.Args().First()) + }, + }, + { + Name: "template", + Aliases: []string{"r"}, + Usage: "options for task templates", + Subcommands: []cli.Command{ + { + Name: "add", + Usage: "add a new template", + Action: func(c *cli.Context) { + println("new task template: ", c.Args().First()) + }, + }, + { + Name: "remove", + Usage: "remove an existing template", + Action: func(c *cli.Context) { + println("removed task template: ", c.Args().First()) + }, + }, + }, + }, +} +... +``` + +### Bash Completion + +You can enable completion commands by setting the `EnableBashCompletion` +flag on the `App` object. By default, this setting will only auto-complete to +show an app's subcommands, but you can write your own completion methods for +the App or its subcommands. +```go +... +var tasks = []string{"cook", "clean", "laundry", "eat", "sleep", "code"} +app := cli.NewApp() +app.EnableBashCompletion = true +app.Commands = []cli.Command{ + { + Name: "complete", + Aliases: []string{"c"}, + Usage: "complete a task on the list", + Action: func(c *cli.Context) { + println("completed task: ", c.Args().First()) + }, + BashComplete: func(c *cli.Context) { + // This will complete if no args are passed + if len(c.Args()) > 0 { + return + } + for _, t := range tasks { + fmt.Println(t) + } + }, + } +} +... +``` + +#### To Enable + +Source the `autocomplete/bash_autocomplete` file in your `.bashrc` file while +setting the `PROG` variable to the name of your program: + +`PROG=myprogram source /.../cli/autocomplete/bash_autocomplete` + +#### To Distribute + +Copy and modify `autocomplete/bash_autocomplete` to use your program name +rather than `$PROG` and have the user copy the file into +`/etc/bash_completion.d/` (or automatically install it there if you are +distributing a package). Alternatively you can just document that users should +source the generic `autocomplete/bash_autocomplete` with `$PROG` set to your +program name in their bash configuration. + +## Contribution Guidelines +Feel free to put up a pull request to fix a bug or maybe add a feature. I will give it a code review and make sure that it does not break backwards compatibility. If I or any other collaborators agree that it is in line with the vision of the project, we will work with you to get the code into a mergeable state and merge it into the master branch. + +If you have contributed something significant to the project, I will most likely add you as a collaborator. As a collaborator you are given the ability to merge others pull requests. It is very important that new code does not break existing code, so be careful about what code you do choose to merge. If you have any questions feel free to link @codegangsta to the issue in question and we can review it together. + +If you feel like you have contributed to the project but have not yet been added as a collaborator, I probably forgot to add you. Hit @codegangsta up over email and we will get it figured out. diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/app.go b/Godeps/_workspace/src/github.com/codegangsta/cli/app.go new file mode 100644 index 0000000..891416d --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/app.go @@ -0,0 +1,298 @@ +package cli + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "time" +) + +// App is the main structure of a cli application. It is recomended that +// and app be created with the cli.NewApp() function +type App struct { + // The name of the program. Defaults to os.Args[0] + Name string + // Description of the program. + Usage string + // Version of the program + Version string + // List of commands to execute + Commands []Command + // List of flags to parse + Flags []Flag + // Boolean to enable bash completion commands + EnableBashCompletion bool + // Boolean to hide built-in help command + HideHelp bool + // Boolean to hide built-in version flag + HideVersion bool + // An action to execute when the bash-completion flag is set + BashComplete func(context *Context) + // An action to execute before any subcommands are run, but after the context is ready + // If a non-nil error is returned, no subcommands are run + Before func(context *Context) error + // An action to execute after any subcommands are run, but after the subcommand has finished + // It is run even if Action() panics + After func(context *Context) error + // The action to execute when no subcommands are specified + Action func(context *Context) + // Execute this function if the proper command cannot be found + CommandNotFound func(context *Context, command string) + // Compilation date + Compiled time.Time + // List of all authors who contributed + Authors []Author + // Name of Author (Note: Use App.Authors, this is deprecated) + Author string + // Email of Author (Note: Use App.Authors, this is deprecated) + Email string + // Writer writer to write output to + Writer io.Writer +} + +// Tries to find out when this binary was compiled. +// Returns the current time if it fails to find it. +func compileTime() time.Time { + info, err := os.Stat(os.Args[0]) + if err != nil { + return time.Now() + } + return info.ModTime() +} + +// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action. +func NewApp() *App { + return &App{ + Name: os.Args[0], + Usage: "A new cli application", + Version: "0.0.0", + BashComplete: DefaultAppComplete, + Action: helpCommand.Action, + Compiled: compileTime(), + Writer: os.Stdout, + } +} + +// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination +func (a *App) Run(arguments []string) (err error) { + if a.Author != "" || a.Email != "" { + a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email}) + } + + // append help to commands + if a.Command(helpCommand.Name) == nil && !a.HideHelp { + a.Commands = append(a.Commands, helpCommand) + if (HelpFlag != BoolFlag{}) { + a.appendFlag(HelpFlag) + } + } + + //append version/help flags + if a.EnableBashCompletion { + a.appendFlag(BashCompletionFlag) + } + + if !a.HideVersion { + a.appendFlag(VersionFlag) + } + + // parse flags + set := flagSet(a.Name, a.Flags) + set.SetOutput(ioutil.Discard) + err = set.Parse(arguments[1:]) + nerr := normalizeFlags(a.Flags, set) + if nerr != nil { + fmt.Fprintln(a.Writer, nerr) + context := NewContext(a, set, set) + ShowAppHelp(context) + fmt.Fprintln(a.Writer) + return nerr + } + context := NewContext(a, set, set) + + if err != nil { + fmt.Fprintf(a.Writer, "Incorrect Usage.\n\n") + ShowAppHelp(context) + fmt.Fprintln(a.Writer) + return err + } + + if checkCompletions(context) { + return nil + } + + if checkHelp(context) { + return nil + } + + if checkVersion(context) { + return nil + } + + if a.After != nil { + defer func() { + // err is always nil here. + // There is a check to see if it is non-nil + // just few lines before. + err = a.After(context) + }() + } + + if a.Before != nil { + err := a.Before(context) + if err != nil { + return err + } + } + + args := context.Args() + if args.Present() { + name := args.First() + c := a.Command(name) + if c != nil { + return c.Run(context) + } + } + + // Run default Action + a.Action(context) + return nil +} + +// Another entry point to the cli app, takes care of passing arguments and error handling +func (a *App) RunAndExitOnError() { + if err := a.Run(os.Args); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags +func (a *App) RunAsSubcommand(ctx *Context) (err error) { + // append help to commands + if len(a.Commands) > 0 { + if a.Command(helpCommand.Name) == nil && !a.HideHelp { + a.Commands = append(a.Commands, helpCommand) + if (HelpFlag != BoolFlag{}) { + a.appendFlag(HelpFlag) + } + } + } + + // append flags + if a.EnableBashCompletion { + a.appendFlag(BashCompletionFlag) + } + + // parse flags + set := flagSet(a.Name, a.Flags) + set.SetOutput(ioutil.Discard) + err = set.Parse(ctx.Args().Tail()) + nerr := normalizeFlags(a.Flags, set) + context := NewContext(a, set, ctx.globalSet) + + if nerr != nil { + fmt.Fprintln(a.Writer, nerr) + if len(a.Commands) > 0 { + ShowSubcommandHelp(context) + } else { + ShowCommandHelp(ctx, context.Args().First()) + } + fmt.Fprintln(a.Writer) + return nerr + } + + if err != nil { + fmt.Fprintf(a.Writer, "Incorrect Usage.\n\n") + ShowSubcommandHelp(context) + return err + } + + if checkCompletions(context) { + return nil + } + + if len(a.Commands) > 0 { + if checkSubcommandHelp(context) { + return nil + } + } else { + if checkCommandHelp(ctx, context.Args().First()) { + return nil + } + } + + if a.After != nil { + defer func() { + // err is always nil here. + // There is a check to see if it is non-nil + // just few lines before. + err = a.After(context) + }() + } + + if a.Before != nil { + err := a.Before(context) + if err != nil { + return err + } + } + + args := context.Args() + if args.Present() { + name := args.First() + c := a.Command(name) + if c != nil { + return c.Run(context) + } + } + + // Run default Action + a.Action(context) + + return nil +} + +// Returns the named command on App. Returns nil if the command does not exist +func (a *App) Command(name string) *Command { + for _, c := range a.Commands { + if c.HasName(name) { + return &c + } + } + + return nil +} + +func (a *App) hasFlag(flag Flag) bool { + for _, f := range a.Flags { + if flag == f { + return true + } + } + + return false +} + +func (a *App) appendFlag(flag Flag) { + if !a.hasFlag(flag) { + a.Flags = append(a.Flags, flag) + } +} + +// Author represents someone who has contributed to a cli project. +type Author struct { + Name string // The Authors name + Email string // The Authors email +} + +// String makes Author comply to the Stringer interface, to allow an easy print in the templating process +func (a Author) String() string { + e := "" + if a.Email != "" { + e = "<" + a.Email + "> " + } + + return fmt.Sprintf("%v %v", a.Name, e) +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/app_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/app_test.go new file mode 100644 index 0000000..ae8bb0f --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/app_test.go @@ -0,0 +1,679 @@ +package cli_test + +import ( + "bytes" + "flag" + "fmt" + "io" + "os" + "strings" + "testing" + + "github.com/codegangsta/cli" +) + +func ExampleApp() { + // set args for examples sake + os.Args = []string{"greet", "--name", "Jeremy"} + + app := cli.NewApp() + app.Name = "greet" + app.Flags = []cli.Flag{ + cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"}, + } + app.Action = func(c *cli.Context) { + fmt.Printf("Hello %v\n", c.String("name")) + } + app.Author = "Harrison" + app.Email = "harrison@lolwut.com" + app.Authors = []cli.Author{cli.Author{Name: "Oliver Allen", Email: "oliver@toyshop.com"}} + app.Run(os.Args) + // Output: + // Hello Jeremy +} + +func ExampleAppSubcommand() { + // set args for examples sake + os.Args = []string{"say", "hi", "english", "--name", "Jeremy"} + app := cli.NewApp() + app.Name = "say" + app.Commands = []cli.Command{ + { + Name: "hello", + Aliases: []string{"hi"}, + Usage: "use it to see a description", + Description: "This is how we describe hello the function", + Subcommands: []cli.Command{ + { + Name: "english", + Aliases: []string{"en"}, + Usage: "sends a greeting in english", + Description: "greets someone in english", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "name", + Value: "Bob", + Usage: "Name of the person to greet", + }, + }, + Action: func(c *cli.Context) { + fmt.Println("Hello,", c.String("name")) + }, + }, + }, + }, + } + + app.Run(os.Args) + // Output: + // Hello, Jeremy +} + +func ExampleAppHelp() { + // set args for examples sake + os.Args = []string{"greet", "h", "describeit"} + + app := cli.NewApp() + app.Name = "greet" + app.Flags = []cli.Flag{ + cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"}, + } + app.Commands = []cli.Command{ + { + Name: "describeit", + Aliases: []string{"d"}, + Usage: "use it to see a description", + Description: "This is how we describe describeit the function", + Action: func(c *cli.Context) { + fmt.Printf("i like to describe things") + }, + }, + } + app.Run(os.Args) + // Output: + // NAME: + // describeit - use it to see a description + // + // USAGE: + // command describeit [arguments...] + // + // DESCRIPTION: + // This is how we describe describeit the function +} + +func ExampleAppBashComplete() { + // set args for examples sake + os.Args = []string{"greet", "--generate-bash-completion"} + + app := cli.NewApp() + app.Name = "greet" + app.EnableBashCompletion = true + app.Commands = []cli.Command{ + { + Name: "describeit", + Aliases: []string{"d"}, + Usage: "use it to see a description", + Description: "This is how we describe describeit the function", + Action: func(c *cli.Context) { + fmt.Printf("i like to describe things") + }, + }, { + Name: "next", + Usage: "next example", + Description: "more stuff to see when generating bash completion", + Action: func(c *cli.Context) { + fmt.Printf("the next example") + }, + }, + } + + app.Run(os.Args) + // Output: + // describeit + // d + // next + // help + // h +} + +func TestApp_Run(t *testing.T) { + s := "" + + app := cli.NewApp() + app.Action = func(c *cli.Context) { + s = s + c.Args().First() + } + + err := app.Run([]string{"command", "foo"}) + expect(t, err, nil) + err = app.Run([]string{"command", "bar"}) + expect(t, err, nil) + expect(t, s, "foobar") +} + +var commandAppTests = []struct { + name string + expected bool +}{ + {"foobar", true}, + {"batbaz", true}, + {"b", true}, + {"f", true}, + {"bat", false}, + {"nothing", false}, +} + +func TestApp_Command(t *testing.T) { + app := cli.NewApp() + fooCommand := cli.Command{Name: "foobar", Aliases: []string{"f"}} + batCommand := cli.Command{Name: "batbaz", Aliases: []string{"b"}} + app.Commands = []cli.Command{ + fooCommand, + batCommand, + } + + for _, test := range commandAppTests { + expect(t, app.Command(test.name) != nil, test.expected) + } +} + +func TestApp_CommandWithArgBeforeFlags(t *testing.T) { + var parsedOption, firstArg string + + app := cli.NewApp() + command := cli.Command{ + Name: "cmd", + Flags: []cli.Flag{ + cli.StringFlag{Name: "option", Value: "", Usage: "some option"}, + }, + Action: func(c *cli.Context) { + parsedOption = c.String("option") + firstArg = c.Args().First() + }, + } + app.Commands = []cli.Command{command} + + app.Run([]string{"", "cmd", "my-arg", "--option", "my-option"}) + + expect(t, parsedOption, "my-option") + expect(t, firstArg, "my-arg") +} + +func TestApp_RunAsSubcommandParseFlags(t *testing.T) { + var context *cli.Context + + a := cli.NewApp() + a.Commands = []cli.Command{ + { + Name: "foo", + Action: func(c *cli.Context) { + context = c + }, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "lang", + Value: "english", + Usage: "language for the greeting", + }, + }, + Before: func(_ *cli.Context) error { return nil }, + }, + } + a.Run([]string{"", "foo", "--lang", "spanish", "abcd"}) + + expect(t, context.Args().Get(0), "abcd") + expect(t, context.String("lang"), "spanish") +} + +func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) { + var parsedOption string + var args []string + + app := cli.NewApp() + command := cli.Command{ + Name: "cmd", + Flags: []cli.Flag{ + cli.StringFlag{Name: "option", Value: "", Usage: "some option"}, + }, + Action: func(c *cli.Context) { + parsedOption = c.String("option") + args = c.Args() + }, + } + app.Commands = []cli.Command{command} + + app.Run([]string{"", "cmd", "my-arg", "--option", "my-option", "--", "--notARealFlag"}) + + expect(t, parsedOption, "my-option") + expect(t, args[0], "my-arg") + expect(t, args[1], "--") + expect(t, args[2], "--notARealFlag") +} + +func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) { + var args []string + + app := cli.NewApp() + command := cli.Command{ + Name: "cmd", + Action: func(c *cli.Context) { + args = c.Args() + }, + } + app.Commands = []cli.Command{command} + + app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"}) + + expect(t, args[0], "my-arg") + expect(t, args[1], "--") + expect(t, args[2], "notAFlagAtAll") +} + +func TestApp_Float64Flag(t *testing.T) { + var meters float64 + + app := cli.NewApp() + app.Flags = []cli.Flag{ + cli.Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"}, + } + app.Action = func(c *cli.Context) { + meters = c.Float64("height") + } + + app.Run([]string{"", "--height", "1.93"}) + expect(t, meters, 1.93) +} + +func TestApp_ParseSliceFlags(t *testing.T) { + var parsedOption, firstArg string + var parsedIntSlice []int + var parsedStringSlice []string + + app := cli.NewApp() + command := cli.Command{ + Name: "cmd", + Flags: []cli.Flag{ + cli.IntSliceFlag{Name: "p", Value: &cli.IntSlice{}, Usage: "set one or more ip addr"}, + cli.StringSliceFlag{Name: "ip", Value: &cli.StringSlice{}, Usage: "set one or more ports to open"}, + }, + Action: func(c *cli.Context) { + parsedIntSlice = c.IntSlice("p") + parsedStringSlice = c.StringSlice("ip") + parsedOption = c.String("option") + firstArg = c.Args().First() + }, + } + app.Commands = []cli.Command{command} + + app.Run([]string{"", "cmd", "my-arg", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4"}) + + IntsEquals := func(a, b []int) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true + } + + StrsEquals := func(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true + } + var expectedIntSlice = []int{22, 80} + var expectedStringSlice = []string{"8.8.8.8", "8.8.4.4"} + + if !IntsEquals(parsedIntSlice, expectedIntSlice) { + t.Errorf("%v does not match %v", parsedIntSlice, expectedIntSlice) + } + + if !StrsEquals(parsedStringSlice, expectedStringSlice) { + t.Errorf("%v does not match %v", parsedStringSlice, expectedStringSlice) + } +} + +func TestApp_DefaultStdout(t *testing.T) { + app := cli.NewApp() + + if app.Writer != os.Stdout { + t.Error("Default output writer not set.") + } +} + +type mockWriter struct { + written []byte +} + +func (fw *mockWriter) Write(p []byte) (n int, err error) { + if fw.written == nil { + fw.written = p + } else { + fw.written = append(fw.written, p...) + } + + return len(p), nil +} + +func (fw *mockWriter) GetWritten() (b []byte) { + return fw.written +} + +func TestApp_SetStdout(t *testing.T) { + w := &mockWriter{} + + app := cli.NewApp() + app.Name = "test" + app.Writer = w + + err := app.Run([]string{"help"}) + + if err != nil { + t.Fatalf("Run error: %s", err) + } + + if len(w.written) == 0 { + t.Error("App did not write output to desired writer.") + } +} + +func TestApp_BeforeFunc(t *testing.T) { + beforeRun, subcommandRun := false, false + beforeError := fmt.Errorf("fail") + var err error + + app := cli.NewApp() + + app.Before = func(c *cli.Context) error { + beforeRun = true + s := c.String("opt") + if s == "fail" { + return beforeError + } + + return nil + } + + app.Commands = []cli.Command{ + cli.Command{ + Name: "sub", + Action: func(c *cli.Context) { + subcommandRun = true + }, + }, + } + + app.Flags = []cli.Flag{ + cli.StringFlag{Name: "opt"}, + } + + // run with the Before() func succeeding + err = app.Run([]string{"command", "--opt", "succeed", "sub"}) + + if err != nil { + t.Fatalf("Run error: %s", err) + } + + if beforeRun == false { + t.Errorf("Before() not executed when expected") + } + + if subcommandRun == false { + t.Errorf("Subcommand not executed when expected") + } + + // reset + beforeRun, subcommandRun = false, false + + // run with the Before() func failing + err = app.Run([]string{"command", "--opt", "fail", "sub"}) + + // should be the same error produced by the Before func + if err != beforeError { + t.Errorf("Run error expected, but not received") + } + + if beforeRun == false { + t.Errorf("Before() not executed when expected") + } + + if subcommandRun == true { + t.Errorf("Subcommand executed when NOT expected") + } + +} + +func TestApp_AfterFunc(t *testing.T) { + afterRun, subcommandRun := false, false + afterError := fmt.Errorf("fail") + var err error + + app := cli.NewApp() + + app.After = func(c *cli.Context) error { + afterRun = true + s := c.String("opt") + if s == "fail" { + return afterError + } + + return nil + } + + app.Commands = []cli.Command{ + cli.Command{ + Name: "sub", + Action: func(c *cli.Context) { + subcommandRun = true + }, + }, + } + + app.Flags = []cli.Flag{ + cli.StringFlag{Name: "opt"}, + } + + // run with the After() func succeeding + err = app.Run([]string{"command", "--opt", "succeed", "sub"}) + + if err != nil { + t.Fatalf("Run error: %s", err) + } + + if afterRun == false { + t.Errorf("After() not executed when expected") + } + + if subcommandRun == false { + t.Errorf("Subcommand not executed when expected") + } + + // reset + afterRun, subcommandRun = false, false + + // run with the Before() func failing + err = app.Run([]string{"command", "--opt", "fail", "sub"}) + + // should be the same error produced by the Before func + if err != afterError { + t.Errorf("Run error expected, but not received") + } + + if afterRun == false { + t.Errorf("After() not executed when expected") + } + + if subcommandRun == false { + t.Errorf("Subcommand not executed when expected") + } +} + +func TestAppNoHelpFlag(t *testing.T) { + oldFlag := cli.HelpFlag + defer func() { + cli.HelpFlag = oldFlag + }() + + cli.HelpFlag = cli.BoolFlag{} + + app := cli.NewApp() + err := app.Run([]string{"test", "-h"}) + + if err != flag.ErrHelp { + t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err) + } +} + +func TestAppHelpPrinter(t *testing.T) { + oldPrinter := cli.HelpPrinter + defer func() { + cli.HelpPrinter = oldPrinter + }() + + var wasCalled = false + cli.HelpPrinter = func(w io.Writer, template string, data interface{}) { + wasCalled = true + } + + app := cli.NewApp() + app.Run([]string{"-h"}) + + if wasCalled == false { + t.Errorf("Help printer expected to be called, but was not") + } +} + +func TestAppVersionPrinter(t *testing.T) { + oldPrinter := cli.VersionPrinter + defer func() { + cli.VersionPrinter = oldPrinter + }() + + var wasCalled = false + cli.VersionPrinter = func(c *cli.Context) { + wasCalled = true + } + + app := cli.NewApp() + ctx := cli.NewContext(app, nil, nil) + cli.ShowVersion(ctx) + + if wasCalled == false { + t.Errorf("Version printer expected to be called, but was not") + } +} + +func TestAppCommandNotFound(t *testing.T) { + beforeRun, subcommandRun := false, false + app := cli.NewApp() + + app.CommandNotFound = func(c *cli.Context, command string) { + beforeRun = true + } + + app.Commands = []cli.Command{ + cli.Command{ + Name: "bar", + Action: func(c *cli.Context) { + subcommandRun = true + }, + }, + } + + app.Run([]string{"command", "foo"}) + + expect(t, beforeRun, true) + expect(t, subcommandRun, false) +} + +func TestGlobalFlagsInSubcommands(t *testing.T) { + subcommandRun := false + app := cli.NewApp() + + app.Flags = []cli.Flag{ + cli.BoolFlag{Name: "debug, d", Usage: "Enable debugging"}, + } + + app.Commands = []cli.Command{ + cli.Command{ + Name: "foo", + Subcommands: []cli.Command{ + { + Name: "bar", + Action: func(c *cli.Context) { + if c.GlobalBool("debug") { + subcommandRun = true + } + }, + }, + }, + }, + } + + app.Run([]string{"command", "-d", "foo", "bar"}) + + expect(t, subcommandRun, true) +} + +func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) { + var subcommandHelpTopics = [][]string{ + {"command", "foo", "--help"}, + {"command", "foo", "-h"}, + {"command", "foo", "help"}, + } + + for _, flagSet := range subcommandHelpTopics { + t.Logf("==> checking with flags %v", flagSet) + + app := cli.NewApp() + buf := new(bytes.Buffer) + app.Writer = buf + + subCmdBar := cli.Command{ + Name: "bar", + Usage: "does bar things", + } + subCmdBaz := cli.Command{ + Name: "baz", + Usage: "does baz things", + } + cmd := cli.Command{ + Name: "foo", + Description: "descriptive wall of text about how it does foo things", + Subcommands: []cli.Command{subCmdBar, subCmdBaz}, + } + + app.Commands = []cli.Command{cmd} + err := app.Run(flagSet) + + if err != nil { + t.Error(err) + } + + output := buf.String() + t.Logf("output: %q\n", buf.Bytes()) + + if strings.Contains(output, "No help topic for") { + t.Errorf("expect a help topic, got none: \n%q", output) + } + + for _, shouldContain := range []string{ + cmd.Name, cmd.Description, + subCmdBar.Name, subCmdBar.Usage, + subCmdBaz.Name, subCmdBaz.Usage, + } { + if !strings.Contains(output, shouldContain) { + t.Errorf("want help to contain %q, did not: \n%q", shouldContain, output) + } + } + } +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/bash_autocomplete b/Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/bash_autocomplete new file mode 100644 index 0000000..9b55dd9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/bash_autocomplete @@ -0,0 +1,13 @@ +#! /bin/bash + +_cli_bash_autocomplete() { + local cur prev opts base + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion ) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + } + + complete -F _cli_bash_autocomplete $PROG \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/zsh_autocomplete b/Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/zsh_autocomplete new file mode 100644 index 0000000..5430a18 --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/zsh_autocomplete @@ -0,0 +1,5 @@ +autoload -U compinit && compinit +autoload -U bashcompinit && bashcompinit + +script_dir=$(dirname $0) +source ${script_dir}/bash_autocomplete diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/cli.go b/Godeps/_workspace/src/github.com/codegangsta/cli/cli.go new file mode 100644 index 0000000..b742545 --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/cli.go @@ -0,0 +1,19 @@ +// Package cli provides a minimal framework for creating and organizing command line +// Go applications. cli is designed to be easy to understand and write, the most simple +// cli application can be written as follows: +// func main() { +// cli.NewApp().Run(os.Args) +// } +// +// Of course this application does not do much, so let's make this an actual application: +// func main() { +// app := cli.NewApp() +// app.Name = "greet" +// app.Usage = "say a greeting" +// app.Action = func(c *cli.Context) { +// println("Greetings") +// } +// +// app.Run(os.Args) +// } +package cli diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/cli_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/cli_test.go new file mode 100644 index 0000000..8a8df97 --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/cli_test.go @@ -0,0 +1,100 @@ +package cli_test + +import ( + "os" + + "github.com/codegangsta/cli" +) + +func Example() { + app := cli.NewApp() + app.Name = "todo" + app.Usage = "task list on the command line" + app.Commands = []cli.Command{ + { + Name: "add", + Aliases: []string{"a"}, + Usage: "add a task to the list", + Action: func(c *cli.Context) { + println("added task: ", c.Args().First()) + }, + }, + { + Name: "complete", + Aliases: []string{"c"}, + Usage: "complete a task on the list", + Action: func(c *cli.Context) { + println("completed task: ", c.Args().First()) + }, + }, + } + + app.Run(os.Args) +} + +func ExampleSubcommand() { + app := cli.NewApp() + app.Name = "say" + app.Commands = []cli.Command{ + { + Name: "hello", + Aliases: []string{"hi"}, + Usage: "use it to see a description", + Description: "This is how we describe hello the function", + Subcommands: []cli.Command{ + { + Name: "english", + Aliases: []string{"en"}, + Usage: "sends a greeting in english", + Description: "greets someone in english", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "name", + Value: "Bob", + Usage: "Name of the person to greet", + }, + }, + Action: func(c *cli.Context) { + println("Hello, ", c.String("name")) + }, + }, { + Name: "spanish", + Aliases: []string{"sp"}, + Usage: "sends a greeting in spanish", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "surname", + Value: "Jones", + Usage: "Surname of the person to greet", + }, + }, + Action: func(c *cli.Context) { + println("Hola, ", c.String("surname")) + }, + }, { + Name: "french", + Aliases: []string{"fr"}, + Usage: "sends a greeting in french", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "nickname", + Value: "Stevie", + Usage: "Nickname of the person to greet", + }, + }, + Action: func(c *cli.Context) { + println("Bonjour, ", c.String("nickname")) + }, + }, + }, + }, { + Name: "bye", + Usage: "says goodbye", + Action: func(c *cli.Context) { + println("bye") + }, + }, + } + + app.Run(os.Args) +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/command.go b/Godeps/_workspace/src/github.com/codegangsta/cli/command.go new file mode 100644 index 0000000..d0bbd0c --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/command.go @@ -0,0 +1,184 @@ +package cli + +import ( + "fmt" + "io/ioutil" + "strings" +) + +// Command is a subcommand for a cli.App. +type Command struct { + // The name of the command + Name string + // short name of the command. Typically one character (deprecated, use `Aliases`) + ShortName string + // A list of aliases for the command + Aliases []string + // A short description of the usage of this command + Usage string + // A longer explanation of how the command works + Description string + // The function to call when checking for bash command completions + BashComplete func(context *Context) + // An action to execute before any sub-subcommands are run, but after the context is ready + // If a non-nil error is returned, no sub-subcommands are run + Before func(context *Context) error + // An action to execute after any subcommands are run, but after the subcommand has finished + // It is run even if Action() panics + After func(context *Context) error + // The function to call when this command is invoked + Action func(context *Context) + // List of child commands + Subcommands []Command + // List of flags to parse + Flags []Flag + // Treat all flags as normal arguments if true + SkipFlagParsing bool + // Boolean to hide built-in help command + HideHelp bool +} + +// Invokes the command given the context, parses ctx.Args() to generate command-specific flags +func (c Command) Run(ctx *Context) error { + + if len(c.Subcommands) > 0 || c.Before != nil || c.After != nil { + return c.startApp(ctx) + } + + if !c.HideHelp && (HelpFlag != BoolFlag{}) { + // append help to flags + c.Flags = append( + c.Flags, + HelpFlag, + ) + } + + if ctx.App.EnableBashCompletion { + c.Flags = append(c.Flags, BashCompletionFlag) + } + + set := flagSet(c.Name, c.Flags) + set.SetOutput(ioutil.Discard) + + firstFlagIndex := -1 + terminatorIndex := -1 + for index, arg := range ctx.Args() { + if arg == "--" { + terminatorIndex = index + break + } else if strings.HasPrefix(arg, "-") && firstFlagIndex == -1 { + firstFlagIndex = index + } + } + + var err error + if firstFlagIndex > -1 && !c.SkipFlagParsing { + args := ctx.Args() + regularArgs := make([]string, len(args[1:firstFlagIndex])) + copy(regularArgs, args[1:firstFlagIndex]) + + var flagArgs []string + if terminatorIndex > -1 { + flagArgs = args[firstFlagIndex:terminatorIndex] + regularArgs = append(regularArgs, args[terminatorIndex:]...) + } else { + flagArgs = args[firstFlagIndex:] + } + + err = set.Parse(append(flagArgs, regularArgs...)) + } else { + err = set.Parse(ctx.Args().Tail()) + } + + if err != nil { + fmt.Fprint(ctx.App.Writer, "Incorrect Usage.\n\n") + ShowCommandHelp(ctx, c.Name) + fmt.Fprintln(ctx.App.Writer) + return err + } + + nerr := normalizeFlags(c.Flags, set) + if nerr != nil { + fmt.Fprintln(ctx.App.Writer, nerr) + fmt.Fprintln(ctx.App.Writer) + ShowCommandHelp(ctx, c.Name) + fmt.Fprintln(ctx.App.Writer) + return nerr + } + context := NewContext(ctx.App, set, ctx.globalSet) + + if checkCommandCompletions(context, c.Name) { + return nil + } + + if checkCommandHelp(context, c.Name) { + return nil + } + context.Command = c + c.Action(context) + return nil +} + +func (c Command) Names() []string { + names := []string{c.Name} + + if c.ShortName != "" { + names = append(names, c.ShortName) + } + + return append(names, c.Aliases...) +} + +// Returns true if Command.Name or Command.ShortName matches given name +func (c Command) HasName(name string) bool { + for _, n := range c.Names() { + if n == name { + return true + } + } + return false +} + +func (c Command) startApp(ctx *Context) error { + app := NewApp() + + // set the name and usage + app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) + if c.Description != "" { + app.Usage = c.Description + } else { + app.Usage = c.Usage + } + + // set CommandNotFound + app.CommandNotFound = ctx.App.CommandNotFound + + // set the flags and commands + app.Commands = c.Subcommands + app.Flags = c.Flags + app.HideHelp = c.HideHelp + + app.Version = ctx.App.Version + app.HideVersion = ctx.App.HideVersion + app.Compiled = ctx.App.Compiled + app.Author = ctx.App.Author + app.Email = ctx.App.Email + app.Writer = ctx.App.Writer + + // bash completion + app.EnableBashCompletion = ctx.App.EnableBashCompletion + if c.BashComplete != nil { + app.BashComplete = c.BashComplete + } + + // set the actions + app.Before = c.Before + app.After = c.After + if c.Action != nil { + app.Action = c.Action + } else { + app.Action = helpSubcommand.Action + } + + return app.RunAsSubcommand(ctx) +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/command_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/command_test.go new file mode 100644 index 0000000..4125b0c --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/command_test.go @@ -0,0 +1,49 @@ +package cli_test + +import ( + "flag" + "testing" + + "github.com/codegangsta/cli" +) + +func TestCommandDoNotIgnoreFlags(t *testing.T) { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + test := []string{"blah", "blah", "-break"} + set.Parse(test) + + c := cli.NewContext(app, set, set) + + command := cli.Command{ + Name: "test-cmd", + Aliases: []string{"tc"}, + Usage: "this is for testing", + Description: "testing", + Action: func(_ *cli.Context) {}, + } + err := command.Run(c) + + expect(t, err.Error(), "flag provided but not defined: -break") +} + +func TestCommandIgnoreFlags(t *testing.T) { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + test := []string{"blah", "blah"} + set.Parse(test) + + c := cli.NewContext(app, set, set) + + command := cli.Command{ + Name: "test-cmd", + Aliases: []string{"tc"}, + Usage: "this is for testing", + Description: "testing", + Action: func(_ *cli.Context) {}, + SkipFlagParsing: true, + } + err := command.Run(c) + + expect(t, err, nil) +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/context.go b/Godeps/_workspace/src/github.com/codegangsta/cli/context.go new file mode 100644 index 0000000..37221bd --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/context.go @@ -0,0 +1,344 @@ +package cli + +import ( + "errors" + "flag" + "strconv" + "strings" + "time" +) + +// Context is a type that is passed through to +// each Handler action in a cli application. Context +// can be used to retrieve context-specific Args and +// parsed command-line options. +type Context struct { + App *App + Command Command + flagSet *flag.FlagSet + globalSet *flag.FlagSet + setFlags map[string]bool + globalSetFlags map[string]bool +} + +// Creates a new context. For use in when invoking an App or Command action. +func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context { + return &Context{App: app, flagSet: set, globalSet: globalSet} +} + +// Looks up the value of a local int flag, returns 0 if no int flag exists +func (c *Context) Int(name string) int { + return lookupInt(name, c.flagSet) +} + +// Looks up the value of a local time.Duration flag, returns 0 if no time.Duration flag exists +func (c *Context) Duration(name string) time.Duration { + return lookupDuration(name, c.flagSet) +} + +// Looks up the value of a local float64 flag, returns 0 if no float64 flag exists +func (c *Context) Float64(name string) float64 { + return lookupFloat64(name, c.flagSet) +} + +// Looks up the value of a local bool flag, returns false if no bool flag exists +func (c *Context) Bool(name string) bool { + return lookupBool(name, c.flagSet) +} + +// Looks up the value of a local boolT flag, returns false if no bool flag exists +func (c *Context) BoolT(name string) bool { + return lookupBoolT(name, c.flagSet) +} + +// Looks up the value of a local string flag, returns "" if no string flag exists +func (c *Context) String(name string) string { + return lookupString(name, c.flagSet) +} + +// Looks up the value of a local string slice flag, returns nil if no string slice flag exists +func (c *Context) StringSlice(name string) []string { + return lookupStringSlice(name, c.flagSet) +} + +// Looks up the value of a local int slice flag, returns nil if no int slice flag exists +func (c *Context) IntSlice(name string) []int { + return lookupIntSlice(name, c.flagSet) +} + +// Looks up the value of a local generic flag, returns nil if no generic flag exists +func (c *Context) Generic(name string) interface{} { + return lookupGeneric(name, c.flagSet) +} + +// Looks up the value of a global int flag, returns 0 if no int flag exists +func (c *Context) GlobalInt(name string) int { + return lookupInt(name, c.globalSet) +} + +// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists +func (c *Context) GlobalDuration(name string) time.Duration { + return lookupDuration(name, c.globalSet) +} + +// Looks up the value of a global bool flag, returns false if no bool flag exists +func (c *Context) GlobalBool(name string) bool { + return lookupBool(name, c.globalSet) +} + +// Looks up the value of a global string flag, returns "" if no string flag exists +func (c *Context) GlobalString(name string) string { + return lookupString(name, c.globalSet) +} + +// Looks up the value of a global string slice flag, returns nil if no string slice flag exists +func (c *Context) GlobalStringSlice(name string) []string { + return lookupStringSlice(name, c.globalSet) +} + +// Looks up the value of a global int slice flag, returns nil if no int slice flag exists +func (c *Context) GlobalIntSlice(name string) []int { + return lookupIntSlice(name, c.globalSet) +} + +// Looks up the value of a global generic flag, returns nil if no generic flag exists +func (c *Context) GlobalGeneric(name string) interface{} { + return lookupGeneric(name, c.globalSet) +} + +// Returns the number of flags set +func (c *Context) NumFlags() int { + return c.flagSet.NFlag() +} + +// Determines if the flag was actually set +func (c *Context) IsSet(name string) bool { + if c.setFlags == nil { + c.setFlags = make(map[string]bool) + c.flagSet.Visit(func(f *flag.Flag) { + c.setFlags[f.Name] = true + }) + } + return c.setFlags[name] == true +} + +// Determines if the global flag was actually set +func (c *Context) GlobalIsSet(name string) bool { + if c.globalSetFlags == nil { + c.globalSetFlags = make(map[string]bool) + c.globalSet.Visit(func(f *flag.Flag) { + c.globalSetFlags[f.Name] = true + }) + } + return c.globalSetFlags[name] == true +} + +// Returns a slice of flag names used in this context. +func (c *Context) FlagNames() (names []string) { + for _, flag := range c.Command.Flags { + name := strings.Split(flag.getName(), ",")[0] + if name == "help" { + continue + } + names = append(names, name) + } + return +} + +// Returns a slice of global flag names used by the app. +func (c *Context) GlobalFlagNames() (names []string) { + for _, flag := range c.App.Flags { + name := strings.Split(flag.getName(), ",")[0] + if name == "help" || name == "version" { + continue + } + names = append(names, name) + } + return +} + +type Args []string + +// Returns the command line arguments associated with the context. +func (c *Context) Args() Args { + args := Args(c.flagSet.Args()) + return args +} + +// Returns the nth argument, or else a blank string +func (a Args) Get(n int) string { + if len(a) > n { + return a[n] + } + return "" +} + +// Returns the first argument, or else a blank string +func (a Args) First() string { + return a.Get(0) +} + +// Return the rest of the arguments (not the first one) +// or else an empty string slice +func (a Args) Tail() []string { + if len(a) >= 2 { + return []string(a)[1:] + } + return []string{} +} + +// Checks if there are any arguments present +func (a Args) Present() bool { + return len(a) != 0 +} + +// Swaps arguments at the given indexes +func (a Args) Swap(from, to int) error { + if from >= len(a) || to >= len(a) { + return errors.New("index out of range") + } + a[from], a[to] = a[to], a[from] + return nil +} + +func lookupInt(name string, set *flag.FlagSet) int { + f := set.Lookup(name) + if f != nil { + val, err := strconv.Atoi(f.Value.String()) + if err != nil { + return 0 + } + return val + } + + return 0 +} + +func lookupDuration(name string, set *flag.FlagSet) time.Duration { + f := set.Lookup(name) + if f != nil { + val, err := time.ParseDuration(f.Value.String()) + if err == nil { + return val + } + } + + return 0 +} + +func lookupFloat64(name string, set *flag.FlagSet) float64 { + f := set.Lookup(name) + if f != nil { + val, err := strconv.ParseFloat(f.Value.String(), 64) + if err != nil { + return 0 + } + return val + } + + return 0 +} + +func lookupString(name string, set *flag.FlagSet) string { + f := set.Lookup(name) + if f != nil { + return f.Value.String() + } + + return "" +} + +func lookupStringSlice(name string, set *flag.FlagSet) []string { + f := set.Lookup(name) + if f != nil { + return (f.Value.(*StringSlice)).Value() + + } + + return nil +} + +func lookupIntSlice(name string, set *flag.FlagSet) []int { + f := set.Lookup(name) + if f != nil { + return (f.Value.(*IntSlice)).Value() + + } + + return nil +} + +func lookupGeneric(name string, set *flag.FlagSet) interface{} { + f := set.Lookup(name) + if f != nil { + return f.Value + } + return nil +} + +func lookupBool(name string, set *flag.FlagSet) bool { + f := set.Lookup(name) + if f != nil { + val, err := strconv.ParseBool(f.Value.String()) + if err != nil { + return false + } + return val + } + + return false +} + +func lookupBoolT(name string, set *flag.FlagSet) bool { + f := set.Lookup(name) + if f != nil { + val, err := strconv.ParseBool(f.Value.String()) + if err != nil { + return true + } + return val + } + + return false +} + +func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) { + switch ff.Value.(type) { + case *StringSlice: + default: + set.Set(name, ff.Value.String()) + } +} + +func normalizeFlags(flags []Flag, set *flag.FlagSet) error { + visited := make(map[string]bool) + set.Visit(func(f *flag.Flag) { + visited[f.Name] = true + }) + for _, f := range flags { + parts := strings.Split(f.getName(), ",") + if len(parts) == 1 { + continue + } + var ff *flag.Flag + for _, name := range parts { + name = strings.Trim(name, " ") + if visited[name] { + if ff != nil { + return errors.New("Cannot use two forms of the same flag: " + name + " " + ff.Name) + } + ff = set.Lookup(name) + } + } + if ff == nil { + continue + } + for _, name := range parts { + name = strings.Trim(name, " ") + if !visited[name] { + copyFlag(name, ff, set) + } + } + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/context_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/context_test.go new file mode 100644 index 0000000..d4a1877 --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/context_test.go @@ -0,0 +1,111 @@ +package cli_test + +import ( + "flag" + "testing" + "time" + + "github.com/codegangsta/cli" +) + +func TestNewContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Int("myflag", 12, "doc") + globalSet := flag.NewFlagSet("test", 0) + globalSet.Int("myflag", 42, "doc") + command := cli.Command{Name: "mycommand"} + c := cli.NewContext(nil, set, globalSet) + c.Command = command + expect(t, c.Int("myflag"), 12) + expect(t, c.GlobalInt("myflag"), 42) + expect(t, c.Command.Name, "mycommand") +} + +func TestContext_Int(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Int("myflag", 12, "doc") + c := cli.NewContext(nil, set, set) + expect(t, c.Int("myflag"), 12) +} + +func TestContext_Duration(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Duration("myflag", time.Duration(12*time.Second), "doc") + c := cli.NewContext(nil, set, set) + expect(t, c.Duration("myflag"), time.Duration(12*time.Second)) +} + +func TestContext_String(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.String("myflag", "hello world", "doc") + c := cli.NewContext(nil, set, set) + expect(t, c.String("myflag"), "hello world") +} + +func TestContext_Bool(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + c := cli.NewContext(nil, set, set) + expect(t, c.Bool("myflag"), false) +} + +func TestContext_BoolT(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", true, "doc") + c := cli.NewContext(nil, set, set) + expect(t, c.BoolT("myflag"), true) +} + +func TestContext_Args(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + c := cli.NewContext(nil, set, set) + set.Parse([]string{"--myflag", "bat", "baz"}) + expect(t, len(c.Args()), 2) + expect(t, c.Bool("myflag"), true) +} + +func TestContext_IsSet(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + set.String("otherflag", "hello world", "doc") + globalSet := flag.NewFlagSet("test", 0) + globalSet.Bool("myflagGlobal", true, "doc") + c := cli.NewContext(nil, set, globalSet) + set.Parse([]string{"--myflag", "bat", "baz"}) + globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"}) + expect(t, c.IsSet("myflag"), true) + expect(t, c.IsSet("otherflag"), false) + expect(t, c.IsSet("bogusflag"), false) + expect(t, c.IsSet("myflagGlobal"), false) +} + +func TestContext_GlobalIsSet(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + set.String("otherflag", "hello world", "doc") + globalSet := flag.NewFlagSet("test", 0) + globalSet.Bool("myflagGlobal", true, "doc") + globalSet.Bool("myflagGlobalUnset", true, "doc") + c := cli.NewContext(nil, set, globalSet) + set.Parse([]string{"--myflag", "bat", "baz"}) + globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"}) + expect(t, c.GlobalIsSet("myflag"), false) + expect(t, c.GlobalIsSet("otherflag"), false) + expect(t, c.GlobalIsSet("bogusflag"), false) + expect(t, c.GlobalIsSet("myflagGlobal"), true) + expect(t, c.GlobalIsSet("myflagGlobalUnset"), false) + expect(t, c.GlobalIsSet("bogusGlobal"), false) +} + +func TestContext_NumFlags(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + set.String("otherflag", "hello world", "doc") + globalSet := flag.NewFlagSet("test", 0) + globalSet.Bool("myflagGlobal", true, "doc") + c := cli.NewContext(nil, set, globalSet) + set.Parse([]string{"--myflag", "--otherflag=foo"}) + globalSet.Parse([]string{"--myflagGlobal"}) + expect(t, c.NumFlags(), 2) +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/flag.go b/Godeps/_workspace/src/github.com/codegangsta/cli/flag.go new file mode 100644 index 0000000..2511586 --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/flag.go @@ -0,0 +1,454 @@ +package cli + +import ( + "flag" + "fmt" + "os" + "strconv" + "strings" + "time" +) + +// This flag enables bash-completion for all commands and subcommands +var BashCompletionFlag = BoolFlag{ + Name: "generate-bash-completion", +} + +// This flag prints the version for the application +var VersionFlag = BoolFlag{ + Name: "version, v", + Usage: "print the version", +} + +// This flag prints the help for all commands and subcommands +// Set to the zero value (BoolFlag{}) to disable flag -- keeps subcommand +// unless HideHelp is set to true) +var HelpFlag = BoolFlag{ + Name: "help, h", + Usage: "show help", +} + +// Flag is a common interface related to parsing flags in cli. +// For more advanced flag parsing techniques, it is recomended that +// this interface be implemented. +type Flag interface { + fmt.Stringer + // Apply Flag settings to the given flag set + Apply(*flag.FlagSet) + getName() string +} + +func flagSet(name string, flags []Flag) *flag.FlagSet { + set := flag.NewFlagSet(name, flag.ContinueOnError) + + for _, f := range flags { + f.Apply(set) + } + return set +} + +func eachName(longName string, fn func(string)) { + parts := strings.Split(longName, ",") + for _, name := range parts { + name = strings.Trim(name, " ") + fn(name) + } +} + +// Generic is a generic parseable type identified by a specific flag +type Generic interface { + Set(value string) error + String() string +} + +// GenericFlag is the flag type for types implementing Generic +type GenericFlag struct { + Name string + Value Generic + Usage string + EnvVar string +} + +// String returns the string representation of the generic flag to display the +// help text to the user (uses the String() method of the generic flag to show +// the value) +func (f GenericFlag) String() string { + return withEnvHint(f.EnvVar, fmt.Sprintf("%s%s \"%v\"\t%v", prefixFor(f.Name), f.Name, f.Value, f.Usage)) +} + +// Apply takes the flagset and calls Set on the generic flag with the value +// provided by the user for parsing by the flag +func (f GenericFlag) Apply(set *flag.FlagSet) { + val := f.Value + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + val.Set(envVal) + break + } + } + } + + eachName(f.Name, func(name string) { + set.Var(f.Value, name, f.Usage) + }) +} + +func (f GenericFlag) getName() string { + return f.Name +} + +type StringSlice []string + +func (f *StringSlice) Set(value string) error { + *f = append(*f, value) + return nil +} + +func (f *StringSlice) String() string { + return fmt.Sprintf("%s", *f) +} + +func (f *StringSlice) Value() []string { + return *f +} + +type StringSliceFlag struct { + Name string + Value *StringSlice + Usage string + EnvVar string +} + +func (f StringSliceFlag) String() string { + firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ") + pref := prefixFor(firstName) + return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage)) +} + +func (f StringSliceFlag) Apply(set *flag.FlagSet) { + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + newVal := &StringSlice{} + for _, s := range strings.Split(envVal, ",") { + s = strings.TrimSpace(s) + newVal.Set(s) + } + f.Value = newVal + break + } + } + } + + eachName(f.Name, func(name string) { + set.Var(f.Value, name, f.Usage) + }) +} + +func (f StringSliceFlag) getName() string { + return f.Name +} + +type IntSlice []int + +func (f *IntSlice) Set(value string) error { + + tmp, err := strconv.Atoi(value) + if err != nil { + return err + } else { + *f = append(*f, tmp) + } + return nil +} + +func (f *IntSlice) String() string { + return fmt.Sprintf("%d", *f) +} + +func (f *IntSlice) Value() []int { + return *f +} + +type IntSliceFlag struct { + Name string + Value *IntSlice + Usage string + EnvVar string +} + +func (f IntSliceFlag) String() string { + firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ") + pref := prefixFor(firstName) + return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage)) +} + +func (f IntSliceFlag) Apply(set *flag.FlagSet) { + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + newVal := &IntSlice{} + for _, s := range strings.Split(envVal, ",") { + s = strings.TrimSpace(s) + err := newVal.Set(s) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + } + } + f.Value = newVal + break + } + } + } + + eachName(f.Name, func(name string) { + set.Var(f.Value, name, f.Usage) + }) +} + +func (f IntSliceFlag) getName() string { + return f.Name +} + +type BoolFlag struct { + Name string + Usage string + EnvVar string +} + +func (f BoolFlag) String() string { + return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage)) +} + +func (f BoolFlag) Apply(set *flag.FlagSet) { + val := false + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + envValBool, err := strconv.ParseBool(envVal) + if err == nil { + val = envValBool + } + break + } + } + } + + eachName(f.Name, func(name string) { + set.Bool(name, val, f.Usage) + }) +} + +func (f BoolFlag) getName() string { + return f.Name +} + +type BoolTFlag struct { + Name string + Usage string + EnvVar string +} + +func (f BoolTFlag) String() string { + return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage)) +} + +func (f BoolTFlag) Apply(set *flag.FlagSet) { + val := true + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + envValBool, err := strconv.ParseBool(envVal) + if err == nil { + val = envValBool + break + } + } + } + } + + eachName(f.Name, func(name string) { + set.Bool(name, val, f.Usage) + }) +} + +func (f BoolTFlag) getName() string { + return f.Name +} + +type StringFlag struct { + Name string + Value string + Usage string + EnvVar string +} + +func (f StringFlag) String() string { + var fmtString string + fmtString = "%s %v\t%v" + + if len(f.Value) > 0 { + fmtString = "%s \"%v\"\t%v" + } else { + fmtString = "%s %v\t%v" + } + + return withEnvHint(f.EnvVar, fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage)) +} + +func (f StringFlag) Apply(set *flag.FlagSet) { + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + f.Value = envVal + break + } + } + } + + eachName(f.Name, func(name string) { + set.String(name, f.Value, f.Usage) + }) +} + +func (f StringFlag) getName() string { + return f.Name +} + +type IntFlag struct { + Name string + Value int + Usage string + EnvVar string +} + +func (f IntFlag) String() string { + return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)) +} + +func (f IntFlag) Apply(set *flag.FlagSet) { + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + envValInt, err := strconv.ParseInt(envVal, 0, 64) + if err == nil { + f.Value = int(envValInt) + break + } + } + } + } + + eachName(f.Name, func(name string) { + set.Int(name, f.Value, f.Usage) + }) +} + +func (f IntFlag) getName() string { + return f.Name +} + +type DurationFlag struct { + Name string + Value time.Duration + Usage string + EnvVar string +} + +func (f DurationFlag) String() string { + return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)) +} + +func (f DurationFlag) Apply(set *flag.FlagSet) { + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + envValDuration, err := time.ParseDuration(envVal) + if err == nil { + f.Value = envValDuration + break + } + } + } + } + + eachName(f.Name, func(name string) { + set.Duration(name, f.Value, f.Usage) + }) +} + +func (f DurationFlag) getName() string { + return f.Name +} + +type Float64Flag struct { + Name string + Value float64 + Usage string + EnvVar string +} + +func (f Float64Flag) String() string { + return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)) +} + +func (f Float64Flag) Apply(set *flag.FlagSet) { + if f.EnvVar != "" { + for _, envVar := range strings.Split(f.EnvVar, ",") { + envVar = strings.TrimSpace(envVar) + if envVal := os.Getenv(envVar); envVal != "" { + envValFloat, err := strconv.ParseFloat(envVal, 10) + if err == nil { + f.Value = float64(envValFloat) + } + } + } + } + + eachName(f.Name, func(name string) { + set.Float64(name, f.Value, f.Usage) + }) +} + +func (f Float64Flag) getName() string { + return f.Name +} + +func prefixFor(name string) (prefix string) { + if len(name) == 1 { + prefix = "-" + } else { + prefix = "--" + } + + return +} + +func prefixedNames(fullName string) (prefixed string) { + parts := strings.Split(fullName, ",") + for i, name := range parts { + name = strings.Trim(name, " ") + prefixed += prefixFor(name) + name + if i < len(parts)-1 { + prefixed += ", " + } + } + return +} + +func withEnvHint(envVar, str string) string { + envText := "" + if envVar != "" { + envText = fmt.Sprintf(" [$%s]", strings.Join(strings.Split(envVar, ","), ", $")) + } + return str + envText +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/flag_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/flag_test.go new file mode 100644 index 0000000..f0f096a --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/flag_test.go @@ -0,0 +1,742 @@ +package cli_test + +import ( + "fmt" + "os" + "reflect" + "strings" + "testing" + + "github.com/codegangsta/cli" +) + +var boolFlagTests = []struct { + name string + expected string +}{ + {"help", "--help\t"}, + {"h", "-h\t"}, +} + +func TestBoolFlagHelpOutput(t *testing.T) { + + for _, test := range boolFlagTests { + flag := cli.BoolFlag{Name: test.name} + output := flag.String() + + if output != test.expected { + t.Errorf("%s does not match %s", output, test.expected) + } + } +} + +var stringFlagTests = []struct { + name string + value string + expected string +}{ + {"help", "", "--help \t"}, + {"h", "", "-h \t"}, + {"h", "", "-h \t"}, + {"test", "Something", "--test \"Something\"\t"}, +} + +func TestStringFlagHelpOutput(t *testing.T) { + + for _, test := range stringFlagTests { + flag := cli.StringFlag{Name: test.name, Value: test.value} + output := flag.String() + + if output != test.expected { + t.Errorf("%s does not match %s", output, test.expected) + } + } +} + +func TestStringFlagWithEnvVarHelpOutput(t *testing.T) { + os.Clearenv() + os.Setenv("APP_FOO", "derp") + for _, test := range stringFlagTests { + flag := cli.StringFlag{Name: test.name, Value: test.value, EnvVar: "APP_FOO"} + output := flag.String() + + if !strings.HasSuffix(output, " [$APP_FOO]") { + t.Errorf("%s does not end with [$APP_FOO]", output) + } + } +} + +var stringSliceFlagTests = []struct { + name string + value *cli.StringSlice + expected string +}{ + {"help", func() *cli.StringSlice { + s := &cli.StringSlice{} + s.Set("") + return s + }(), "--help [--help option --help option]\t"}, + {"h", func() *cli.StringSlice { + s := &cli.StringSlice{} + s.Set("") + return s + }(), "-h [-h option -h option]\t"}, + {"h", func() *cli.StringSlice { + s := &cli.StringSlice{} + s.Set("") + return s + }(), "-h [-h option -h option]\t"}, + {"test", func() *cli.StringSlice { + s := &cli.StringSlice{} + s.Set("Something") + return s + }(), "--test [--test option --test option]\t"}, +} + +func TestStringSliceFlagHelpOutput(t *testing.T) { + + for _, test := range stringSliceFlagTests { + flag := cli.StringSliceFlag{Name: test.name, Value: test.value} + output := flag.String() + + if output != test.expected { + t.Errorf("%q does not match %q", output, test.expected) + } + } +} + +func TestStringSliceFlagWithEnvVarHelpOutput(t *testing.T) { + os.Clearenv() + os.Setenv("APP_QWWX", "11,4") + for _, test := range stringSliceFlagTests { + flag := cli.StringSliceFlag{Name: test.name, Value: test.value, EnvVar: "APP_QWWX"} + output := flag.String() + + if !strings.HasSuffix(output, " [$APP_QWWX]") { + t.Errorf("%q does not end with [$APP_QWWX]", output) + } + } +} + +var intFlagTests = []struct { + name string + expected string +}{ + {"help", "--help \"0\"\t"}, + {"h", "-h \"0\"\t"}, +} + +func TestIntFlagHelpOutput(t *testing.T) { + + for _, test := range intFlagTests { + flag := cli.IntFlag{Name: test.name} + output := flag.String() + + if output != test.expected { + t.Errorf("%s does not match %s", output, test.expected) + } + } +} + +func TestIntFlagWithEnvVarHelpOutput(t *testing.T) { + os.Clearenv() + os.Setenv("APP_BAR", "2") + for _, test := range intFlagTests { + flag := cli.IntFlag{Name: test.name, EnvVar: "APP_BAR"} + output := flag.String() + + if !strings.HasSuffix(output, " [$APP_BAR]") { + t.Errorf("%s does not end with [$APP_BAR]", output) + } + } +} + +var durationFlagTests = []struct { + name string + expected string +}{ + {"help", "--help \"0\"\t"}, + {"h", "-h \"0\"\t"}, +} + +func TestDurationFlagHelpOutput(t *testing.T) { + + for _, test := range durationFlagTests { + flag := cli.DurationFlag{Name: test.name} + output := flag.String() + + if output != test.expected { + t.Errorf("%s does not match %s", output, test.expected) + } + } +} + +func TestDurationFlagWithEnvVarHelpOutput(t *testing.T) { + os.Clearenv() + os.Setenv("APP_BAR", "2h3m6s") + for _, test := range durationFlagTests { + flag := cli.DurationFlag{Name: test.name, EnvVar: "APP_BAR"} + output := flag.String() + + if !strings.HasSuffix(output, " [$APP_BAR]") { + t.Errorf("%s does not end with [$APP_BAR]", output) + } + } +} + +var intSliceFlagTests = []struct { + name string + value *cli.IntSlice + expected string +}{ + {"help", &cli.IntSlice{}, "--help [--help option --help option]\t"}, + {"h", &cli.IntSlice{}, "-h [-h option -h option]\t"}, + {"h", &cli.IntSlice{}, "-h [-h option -h option]\t"}, + {"test", func() *cli.IntSlice { + i := &cli.IntSlice{} + i.Set("9") + return i + }(), "--test [--test option --test option]\t"}, +} + +func TestIntSliceFlagHelpOutput(t *testing.T) { + + for _, test := range intSliceFlagTests { + flag := cli.IntSliceFlag{Name: test.name, Value: test.value} + output := flag.String() + + if output != test.expected { + t.Errorf("%q does not match %q", output, test.expected) + } + } +} + +func TestIntSliceFlagWithEnvVarHelpOutput(t *testing.T) { + os.Clearenv() + os.Setenv("APP_SMURF", "42,3") + for _, test := range intSliceFlagTests { + flag := cli.IntSliceFlag{Name: test.name, Value: test.value, EnvVar: "APP_SMURF"} + output := flag.String() + + if !strings.HasSuffix(output, " [$APP_SMURF]") { + t.Errorf("%q does not end with [$APP_SMURF]", output) + } + } +} + +var float64FlagTests = []struct { + name string + expected string +}{ + {"help", "--help \"0\"\t"}, + {"h", "-h \"0\"\t"}, +} + +func TestFloat64FlagHelpOutput(t *testing.T) { + + for _, test := range float64FlagTests { + flag := cli.Float64Flag{Name: test.name} + output := flag.String() + + if output != test.expected { + t.Errorf("%s does not match %s", output, test.expected) + } + } +} + +func TestFloat64FlagWithEnvVarHelpOutput(t *testing.T) { + os.Clearenv() + os.Setenv("APP_BAZ", "99.4") + for _, test := range float64FlagTests { + flag := cli.Float64Flag{Name: test.name, EnvVar: "APP_BAZ"} + output := flag.String() + + if !strings.HasSuffix(output, " [$APP_BAZ]") { + t.Errorf("%s does not end with [$APP_BAZ]", output) + } + } +} + +var genericFlagTests = []struct { + name string + value cli.Generic + expected string +}{ + {"test", &Parser{"abc", "def"}, "--test \"abc,def\"\ttest flag"}, + {"t", &Parser{"abc", "def"}, "-t \"abc,def\"\ttest flag"}, +} + +func TestGenericFlagHelpOutput(t *testing.T) { + + for _, test := range genericFlagTests { + flag := cli.GenericFlag{Name: test.name, Value: test.value, Usage: "test flag"} + output := flag.String() + + if output != test.expected { + t.Errorf("%q does not match %q", output, test.expected) + } + } +} + +func TestGenericFlagWithEnvVarHelpOutput(t *testing.T) { + os.Clearenv() + os.Setenv("APP_ZAP", "3") + for _, test := range genericFlagTests { + flag := cli.GenericFlag{Name: test.name, EnvVar: "APP_ZAP"} + output := flag.String() + + if !strings.HasSuffix(output, " [$APP_ZAP]") { + t.Errorf("%s does not end with [$APP_ZAP]", output) + } + } +} + +func TestParseMultiString(t *testing.T) { + (&cli.App{ + Flags: []cli.Flag{ + cli.StringFlag{Name: "serve, s"}, + }, + Action: func(ctx *cli.Context) { + if ctx.String("serve") != "10" { + t.Errorf("main name not set") + } + if ctx.String("s") != "10" { + t.Errorf("short name not set") + } + }, + }).Run([]string{"run", "-s", "10"}) +} + +func TestParseMultiStringFromEnv(t *testing.T) { + os.Clearenv() + os.Setenv("APP_COUNT", "20") + (&cli.App{ + Flags: []cli.Flag{ + cli.StringFlag{Name: "count, c", EnvVar: "APP_COUNT"}, + }, + Action: func(ctx *cli.Context) { + if ctx.String("count") != "20" { + t.Errorf("main name not set") + } + if ctx.String("c") != "20" { + t.Errorf("short name not set") + } + }, + }).Run([]string{"run"}) +} + +func TestParseMultiStringFromEnvCascade(t *testing.T) { + os.Clearenv() + os.Setenv("APP_COUNT", "20") + (&cli.App{ + Flags: []cli.Flag{ + cli.StringFlag{Name: "count, c", EnvVar: "COMPAT_COUNT,APP_COUNT"}, + }, + Action: func(ctx *cli.Context) { + if ctx.String("count") != "20" { + t.Errorf("main name not set") + } + if ctx.String("c") != "20" { + t.Errorf("short name not set") + } + }, + }).Run([]string{"run"}) +} + +func TestParseMultiStringSlice(t *testing.T) { + (&cli.App{ + Flags: []cli.Flag{ + cli.StringSliceFlag{Name: "serve, s", Value: &cli.StringSlice{}}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) { + t.Errorf("main name not set") + } + if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"10", "20"}) { + t.Errorf("short name not set") + } + }, + }).Run([]string{"run", "-s", "10", "-s", "20"}) +} + +func TestParseMultiStringSliceFromEnv(t *testing.T) { + os.Clearenv() + os.Setenv("APP_INTERVALS", "20,30,40") + + (&cli.App{ + Flags: []cli.Flag{ + cli.StringSliceFlag{Name: "intervals, i", Value: &cli.StringSlice{}, EnvVar: "APP_INTERVALS"}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) { + t.Errorf("main name not set from env") + } + if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) { + t.Errorf("short name not set from env") + } + }, + }).Run([]string{"run"}) +} + +func TestParseMultiStringSliceFromEnvCascade(t *testing.T) { + os.Clearenv() + os.Setenv("APP_INTERVALS", "20,30,40") + + (&cli.App{ + Flags: []cli.Flag{ + cli.StringSliceFlag{Name: "intervals, i", Value: &cli.StringSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) { + t.Errorf("main name not set from env") + } + if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) { + t.Errorf("short name not set from env") + } + }, + }).Run([]string{"run"}) +} + +func TestParseMultiInt(t *testing.T) { + a := cli.App{ + Flags: []cli.Flag{ + cli.IntFlag{Name: "serve, s"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Int("serve") != 10 { + t.Errorf("main name not set") + } + if ctx.Int("s") != 10 { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run", "-s", "10"}) +} + +func TestParseMultiIntFromEnv(t *testing.T) { + os.Clearenv() + os.Setenv("APP_TIMEOUT_SECONDS", "10") + a := cli.App{ + Flags: []cli.Flag{ + cli.IntFlag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Int("timeout") != 10 { + t.Errorf("main name not set") + } + if ctx.Int("t") != 10 { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run"}) +} + +func TestParseMultiIntFromEnvCascade(t *testing.T) { + os.Clearenv() + os.Setenv("APP_TIMEOUT_SECONDS", "10") + a := cli.App{ + Flags: []cli.Flag{ + cli.IntFlag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Int("timeout") != 10 { + t.Errorf("main name not set") + } + if ctx.Int("t") != 10 { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run"}) +} + +func TestParseMultiIntSlice(t *testing.T) { + (&cli.App{ + Flags: []cli.Flag{ + cli.IntSliceFlag{Name: "serve, s", Value: &cli.IntSlice{}}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.IntSlice("serve"), []int{10, 20}) { + t.Errorf("main name not set") + } + if !reflect.DeepEqual(ctx.IntSlice("s"), []int{10, 20}) { + t.Errorf("short name not set") + } + }, + }).Run([]string{"run", "-s", "10", "-s", "20"}) +} + +func TestParseMultiIntSliceFromEnv(t *testing.T) { + os.Clearenv() + os.Setenv("APP_INTERVALS", "20,30,40") + + (&cli.App{ + Flags: []cli.Flag{ + cli.IntSliceFlag{Name: "intervals, i", Value: &cli.IntSlice{}, EnvVar: "APP_INTERVALS"}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) { + t.Errorf("main name not set from env") + } + if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) { + t.Errorf("short name not set from env") + } + }, + }).Run([]string{"run"}) +} + +func TestParseMultiIntSliceFromEnvCascade(t *testing.T) { + os.Clearenv() + os.Setenv("APP_INTERVALS", "20,30,40") + + (&cli.App{ + Flags: []cli.Flag{ + cli.IntSliceFlag{Name: "intervals, i", Value: &cli.IntSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) { + t.Errorf("main name not set from env") + } + if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) { + t.Errorf("short name not set from env") + } + }, + }).Run([]string{"run"}) +} + +func TestParseMultiFloat64(t *testing.T) { + a := cli.App{ + Flags: []cli.Flag{ + cli.Float64Flag{Name: "serve, s"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Float64("serve") != 10.2 { + t.Errorf("main name not set") + } + if ctx.Float64("s") != 10.2 { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run", "-s", "10.2"}) +} + +func TestParseMultiFloat64FromEnv(t *testing.T) { + os.Clearenv() + os.Setenv("APP_TIMEOUT_SECONDS", "15.5") + a := cli.App{ + Flags: []cli.Flag{ + cli.Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Float64("timeout") != 15.5 { + t.Errorf("main name not set") + } + if ctx.Float64("t") != 15.5 { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run"}) +} + +func TestParseMultiFloat64FromEnvCascade(t *testing.T) { + os.Clearenv() + os.Setenv("APP_TIMEOUT_SECONDS", "15.5") + a := cli.App{ + Flags: []cli.Flag{ + cli.Float64Flag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Float64("timeout") != 15.5 { + t.Errorf("main name not set") + } + if ctx.Float64("t") != 15.5 { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run"}) +} + +func TestParseMultiBool(t *testing.T) { + a := cli.App{ + Flags: []cli.Flag{ + cli.BoolFlag{Name: "serve, s"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Bool("serve") != true { + t.Errorf("main name not set") + } + if ctx.Bool("s") != true { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run", "--serve"}) +} + +func TestParseMultiBoolFromEnv(t *testing.T) { + os.Clearenv() + os.Setenv("APP_DEBUG", "1") + a := cli.App{ + Flags: []cli.Flag{ + cli.BoolFlag{Name: "debug, d", EnvVar: "APP_DEBUG"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Bool("debug") != true { + t.Errorf("main name not set from env") + } + if ctx.Bool("d") != true { + t.Errorf("short name not set from env") + } + }, + } + a.Run([]string{"run"}) +} + +func TestParseMultiBoolFromEnvCascade(t *testing.T) { + os.Clearenv() + os.Setenv("APP_DEBUG", "1") + a := cli.App{ + Flags: []cli.Flag{ + cli.BoolFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"}, + }, + Action: func(ctx *cli.Context) { + if ctx.Bool("debug") != true { + t.Errorf("main name not set from env") + } + if ctx.Bool("d") != true { + t.Errorf("short name not set from env") + } + }, + } + a.Run([]string{"run"}) +} + +func TestParseMultiBoolT(t *testing.T) { + a := cli.App{ + Flags: []cli.Flag{ + cli.BoolTFlag{Name: "serve, s"}, + }, + Action: func(ctx *cli.Context) { + if ctx.BoolT("serve") != true { + t.Errorf("main name not set") + } + if ctx.BoolT("s") != true { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run", "--serve"}) +} + +func TestParseMultiBoolTFromEnv(t *testing.T) { + os.Clearenv() + os.Setenv("APP_DEBUG", "0") + a := cli.App{ + Flags: []cli.Flag{ + cli.BoolTFlag{Name: "debug, d", EnvVar: "APP_DEBUG"}, + }, + Action: func(ctx *cli.Context) { + if ctx.BoolT("debug") != false { + t.Errorf("main name not set from env") + } + if ctx.BoolT("d") != false { + t.Errorf("short name not set from env") + } + }, + } + a.Run([]string{"run"}) +} + +func TestParseMultiBoolTFromEnvCascade(t *testing.T) { + os.Clearenv() + os.Setenv("APP_DEBUG", "0") + a := cli.App{ + Flags: []cli.Flag{ + cli.BoolTFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"}, + }, + Action: func(ctx *cli.Context) { + if ctx.BoolT("debug") != false { + t.Errorf("main name not set from env") + } + if ctx.BoolT("d") != false { + t.Errorf("short name not set from env") + } + }, + } + a.Run([]string{"run"}) +} + +type Parser [2]string + +func (p *Parser) Set(value string) error { + parts := strings.Split(value, ",") + if len(parts) != 2 { + return fmt.Errorf("invalid format") + } + + (*p)[0] = parts[0] + (*p)[1] = parts[1] + + return nil +} + +func (p *Parser) String() string { + return fmt.Sprintf("%s,%s", p[0], p[1]) +} + +func TestParseGeneric(t *testing.T) { + a := cli.App{ + Flags: []cli.Flag{ + cli.GenericFlag{Name: "serve, s", Value: &Parser{}}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"10", "20"}) { + t.Errorf("main name not set") + } + if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"10", "20"}) { + t.Errorf("short name not set") + } + }, + } + a.Run([]string{"run", "-s", "10,20"}) +} + +func TestParseGenericFromEnv(t *testing.T) { + os.Clearenv() + os.Setenv("APP_SERVE", "20,30") + a := cli.App{ + Flags: []cli.Flag{ + cli.GenericFlag{Name: "serve, s", Value: &Parser{}, EnvVar: "APP_SERVE"}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"20", "30"}) { + t.Errorf("main name not set from env") + } + if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"20", "30"}) { + t.Errorf("short name not set from env") + } + }, + } + a.Run([]string{"run"}) +} + +func TestParseGenericFromEnvCascade(t *testing.T) { + os.Clearenv() + os.Setenv("APP_FOO", "99,2000") + a := cli.App{ + Flags: []cli.Flag{ + cli.GenericFlag{Name: "foos", Value: &Parser{}, EnvVar: "COMPAT_FOO,APP_FOO"}, + }, + Action: func(ctx *cli.Context) { + if !reflect.DeepEqual(ctx.Generic("foos"), &Parser{"99", "2000"}) { + t.Errorf("value not set from env") + } + }, + } + a.Run([]string{"run"}) +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/help.go b/Godeps/_workspace/src/github.com/codegangsta/cli/help.go new file mode 100644 index 0000000..1117945 --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/help.go @@ -0,0 +1,235 @@ +package cli + +import ( + "fmt" + "io" + "strings" + "text/tabwriter" + "text/template" +) + +// The text template for the Default help topic. +// cli.go uses text/template to render templates. You can +// render custom help text by setting this variable. +var AppHelpTemplate = `NAME: + {{.Name}} - {{.Usage}} + +USAGE: + {{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...] + +VERSION: + {{.Version}}{{if len .Authors}} + +AUTHOR(S): + {{range .Authors}}{{ . }}{{end}}{{end}} + +COMMANDS: + {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} + {{end}}{{if .Flags}} +GLOBAL OPTIONS: + {{range .Flags}}{{.}} + {{end}}{{end}} +` + +// The text template for the command help topic. +// cli.go uses text/template to render templates. You can +// render custom help text by setting this variable. +var CommandHelpTemplate = `NAME: + {{.Name}} - {{.Usage}} + +USAGE: + command {{.Name}}{{if .Flags}} [command options]{{end}} [arguments...]{{if .Description}} + +DESCRIPTION: + {{.Description}}{{end}}{{if .Flags}} + +OPTIONS: + {{range .Flags}}{{.}} + {{end}}{{ end }} +` + +// The text template for the subcommand help topic. +// cli.go uses text/template to render templates. You can +// render custom help text by setting this variable. +var SubcommandHelpTemplate = `NAME: + {{.Name}} - {{.Usage}} + +USAGE: + {{.Name}} command{{if .Flags}} [command options]{{end}} [arguments...] + +COMMANDS: + {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} + {{end}}{{if .Flags}} +OPTIONS: + {{range .Flags}}{{.}} + {{end}}{{end}} +` + +var helpCommand = Command{ + Name: "help", + Aliases: []string{"h"}, + Usage: "Shows a list of commands or help for one command", + Action: func(c *Context) { + args := c.Args() + if args.Present() { + ShowCommandHelp(c, args.First()) + } else { + ShowAppHelp(c) + } + }, +} + +var helpSubcommand = Command{ + Name: "help", + Aliases: []string{"h"}, + Usage: "Shows a list of commands or help for one command", + Action: func(c *Context) { + args := c.Args() + if args.Present() { + ShowCommandHelp(c, args.First()) + } else { + ShowSubcommandHelp(c) + } + }, +} + +// Prints help for the App or Command +type helpPrinter func(w io.Writer, templ string, data interface{}) + +var HelpPrinter helpPrinter = printHelp + +// Prints version for the App +var VersionPrinter = printVersion + +func ShowAppHelp(c *Context) { + HelpPrinter(c.App.Writer, AppHelpTemplate, c.App) +} + +// Prints the list of subcommands as the default app completion method +func DefaultAppComplete(c *Context) { + for _, command := range c.App.Commands { + for _, name := range command.Names() { + fmt.Fprintln(c.App.Writer, name) + } + } +} + +// Prints help for the given command +func ShowCommandHelp(ctx *Context, command string) { + // show the subcommand help for a command with subcommands + if command == "" { + HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App) + return + } + + for _, c := range ctx.App.Commands { + if c.HasName(command) { + HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c) + return + } + } + + if ctx.App.CommandNotFound != nil { + ctx.App.CommandNotFound(ctx, command) + } else { + fmt.Fprintf(ctx.App.Writer, "No help topic for '%v'\n", command) + } +} + +// Prints help for the given subcommand +func ShowSubcommandHelp(c *Context) { + ShowCommandHelp(c, c.Command.Name) +} + +// Prints the version number of the App +func ShowVersion(c *Context) { + VersionPrinter(c) +} + +func printVersion(c *Context) { + fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version) +} + +// Prints the lists of commands within a given context +func ShowCompletions(c *Context) { + a := c.App + if a != nil && a.BashComplete != nil { + a.BashComplete(c) + } +} + +// Prints the custom completions for a given command +func ShowCommandCompletions(ctx *Context, command string) { + c := ctx.App.Command(command) + if c != nil && c.BashComplete != nil { + c.BashComplete(ctx) + } +} + +func printHelp(out io.Writer, templ string, data interface{}) { + funcMap := template.FuncMap{ + "join": strings.Join, + } + + w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0) + t := template.Must(template.New("help").Funcs(funcMap).Parse(templ)) + err := t.Execute(w, data) + if err != nil { + panic(err) + } + w.Flush() +} + +func checkVersion(c *Context) bool { + if c.GlobalBool("version") { + ShowVersion(c) + return true + } + + return false +} + +func checkHelp(c *Context) bool { + if c.GlobalBool("h") || c.GlobalBool("help") { + ShowAppHelp(c) + return true + } + + return false +} + +func checkCommandHelp(c *Context, name string) bool { + if c.Bool("h") || c.Bool("help") { + ShowCommandHelp(c, name) + return true + } + + return false +} + +func checkSubcommandHelp(c *Context) bool { + if c.GlobalBool("h") || c.GlobalBool("help") { + ShowSubcommandHelp(c) + return true + } + + return false +} + +func checkCompletions(c *Context) bool { + if (c.GlobalBool(BashCompletionFlag.Name) || c.Bool(BashCompletionFlag.Name)) && c.App.EnableBashCompletion { + ShowCompletions(c) + return true + } + + return false +} + +func checkCommandCompletions(c *Context, name string) bool { + if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion { + ShowCommandCompletions(c, name) + return true + } + + return false +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/help_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/help_test.go new file mode 100644 index 0000000..b3c1fda --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/help_test.go @@ -0,0 +1,22 @@ +package cli_test + +import ( + "bytes" + "testing" + + "github.com/codegangsta/cli" +) + +func Test_ShowAppHelp_NoAuthor(t *testing.T) { + output := new(bytes.Buffer) + app := cli.NewApp() + app.Writer = output + + c := cli.NewContext(app, nil, nil) + + cli.ShowAppHelp(c) + + if bytes.Index(output.Bytes(), []byte("AUTHOR(S):")) != -1 { + t.Errorf("expected\n%snot to include %s", output.String(), "AUTHOR(S):") + } +} diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/helpers_test.go b/Godeps/_workspace/src/github.com/codegangsta/cli/helpers_test.go new file mode 100644 index 0000000..cdc4feb --- /dev/null +++ b/Godeps/_workspace/src/github.com/codegangsta/cli/helpers_test.go @@ -0,0 +1,19 @@ +package cli_test + +import ( + "reflect" + "testing" +) + +/* Test Helpers */ +func expect(t *testing.T, a interface{}, b interface{}) { + if a != b { + t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) + } +} + +func refute(t *testing.T, a interface{}, b interface{}) { + if a == b { + t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) + } +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/.gitignore b/Godeps/_workspace/src/github.com/macaron-contrib/session/.gitignore new file mode 100644 index 0000000..9297dbc --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/.gitignore @@ -0,0 +1,2 @@ +ledis/tmp.db +nodb/tmp.db \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/LICENSE b/Godeps/_workspace/src/github.com/macaron-contrib/session/LICENSE new file mode 100644 index 0000000..8405e89 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/LICENSE @@ -0,0 +1,191 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets "[]" replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same "printed page" as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/README.md b/Godeps/_workspace/src/github.com/macaron-contrib/session/README.md new file mode 100644 index 0000000..01de811 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/README.md @@ -0,0 +1,21 @@ +session [![Build Status](https://drone.io/github.com/macaron-contrib/session/status.png)](https://drone.io/github.com/macaron-contrib/session/latest) [![](http://gocover.io/_badge/github.com/macaron-contrib/session)](http://gocover.io/github.com/macaron-contrib/session) +======= + +Middleware session provides session management for [Macaron](https://github.com/Unknwon/macaron). It can use many session providers, including memory, file, Redis, Memcache, PostgreSQL, MySQL, Couchbase, Ledis and Nodb. + +### Installation + + go get github.com/macaron-contrib/session + +## Getting Help + +- [API Reference](https://gowalker.org/github.com/macaron-contrib/session) +- [Documentation](http://macaron.gogs.io/docs/middlewares/session) + +## Credits + +This package is forked from [beego/session](https://github.com/astaxie/beego/tree/master/session) with reconstruction(over 80%). + +## License + +This project is under Apache v2 License. See the [LICENSE](LICENSE) file for the full license text. diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/couchbase/couchbase.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/couchbase/couchbase.go new file mode 100644 index 0000000..93953c6 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/couchbase/couchbase.go @@ -0,0 +1,223 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "strings" + "sync" + + "github.com/couchbaselabs/go-couchbase" + + "github.com/macaron-contrib/session" +) + +// CouchbaseSessionStore represents a couchbase session store implementation. +type CouchbaseSessionStore struct { + b *couchbase.Bucket + sid string + lock sync.RWMutex + data map[interface{}]interface{} + maxlifetime int64 +} + +// Set sets value to given key in session. +func (s *CouchbaseSessionStore) Set(key, val interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = val + return nil +} + +// Get gets value by given key in session. +func (s *CouchbaseSessionStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete delete a key from session. +func (s *CouchbaseSessionStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *CouchbaseSessionStore) ID() string { + return s.sid +} + +// Release releases resource and save data to provider. +func (s *CouchbaseSessionStore) Release() error { + defer s.b.Close() + + data, err := session.EncodeGob(s.data) + if err != nil { + return err + } + + return s.b.Set(s.sid, int(s.maxlifetime), data) +} + +// Flush deletes all session data. +func (s *CouchbaseSessionStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// CouchbaseProvider represents a couchbase session provider implementation. +type CouchbaseProvider struct { + maxlifetime int64 + connStr string + pool string + bucket string + b *couchbase.Bucket +} + +func (cp *CouchbaseProvider) getBucket() *couchbase.Bucket { + c, err := couchbase.Connect(cp.connStr) + if err != nil { + return nil + } + + pool, err := c.GetPool(cp.pool) + if err != nil { + return nil + } + + bucket, err := pool.GetBucket(cp.bucket) + if err != nil { + return nil + } + + return bucket +} + +// Init initializes memory session provider. +// connStr is couchbase server REST/JSON URL +// e.g. http://host:port/, Pool, Bucket +func (p *CouchbaseProvider) Init(maxlifetime int64, connStr string) error { + p.maxlifetime = maxlifetime + configs := strings.Split(connStr, ",") + if len(configs) > 0 { + p.connStr = configs[0] + } + if len(configs) > 1 { + p.pool = configs[1] + } + if len(configs) > 2 { + p.bucket = configs[2] + } + + return nil +} + +// Read returns raw session store by session ID. +func (p *CouchbaseProvider) Read(sid string) (session.RawStore, error) { + p.b = p.getBucket() + + var doc []byte + + err := p.b.Get(sid, &doc) + var kv map[interface{}]interface{} + if doc == nil { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob(doc) + if err != nil { + return nil, err + } + } + + cs := &CouchbaseSessionStore{b: p.b, sid: sid, data: kv, maxlifetime: p.maxlifetime} + return cs, nil +} + +// Exist returns true if session with given ID exists. +func (p *CouchbaseProvider) Exist(sid string) bool { + p.b = p.getBucket() + defer p.b.Close() + + var doc []byte + + if err := p.b.Get(sid, &doc); err != nil || doc == nil { + return false + } else { + return true + } +} + +// Destory deletes a session by session ID. +func (p *CouchbaseProvider) Destory(sid string) error { + p.b = p.getBucket() + defer p.b.Close() + + p.b.Delete(sid) + return nil +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *CouchbaseProvider) Regenerate(oldsid, sid string) (session.RawStore, error) { + p.b = p.getBucket() + + var doc []byte + if err := p.b.Get(oldsid, &doc); err != nil || doc == nil { + p.b.Set(sid, int(p.maxlifetime), "") + } else { + err := p.b.Delete(oldsid) + if err != nil { + return nil, err + } + _, _ = p.b.Add(sid, int(p.maxlifetime), doc) + } + + err := p.b.Get(sid, &doc) + if err != nil { + return nil, err + } + var kv map[interface{}]interface{} + if doc == nil { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob(doc) + if err != nil { + return nil, err + } + } + + cs := &CouchbaseSessionStore{b: p.b, sid: sid, data: kv, maxlifetime: p.maxlifetime} + return cs, nil +} + +// Count counts and returns number of sessions. +func (p *CouchbaseProvider) Count() int { + // FIXME + return 0 +} + +// GC calls GC to clean expired sessions. +func (p *CouchbaseProvider) GC() {} + +func init() { + session.Register("couchbase", &CouchbaseProvider{}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/file.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/file.go new file mode 100644 index 0000000..cab807d --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/file.go @@ -0,0 +1,243 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path" + "path/filepath" + "sync" + "time" + + "github.com/Unknwon/com" +) + +// FileStore represents a file session store implementation. +type FileStore struct { + p *FileProvider + sid string + lock sync.RWMutex + data map[interface{}]interface{} +} + +// NewFileStore creates and returns a file session store. +func NewFileStore(p *FileProvider, sid string, kv map[interface{}]interface{}) *FileStore { + return &FileStore{ + p: p, + sid: sid, + data: kv, + } +} + +// Set sets value to given key in session. +func (s *FileStore) Set(key, val interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = val + return nil +} + +// Get gets value by given key in session. +func (s *FileStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete delete a key from session. +func (s *FileStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *FileStore) ID() string { + return s.sid +} + +// Release releases resource and save data to provider. +func (s *FileStore) Release() error { + data, err := EncodeGob(s.data) + if err != nil { + return err + } + + return ioutil.WriteFile(s.p.filepath(s.sid), data, os.ModePerm) +} + +// Flush deletes all session data. +func (s *FileStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// FileProvider represents a file session provider implementation. +type FileProvider struct { + maxlifetime int64 + rootPath string +} + +// Init initializes file session provider with given root path. +func (p *FileProvider) Init(maxlifetime int64, rootPath string) error { + p.maxlifetime = maxlifetime + p.rootPath = rootPath + return nil +} + +func (p *FileProvider) filepath(sid string) string { + return path.Join(p.rootPath, string(sid[0]), string(sid[1]), sid) +} + +// Read returns raw session store by session ID. +func (p *FileProvider) Read(sid string) (_ RawStore, err error) { + filename := p.filepath(sid) + if err = os.MkdirAll(path.Dir(filename), os.ModePerm); err != nil { + return nil, err + } + + var f *os.File + if com.IsFile(filename) { + f, err = os.OpenFile(filename, os.O_RDWR, os.ModePerm) + } else { + f, err = os.Create(filename) + } + if err != nil { + return nil, err + } + defer f.Close() + + if err = os.Chtimes(filename, time.Now(), time.Now()); err != nil { + return nil, err + } + + var kv map[interface{}]interface{} + data, err := ioutil.ReadAll(f) + if err != nil { + return nil, err + } + if len(data) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = DecodeGob(data) + if err != nil { + return nil, err + } + } + return NewFileStore(p, sid, kv), nil +} + +// Exist returns true if session with given ID exists. +func (p *FileProvider) Exist(sid string) bool { + return com.IsFile(p.filepath(sid)) +} + +// Destory deletes a session by session ID. +func (p *FileProvider) Destory(sid string) error { + return os.Remove(p.filepath(sid)) +} + +func (p *FileProvider) regenerate(oldsid, sid string) (err error) { + filename := p.filepath(sid) + if com.IsExist(filename) { + return fmt.Errorf("new sid '%s' already exists", sid) + } + + oldname := p.filepath(oldsid) + if !com.IsFile(oldname) { + data, err := EncodeGob(make(map[interface{}]interface{})) + if err != nil { + return err + } + if err = os.MkdirAll(path.Dir(oldname), os.ModePerm); err != nil { + return err + } + if err = ioutil.WriteFile(oldname, data, os.ModePerm); err != nil { + return err + } + } + + if err = os.MkdirAll(path.Dir(filename), os.ModePerm); err != nil { + return err + } + if err = os.Rename(oldname, filename); err != nil { + return err + } + return nil +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *FileProvider) Regenerate(oldsid, sid string) (_ RawStore, err error) { + if err := p.regenerate(oldsid, sid); err != nil { + return nil, err + } + + return p.Read(sid) +} + +// Count counts and returns number of sessions. +func (p *FileProvider) Count() int { + count := 0 + if err := filepath.Walk(p.rootPath, func(path string, fi os.FileInfo, err error) error { + if err != nil { + return err + } + + if !fi.IsDir() { + count++ + } + return nil + }); err != nil { + log.Printf("error counting session files: %v", err) + return 0 + } + return count +} + +// GC calls GC to clean expired sessions. +func (p *FileProvider) GC() { + if !com.IsExist(p.rootPath) { + return + } + + if err := filepath.Walk(p.rootPath, func(path string, fi os.FileInfo, err error) error { + if err != nil { + return err + } + + if !fi.IsDir() && + (fi.ModTime().Unix()+p.maxlifetime) < time.Now().Unix() { + return os.Remove(path) + } + return nil + }); err != nil { + log.Printf("error garbage collecting session files: %v", err) + } +} + +func init() { + Register("file", &FileProvider{}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/file_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/file_test.go new file mode 100644 index 0000000..9c83555 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/file_test.go @@ -0,0 +1,34 @@ +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "os" + "path" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_FileProvider(t *testing.T) { + Convey("Test file session provider", t, func() { + dir := path.Join(os.TempDir(), "data/sessions") + os.RemoveAll(dir) + testProvider(Options{ + Provider: "file", + ProviderConfig: dir, + }) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis.go new file mode 100644 index 0000000..afde713 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis.go @@ -0,0 +1,222 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "fmt" + "strings" + "sync" + + "github.com/Unknwon/com" + "github.com/siddontang/ledisdb/config" + "github.com/siddontang/ledisdb/ledis" + "gopkg.in/ini.v1" + + "github.com/macaron-contrib/session" +) + +// LedisStore represents a ledis session store implementation. +type LedisStore struct { + c *ledis.DB + sid string + expire int64 + lock sync.RWMutex + data map[interface{}]interface{} +} + +// NewLedisStore creates and returns a ledis session store. +func NewLedisStore(c *ledis.DB, sid string, expire int64, kv map[interface{}]interface{}) *LedisStore { + return &LedisStore{ + c: c, + expire: expire, + sid: sid, + data: kv, + } +} + +// Set sets value to given key in session. +func (s *LedisStore) Set(key, val interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = val + return nil +} + +// Get gets value by given key in session. +func (s *LedisStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete delete a key from session. +func (s *LedisStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *LedisStore) ID() string { + return s.sid +} + +// Release releases resource and save data to provider. +func (s *LedisStore) Release() error { + data, err := session.EncodeGob(s.data) + if err != nil { + return err + } + + if err = s.c.Set([]byte(s.sid), data); err != nil { + return err + } + _, err = s.c.Expire([]byte(s.sid), s.expire) + return err +} + +// Flush deletes all session data. +func (s *LedisStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// LedisProvider represents a ledis session provider implementation. +type LedisProvider struct { + c *ledis.DB + expire int64 +} + +// Init initializes ledis session provider. +// configs: data_dir=./app.db,db=0 +func (p *LedisProvider) Init(expire int64, configs string) error { + p.expire = expire + + cfg, err := ini.Load([]byte(strings.Replace(configs, ",", "\n", -1))) + if err != nil { + return err + } + + db := 0 + opt := new(config.Config) + for k, v := range cfg.Section("").KeysHash() { + switch k { + case "data_dir": + opt.DataDir = v + case "db": + db = com.StrTo(v).MustInt() + default: + return fmt.Errorf("session/ledis: unsupported option '%s'", k) + } + } + + l, err := ledis.Open(opt) + if err != nil { + return fmt.Errorf("session/ledis: error opening db: %v", err) + } + p.c, err = l.Select(db) + return err +} + +// Read returns raw session store by session ID. +func (p *LedisProvider) Read(sid string) (session.RawStore, error) { + if !p.Exist(sid) { + if err := p.c.Set([]byte(sid), []byte("")); err != nil { + return nil, err + } + } + + var kv map[interface{}]interface{} + kvs, err := p.c.Get([]byte(sid)) + if err != nil { + return nil, err + } + if len(kvs) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob(kvs) + if err != nil { + return nil, err + } + } + + return NewLedisStore(p.c, sid, p.expire, kv), nil +} + +// Exist returns true if session with given ID exists. +func (p *LedisProvider) Exist(sid string) bool { + count, err := p.c.Exists([]byte(sid)) + return err == nil && count > 0 +} + +// Destory deletes a session by session ID. +func (p *LedisProvider) Destory(sid string) error { + _, err := p.c.Del([]byte(sid)) + return err +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *LedisProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) { + if p.Exist(sid) { + return nil, fmt.Errorf("new sid '%s' already exists", sid) + } + + kvs := make([]byte, 0) + if p.Exist(oldsid) { + if kvs, err = p.c.Get([]byte(oldsid)); err != nil { + return nil, err + } else if _, err = p.c.Del([]byte(oldsid)); err != nil { + return nil, err + } + } + if err = p.c.SetEX([]byte(sid), p.expire, kvs); err != nil { + return nil, err + } + + var kv map[interface{}]interface{} + if len(kvs) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob([]byte(kvs)) + if err != nil { + return nil, err + } + } + + return NewLedisStore(p.c, sid, p.expire, kv), nil +} + +// Count counts and returns number of sessions. +func (p *LedisProvider) Count() int { + // FIXME: how come this library does not have DbSize() method? + return -1 +} + +// GC calls GC to clean expired sessions. +func (p *LedisProvider) GC() { + // FIXME: wtf??? +} + +func init() { + session.Register("ledis", &LedisProvider{}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis.goconvey b/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis.goconvey new file mode 100644 index 0000000..8485e98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis.goconvey @@ -0,0 +1 @@ +ignore \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis_test.go new file mode 100644 index 0000000..dac42a3 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/ledis/ledis_test.go @@ -0,0 +1,105 @@ +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/Unknwon/macaron" + . "github.com/smartystreets/goconvey/convey" + + "github.com/macaron-contrib/session" +) + +func Test_LedisProvider(t *testing.T) { + Convey("Test ledis session provider", t, func() { + opt := session.Options{ + Provider: "ledis", + ProviderConfig: "data_dir=./tmp.db", + } + + Convey("Basic operation", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + sess.Set("uname", "unknwon") + }) + m.Get("/reg", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := raw.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + }) + m.Get("/get", func(ctx *macaron.Context, sess session.Store) { + sid := sess.ID() + So(sid, ShouldNotBeEmpty) + + raw, err := sess.Read(sid) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Delete("uname"), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + cookie := resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/reg", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + cookie = resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + Convey("Regenrate empty session", func() { + m.Get("/empty", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + }) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/empty", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf486; Path=/;") + m.ServeHTTP(resp, req) + }) + }) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache.go new file mode 100644 index 0000000..b4fcdde --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache.go @@ -0,0 +1,199 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "fmt" + "strings" + "sync" + + "github.com/bradfitz/gomemcache/memcache" + + "github.com/macaron-contrib/session" +) + +// MemcacheStore represents a memcache session store implementation. +type MemcacheStore struct { + c *memcache.Client + sid string + expire int32 + lock sync.RWMutex + data map[interface{}]interface{} +} + +// NewMemcacheStore creates and returns a memcache session store. +func NewMemcacheStore(c *memcache.Client, sid string, expire int32, kv map[interface{}]interface{}) *MemcacheStore { + return &MemcacheStore{ + c: c, + sid: sid, + expire: expire, + data: kv, + } +} + +func NewItem(sid string, data []byte, expire int32) *memcache.Item { + return &memcache.Item{ + Key: sid, + Value: data, + Expiration: expire, + } +} + +// Set sets value to given key in session. +func (s *MemcacheStore) Set(key, val interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = val + return nil +} + +// Get gets value by given key in session. +func (s *MemcacheStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete delete a key from session. +func (s *MemcacheStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *MemcacheStore) ID() string { + return s.sid +} + +// Release releases resource and save data to provider. +func (s *MemcacheStore) Release() error { + data, err := session.EncodeGob(s.data) + if err != nil { + return err + } + + return s.c.Set(NewItem(s.sid, data, s.expire)) +} + +// Flush deletes all session data. +func (s *MemcacheStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// MemcacheProvider represents a memcache session provider implementation. +type MemcacheProvider struct { + c *memcache.Client + expire int32 +} + +// Init initializes memcache session provider. +// connStrs: 127.0.0.1:9090;127.0.0.1:9091 +func (p *MemcacheProvider) Init(expire int64, connStrs string) error { + p.expire = int32(expire) + p.c = memcache.New(strings.Split(connStrs, ";")...) + return nil +} + +// Read returns raw session store by session ID. +func (p *MemcacheProvider) Read(sid string) (session.RawStore, error) { + if !p.Exist(sid) { + if err := p.c.Set(NewItem(sid, []byte(""), p.expire)); err != nil { + return nil, err + } + } + + var kv map[interface{}]interface{} + item, err := p.c.Get(sid) + if err != nil { + return nil, err + } + if len(item.Value) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob(item.Value) + if err != nil { + return nil, err + } + } + + return NewMemcacheStore(p.c, sid, p.expire, kv), nil +} + +// Exist returns true if session with given ID exists. +func (p *MemcacheProvider) Exist(sid string) bool { + _, err := p.c.Get(sid) + return err == nil +} + +// Destory deletes a session by session ID. +func (p *MemcacheProvider) Destory(sid string) error { + return p.c.Delete(sid) +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *MemcacheProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) { + if p.Exist(sid) { + return nil, fmt.Errorf("new sid '%s' already exists", sid) + } + + item := NewItem(sid, []byte(""), p.expire) + if p.Exist(oldsid) { + item, err = p.c.Get(oldsid) + if err != nil { + return nil, err + } else if err = p.c.Delete(oldsid); err != nil { + return nil, err + } + item.Key = sid + } + if err = p.c.Set(item); err != nil { + return nil, err + } + + var kv map[interface{}]interface{} + if len(item.Value) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob(item.Value) + if err != nil { + return nil, err + } + } + + return NewMemcacheStore(p.c, sid, p.expire, kv), nil +} + +// Count counts and returns number of sessions. +func (p *MemcacheProvider) Count() int { + // FIXME: how come this library does not have Stats method? + return -1 +} + +// GC calls GC to clean expired sessions. +func (p *MemcacheProvider) GC() {} + +func init() { + session.Register("memcache", &MemcacheProvider{}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache.goconvey b/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache.goconvey new file mode 100644 index 0000000..8485e98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache.goconvey @@ -0,0 +1 @@ +ignore \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache_test.go new file mode 100644 index 0000000..beb272d --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/memcache/memcache_test.go @@ -0,0 +1,107 @@ +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/Unknwon/macaron" + . "github.com/smartystreets/goconvey/convey" + + "github.com/macaron-contrib/session" +) + +func Test_MemcacheProvider(t *testing.T) { + Convey("Test memcache session provider", t, func() { + opt := session.Options{ + Provider: "memcache", + ProviderConfig: "127.0.0.1:9090", + } + + Convey("Basic operation", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + sess.Set("uname", "unknwon") + }) + m.Get("/reg", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := raw.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + }) + m.Get("/get", func(ctx *macaron.Context, sess session.Store) { + sid := sess.ID() + So(sid, ShouldNotBeEmpty) + + raw, err := sess.Read(sid) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Delete("uname"), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + cookie := resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/reg", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + cookie = resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + }) + + Convey("Regenrate empty session", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf486; Path=/;") + m.ServeHTTP(resp, req) + }) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/memory.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/memory.go new file mode 100644 index 0000000..e717635 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/memory.go @@ -0,0 +1,212 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "container/list" + "fmt" + "sync" + "time" +) + +// MemStore represents a in-memory session store implementation. +type MemStore struct { + sid string + lock sync.RWMutex + data map[interface{}]interface{} + lastAccess time.Time +} + +// NewMemStore creates and returns a memory session store. +func NewMemStore(sid string) *MemStore { + return &MemStore{ + sid: sid, + data: make(map[interface{}]interface{}), + lastAccess: time.Now(), + } +} + +// Set sets value to given key in session. +func (s *MemStore) Set(key, val interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = val + return nil +} + +// Get gets value by given key in session. +func (s *MemStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete deletes a key from session. +func (s *MemStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *MemStore) ID() string { + return s.sid +} + +// Release releases resource and save data to provider. +func (_ *MemStore) Release() error { + return nil +} + +// Flush deletes all session data. +func (s *MemStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// MemProvider represents a in-memory session provider implementation. +type MemProvider struct { + lock sync.RWMutex + maxLifetime int64 + data map[string]*list.Element + // A priority list whose lastAccess newer gets higer priority. + list *list.List +} + +// Init initializes memory session provider. +func (p *MemProvider) Init(maxLifetime int64, _ string) error { + p.maxLifetime = maxLifetime + return nil +} + +// update expands time of session store by given ID. +func (p *MemProvider) update(sid string) error { + p.lock.Lock() + defer p.lock.Unlock() + + if e, ok := p.data[sid]; ok { + e.Value.(*MemStore).lastAccess = time.Now() + p.list.MoveToFront(e) + return nil + } + return nil +} + +// Read returns raw session store by session ID. +func (p *MemProvider) Read(sid string) (_ RawStore, err error) { + p.lock.RLock() + e, ok := p.data[sid] + p.lock.RUnlock() + + if ok { + if err = p.update(sid); err != nil { + return nil, err + } + return e.Value.(*MemStore), nil + } + + // Create a new session. + p.lock.Lock() + defer p.lock.Unlock() + + s := NewMemStore(sid) + p.data[sid] = p.list.PushBack(s) + return s, nil +} + +// Exist returns true if session with given ID exists. +func (p *MemProvider) Exist(sid string) bool { + p.lock.RLock() + defer p.lock.RUnlock() + + _, ok := p.data[sid] + return ok +} + +// Destory deletes a session by session ID. +func (p *MemProvider) Destory(sid string) error { + p.lock.Lock() + defer p.lock.Unlock() + + e, ok := p.data[sid] + if !ok { + return nil + } + + p.list.Remove(e) + delete(p.data, sid) + return nil +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *MemProvider) Regenerate(oldsid, sid string) (RawStore, error) { + if p.Exist(sid) { + return nil, fmt.Errorf("new sid '%s' already exists", sid) + } + + s, err := p.Read(oldsid) + if err != nil { + return nil, err + } + + if err = p.Destory(oldsid); err != nil { + return nil, err + } + + s.(*MemStore).sid = sid + p.data[sid] = p.list.PushBack(s) + return s, nil +} + +// Count counts and returns number of sessions. +func (p *MemProvider) Count() int { + return p.list.Len() +} + +// GC calls GC to clean expired sessions. +func (p *MemProvider) GC() { + p.lock.RLock() + for { + // No session in the list. + e := p.list.Back() + if e == nil { + break + } + + if (e.Value.(*MemStore).lastAccess.Unix() + p.maxLifetime) < time.Now().Unix() { + p.lock.RUnlock() + p.lock.Lock() + p.list.Remove(e) + delete(p.data, e.Value.(*MemStore).sid) + p.lock.Unlock() + p.lock.RLock() + } else { + break + } + } + p.lock.RUnlock() +} + +func init() { + Register("memory", &MemProvider{list: list.New(), data: make(map[string]*list.Element)}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/memory_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/memory_test.go new file mode 100644 index 0000000..41659bb --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/memory_test.go @@ -0,0 +1,27 @@ +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_MemProvider(t *testing.T) { + Convey("Test memory session provider", t, func() { + testProvider(Options{}) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql.go new file mode 100644 index 0000000..7997e03 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql.go @@ -0,0 +1,195 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "database/sql" + "fmt" + "log" + "sync" + "time" + + _ "github.com/go-sql-driver/mysql" + + "github.com/macaron-contrib/session" +) + +// MysqlStore represents a mysql session store implementation. +type MysqlStore struct { + c *sql.DB + sid string + lock sync.RWMutex + data map[interface{}]interface{} +} + +// NewMysqlStore creates and returns a mysql session store. +func NewMysqlStore(c *sql.DB, sid string, kv map[interface{}]interface{}) *MysqlStore { + return &MysqlStore{ + c: c, + sid: sid, + data: kv, + } +} + +// Set sets value to given key in session. +func (s *MysqlStore) Set(key, val interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = val + return nil +} + +// Get gets value by given key in session. +func (s *MysqlStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete delete a key from session. +func (s *MysqlStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *MysqlStore) ID() string { + return s.sid +} + +// Release releases resource and save data to provider. +func (s *MysqlStore) Release() error { + data, err := session.EncodeGob(s.data) + if err != nil { + return err + } + + _, err = s.c.Exec("UPDATE session SET data=?, expiry=? WHERE `key`=?", + data, time.Now().Unix(), s.sid) + return err +} + +// Flush deletes all session data. +func (s *MysqlStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// MysqlProvider represents a mysql session provider implementation. +type MysqlProvider struct { + c *sql.DB + expire int64 +} + +// Init initializes mysql session provider. +// connStr: username:password@protocol(address)/dbname?param=value +func (p *MysqlProvider) Init(expire int64, connStr string) (err error) { + p.expire = expire + + p.c, err = sql.Open("mysql", connStr) + if err != nil { + return err + } + return p.c.Ping() +} + +// Read returns raw session store by session ID. +func (p *MysqlProvider) Read(sid string) (session.RawStore, error) { + var data []byte + err := p.c.QueryRow("SELECT data FROM session WHERE `key`=?", sid).Scan(&data) + if err == sql.ErrNoRows { + _, err = p.c.Exec("INSERT INTO session(`key`,data,expiry) VALUES(?,?,?)", + sid, "", time.Now().Unix()) + } + if err != nil { + return nil, err + } + + var kv map[interface{}]interface{} + if len(data) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob(data) + if err != nil { + return nil, err + } + } + + return NewMysqlStore(p.c, sid, kv), nil +} + +// Exist returns true if session with given ID exists. +func (p *MysqlProvider) Exist(sid string) bool { + var data []byte + err := p.c.QueryRow("SELECT data FROM session WHERE `key`=?", sid).Scan(&data) + if err != nil && err != sql.ErrNoRows { + panic("session/mysql: error checking existence: " + err.Error()) + } + return err != sql.ErrNoRows +} + +// Destory deletes a session by session ID. +func (p *MysqlProvider) Destory(sid string) error { + _, err := p.c.Exec("DELETE FROM session WHERE `key`=?", sid) + return err +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *MysqlProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) { + if p.Exist(sid) { + return nil, fmt.Errorf("new sid '%s' already exists", sid) + } + + if !p.Exist(oldsid) { + if _, err = p.c.Exec("INSERT INTO session(`key`,data,expiry) VALUES(?,?,?)", + oldsid, "", time.Now().Unix()); err != nil { + return nil, err + } + } + + if _, err = p.c.Exec("UPDATE session SET `key`=? WHERE `key`=?", sid, oldsid); err != nil { + return nil, err + } + + return p.Read(sid) +} + +// Count counts and returns number of sessions. +func (p *MysqlProvider) Count() (total int) { + if err := p.c.QueryRow("SELECT COUNT(*) AS NUM FROM session").Scan(&total); err != nil { + panic("session/mysql: error counting records: " + err.Error()) + } + return total +} + +// GC calls GC to clean expired sessions. +func (p *MysqlProvider) GC() { + if _, err := p.c.Exec("DELETE FROM session WHERE UNIX_TIMESTAMP(NOW()) - expiry > ?", p.expire); err != nil { + log.Printf("session/mysql: error garbage collecting: %v", err) + } +} + +func init() { + session.Register("mysql", &MysqlProvider{}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql.goconvey b/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql.goconvey new file mode 100644 index 0000000..8485e98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql.goconvey @@ -0,0 +1 @@ +ignore \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql_test.go new file mode 100644 index 0000000..15b3996 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/mysql/mysql_test.go @@ -0,0 +1,138 @@ +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/Unknwon/macaron" + . "github.com/smartystreets/goconvey/convey" + + "github.com/macaron-contrib/session" +) + +func Test_MysqlProvider(t *testing.T) { + Convey("Test mysql session provider", t, func() { + opt := session.Options{ + Provider: "mysql", + ProviderConfig: "root:@tcp(localhost:3306)/macaron?charset=utf8", + } + + Convey("Basic operation", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + sess.Set("uname", "unknwon") + }) + m.Get("/reg", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := raw.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + }) + m.Get("/get", func(ctx *macaron.Context, sess session.Store) { + sid := sess.ID() + So(sid, ShouldNotBeEmpty) + + raw, err := sess.Read(sid) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + So(raw.Release(), ShouldBeNil) + + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Delete("uname"), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + cookie := resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/reg", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + cookie = resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + }) + + Convey("Regenrate empty session", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf48; Path=/;") + m.ServeHTTP(resp, req) + }) + + Convey("GC session", func() { + m := macaron.New() + opt2 := opt + opt2.Gclifetime = 1 + m.Use(session.Sessioner(opt2)) + + m.Get("/", func(sess session.Store) { + sess.Set("uname", "unknwon") + So(sess.ID(), ShouldNotBeEmpty) + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Flush(), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + time.Sleep(2 * time.Second) + sess.GC() + So(sess.Count(), ShouldEqual, 0) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb.go new file mode 100644 index 0000000..7f017bf --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb.go @@ -0,0 +1,203 @@ +// Copyright 2015 Unknwon +// +// 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 session + +import ( + "fmt" + "sync" + + "github.com/lunny/nodb" + "github.com/lunny/nodb/config" + + "github.com/macaron-contrib/session" +) + +// NodbStore represents a nodb session store implementation. +type NodbStore struct { + c *nodb.DB + sid string + expire int64 + lock sync.RWMutex + data map[interface{}]interface{} +} + +// NewNodbStore creates and returns a ledis session store. +func NewNodbStore(c *nodb.DB, sid string, expire int64, kv map[interface{}]interface{}) *NodbStore { + return &NodbStore{ + c: c, + expire: expire, + sid: sid, + data: kv, + } +} + +// Set sets value to given key in session. +func (s *NodbStore) Set(key, val interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = val + return nil +} + +// Get gets value by given key in session. +func (s *NodbStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete delete a key from session. +func (s *NodbStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *NodbStore) ID() string { + return s.sid +} + +// Release releases resource and save data to provider. +func (s *NodbStore) Release() error { + data, err := session.EncodeGob(s.data) + if err != nil { + return err + } + + if err = s.c.Set([]byte(s.sid), data); err != nil { + return err + } + _, err = s.c.Expire([]byte(s.sid), s.expire) + return err +} + +// Flush deletes all session data. +func (s *NodbStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// NodbProvider represents a ledis session provider implementation. +type NodbProvider struct { + c *nodb.DB + expire int64 +} + +// Init initializes nodb session provider. +func (p *NodbProvider) Init(expire int64, configs string) error { + p.expire = expire + + cfg := new(config.Config) + cfg.DataDir = configs + dbs, err := nodb.Open(cfg) + if err != nil { + return fmt.Errorf("session/nodb: error opening db: %v", err) + } + + p.c, err = dbs.Select(0) + return err +} + +// Read returns raw session store by session ID. +func (p *NodbProvider) Read(sid string) (session.RawStore, error) { + if !p.Exist(sid) { + if err := p.c.Set([]byte(sid), []byte("")); err != nil { + return nil, err + } + } + + var kv map[interface{}]interface{} + kvs, err := p.c.Get([]byte(sid)) + if err != nil { + return nil, err + } + if len(kvs) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob(kvs) + if err != nil { + return nil, err + } + } + + return NewNodbStore(p.c, sid, p.expire, kv), nil +} + +// Exist returns true if session with given ID exists. +func (p *NodbProvider) Exist(sid string) bool { + count, err := p.c.Exists([]byte(sid)) + return err == nil && count > 0 +} + +// Destory deletes a session by session ID. +func (p *NodbProvider) Destory(sid string) error { + _, err := p.c.Del([]byte(sid)) + return err +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *NodbProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) { + if p.Exist(sid) { + return nil, fmt.Errorf("new sid '%s' already exists", sid) + } + + kvs := make([]byte, 0) + if p.Exist(oldsid) { + if kvs, err = p.c.Get([]byte(oldsid)); err != nil { + return nil, err + } else if _, err = p.c.Del([]byte(oldsid)); err != nil { + return nil, err + } + } + + if err = p.c.Set([]byte(sid), kvs); err != nil { + return nil, err + } else if _, err = p.c.Expire([]byte(sid), p.expire); err != nil { + return nil, err + } + + var kv map[interface{}]interface{} + if len(kvs) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob([]byte(kvs)) + if err != nil { + return nil, err + } + } + + return NewNodbStore(p.c, sid, p.expire, kv), nil +} + +// Count counts and returns number of sessions. +func (p *NodbProvider) Count() int { + // FIXME: how come this library does not have DbSize() method? + return -1 +} + +// GC calls GC to clean expired sessions. +func (p *NodbProvider) GC() {} + +func init() { + session.Register("nodb", &NodbProvider{}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb.goconvey b/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb.goconvey new file mode 100644 index 0000000..8485e98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb.goconvey @@ -0,0 +1 @@ +ignore \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb_test.go new file mode 100644 index 0000000..c86ba98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/nodb/nodb_test.go @@ -0,0 +1,105 @@ +// Copyright 2015 Unknwon +// +// 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 session + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/Unknwon/macaron" + . "github.com/smartystreets/goconvey/convey" + + "github.com/macaron-contrib/session" +) + +func Test_LedisProvider(t *testing.T) { + Convey("Test nodb session provider", t, func() { + opt := session.Options{ + Provider: "nodb", + ProviderConfig: "./tmp.db", + } + + Convey("Basic operation", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + sess.Set("uname", "unknwon") + }) + m.Get("/reg", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := raw.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + }) + m.Get("/get", func(ctx *macaron.Context, sess session.Store) { + sid := sess.ID() + So(sid, ShouldNotBeEmpty) + + raw, err := sess.Read(sid) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Delete("uname"), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + cookie := resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/reg", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + cookie = resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + Convey("Regenrate empty session", func() { + m.Get("/empty", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + }) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/empty", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf486; Path=/;") + m.ServeHTTP(resp, req) + }) + }) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres.go new file mode 100644 index 0000000..5cb4c82 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres.go @@ -0,0 +1,196 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "database/sql" + "fmt" + "log" + "sync" + "time" + + _ "github.com/lib/pq" + + "github.com/macaron-contrib/session" +) + +// PostgresStore represents a postgres session store implementation. +type PostgresStore struct { + c *sql.DB + sid string + lock sync.RWMutex + data map[interface{}]interface{} +} + +// NewPostgresStore creates and returns a postgres session store. +func NewPostgresStore(c *sql.DB, sid string, kv map[interface{}]interface{}) *PostgresStore { + return &PostgresStore{ + c: c, + sid: sid, + data: kv, + } +} + +// Set sets value to given key in session. +func (s *PostgresStore) Set(key, value interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = value + return nil +} + +// Get gets value by given key in session. +func (s *PostgresStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete delete a key from session. +func (s *PostgresStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *PostgresStore) ID() string { + return s.sid +} + +// save postgres session values to database. +// must call this method to save values to database. +func (s *PostgresStore) Release() error { + data, err := session.EncodeGob(s.data) + if err != nil { + return err + } + + _, err = s.c.Exec("UPDATE session SET data=$1, expiry=$2 WHERE key=$3", + data, time.Now().Unix(), s.sid) + return err +} + +// Flush deletes all session data. +func (s *PostgresStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// PostgresProvider represents a postgres session provider implementation. +type PostgresProvider struct { + c *sql.DB + maxlifetime int64 +} + +// Init initializes postgres session provider. +// connStr: user=a password=b host=localhost port=5432 dbname=c sslmode=disable +func (p *PostgresProvider) Init(maxlifetime int64, connStr string) (err error) { + p.maxlifetime = maxlifetime + + p.c, err = sql.Open("postgres", connStr) + if err != nil { + return err + } + return p.c.Ping() +} + +// Read returns raw session store by session ID. +func (p *PostgresProvider) Read(sid string) (session.RawStore, error) { + var data []byte + err := p.c.QueryRow("SELECT data FROM session WHERE key=$1", sid).Scan(&data) + if err == sql.ErrNoRows { + _, err = p.c.Exec("INSERT INTO session(key,data,expiry) VALUES($1,$2,$3)", + sid, "", time.Now().Unix()) + } + if err != nil { + return nil, err + } + + var kv map[interface{}]interface{} + if len(data) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob(data) + if err != nil { + return nil, err + } + } + + return NewPostgresStore(p.c, sid, kv), nil +} + +// Exist returns true if session with given ID exists. +func (p *PostgresProvider) Exist(sid string) bool { + var data []byte + err := p.c.QueryRow("SELECT data FROM session WHERE key=$1", sid).Scan(&data) + if err != nil && err != sql.ErrNoRows { + panic("session/postgres: error checking existence: " + err.Error()) + } + return err != sql.ErrNoRows +} + +// Destory deletes a session by session ID. +func (p *PostgresProvider) Destory(sid string) error { + _, err := p.c.Exec("DELETE FROM session WHERE key=$1", sid) + return err +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *PostgresProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) { + if p.Exist(sid) { + return nil, fmt.Errorf("new sid '%s' already exists", sid) + } + + if !p.Exist(oldsid) { + if _, err = p.c.Exec("INSERT INTO session(key,data,expiry) VALUES($1,$2,$3)", + oldsid, "", time.Now().Unix()); err != nil { + return nil, err + } + } + + if _, err = p.c.Exec("UPDATE session SET key=$1 WHERE key=$2", sid, oldsid); err != nil { + return nil, err + } + + return p.Read(sid) +} + +// Count counts and returns number of sessions. +func (p *PostgresProvider) Count() (total int) { + if err := p.c.QueryRow("SELECT COUNT(*) AS NUM FROM session").Scan(&total); err != nil { + panic("session/postgres: error counting records: " + err.Error()) + } + return total +} + +// GC calls GC to clean expired sessions. +func (p *PostgresProvider) GC() { + if _, err := p.c.Exec("DELETE FROM session WHERE EXTRACT(EPOCH FROM NOW()) - expiry > $1", p.maxlifetime); err != nil { + log.Printf("session/postgres: error garbage collecting: %v", err) + } +} + +func init() { + session.Register("postgres", &PostgresProvider{}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres.goconvey b/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres.goconvey new file mode 100644 index 0000000..8485e98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres.goconvey @@ -0,0 +1 @@ +ignore \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres_test.go new file mode 100644 index 0000000..ea212c7 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/postgres/postgres_test.go @@ -0,0 +1,138 @@ +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/Unknwon/macaron" + . "github.com/smartystreets/goconvey/convey" + + "github.com/macaron-contrib/session" +) + +func Test_PostgresProvider(t *testing.T) { + Convey("Test postgres session provider", t, func() { + opt := session.Options{ + Provider: "postgres", + ProviderConfig: "user=jiahuachen dbname=macaron port=5432 sslmode=disable", + } + + Convey("Basic operation", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + sess.Set("uname", "unknwon") + }) + m.Get("/reg", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := raw.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + }) + m.Get("/get", func(ctx *macaron.Context, sess session.Store) { + sid := sess.ID() + So(sid, ShouldNotBeEmpty) + + raw, err := sess.Read(sid) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + So(raw.Release(), ShouldBeNil) + + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Delete("uname"), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + cookie := resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/reg", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + cookie = resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + }) + + Convey("Regenrate empty session", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf48; Path=/;") + m.ServeHTTP(resp, req) + }) + + Convey("GC session", func() { + m := macaron.New() + opt2 := opt + opt2.Gclifetime = 1 + m.Use(session.Sessioner(opt2)) + + m.Get("/", func(sess session.Store) { + sess.Set("uname", "unknwon") + So(sess.ID(), ShouldNotBeEmpty) + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Flush(), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + time.Sleep(2 * time.Second) + sess.GC() + So(sess.Count(), ShouldEqual, 0) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis.go new file mode 100644 index 0000000..6d6a2c4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis.go @@ -0,0 +1,227 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "fmt" + "strings" + "sync" + "time" + + "github.com/Unknwon/com" + "gopkg.in/ini.v1" + "gopkg.in/redis.v2" + + "github.com/macaron-contrib/session" +) + +// RedisStore represents a redis session store implementation. +type RedisStore struct { + c *redis.Client + sid string + duration time.Duration + lock sync.RWMutex + data map[interface{}]interface{} +} + +// NewRedisStore creates and returns a redis session store. +func NewRedisStore(c *redis.Client, sid string, dur time.Duration, kv map[interface{}]interface{}) *RedisStore { + return &RedisStore{ + c: c, + sid: sid, + duration: dur, + data: kv, + } +} + +// Set sets value to given key in session. +func (s *RedisStore) Set(key, val interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data[key] = val + return nil +} + +// Get gets value by given key in session. +func (s *RedisStore) Get(key interface{}) interface{} { + s.lock.RLock() + defer s.lock.RUnlock() + + return s.data[key] +} + +// Delete delete a key from session. +func (s *RedisStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + + delete(s.data, key) + return nil +} + +// ID returns current session ID. +func (s *RedisStore) ID() string { + return s.sid +} + +// Release releases resource and save data to provider. +func (s *RedisStore) Release() error { + data, err := session.EncodeGob(s.data) + if err != nil { + return err + } + + return s.c.SetEx(s.sid, s.duration, string(data)).Err() +} + +// Flush deletes all session data. +func (s *RedisStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + + s.data = make(map[interface{}]interface{}) + return nil +} + +// RedisProvider represents a redis session provider implementation. +type RedisProvider struct { + c *redis.Client + duration time.Duration +} + +// Init initializes redis session provider. +// configs: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180 +func (p *RedisProvider) Init(maxlifetime int64, configs string) (err error) { + p.duration, err = time.ParseDuration(fmt.Sprintf("%ds", maxlifetime)) + if err != nil { + return err + } + + cfg, err := ini.Load([]byte(strings.Replace(configs, ",", "\n", -1))) + if err != nil { + return err + } + + opt := &redis.Options{ + Network: "tcp", + } + for k, v := range cfg.Section("").KeysHash() { + switch k { + case "network": + opt.Network = v + case "addr": + opt.Addr = v + case "password": + opt.Password = v + case "db": + opt.DB = com.StrTo(v).MustInt64() + case "pool_size": + opt.PoolSize = com.StrTo(v).MustInt() + case "idle_timeout": + opt.IdleTimeout, err = time.ParseDuration(v + "s") + if err != nil { + return fmt.Errorf("error parsing idle timeout: %v", err) + } + default: + return fmt.Errorf("session/redis: unsupported option '%s'", k) + } + } + + p.c = redis.NewClient(opt) + return p.c.Ping().Err() +} + +// Read returns raw session store by session ID. +func (p *RedisProvider) Read(sid string) (session.RawStore, error) { + if !p.Exist(sid) { + if err := p.c.Set(sid, "").Err(); err != nil { + return nil, err + } + } + + var kv map[interface{}]interface{} + kvs, err := p.c.Get(sid).Result() + if err != nil { + return nil, err + } + if len(kvs) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob([]byte(kvs)) + if err != nil { + return nil, err + } + } + + return NewRedisStore(p.c, sid, p.duration, kv), nil +} + +// Exist returns true if session with given ID exists. +func (p *RedisProvider) Exist(sid string) bool { + has, err := p.c.Exists(sid).Result() + return err == nil && has +} + +// Destory deletes a session by session ID. +func (p *RedisProvider) Destory(sid string) error { + return p.c.Del(sid).Err() +} + +// Regenerate regenerates a session store from old session ID to new one. +func (p *RedisProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) { + if p.Exist(sid) { + return nil, fmt.Errorf("new sid '%s' already exists", sid) + } else if !p.Exist(oldsid) { + // Make a fake old session. + if err = p.c.SetEx(oldsid, p.duration, "").Err(); err != nil { + return nil, err + } + } + + if err = p.c.Rename(oldsid, sid).Err(); err != nil { + return nil, err + } + + var kv map[interface{}]interface{} + kvs, err := p.c.Get(sid).Result() + if err != nil { + return nil, err + } + + if len(kvs) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob([]byte(kvs)) + if err != nil { + return nil, err + } + } + + return NewRedisStore(p.c, sid, p.duration, kv), nil +} + +// Count counts and returns number of sessions. +func (p *RedisProvider) Count() int { + return int(p.c.DbSize().Val()) +} + +// GC calls GC to clean expired sessions. +func (_ *RedisProvider) GC() {} + +func init() { + session.Register("redis", &RedisProvider{}) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis.goconvey b/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis.goconvey new file mode 100644 index 0000000..8485e98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis.goconvey @@ -0,0 +1 @@ +ignore \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis_test.go new file mode 100644 index 0000000..9fd8e65 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/redis/redis_test.go @@ -0,0 +1,107 @@ +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/Unknwon/macaron" + . "github.com/smartystreets/goconvey/convey" + + "github.com/macaron-contrib/session" +) + +func Test_RedisProvider(t *testing.T) { + Convey("Test redis session provider", t, func() { + opt := session.Options{ + Provider: "redis", + ProviderConfig: "addr=:6379", + } + + Convey("Basic operation", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + sess.Set("uname", "unknwon") + }) + m.Get("/reg", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := raw.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + }) + m.Get("/get", func(ctx *macaron.Context, sess session.Store) { + sid := sess.ID() + So(sid, ShouldNotBeEmpty) + + raw, err := sess.Read(sid) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Delete("uname"), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + cookie := resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/reg", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + cookie = resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + }) + + Convey("Regenrate empty session", func() { + m := macaron.New() + m.Use(session.Sessioner(opt)) + m.Get("/", func(ctx *macaron.Context, sess session.Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf486; Path=/;") + m.ServeHTTP(resp, req) + }) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/session.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/session.go new file mode 100644 index 0000000..9cc1d52 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/session.go @@ -0,0 +1,401 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session a middleware that provides the session management of Macaron. +package session + +// NOTE: last sync 000033e on Nov 4, 2014. + +import ( + "encoding/hex" + "fmt" + "net/http" + "net/url" + "time" + + "github.com/Unknwon/macaron" +) + +const _VERSION = "0.1.6" + +func Version() string { + return _VERSION +} + +// RawStore is the interface that operates the session data. +type RawStore interface { + // Set sets value to given key in session. + Set(interface{}, interface{}) error + // Get gets value by given key in session. + Get(interface{}) interface{} + // Delete deletes a key from session. + Delete(interface{}) error + // ID returns current session ID. + ID() string + // Release releases session resource and save data to provider. + Release() error + // Flush deletes all session data. + Flush() error +} + +// Store is the interface that contains all data for one session process with specific ID. +type Store interface { + RawStore + // Read returns raw session store by session ID. + Read(string) (RawStore, error) + // Destory deletes a session. + Destory(*macaron.Context) error + // RegenerateId regenerates a session store from old session ID to new one. + RegenerateId(*macaron.Context) (RawStore, error) + // Count counts and returns number of sessions. + Count() int + // GC calls GC to clean expired sessions. + GC() +} + +type store struct { + RawStore + *Manager +} + +var _ Store = &store{} + +// Options represents a struct for specifying configuration options for the session middleware. +type Options struct { + // Name of provider. Default is "memory". + Provider string + // Provider configuration, it's corresponding to provider. + ProviderConfig string + // Cookie name to save session ID. Default is "MacaronSession". + CookieName string + // Cookie path to store. Default is "/". + CookiePath string + // GC interval time in seconds. Default is 3600. + Gclifetime int64 + // Max life time in seconds. Default is whatever GC interval time is. + Maxlifetime int64 + // Use HTTPS only. Default is false. + Secure bool + // Cookie life time. Default is 0. + CookieLifeTime int + // Cookie domain name. Default is empty. + Domain string + // Session ID length. Default is 16. + IDLength int + // Configuration section name. Default is "session". + Section string +} + +func prepareOptions(options []Options) Options { + var opt Options + if len(options) > 0 { + opt = options[0] + } + if len(opt.Section) == 0 { + opt.Section = "session" + } + sec := macaron.Config().Section(opt.Section) + + if len(opt.Provider) == 0 { + opt.Provider = sec.Key("PROVIDER").MustString("memory") + } + if len(opt.ProviderConfig) == 0 { + opt.ProviderConfig = sec.Key("PROVIDER_CONFIG").MustString("data/sessions") + } + if len(opt.CookieName) == 0 { + opt.CookieName = sec.Key("COOKIE_NAME").MustString("MacaronSession") + } + if len(opt.CookiePath) == 0 { + opt.CookiePath = sec.Key("COOKIE_PATH").MustString("/") + } + if opt.Gclifetime == 0 { + opt.Gclifetime = sec.Key("GC_INTERVAL_TIME").MustInt64(3600) + } + if opt.Maxlifetime == 0 { + opt.Maxlifetime = sec.Key("MAX_LIFE_TIME").MustInt64(opt.Gclifetime) + } + if !opt.Secure { + opt.Secure = sec.Key("SECURE").MustBool() + } + if opt.CookieLifeTime == 0 { + opt.CookieLifeTime = sec.Key("COOKIE_LIFE_TIME").MustInt() + } + if len(opt.Domain) == 0 { + opt.Domain = sec.Key("DOMAIN").String() + } + if opt.IDLength == 0 { + opt.IDLength = sec.Key("ID_LENGTH").MustInt(16) + } + + return opt +} + +// Sessioner is a middleware that maps a session.SessionStore service into the Macaron handler chain. +// An single variadic session.Options struct can be optionally provided to configure. +func Sessioner(options ...Options) macaron.Handler { + opt := prepareOptions(options) + manager, err := NewManager(opt.Provider, opt) + if err != nil { + panic(err) + } + go manager.startGC() + + return func(ctx *macaron.Context) { + sess, err := manager.Start(ctx) + if err != nil { + panic("session(start): " + err.Error()) + } + + // Get flash. + vals, _ := url.ParseQuery(ctx.GetCookie("macaron_flash")) + if len(vals) > 0 { + f := &Flash{Values: vals} + f.ErrorMsg = f.Get("error") + f.SuccessMsg = f.Get("success") + f.InfoMsg = f.Get("info") + f.WarningMsg = f.Get("warning") + ctx.Data["Flash"] = f + ctx.SetCookie("macaron_flash", "", -1, opt.CookiePath) + } + + f := &Flash{ctx, url.Values{}, "", "", "", ""} + ctx.Resp.Before(func(macaron.ResponseWriter) { + if flash := f.Encode(); len(flash) > 0 { + ctx.SetCookie("macaron_flash", flash, 0, opt.CookiePath) + } + }) + + ctx.Map(f) + s := store{ + RawStore: sess, + Manager: manager, + } + + ctx.MapTo(s, (*Store)(nil)) + + ctx.Next() + + if err = sess.Release(); err != nil { + panic("session(release): " + err.Error()) + } + } +} + +// Provider is the interface that provides session manipulations. +type Provider interface { + // Init initializes session provider. + Init(gclifetime int64, config string) error + // Read returns raw session store by session ID. + Read(sid string) (RawStore, error) + // Exist returns true if session with given ID exists. + Exist(sid string) bool + // Destory deletes a session by session ID. + Destory(sid string) error + // Regenerate regenerates a session store from old session ID to new one. + Regenerate(oldsid, sid string) (RawStore, error) + // Count counts and returns number of sessions. + Count() int + // GC calls GC to clean expired sessions. + GC() +} + +var providers = make(map[string]Provider) + +// Register registers a provider. +func Register(name string, provider Provider) { + if provider == nil { + panic("session: cannot register provider with nil value") + } + if _, dup := providers[name]; dup { + panic(fmt.Errorf("session: cannot register provider '%s' twice", name)) + } + providers[name] = provider +} + +// _____ +// / \ _____ ____ _____ ____ ___________ +// / \ / \\__ \ / \\__ \ / ___\_/ __ \_ __ \ +// / Y \/ __ \| | \/ __ \_/ /_/ > ___/| | \/ +// \____|__ (____ /___| (____ /\___ / \___ >__| +// \/ \/ \/ \//_____/ \/ + +// Manager represents a struct that contains session provider and its configuration. +type Manager struct { + provider Provider + opt Options +} + +// NewManager creates and returns a new session manager by given provider name and configuration. +// It panics when given provider isn't registered. +func NewManager(name string, opt Options) (*Manager, error) { + p, ok := providers[name] + if !ok { + return nil, fmt.Errorf("session: unknown provider '%s'(forgotten import?)", name) + } + return &Manager{p, opt}, p.Init(opt.Maxlifetime, opt.ProviderConfig) +} + +// sessionId generates a new session ID with rand string, unix nano time, remote addr by hash function. +func (m *Manager) sessionId() string { + return hex.EncodeToString(generateRandomKey(m.opt.IDLength / 2)) +} + +// Start starts a session by generating new one +// or retrieve existence one by reading session ID from HTTP request if it's valid. +func (m *Manager) Start(ctx *macaron.Context) (RawStore, error) { + sid := ctx.GetCookie(m.opt.CookieName) + if len(sid) > 0 && m.provider.Exist(sid) { + return m.provider.Read(sid) + } + + sid = m.sessionId() + sess, err := m.provider.Read(sid) + if err != nil { + return nil, err + } + + cookie := &http.Cookie{ + Name: m.opt.CookieName, + Value: sid, + Path: m.opt.CookiePath, + HttpOnly: true, + Secure: m.opt.Secure, + Domain: m.opt.Domain, + } + if m.opt.CookieLifeTime >= 0 { + cookie.MaxAge = m.opt.CookieLifeTime + } + http.SetCookie(ctx.Resp, cookie) + ctx.Req.AddCookie(cookie) + return sess, nil +} + +// Read returns raw session store by session ID. +func (m *Manager) Read(sid string) (RawStore, error) { + return m.provider.Read(sid) +} + +// Destory deletes a session by given ID. +func (m *Manager) Destory(ctx *macaron.Context) error { + sid := ctx.GetCookie(m.opt.CookieName) + if len(sid) == 0 { + return nil + } + + if err := m.provider.Destory(sid); err != nil { + return err + } + cookie := &http.Cookie{ + Name: m.opt.CookieName, + Path: m.opt.CookiePath, + HttpOnly: true, + Expires: time.Now(), + MaxAge: -1, + } + http.SetCookie(ctx.Resp, cookie) + return nil +} + +// RegenerateId regenerates a session store from old session ID to new one. +func (m *Manager) RegenerateId(ctx *macaron.Context) (sess RawStore, err error) { + sid := m.sessionId() + oldsid := ctx.GetCookie(m.opt.CookieName) + sess, err = m.provider.Regenerate(oldsid, sid) + if err != nil { + return nil, err + } + ck := &http.Cookie{ + Name: m.opt.CookieName, + Value: sid, + Path: m.opt.CookiePath, + HttpOnly: true, + Secure: m.opt.Secure, + Domain: m.opt.Domain, + } + if m.opt.CookieLifeTime >= 0 { + ck.MaxAge = m.opt.CookieLifeTime + } + http.SetCookie(ctx.Resp, ck) + ctx.Req.AddCookie(ck) + return sess, nil +} + +// Count counts and returns number of sessions. +func (m *Manager) Count() int { + return m.provider.Count() +} + +// GC starts GC job in a certain period. +func (m *Manager) GC() { + m.provider.GC() +} + +// startGC starts GC job in a certain period. +func (m *Manager) startGC() { + m.GC() + time.AfterFunc(time.Duration(m.opt.Gclifetime)*time.Second, func() { m.startGC() }) +} + +// SetSecure indicates whether to set cookie with HTTPS or not. +func (m *Manager) SetSecure(secure bool) { + m.opt.Secure = secure +} + +// ___________.____ _____ _________ ___ ___ +// \_ _____/| | / _ \ / _____// | \ +// | __) | | / /_\ \ \_____ \/ ~ \ +// | \ | |___/ | \/ \ Y / +// \___ / |_______ \____|__ /_______ /\___|_ / +// \/ \/ \/ \/ \/ + +type Flash struct { + ctx *macaron.Context + url.Values + ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string +} + +func (f *Flash) set(name, msg string, current ...bool) { + isShow := false + if (len(current) == 0 && macaron.FlashNow) || + (len(current) > 0 && current[0]) { + isShow = true + } + + if isShow { + f.ctx.Data["Flash"] = f + } else { + f.Set(name, msg) + } +} + +func (f *Flash) Error(msg string, current ...bool) { + f.ErrorMsg = msg + f.set("error", msg, current...) +} + +func (f *Flash) Warning(msg string, current ...bool) { + f.WarningMsg = msg + f.set("warning", msg, current...) +} + +func (f *Flash) Info(msg string, current ...bool) { + f.InfoMsg = msg + f.set("info", msg, current...) +} + +func (f *Flash) Success(msg string, current ...bool) { + f.SuccessMsg = msg + f.set("success", msg, current...) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/session_test.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/session_test.go new file mode 100644 index 0000000..82efc27 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/session_test.go @@ -0,0 +1,200 @@ +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/Unknwon/macaron" + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Version(t *testing.T) { + Convey("Check package version", t, func() { + So(Version(), ShouldEqual, _VERSION) + }) +} + +func Test_Sessioner(t *testing.T) { + Convey("Use session middleware", t, func() { + m := macaron.New() + m.Use(Sessioner()) + m.Get("/", func() {}) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) + + Convey("Register invalid provider", t, func() { + Convey("Provider not exists", func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + + m := macaron.New() + m.Use(Sessioner(Options{ + Provider: "fake", + })) + }) + + Convey("Provider value is nil", func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + + Register("fake", nil) + }) + + Convey("Register twice", func() { + defer func() { + So(recover(), ShouldNotBeNil) + }() + + Register("memory", &MemProvider{}) + }) + }) +} + +func testProvider(opt Options) { + Convey("Basic operation", func() { + m := macaron.New() + m.Use(Sessioner(opt)) + + m.Get("/", func(ctx *macaron.Context, sess Store) { + sess.Set("uname", "unknwon") + }) + m.Get("/reg", func(ctx *macaron.Context, sess Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := raw.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + }) + m.Get("/get", func(ctx *macaron.Context, sess Store) { + sid := sess.ID() + So(sid, ShouldNotBeEmpty) + + raw, err := sess.Read(sid) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Delete("uname"), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + So(sess.Destory(ctx), ShouldBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + cookie := resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/reg", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + + cookie = resp.Header().Get("Set-Cookie") + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", cookie) + m.ServeHTTP(resp, req) + }) + + Convey("Regenrate empty session", func() { + m := macaron.New() + m.Use(Sessioner(opt)) + m.Get("/", func(ctx *macaron.Context, sess Store) { + raw, err := sess.RegenerateId(ctx) + So(err, ShouldBeNil) + So(raw, ShouldNotBeNil) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf486; Path=/;") + m.ServeHTTP(resp, req) + }) + + Convey("GC session", func() { + m := macaron.New() + opt2 := opt + opt2.Gclifetime = 1 + m.Use(Sessioner(opt2)) + + m.Get("/", func(sess Store) { + sess.Set("uname", "unknwon") + So(sess.ID(), ShouldNotBeEmpty) + uname := sess.Get("uname") + So(uname, ShouldNotBeNil) + So(uname, ShouldEqual, "unknwon") + + So(sess.Flush(), ShouldBeNil) + So(sess.Get("uname"), ShouldBeNil) + + time.Sleep(2 * time.Second) + sess.GC() + So(sess.Count(), ShouldEqual, 0) + }) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + }) +} + +func Test_Flash(t *testing.T) { + Convey("Test flash", t, func() { + m := macaron.New() + m.Use(Sessioner()) + m.Get("/set", func(f *Flash) string { + f.Success("success") + f.Error("error") + f.Warning("warning") + f.Info("info") + return "" + }) + m.Get("/get", func() {}) + + resp := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/set", nil) + So(err, ShouldBeNil) + m.ServeHTTP(resp, req) + + resp = httptest.NewRecorder() + req, err = http.NewRequest("GET", "/get", nil) + So(err, ShouldBeNil) + req.Header.Set("Cookie", "macaron_flash=error%3Derror%26info%3Dinfo%26success%3Dsuccess%26warning%3Dwarning; Path=/") + m.ServeHTTP(resp, req) + }) +} diff --git a/Godeps/_workspace/src/github.com/macaron-contrib/session/utils.go b/Godeps/_workspace/src/github.com/macaron-contrib/session/utils.go new file mode 100644 index 0000000..6c9ea49 --- /dev/null +++ b/Godeps/_workspace/src/github.com/macaron-contrib/session/utils.go @@ -0,0 +1,49 @@ +// Copyright 2013 Beego Authors +// Copyright 2014 Unknwon +// +// 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 session + +import ( + "bytes" + "crypto/rand" + "encoding/gob" + "io" + + "github.com/Unknwon/com" +) + +func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) { + for _, v := range obj { + gob.Register(v) + } + buf := bytes.NewBuffer(nil) + err := gob.NewEncoder(buf).Encode(obj) + return buf.Bytes(), err +} + +func DecodeGob(encoded []byte) (out map[interface{}]interface{}, err error) { + buf := bytes.NewBuffer(encoded) + err = gob.NewDecoder(buf).Decode(&out) + return out, err +} + +// generateRandomKey creates a random key with the given strength. +func generateRandomKey(strength int) []byte { + k := make([]byte, strength) + if n, err := io.ReadFull(rand.Reader, k); n != strength || err != nil { + return com.RandomCreateBytes(strength) + } + return k +} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/.travis.yml b/Godeps/_workspace/src/gopkg.in/bufio.v1/.travis.yml new file mode 100644 index 0000000..ccca6bb --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/.travis.yml @@ -0,0 +1,11 @@ +language: go + +go: + - 1.0 + - 1.1 + - 1.2 + - tip + +install: + - go get launchpad.net/gocheck + - go get gopkg.in/bufio.v1 diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/LICENSE b/Godeps/_workspace/src/gopkg.in/bufio.v1/LICENSE new file mode 100644 index 0000000..07a316c --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2013 The bufio Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/Makefile b/Godeps/_workspace/src/gopkg.in/bufio.v1/Makefile new file mode 100644 index 0000000..038ed47 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/Makefile @@ -0,0 +1,2 @@ +all: + go test gopkg.in/bufio.v1 diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/README.md b/Godeps/_workspace/src/gopkg.in/bufio.v1/README.md new file mode 100644 index 0000000..bfb85ee --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/README.md @@ -0,0 +1,4 @@ +bufio +===== + +This is a fork of the http://golang.org/pkg/bufio/ package. It adds `ReadN` method that allows reading next `n` bytes from the internal buffer without allocating intermediate buffer. This method works just like the [Buffer.Next](http://golang.org/pkg/bytes/#Buffer.Next) method, but has slightly different signature. diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer.go new file mode 100644 index 0000000..8b91560 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer.go @@ -0,0 +1,413 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package bufio + +// Simple byte buffer for marshaling data. + +import ( + "bytes" + "errors" + "io" + "unicode/utf8" +) + +// A Buffer is a variable-sized buffer of bytes with Read and Write methods. +// The zero value for Buffer is an empty buffer ready to use. +type Buffer struct { + buf []byte // contents are the bytes buf[off : len(buf)] + off int // read at &buf[off], write at &buf[len(buf)] + runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune + bootstrap [64]byte // memory to hold first slice; helps small buffers (Printf) avoid allocation. + lastRead readOp // last read operation, so that Unread* can work correctly. +} + +// The readOp constants describe the last action performed on +// the buffer, so that UnreadRune and UnreadByte can +// check for invalid usage. +type readOp int + +const ( + opInvalid readOp = iota // Non-read operation. + opReadRune // Read rune. + opRead // Any other read operation. +) + +// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer. +var ErrTooLarge = errors.New("bytes.Buffer: too large") + +// Bytes returns a slice of the contents of the unread portion of the buffer; +// len(b.Bytes()) == b.Len(). If the caller changes the contents of the +// returned slice, the contents of the buffer will change provided there +// are no intervening method calls on the Buffer. +func (b *Buffer) Bytes() []byte { return b.buf[b.off:] } + +// String returns the contents of the unread portion of the buffer +// as a string. If the Buffer is a nil pointer, it returns "". +func (b *Buffer) String() string { + if b == nil { + // Special case, useful in debugging. + return "" + } + return string(b.buf[b.off:]) +} + +// Len returns the number of bytes of the unread portion of the buffer; +// b.Len() == len(b.Bytes()). +func (b *Buffer) Len() int { return len(b.buf) - b.off } + +// Truncate discards all but the first n unread bytes from the buffer. +// It panics if n is negative or greater than the length of the buffer. +func (b *Buffer) Truncate(n int) { + b.lastRead = opInvalid + switch { + case n < 0 || n > b.Len(): + panic("bytes.Buffer: truncation out of range") + case n == 0: + // Reuse buffer space. + b.off = 0 + } + b.buf = b.buf[0 : b.off+n] +} + +// Reset resets the buffer so it has no content. +// b.Reset() is the same as b.Truncate(0). +func (b *Buffer) Reset() { b.Truncate(0) } + +// grow grows the buffer to guarantee space for n more bytes. +// It returns the index where bytes should be written. +// If the buffer can't grow it will panic with ErrTooLarge. +func (b *Buffer) grow(n int) int { + m := b.Len() + // If buffer is empty, reset to recover space. + if m == 0 && b.off != 0 { + b.Truncate(0) + } + if len(b.buf)+n > cap(b.buf) { + var buf []byte + if b.buf == nil && n <= len(b.bootstrap) { + buf = b.bootstrap[0:] + } else if m+n <= cap(b.buf)/2 { + // We can slide things down instead of allocating a new + // slice. We only need m+n <= cap(b.buf) to slide, but + // we instead let capacity get twice as large so we + // don't spend all our time copying. + copy(b.buf[:], b.buf[b.off:]) + buf = b.buf[:m] + } else { + // not enough space anywhere + buf = makeSlice(2*cap(b.buf) + n) + copy(buf, b.buf[b.off:]) + } + b.buf = buf + b.off = 0 + } + b.buf = b.buf[0 : b.off+m+n] + return b.off + m +} + +// Grow grows the buffer's capacity, if necessary, to guarantee space for +// another n bytes. After Grow(n), at least n bytes can be written to the +// buffer without another allocation. +// If n is negative, Grow will panic. +// If the buffer can't grow it will panic with ErrTooLarge. +func (b *Buffer) Grow(n int) { + if n < 0 { + panic("bytes.Buffer.Grow: negative count") + } + m := b.grow(n) + b.buf = b.buf[0:m] +} + +// Write appends the contents of p to the buffer, growing the buffer as +// needed. The return value n is the length of p; err is always nil. If the +// buffer becomes too large, Write will panic with ErrTooLarge. +func (b *Buffer) Write(p []byte) (n int, err error) { + b.lastRead = opInvalid + m := b.grow(len(p)) + return copy(b.buf[m:], p), nil +} + +// WriteString appends the contents of s to the buffer, growing the buffer as +// needed. The return value n is the length of s; err is always nil. If the +// buffer becomes too large, WriteString will panic with ErrTooLarge. +func (b *Buffer) WriteString(s string) (n int, err error) { + b.lastRead = opInvalid + m := b.grow(len(s)) + return copy(b.buf[m:], s), nil +} + +// MinRead is the minimum slice size passed to a Read call by +// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond +// what is required to hold the contents of r, ReadFrom will not grow the +// underlying buffer. +const MinRead = 512 + +// ReadFrom reads data from r until EOF and appends it to the buffer, growing +// the buffer as needed. The return value n is the number of bytes read. Any +// error except io.EOF encountered during the read is also returned. If the +// buffer becomes too large, ReadFrom will panic with ErrTooLarge. +func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) { + b.lastRead = opInvalid + // If buffer is empty, reset to recover space. + if b.off >= len(b.buf) { + b.Truncate(0) + } + for { + if free := cap(b.buf) - len(b.buf); free < MinRead { + // not enough space at end + newBuf := b.buf + if b.off+free < MinRead { + // not enough space using beginning of buffer; + // double buffer capacity + newBuf = makeSlice(2*cap(b.buf) + MinRead) + } + copy(newBuf, b.buf[b.off:]) + b.buf = newBuf[:len(b.buf)-b.off] + b.off = 0 + } + m, e := r.Read(b.buf[len(b.buf):cap(b.buf)]) + b.buf = b.buf[0 : len(b.buf)+m] + n += int64(m) + if e == io.EOF { + break + } + if e != nil { + return n, e + } + } + return n, nil // err is EOF, so return nil explicitly +} + +// makeSlice allocates a slice of size n. If the allocation fails, it panics +// with ErrTooLarge. +func makeSlice(n int) []byte { + // If the make fails, give a known error. + defer func() { + if recover() != nil { + panic(ErrTooLarge) + } + }() + return make([]byte, n) +} + +// WriteTo writes data to w until the buffer is drained or an error occurs. +// The return value n is the number of bytes written; it always fits into an +// int, but it is int64 to match the io.WriterTo interface. Any error +// encountered during the write is also returned. +func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) { + b.lastRead = opInvalid + if b.off < len(b.buf) { + nBytes := b.Len() + m, e := w.Write(b.buf[b.off:]) + if m > nBytes { + panic("bytes.Buffer.WriteTo: invalid Write count") + } + b.off += m + n = int64(m) + if e != nil { + return n, e + } + // all bytes should have been written, by definition of + // Write method in io.Writer + if m != nBytes { + return n, io.ErrShortWrite + } + } + // Buffer is now empty; reset. + b.Truncate(0) + return +} + +// WriteByte appends the byte c to the buffer, growing the buffer as needed. +// The returned error is always nil, but is included to match bufio.Writer's +// WriteByte. If the buffer becomes too large, WriteByte will panic with +// ErrTooLarge. +func (b *Buffer) WriteByte(c byte) error { + b.lastRead = opInvalid + m := b.grow(1) + b.buf[m] = c + return nil +} + +// WriteRune appends the UTF-8 encoding of Unicode code point r to the +// buffer, returning its length and an error, which is always nil but is +// included to match bufio.Writer's WriteRune. The buffer is grown as needed; +// if it becomes too large, WriteRune will panic with ErrTooLarge. +func (b *Buffer) WriteRune(r rune) (n int, err error) { + if r < utf8.RuneSelf { + b.WriteByte(byte(r)) + return 1, nil + } + n = utf8.EncodeRune(b.runeBytes[0:], r) + b.Write(b.runeBytes[0:n]) + return n, nil +} + +// Read reads the next len(p) bytes from the buffer or until the buffer +// is drained. The return value n is the number of bytes read. If the +// buffer has no data to return, err is io.EOF (unless len(p) is zero); +// otherwise it is nil. +func (b *Buffer) Read(p []byte) (n int, err error) { + b.lastRead = opInvalid + if b.off >= len(b.buf) { + // Buffer is empty, reset to recover space. + b.Truncate(0) + if len(p) == 0 { + return + } + return 0, io.EOF + } + n = copy(p, b.buf[b.off:]) + b.off += n + if n > 0 { + b.lastRead = opRead + } + return +} + +// Next returns a slice containing the next n bytes from the buffer, +// advancing the buffer as if the bytes had been returned by Read. +// If there are fewer than n bytes in the buffer, Next returns the entire buffer. +// The slice is only valid until the next call to a read or write method. +func (b *Buffer) Next(n int) []byte { + b.lastRead = opInvalid + m := b.Len() + if n > m { + n = m + } + data := b.buf[b.off : b.off+n] + b.off += n + if n > 0 { + b.lastRead = opRead + } + return data +} + +// ReadByte reads and returns the next byte from the buffer. +// If no byte is available, it returns error io.EOF. +func (b *Buffer) ReadByte() (c byte, err error) { + b.lastRead = opInvalid + if b.off >= len(b.buf) { + // Buffer is empty, reset to recover space. + b.Truncate(0) + return 0, io.EOF + } + c = b.buf[b.off] + b.off++ + b.lastRead = opRead + return c, nil +} + +// ReadRune reads and returns the next UTF-8-encoded +// Unicode code point from the buffer. +// If no bytes are available, the error returned is io.EOF. +// If the bytes are an erroneous UTF-8 encoding, it +// consumes one byte and returns U+FFFD, 1. +func (b *Buffer) ReadRune() (r rune, size int, err error) { + b.lastRead = opInvalid + if b.off >= len(b.buf) { + // Buffer is empty, reset to recover space. + b.Truncate(0) + return 0, 0, io.EOF + } + b.lastRead = opReadRune + c := b.buf[b.off] + if c < utf8.RuneSelf { + b.off++ + return rune(c), 1, nil + } + r, n := utf8.DecodeRune(b.buf[b.off:]) + b.off += n + return r, n, nil +} + +// UnreadRune unreads the last rune returned by ReadRune. +// If the most recent read or write operation on the buffer was +// not a ReadRune, UnreadRune returns an error. (In this regard +// it is stricter than UnreadByte, which will unread the last byte +// from any read operation.) +func (b *Buffer) UnreadRune() error { + if b.lastRead != opReadRune { + return errors.New("bytes.Buffer: UnreadRune: previous operation was not ReadRune") + } + b.lastRead = opInvalid + if b.off > 0 { + _, n := utf8.DecodeLastRune(b.buf[0:b.off]) + b.off -= n + } + return nil +} + +// UnreadByte unreads the last byte returned by the most recent +// read operation. If write has happened since the last read, UnreadByte +// returns an error. +func (b *Buffer) UnreadByte() error { + if b.lastRead != opReadRune && b.lastRead != opRead { + return errors.New("bytes.Buffer: UnreadByte: previous operation was not a read") + } + b.lastRead = opInvalid + if b.off > 0 { + b.off-- + } + return nil +} + +// ReadBytes reads until the first occurrence of delim in the input, +// returning a slice containing the data up to and including the delimiter. +// If ReadBytes encounters an error before finding a delimiter, +// it returns the data read before the error and the error itself (often io.EOF). +// ReadBytes returns err != nil if and only if the returned data does not end in +// delim. +func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) { + slice, err := b.readSlice(delim) + // return a copy of slice. The buffer's backing array may + // be overwritten by later calls. + line = append(line, slice...) + return +} + +// readSlice is like ReadBytes but returns a reference to internal buffer data. +func (b *Buffer) readSlice(delim byte) (line []byte, err error) { + i := bytes.IndexByte(b.buf[b.off:], delim) + end := b.off + i + 1 + if i < 0 { + end = len(b.buf) + err = io.EOF + } + line = b.buf[b.off:end] + b.off = end + b.lastRead = opRead + return line, err +} + +// ReadString reads until the first occurrence of delim in the input, +// returning a string containing the data up to and including the delimiter. +// If ReadString encounters an error before finding a delimiter, +// it returns the data read before the error and the error itself (often io.EOF). +// ReadString returns err != nil if and only if the returned data does not end +// in delim. +func (b *Buffer) ReadString(delim byte) (line string, err error) { + slice, err := b.readSlice(delim) + return string(slice), err +} + +// NewBuffer creates and initializes a new Buffer using buf as its initial +// contents. It is intended to prepare a Buffer to read existing data. It +// can also be used to size the internal buffer for writing. To do that, +// buf should have the desired capacity but a length of zero. +// +// In most cases, new(Buffer) (or just declaring a Buffer variable) is +// sufficient to initialize a Buffer. +func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} } + +// NewBufferString creates and initializes a new Buffer using string s as its +// initial contents. It is intended to prepare a buffer to read an existing +// string. +// +// In most cases, new(Buffer) (or just declaring a Buffer variable) is +// sufficient to initialize a Buffer. +func NewBufferString(s string) *Buffer { + return &Buffer{buf: []byte(s)} +} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer_test.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer_test.go new file mode 100644 index 0000000..ca1ac21 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer_test.go @@ -0,0 +1,527 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package bufio + +import ( + "bytes" + "io" + "math/rand" + "runtime" + "testing" + "unicode/utf8" +) + +const N = 10000 // make this bigger for a larger (and slower) test +var data string // test data for write tests +var testBytes []byte // test data; same as data but as a slice. + +func init() { + testBytes = make([]byte, N) + for i := 0; i < N; i++ { + testBytes[i] = 'a' + byte(i%26) + } + data = string(testBytes) +} + +// Verify that contents of buf match the string s. +func check(t *testing.T, testname string, buf *Buffer, s string) { + bytes := buf.Bytes() + str := buf.String() + if buf.Len() != len(bytes) { + t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes)) + } + + if buf.Len() != len(str) { + t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str)) + } + + if buf.Len() != len(s) { + t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s)) + } + + if string(bytes) != s { + t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s) + } +} + +// Fill buf through n writes of string fus. +// The initial contents of buf corresponds to the string s; +// the result is the final contents of buf returned as a string. +func fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string { + check(t, testname+" (fill 1)", buf, s) + for ; n > 0; n-- { + m, err := buf.WriteString(fus) + if m != len(fus) { + t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fus)) + } + if err != nil { + t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err) + } + s += fus + check(t, testname+" (fill 4)", buf, s) + } + return s +} + +// Fill buf through n writes of byte slice fub. +// The initial contents of buf corresponds to the string s; +// the result is the final contents of buf returned as a string. +func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string { + check(t, testname+" (fill 1)", buf, s) + for ; n > 0; n-- { + m, err := buf.Write(fub) + if m != len(fub) { + t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub)) + } + if err != nil { + t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err) + } + s += string(fub) + check(t, testname+" (fill 4)", buf, s) + } + return s +} + +func TestNewBuffer(t *testing.T) { + buf := NewBuffer(testBytes) + check(t, "NewBuffer", buf, data) +} + +func TestNewBufferString(t *testing.T) { + buf := NewBufferString(data) + check(t, "NewBufferString", buf, data) +} + +// Empty buf through repeated reads into fub. +// The initial contents of buf corresponds to the string s. +func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) { + check(t, testname+" (empty 1)", buf, s) + + for { + n, err := buf.Read(fub) + if n == 0 { + break + } + if err != nil { + t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err) + } + s = s[n:] + check(t, testname+" (empty 3)", buf, s) + } + + check(t, testname+" (empty 4)", buf, "") +} + +func TestBasicOperations(t *testing.T) { + var buf Buffer + + for i := 0; i < 5; i++ { + check(t, "TestBasicOperations (1)", &buf, "") + + buf.Reset() + check(t, "TestBasicOperations (2)", &buf, "") + + buf.Truncate(0) + check(t, "TestBasicOperations (3)", &buf, "") + + n, err := buf.Write([]byte(data[0:1])) + if n != 1 { + t.Errorf("wrote 1 byte, but n == %d", n) + } + if err != nil { + t.Errorf("err should always be nil, but err == %s", err) + } + check(t, "TestBasicOperations (4)", &buf, "a") + + buf.WriteByte(data[1]) + check(t, "TestBasicOperations (5)", &buf, "ab") + + n, err = buf.Write([]byte(data[2:26])) + if n != 24 { + t.Errorf("wrote 25 bytes, but n == %d", n) + } + check(t, "TestBasicOperations (6)", &buf, string(data[0:26])) + + buf.Truncate(26) + check(t, "TestBasicOperations (7)", &buf, string(data[0:26])) + + buf.Truncate(20) + check(t, "TestBasicOperations (8)", &buf, string(data[0:20])) + + empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5)) + empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100)) + + buf.WriteByte(data[1]) + c, err := buf.ReadByte() + if err != nil { + t.Error("ReadByte unexpected eof") + } + if c != data[1] { + t.Errorf("ReadByte wrong value c=%v", c) + } + c, err = buf.ReadByte() + if err == nil { + t.Error("ReadByte unexpected not eof") + } + } +} + +func TestLargeStringWrites(t *testing.T) { + var buf Buffer + limit := 30 + if testing.Short() { + limit = 9 + } + for i := 3; i < limit; i += 3 { + s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, data) + empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(data)/i)) + } + check(t, "TestLargeStringWrites (3)", &buf, "") +} + +func TestLargeByteWrites(t *testing.T) { + var buf Buffer + limit := 30 + if testing.Short() { + limit = 9 + } + for i := 3; i < limit; i += 3 { + s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes) + empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i)) + } + check(t, "TestLargeByteWrites (3)", &buf, "") +} + +func TestLargeStringReads(t *testing.T) { + var buf Buffer + for i := 3; i < 30; i += 3 { + s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0:len(data)/i]) + empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data))) + } + check(t, "TestLargeStringReads (3)", &buf, "") +} + +func TestLargeByteReads(t *testing.T) { + var buf Buffer + for i := 3; i < 30; i += 3 { + s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) + empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data))) + } + check(t, "TestLargeByteReads (3)", &buf, "") +} + +func TestMixedReadsAndWrites(t *testing.T) { + var buf Buffer + s := "" + for i := 0; i < 50; i++ { + wlen := rand.Intn(len(data)) + if i%2 == 0 { + s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen]) + } else { + s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen]) + } + + rlen := rand.Intn(len(data)) + fub := make([]byte, rlen) + n, _ := buf.Read(fub) + s = s[n:] + } + empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len())) +} + +func TestNil(t *testing.T) { + var b *Buffer + if b.String() != "" { + t.Errorf("expected ; got %q", b.String()) + } +} + +func TestReadFrom(t *testing.T) { + var buf Buffer + for i := 3; i < 30; i += 3 { + s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) + var b Buffer + b.ReadFrom(&buf) + empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data))) + } +} + +func TestWriteTo(t *testing.T) { + var buf Buffer + for i := 3; i < 30; i += 3 { + s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) + var b Buffer + buf.WriteTo(&b) + empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data))) + } +} + +func TestRuneIO(t *testing.T) { + const NRune = 1000 + // Built a test slice while we write the data + b := make([]byte, utf8.UTFMax*NRune) + var buf Buffer + n := 0 + for r := rune(0); r < NRune; r++ { + size := utf8.EncodeRune(b[n:], r) + nbytes, err := buf.WriteRune(r) + if err != nil { + t.Fatalf("WriteRune(%U) error: %s", r, err) + } + if nbytes != size { + t.Fatalf("WriteRune(%U) expected %d, got %d", r, size, nbytes) + } + n += size + } + b = b[0:n] + + // Check the resulting bytes + if !bytes.Equal(buf.Bytes(), b) { + t.Fatalf("incorrect result from WriteRune: %q not %q", buf.Bytes(), b) + } + + p := make([]byte, utf8.UTFMax) + // Read it back with ReadRune + for r := rune(0); r < NRune; r++ { + size := utf8.EncodeRune(p, r) + nr, nbytes, err := buf.ReadRune() + if nr != r || nbytes != size || err != nil { + t.Fatalf("ReadRune(%U) got %U,%d not %U,%d (err=%s)", r, nr, nbytes, r, size, err) + } + } + + // Check that UnreadRune works + buf.Reset() + buf.Write(b) + for r := rune(0); r < NRune; r++ { + r1, size, _ := buf.ReadRune() + if err := buf.UnreadRune(); err != nil { + t.Fatalf("UnreadRune(%U) got error %q", r, err) + } + r2, nbytes, err := buf.ReadRune() + if r1 != r2 || r1 != r || nbytes != size || err != nil { + t.Fatalf("ReadRune(%U) after UnreadRune got %U,%d not %U,%d (err=%s)", r, r2, nbytes, r, size, err) + } + } +} + +func TestNext(t *testing.T) { + b := []byte{0, 1, 2, 3, 4} + tmp := make([]byte, 5) + for i := 0; i <= 5; i++ { + for j := i; j <= 5; j++ { + for k := 0; k <= 6; k++ { + // 0 <= i <= j <= 5; 0 <= k <= 6 + // Check that if we start with a buffer + // of length j at offset i and ask for + // Next(k), we get the right bytes. + buf := NewBuffer(b[0:j]) + n, _ := buf.Read(tmp[0:i]) + if n != i { + t.Fatalf("Read %d returned %d", i, n) + } + bb := buf.Next(k) + want := k + if want > j-i { + want = j - i + } + if len(bb) != want { + t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb)) + } + for l, v := range bb { + if v != byte(l+i) { + t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i) + } + } + } + } + } +} + +var readBytesTests = []struct { + buffer string + delim byte + expected []string + err error +}{ + {"", 0, []string{""}, io.EOF}, + {"a\x00", 0, []string{"a\x00"}, nil}, + {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil}, + {"hello\x01world", 1, []string{"hello\x01"}, nil}, + {"foo\nbar", 0, []string{"foo\nbar"}, io.EOF}, + {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil}, + {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io.EOF}, +} + +func TestReadBytes(t *testing.T) { + for _, test := range readBytesTests { + buf := NewBufferString(test.buffer) + var err error + for _, expected := range test.expected { + var bytes []byte + bytes, err = buf.ReadBytes(test.delim) + if string(bytes) != expected { + t.Errorf("expected %q, got %q", expected, bytes) + } + if err != nil { + break + } + } + if err != test.err { + t.Errorf("expected error %v, got %v", test.err, err) + } + } +} + +func TestReadString(t *testing.T) { + for _, test := range readBytesTests { + buf := NewBufferString(test.buffer) + var err error + for _, expected := range test.expected { + var s string + s, err = buf.ReadString(test.delim) + if s != expected { + t.Errorf("expected %q, got %q", expected, s) + } + if err != nil { + break + } + } + if err != test.err { + t.Errorf("expected error %v, got %v", test.err, err) + } + } +} + +func BenchmarkReadString(b *testing.B) { + const n = 32 << 10 + + data := make([]byte, n) + data[n-1] = 'x' + b.SetBytes(int64(n)) + for i := 0; i < b.N; i++ { + buf := NewBuffer(data) + _, err := buf.ReadString('x') + if err != nil { + b.Fatal(err) + } + } +} + +func TestGrow(t *testing.T) { + x := []byte{'x'} + y := []byte{'y'} + tmp := make([]byte, 72) + for _, startLen := range []int{0, 100, 1000, 10000, 100000} { + xBytes := bytes.Repeat(x, startLen) + for _, growLen := range []int{0, 100, 1000, 10000, 100000} { + buf := NewBuffer(xBytes) + // If we read, this affects buf.off, which is good to test. + readBytes, _ := buf.Read(tmp) + buf.Grow(growLen) + yBytes := bytes.Repeat(y, growLen) + // Check no allocation occurs in write, as long as we're single-threaded. + var m1, m2 runtime.MemStats + runtime.ReadMemStats(&m1) + buf.Write(yBytes) + runtime.ReadMemStats(&m2) + if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs { + t.Errorf("allocation occurred during write") + } + // Check that buffer has correct data. + if !bytes.Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) { + t.Errorf("bad initial data at %d %d", startLen, growLen) + } + if !bytes.Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) { + t.Errorf("bad written data at %d %d", startLen, growLen) + } + } + } +} + +// Was a bug: used to give EOF reading empty slice at EOF. +func TestReadEmptyAtEOF(t *testing.T) { + b := new(Buffer) + slice := make([]byte, 0) + n, err := b.Read(slice) + if err != nil { + t.Errorf("read error: %v", err) + } + if n != 0 { + t.Errorf("wrong count; got %d want 0", n) + } +} + +func TestBufferUnreadByte(t *testing.T) { + b := new(Buffer) + b.WriteString("abcdefghijklmnopqrstuvwxyz") + + _, err := b.ReadBytes('m') + if err != nil { + t.Fatalf("ReadBytes: %v", err) + } + + err = b.UnreadByte() + if err != nil { + t.Fatalf("UnreadByte: %v", err) + } + c, err := b.ReadByte() + if err != nil { + t.Fatalf("ReadByte: %v", err) + } + if c != 'm' { + t.Errorf("ReadByte = %q; want %q", c, 'm') + } +} + +// Tests that we occasionally compact. Issue 5154. +func TestBufferGrowth(t *testing.T) { + var b Buffer + buf := make([]byte, 1024) + b.Write(buf[0:1]) + var cap0 int + for i := 0; i < 5<<10; i++ { + b.Write(buf) + b.Read(buf) + if i == 0 { + cap0 = b.Cap() + } + } + cap1 := b.Cap() + // (*Buffer).grow allows for 2x capacity slop before sliding, + // so set our error threshold at 3x. + if cap1 > cap0*3 { + t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0) + } +} + +// From Issue 5154. +func BenchmarkBufferNotEmptyWriteRead(b *testing.B) { + buf := make([]byte, 1024) + for i := 0; i < b.N; i++ { + var b Buffer + b.Write(buf[0:1]) + for i := 0; i < 5<<10; i++ { + b.Write(buf) + b.Read(buf) + } + } +} + +// Check that we don't compact too often. From Issue 5154. +func BenchmarkBufferFullSmallReads(b *testing.B) { + buf := make([]byte, 1024) + for i := 0; i < b.N; i++ { + var b Buffer + b.Write(buf) + for b.Len()+20 < b.Cap() { + b.Write(buf[:10]) + } + for i := 0; i < 5<<10; i++ { + b.Read(buf[:1]) + b.Write(buf[:1]) + } + } +} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio.go new file mode 100644 index 0000000..8f5cdc0 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio.go @@ -0,0 +1,728 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer +// object, creating another object (Reader or Writer) that also implements +// the interface but provides buffering and some help for textual I/O. +package bufio + +import ( + "bytes" + "errors" + "io" + "unicode/utf8" +) + +const ( + defaultBufSize = 4096 +) + +var ( + ErrInvalidUnreadByte = errors.New("bufio: invalid use of UnreadByte") + ErrInvalidUnreadRune = errors.New("bufio: invalid use of UnreadRune") + ErrBufferFull = errors.New("bufio: buffer full") + ErrNegativeCount = errors.New("bufio: negative count") +) + +// Buffered input. + +// Reader implements buffering for an io.Reader object. +type Reader struct { + buf []byte + rd io.Reader + r, w int + err error + lastByte int + lastRuneSize int +} + +const minReadBufferSize = 16 +const maxConsecutiveEmptyReads = 100 + +// NewReaderSize returns a new Reader whose buffer has at least the specified +// size. If the argument io.Reader is already a Reader with large enough +// size, it returns the underlying Reader. +func NewReaderSize(rd io.Reader, size int) *Reader { + // Is it already a Reader? + b, ok := rd.(*Reader) + if ok && len(b.buf) >= size { + return b + } + if size < minReadBufferSize { + size = minReadBufferSize + } + r := new(Reader) + r.reset(make([]byte, size), rd) + return r +} + +// NewReader returns a new Reader whose buffer has the default size. +func NewReader(rd io.Reader) *Reader { + return NewReaderSize(rd, defaultBufSize) +} + +// Reset discards any buffered data, resets all state, and switches +// the buffered reader to read from r. +func (b *Reader) Reset(r io.Reader) { + b.reset(b.buf, r) +} + +func (b *Reader) reset(buf []byte, r io.Reader) { + *b = Reader{ + buf: buf, + rd: r, + lastByte: -1, + lastRuneSize: -1, + } +} + +var errNegativeRead = errors.New("bufio: reader returned negative count from Read") + +// fill reads a new chunk into the buffer. +func (b *Reader) fill() { + // Slide existing data to beginning. + if b.r > 0 { + copy(b.buf, b.buf[b.r:b.w]) + b.w -= b.r + b.r = 0 + } + + if b.w >= len(b.buf) { + panic("bufio: tried to fill full buffer") + } + + // Read new data: try a limited number of times. + for i := maxConsecutiveEmptyReads; i > 0; i-- { + n, err := b.rd.Read(b.buf[b.w:]) + if n < 0 { + panic(errNegativeRead) + } + b.w += n + if err != nil { + b.err = err + return + } + if n > 0 { + return + } + } + b.err = io.ErrNoProgress +} + +func (b *Reader) readErr() error { + err := b.err + b.err = nil + return err +} + +// Peek returns the next n bytes without advancing the reader. The bytes stop +// being valid at the next read call. If Peek returns fewer than n bytes, it +// also returns an error explaining why the read is short. The error is +// ErrBufferFull if n is larger than b's buffer size. +func (b *Reader) Peek(n int) ([]byte, error) { + if n < 0 { + return nil, ErrNegativeCount + } + if n > len(b.buf) { + return nil, ErrBufferFull + } + // 0 <= n <= len(b.buf) + for b.w-b.r < n && b.err == nil { + b.fill() // b.w-b.r < len(b.buf) => buffer is not full + } + m := b.w - b.r + if m > n { + m = n + } + var err error + if m < n { + err = b.readErr() + if err == nil { + err = ErrBufferFull + } + } + return b.buf[b.r : b.r+m], err +} + +// Read reads data into p. +// It returns the number of bytes read into p. +// It calls Read at most once on the underlying Reader, +// hence n may be less than len(p). +// At EOF, the count will be zero and err will be io.EOF. +func (b *Reader) Read(p []byte) (n int, err error) { + n = len(p) + if n == 0 { + return 0, b.readErr() + } + if b.r == b.w { + if b.err != nil { + return 0, b.readErr() + } + if len(p) >= len(b.buf) { + // Large read, empty buffer. + // Read directly into p to avoid copy. + n, b.err = b.rd.Read(p) + if n < 0 { + panic(errNegativeRead) + } + if n > 0 { + b.lastByte = int(p[n-1]) + b.lastRuneSize = -1 + } + return n, b.readErr() + } + b.fill() // buffer is empty + if b.w == b.r { + return 0, b.readErr() + } + } + + if n > b.w-b.r { + n = b.w - b.r + } + copy(p[0:n], b.buf[b.r:]) + b.r += n + b.lastByte = int(b.buf[b.r-1]) + b.lastRuneSize = -1 + return n, nil +} + +// ReadByte reads and returns a single byte. +// If no byte is available, returns an error. +func (b *Reader) ReadByte() (c byte, err error) { + b.lastRuneSize = -1 + for b.r == b.w { + if b.err != nil { + return 0, b.readErr() + } + b.fill() // buffer is empty + } + c = b.buf[b.r] + b.r++ + b.lastByte = int(c) + return c, nil +} + +// UnreadByte unreads the last byte. Only the most recently read byte can be unread. +func (b *Reader) UnreadByte() error { + if b.lastByte < 0 || b.r == 0 && b.w > 0 { + return ErrInvalidUnreadByte + } + // b.r > 0 || b.w == 0 + if b.r > 0 { + b.r-- + } else { + // b.r == 0 && b.w == 0 + b.w = 1 + } + b.buf[b.r] = byte(b.lastByte) + b.lastByte = -1 + b.lastRuneSize = -1 + return nil +} + +// ReadRune reads a single UTF-8 encoded Unicode character and returns the +// rune and its size in bytes. If the encoded rune is invalid, it consumes one byte +// and returns unicode.ReplacementChar (U+FFFD) with a size of 1. +func (b *Reader) ReadRune() (r rune, size int, err error) { + for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil && b.w-b.r < len(b.buf) { + b.fill() // b.w-b.r < len(buf) => buffer is not full + } + b.lastRuneSize = -1 + if b.r == b.w { + return 0, 0, b.readErr() + } + r, size = rune(b.buf[b.r]), 1 + if r >= 0x80 { + r, size = utf8.DecodeRune(b.buf[b.r:b.w]) + } + b.r += size + b.lastByte = int(b.buf[b.r-1]) + b.lastRuneSize = size + return r, size, nil +} + +// UnreadRune unreads the last rune. If the most recent read operation on +// the buffer was not a ReadRune, UnreadRune returns an error. (In this +// regard it is stricter than UnreadByte, which will unread the last byte +// from any read operation.) +func (b *Reader) UnreadRune() error { + if b.lastRuneSize < 0 || b.r < b.lastRuneSize { + return ErrInvalidUnreadRune + } + b.r -= b.lastRuneSize + b.lastByte = -1 + b.lastRuneSize = -1 + return nil +} + +// Buffered returns the number of bytes that can be read from the current buffer. +func (b *Reader) Buffered() int { return b.w - b.r } + +// ReadSlice reads until the first occurrence of delim in the input, +// returning a slice pointing at the bytes in the buffer. +// The bytes stop being valid at the next read. +// If ReadSlice encounters an error before finding a delimiter, +// it returns all the data in the buffer and the error itself (often io.EOF). +// ReadSlice fails with error ErrBufferFull if the buffer fills without a delim. +// Because the data returned from ReadSlice will be overwritten +// by the next I/O operation, most clients should use +// ReadBytes or ReadString instead. +// ReadSlice returns err != nil if and only if line does not end in delim. +func (b *Reader) ReadSlice(delim byte) (line []byte, err error) { + for { + // Search buffer. + if i := bytes.IndexByte(b.buf[b.r:b.w], delim); i >= 0 { + line = b.buf[b.r : b.r+i+1] + b.r += i + 1 + break + } + + // Pending error? + if b.err != nil { + line = b.buf[b.r:b.w] + b.r = b.w + err = b.readErr() + break + } + + // Buffer full? + if n := b.Buffered(); n >= len(b.buf) { + b.r = b.w + line = b.buf + err = ErrBufferFull + break + } + + b.fill() // buffer is not full + } + + // Handle last byte, if any. + if i := len(line) - 1; i >= 0 { + b.lastByte = int(line[i]) + } + + return +} + +// ReadN tries to read exactly n bytes. +// The bytes stop being valid at the next read call. +// If ReadN encounters an error before reading n bytes, +// it returns all the data in the buffer and the error itself (often io.EOF). +// ReadN fails with error ErrBufferFull if the buffer fills +// without reading N bytes. +// Because the data returned from ReadN will be overwritten +// by the next I/O operation, most clients should use +// ReadBytes or ReadString instead. +func (b *Reader) ReadN(n int) ([]byte, error) { + for b.Buffered() < n { + if b.err != nil { + buf := b.buf[b.r:b.w] + b.r = b.w + return buf, b.readErr() + } + + // Buffer is full? + if b.Buffered() >= len(b.buf) { + b.r = b.w + return b.buf, ErrBufferFull + } + + b.fill() + } + buf := b.buf[b.r : b.r+n] + b.r += n + return buf, nil +} + +// ReadLine is a low-level line-reading primitive. Most callers should use +// ReadBytes('\n') or ReadString('\n') instead or use a Scanner. +// +// ReadLine tries to return a single line, not including the end-of-line bytes. +// If the line was too long for the buffer then isPrefix is set and the +// beginning of the line is returned. The rest of the line will be returned +// from future calls. isPrefix will be false when returning the last fragment +// of the line. The returned buffer is only valid until the next call to +// ReadLine. ReadLine either returns a non-nil line or it returns an error, +// never both. +// +// The text returned from ReadLine does not include the line end ("\r\n" or "\n"). +// No indication or error is given if the input ends without a final line end. +// Calling UnreadByte after ReadLine will always unread the last byte read +// (possibly a character belonging to the line end) even if that byte is not +// part of the line returned by ReadLine. +func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) { + line, err = b.ReadSlice('\n') + if err == ErrBufferFull { + // Handle the case where "\r\n" straddles the buffer. + if len(line) > 0 && line[len(line)-1] == '\r' { + // Put the '\r' back on buf and drop it from line. + // Let the next call to ReadLine check for "\r\n". + if b.r == 0 { + // should be unreachable + panic("bufio: tried to rewind past start of buffer") + } + b.r-- + line = line[:len(line)-1] + } + return line, true, nil + } + + if len(line) == 0 { + if err != nil { + line = nil + } + return + } + err = nil + + if line[len(line)-1] == '\n' { + drop := 1 + if len(line) > 1 && line[len(line)-2] == '\r' { + drop = 2 + } + line = line[:len(line)-drop] + } + return +} + +// ReadBytes reads until the first occurrence of delim in the input, +// returning a slice containing the data up to and including the delimiter. +// If ReadBytes encounters an error before finding a delimiter, +// it returns the data read before the error and the error itself (often io.EOF). +// ReadBytes returns err != nil if and only if the returned data does not end in +// delim. +// For simple uses, a Scanner may be more convenient. +func (b *Reader) ReadBytes(delim byte) (line []byte, err error) { + // Use ReadSlice to look for array, + // accumulating full buffers. + var frag []byte + var full [][]byte + err = nil + + for { + var e error + frag, e = b.ReadSlice(delim) + if e == nil { // got final fragment + break + } + if e != ErrBufferFull { // unexpected error + err = e + break + } + + // Make a copy of the buffer. + buf := make([]byte, len(frag)) + copy(buf, frag) + full = append(full, buf) + } + + // Allocate new buffer to hold the full pieces and the fragment. + n := 0 + for i := range full { + n += len(full[i]) + } + n += len(frag) + + // Copy full pieces and fragment in. + buf := make([]byte, n) + n = 0 + for i := range full { + n += copy(buf[n:], full[i]) + } + copy(buf[n:], frag) + return buf, err +} + +// ReadString reads until the first occurrence of delim in the input, +// returning a string containing the data up to and including the delimiter. +// If ReadString encounters an error before finding a delimiter, +// it returns the data read before the error and the error itself (often io.EOF). +// ReadString returns err != nil if and only if the returned data does not end in +// delim. +// For simple uses, a Scanner may be more convenient. +func (b *Reader) ReadString(delim byte) (line string, err error) { + bytes, err := b.ReadBytes(delim) + line = string(bytes) + return line, err +} + +// WriteTo implements io.WriterTo. +func (b *Reader) WriteTo(w io.Writer) (n int64, err error) { + n, err = b.writeBuf(w) + if err != nil { + return + } + + if r, ok := b.rd.(io.WriterTo); ok { + m, err := r.WriteTo(w) + n += m + return n, err + } + + if w, ok := w.(io.ReaderFrom); ok { + m, err := w.ReadFrom(b.rd) + n += m + return n, err + } + + if b.w-b.r < len(b.buf) { + b.fill() // buffer not full + } + + for b.r < b.w { + // b.r < b.w => buffer is not empty + m, err := b.writeBuf(w) + n += m + if err != nil { + return n, err + } + b.fill() // buffer is empty + } + + if b.err == io.EOF { + b.err = nil + } + + return n, b.readErr() +} + +// writeBuf writes the Reader's buffer to the writer. +func (b *Reader) writeBuf(w io.Writer) (int64, error) { + n, err := w.Write(b.buf[b.r:b.w]) + if n < b.r-b.w { + panic(errors.New("bufio: writer did not write all data")) + } + b.r += n + return int64(n), err +} + +// buffered output + +// Writer implements buffering for an io.Writer object. +// If an error occurs writing to a Writer, no more data will be +// accepted and all subsequent writes will return the error. +// After all data has been written, the client should call the +// Flush method to guarantee all data has been forwarded to +// the underlying io.Writer. +type Writer struct { + err error + buf []byte + n int + wr io.Writer +} + +// NewWriterSize returns a new Writer whose buffer has at least the specified +// size. If the argument io.Writer is already a Writer with large enough +// size, it returns the underlying Writer. +func NewWriterSize(w io.Writer, size int) *Writer { + // Is it already a Writer? + b, ok := w.(*Writer) + if ok && len(b.buf) >= size { + return b + } + if size <= 0 { + size = defaultBufSize + } + return &Writer{ + buf: make([]byte, size), + wr: w, + } +} + +// NewWriter returns a new Writer whose buffer has the default size. +func NewWriter(w io.Writer) *Writer { + return NewWriterSize(w, defaultBufSize) +} + +// Reset discards any unflushed buffered data, clears any error, and +// resets b to write its output to w. +func (b *Writer) Reset(w io.Writer) { + b.err = nil + b.n = 0 + b.wr = w +} + +// Flush writes any buffered data to the underlying io.Writer. +func (b *Writer) Flush() error { + err := b.flush() + return err +} + +func (b *Writer) flush() error { + if b.err != nil { + return b.err + } + if b.n == 0 { + return nil + } + n, err := b.wr.Write(b.buf[0:b.n]) + if n < b.n && err == nil { + err = io.ErrShortWrite + } + if err != nil { + if n > 0 && n < b.n { + copy(b.buf[0:b.n-n], b.buf[n:b.n]) + } + b.n -= n + b.err = err + return err + } + b.n = 0 + return nil +} + +// Available returns how many bytes are unused in the buffer. +func (b *Writer) Available() int { return len(b.buf) - b.n } + +// Buffered returns the number of bytes that have been written into the current buffer. +func (b *Writer) Buffered() int { return b.n } + +// Write writes the contents of p into the buffer. +// It returns the number of bytes written. +// If nn < len(p), it also returns an error explaining +// why the write is short. +func (b *Writer) Write(p []byte) (nn int, err error) { + for len(p) > b.Available() && b.err == nil { + var n int + if b.Buffered() == 0 { + // Large write, empty buffer. + // Write directly from p to avoid copy. + n, b.err = b.wr.Write(p) + } else { + n = copy(b.buf[b.n:], p) + b.n += n + b.flush() + } + nn += n + p = p[n:] + } + if b.err != nil { + return nn, b.err + } + n := copy(b.buf[b.n:], p) + b.n += n + nn += n + return nn, nil +} + +// WriteByte writes a single byte. +func (b *Writer) WriteByte(c byte) error { + if b.err != nil { + return b.err + } + if b.Available() <= 0 && b.flush() != nil { + return b.err + } + b.buf[b.n] = c + b.n++ + return nil +} + +// WriteRune writes a single Unicode code point, returning +// the number of bytes written and any error. +func (b *Writer) WriteRune(r rune) (size int, err error) { + if r < utf8.RuneSelf { + err = b.WriteByte(byte(r)) + if err != nil { + return 0, err + } + return 1, nil + } + if b.err != nil { + return 0, b.err + } + n := b.Available() + if n < utf8.UTFMax { + if b.flush(); b.err != nil { + return 0, b.err + } + n = b.Available() + if n < utf8.UTFMax { + // Can only happen if buffer is silly small. + return b.WriteString(string(r)) + } + } + size = utf8.EncodeRune(b.buf[b.n:], r) + b.n += size + return size, nil +} + +// WriteString writes a string. +// It returns the number of bytes written. +// If the count is less than len(s), it also returns an error explaining +// why the write is short. +func (b *Writer) WriteString(s string) (int, error) { + nn := 0 + for len(s) > b.Available() && b.err == nil { + n := copy(b.buf[b.n:], s) + b.n += n + nn += n + s = s[n:] + b.flush() + } + if b.err != nil { + return nn, b.err + } + n := copy(b.buf[b.n:], s) + b.n += n + nn += n + return nn, nil +} + +// ReadFrom implements io.ReaderFrom. +func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) { + if b.Buffered() == 0 { + if w, ok := b.wr.(io.ReaderFrom); ok { + return w.ReadFrom(r) + } + } + var m int + for { + if b.Available() == 0 { + if err1 := b.flush(); err1 != nil { + return n, err1 + } + } + nr := 0 + for nr < maxConsecutiveEmptyReads { + m, err = r.Read(b.buf[b.n:]) + if m != 0 || err != nil { + break + } + nr++ + } + if nr == maxConsecutiveEmptyReads { + return n, io.ErrNoProgress + } + b.n += m + n += int64(m) + if err != nil { + break + } + } + if err == io.EOF { + // If we filled the buffer exactly, flush pre-emptively. + if b.Available() == 0 { + err = b.flush() + } else { + err = nil + } + } + return n, err +} + +// buffered input and output + +// ReadWriter stores pointers to a Reader and a Writer. +// It implements io.ReadWriter. +type ReadWriter struct { + *Reader + *Writer +} + +// NewReadWriter allocates a new ReadWriter that dispatches to r and w. +func NewReadWriter(r *Reader, w *Writer) *ReadWriter { + return &ReadWriter{r, w} +} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio_test.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio_test.go new file mode 100644 index 0000000..f19d9bd --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio_test.go @@ -0,0 +1,1418 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package bufio_test + +import ( + "bytes" + "errors" + "fmt" + "io" + "io/ioutil" + "strings" + "testing" + "testing/iotest" + "time" + "unicode/utf8" + + . "gopkg.in/bufio.v1" +) + +// Reads from a reader and rot13s the result. +type rot13Reader struct { + r io.Reader +} + +func newRot13Reader(r io.Reader) *rot13Reader { + r13 := new(rot13Reader) + r13.r = r + return r13 +} + +func (r13 *rot13Reader) Read(p []byte) (int, error) { + n, err := r13.r.Read(p) + if err != nil { + return n, err + } + for i := 0; i < n; i++ { + c := p[i] | 0x20 // lowercase byte + if 'a' <= c && c <= 'm' { + p[i] += 13 + } else if 'n' <= c && c <= 'z' { + p[i] -= 13 + } + } + return n, nil +} + +// Call ReadByte to accumulate the text of a file +func readBytes(buf *Reader) string { + var b [1000]byte + nb := 0 + for { + c, err := buf.ReadByte() + if err == io.EOF { + break + } + if err == nil { + b[nb] = c + nb++ + } else if err != iotest.ErrTimeout { + panic("Data: " + err.Error()) + } + } + return string(b[0:nb]) +} + +func TestReaderSimple(t *testing.T) { + data := "hello world" + b := NewReader(strings.NewReader(data)) + if s := readBytes(b); s != "hello world" { + t.Errorf("simple hello world test failed: got %q", s) + } + + b = NewReader(newRot13Reader(strings.NewReader(data))) + if s := readBytes(b); s != "uryyb jbeyq" { + t.Errorf("rot13 hello world test failed: got %q", s) + } +} + +type readMaker struct { + name string + fn func(io.Reader) io.Reader +} + +var readMakers = []readMaker{ + {"full", func(r io.Reader) io.Reader { return r }}, + {"byte", iotest.OneByteReader}, + {"half", iotest.HalfReader}, + {"data+err", iotest.DataErrReader}, + {"timeout", iotest.TimeoutReader}, +} + +// Call ReadString (which ends up calling everything else) +// to accumulate the text of a file. +func readLines(b *Reader) string { + s := "" + for { + s1, err := b.ReadString('\n') + if err == io.EOF { + break + } + if err != nil && err != iotest.ErrTimeout { + panic("GetLines: " + err.Error()) + } + s += s1 + } + return s +} + +// Call Read to accumulate the text of a file +func reads(buf *Reader, m int) string { + var b [1000]byte + nb := 0 + for { + n, err := buf.Read(b[nb : nb+m]) + nb += n + if err == io.EOF { + break + } + } + return string(b[0:nb]) +} + +type bufReader struct { + name string + fn func(*Reader) string +} + +var bufreaders = []bufReader{ + {"1", func(b *Reader) string { return reads(b, 1) }}, + {"2", func(b *Reader) string { return reads(b, 2) }}, + {"3", func(b *Reader) string { return reads(b, 3) }}, + {"4", func(b *Reader) string { return reads(b, 4) }}, + {"5", func(b *Reader) string { return reads(b, 5) }}, + {"7", func(b *Reader) string { return reads(b, 7) }}, + {"bytes", readBytes}, + {"lines", readLines}, +} + +const minReadBufferSize = 16 + +var bufsizes = []int{ + 0, minReadBufferSize, 23, 32, 46, 64, 93, 128, 1024, 4096, +} + +func TestReader(t *testing.T) { + var texts [31]string + str := "" + all := "" + for i := 0; i < len(texts)-1; i++ { + texts[i] = str + "\n" + all += texts[i] + str += string(i%26 + 'a') + } + texts[len(texts)-1] = all + + for h := 0; h < len(texts); h++ { + text := texts[h] + for i := 0; i < len(readMakers); i++ { + for j := 0; j < len(bufreaders); j++ { + for k := 0; k < len(bufsizes); k++ { + readmaker := readMakers[i] + bufreader := bufreaders[j] + bufsize := bufsizes[k] + read := readmaker.fn(strings.NewReader(text)) + buf := NewReaderSize(read, bufsize) + s := bufreader.fn(buf) + if s != text { + t.Errorf("reader=%s fn=%s bufsize=%d want=%q got=%q", + readmaker.name, bufreader.name, bufsize, text, s) + } + } + } + } + } +} + +type zeroReader struct{} + +func (zeroReader) Read(p []byte) (int, error) { + return 0, nil +} + +func TestZeroReader(t *testing.T) { + var z zeroReader + r := NewReader(z) + + c := make(chan error) + go func() { + _, err := r.ReadByte() + c <- err + }() + + select { + case err := <-c: + if err == nil { + t.Error("error expected") + } else if err != io.ErrNoProgress { + t.Error("unexpected error:", err) + } + case <-time.After(time.Second): + t.Error("test timed out (endless loop in ReadByte?)") + } +} + +// A StringReader delivers its data one string segment at a time via Read. +type StringReader struct { + data []string + step int +} + +func (r *StringReader) Read(p []byte) (n int, err error) { + if r.step < len(r.data) { + s := r.data[r.step] + n = copy(p, s) + r.step++ + } else { + err = io.EOF + } + return +} + +func readRuneSegments(t *testing.T, segments []string) { + got := "" + want := strings.Join(segments, "") + r := NewReader(&StringReader{data: segments}) + for { + r, _, err := r.ReadRune() + if err != nil { + if err != io.EOF { + return + } + break + } + got += string(r) + } + if got != want { + t.Errorf("segments=%v got=%s want=%s", segments, got, want) + } +} + +var segmentList = [][]string{ + {}, + {""}, + {"日", "本語"}, + {"\u65e5", "\u672c", "\u8a9e"}, + {"\U000065e5", "\U0000672c", "\U00008a9e"}, + {"\xe6", "\x97\xa5\xe6", "\x9c\xac\xe8\xaa\x9e"}, + {"Hello", ", ", "World", "!"}, + {"Hello", ", ", "", "World", "!"}, +} + +func TestReadRune(t *testing.T) { + for _, s := range segmentList { + readRuneSegments(t, s) + } +} + +func TestUnreadRune(t *testing.T) { + segments := []string{"Hello, world:", "日本語"} + r := NewReader(&StringReader{data: segments}) + got := "" + want := strings.Join(segments, "") + // Normal execution. + for { + r1, _, err := r.ReadRune() + if err != nil { + if err != io.EOF { + t.Error("unexpected error on ReadRune:", err) + } + break + } + got += string(r1) + // Put it back and read it again. + if err = r.UnreadRune(); err != nil { + t.Fatal("unexpected error on UnreadRune:", err) + } + r2, _, err := r.ReadRune() + if err != nil { + t.Fatal("unexpected error reading after unreading:", err) + } + if r1 != r2 { + t.Fatalf("incorrect rune after unread: got %c, want %c", r1, r2) + } + } + if got != want { + t.Errorf("got %q, want %q", got, want) + } +} + +func TestReaderUnreadByte(t *testing.T) { + segments := []string{"Hello, ", "world"} + r := NewReader(&StringReader{data: segments}) + got := "" + want := strings.Join(segments, "") + // Normal execution. + for { + b1, err := r.ReadByte() + if err != nil { + if err != io.EOF { + t.Error("unexpected error on ReadByte:", err) + } + break + } + got += string(b1) + // Put it back and read it again. + if err = r.UnreadByte(); err != nil { + t.Fatal("unexpected error on UnreadByte:", err) + } + b2, err := r.ReadByte() + if err != nil { + t.Fatal("unexpected error reading after unreading:", err) + } + if b1 != b2 { + t.Fatalf("incorrect byte after unread: got %q, want %q", b1, b2) + } + } + if got != want { + t.Errorf("got %q, want %q", got, want) + } +} + +func TestUnreadByteMultiple(t *testing.T) { + segments := []string{"Hello, ", "world"} + data := strings.Join(segments, "") + for n := 0; n <= len(data); n++ { + r := NewReader(&StringReader{data: segments}) + // Read n bytes. + for i := 0; i < n; i++ { + b, err := r.ReadByte() + if err != nil { + t.Fatalf("n = %d: unexpected error on ReadByte: %v", n, err) + } + if b != data[i] { + t.Fatalf("n = %d: incorrect byte returned from ReadByte: got %q, want %q", n, b, data[i]) + } + } + // Unread one byte if there is one. + if n > 0 { + if err := r.UnreadByte(); err != nil { + t.Errorf("n = %d: unexpected error on UnreadByte: %v", n, err) + } + } + // Test that we cannot unread any further. + if err := r.UnreadByte(); err == nil { + t.Errorf("n = %d: expected error on UnreadByte", n) + } + } +} + +func TestUnreadByteOthers(t *testing.T) { + // A list of readers to use in conjunction with UnreadByte. + var readers = []func(*Reader, byte) ([]byte, error){ + (*Reader).ReadBytes, + (*Reader).ReadSlice, + func(r *Reader, delim byte) ([]byte, error) { + data, err := r.ReadString(delim) + return []byte(data), err + }, + // ReadLine doesn't fit the data/pattern easily + // so we leave it out. It should be covered via + // the ReadSlice test since ReadLine simply calls + // ReadSlice, and it's that function that handles + // the last byte. + } + + // Try all readers with UnreadByte. + for rno, read := range readers { + // Some input data that is longer than the minimum reader buffer size. + const n = 10 + var buf bytes.Buffer + for i := 0; i < n; i++ { + buf.WriteString("abcdefg") + } + + r := NewReaderSize(&buf, minReadBufferSize) + readTo := func(delim byte, want string) { + data, err := read(r, delim) + if err != nil { + t.Fatalf("#%d: unexpected error reading to %c: %v", rno, delim, err) + } + if got := string(data); got != want { + t.Fatalf("#%d: got %q, want %q", rno, got, want) + } + } + + // Read the data with occasional UnreadByte calls. + for i := 0; i < n; i++ { + readTo('d', "abcd") + for j := 0; j < 3; j++ { + if err := r.UnreadByte(); err != nil { + t.Fatalf("#%d: unexpected error on UnreadByte: %v", rno, err) + } + readTo('d', "d") + } + readTo('g', "efg") + } + + // All data should have been read. + _, err := r.ReadByte() + if err != io.EOF { + t.Errorf("#%d: got error %v; want EOF", rno, err) + } + } +} + +// Test that UnreadRune fails if the preceding operation was not a ReadRune. +func TestUnreadRuneError(t *testing.T) { + buf := make([]byte, 3) // All runes in this test are 3 bytes long + r := NewReader(&StringReader{data: []string{"日本語日本語日本語"}}) + if r.UnreadRune() == nil { + t.Error("expected error on UnreadRune from fresh buffer") + } + _, _, err := r.ReadRune() + if err != nil { + t.Error("unexpected error on ReadRune (1):", err) + } + if err = r.UnreadRune(); err != nil { + t.Error("unexpected error on UnreadRune (1):", err) + } + if r.UnreadRune() == nil { + t.Error("expected error after UnreadRune (1)") + } + // Test error after Read. + _, _, err = r.ReadRune() // reset state + if err != nil { + t.Error("unexpected error on ReadRune (2):", err) + } + _, err = r.Read(buf) + if err != nil { + t.Error("unexpected error on Read (2):", err) + } + if r.UnreadRune() == nil { + t.Error("expected error after Read (2)") + } + // Test error after ReadByte. + _, _, err = r.ReadRune() // reset state + if err != nil { + t.Error("unexpected error on ReadRune (2):", err) + } + for _ = range buf { + _, err = r.ReadByte() + if err != nil { + t.Error("unexpected error on ReadByte (2):", err) + } + } + if r.UnreadRune() == nil { + t.Error("expected error after ReadByte") + } + // Test error after UnreadByte. + _, _, err = r.ReadRune() // reset state + if err != nil { + t.Error("unexpected error on ReadRune (3):", err) + } + _, err = r.ReadByte() + if err != nil { + t.Error("unexpected error on ReadByte (3):", err) + } + err = r.UnreadByte() + if err != nil { + t.Error("unexpected error on UnreadByte (3):", err) + } + if r.UnreadRune() == nil { + t.Error("expected error after UnreadByte (3)") + } +} + +func TestUnreadRuneAtEOF(t *testing.T) { + // UnreadRune/ReadRune should error at EOF (was a bug; used to panic) + r := NewReader(strings.NewReader("x")) + r.ReadRune() + r.ReadRune() + r.UnreadRune() + _, _, err := r.ReadRune() + if err == nil { + t.Error("expected error at EOF") + } else if err != io.EOF { + t.Error("expected EOF; got", err) + } +} + +func TestReadWriteRune(t *testing.T) { + const NRune = 1000 + byteBuf := new(bytes.Buffer) + w := NewWriter(byteBuf) + // Write the runes out using WriteRune + buf := make([]byte, utf8.UTFMax) + for r := rune(0); r < NRune; r++ { + size := utf8.EncodeRune(buf, r) + nbytes, err := w.WriteRune(r) + if err != nil { + t.Fatalf("WriteRune(0x%x) error: %s", r, err) + } + if nbytes != size { + t.Fatalf("WriteRune(0x%x) expected %d, got %d", r, size, nbytes) + } + } + w.Flush() + + r := NewReader(byteBuf) + // Read them back with ReadRune + for r1 := rune(0); r1 < NRune; r1++ { + size := utf8.EncodeRune(buf, r1) + nr, nbytes, err := r.ReadRune() + if nr != r1 || nbytes != size || err != nil { + t.Fatalf("ReadRune(0x%x) got 0x%x,%d not 0x%x,%d (err=%s)", r1, nr, nbytes, r1, size, err) + } + } +} + +func TestWriter(t *testing.T) { + var data [8192]byte + + for i := 0; i < len(data); i++ { + data[i] = byte(' ' + i%('~'-' ')) + } + w := new(bytes.Buffer) + for i := 0; i < len(bufsizes); i++ { + for j := 0; j < len(bufsizes); j++ { + nwrite := bufsizes[i] + bs := bufsizes[j] + + // Write nwrite bytes using buffer size bs. + // Check that the right amount makes it out + // and that the data is correct. + + w.Reset() + buf := NewWriterSize(w, bs) + context := fmt.Sprintf("nwrite=%d bufsize=%d", nwrite, bs) + n, e1 := buf.Write(data[0:nwrite]) + if e1 != nil || n != nwrite { + t.Errorf("%s: buf.Write %d = %d, %v", context, nwrite, n, e1) + continue + } + if e := buf.Flush(); e != nil { + t.Errorf("%s: buf.Flush = %v", context, e) + } + + written := w.Bytes() + if len(written) != nwrite { + t.Errorf("%s: %d bytes written", context, len(written)) + } + for l := 0; l < len(written); l++ { + if written[i] != data[i] { + t.Errorf("wrong bytes written") + t.Errorf("want=%q", data[0:len(written)]) + t.Errorf("have=%q", written) + } + } + } + } +} + +// Check that write errors are returned properly. + +type errorWriterTest struct { + n, m int + err error + expect error +} + +func (w errorWriterTest) Write(p []byte) (int, error) { + return len(p) * w.n / w.m, w.err +} + +var errorWriterTests = []errorWriterTest{ + {0, 1, nil, io.ErrShortWrite}, + {1, 2, nil, io.ErrShortWrite}, + {1, 1, nil, nil}, + {0, 1, io.ErrClosedPipe, io.ErrClosedPipe}, + {1, 2, io.ErrClosedPipe, io.ErrClosedPipe}, + {1, 1, io.ErrClosedPipe, io.ErrClosedPipe}, +} + +func TestWriteErrors(t *testing.T) { + for _, w := range errorWriterTests { + buf := NewWriter(w) + _, e := buf.Write([]byte("hello world")) + if e != nil { + t.Errorf("Write hello to %v: %v", w, e) + continue + } + // Two flushes, to verify the error is sticky. + for i := 0; i < 2; i++ { + e = buf.Flush() + if e != w.expect { + t.Errorf("Flush %d/2 %v: got %v, wanted %v", i+1, w, e, w.expect) + } + } + } +} + +func TestNewReaderSizeIdempotent(t *testing.T) { + const BufSize = 1000 + b := NewReaderSize(strings.NewReader("hello world"), BufSize) + // Does it recognize itself? + b1 := NewReaderSize(b, BufSize) + if b1 != b { + t.Error("NewReaderSize did not detect underlying Reader") + } + // Does it wrap if existing buffer is too small? + b2 := NewReaderSize(b, 2*BufSize) + if b2 == b { + t.Error("NewReaderSize did not enlarge buffer") + } +} + +func TestNewWriterSizeIdempotent(t *testing.T) { + const BufSize = 1000 + b := NewWriterSize(new(bytes.Buffer), BufSize) + // Does it recognize itself? + b1 := NewWriterSize(b, BufSize) + if b1 != b { + t.Error("NewWriterSize did not detect underlying Writer") + } + // Does it wrap if existing buffer is too small? + b2 := NewWriterSize(b, 2*BufSize) + if b2 == b { + t.Error("NewWriterSize did not enlarge buffer") + } +} + +func TestWriteString(t *testing.T) { + const BufSize = 8 + buf := new(bytes.Buffer) + b := NewWriterSize(buf, BufSize) + b.WriteString("0") // easy + b.WriteString("123456") // still easy + b.WriteString("7890") // easy after flush + b.WriteString("abcdefghijklmnopqrstuvwxy") // hard + b.WriteString("z") + if err := b.Flush(); err != nil { + t.Error("WriteString", err) + } + s := "01234567890abcdefghijklmnopqrstuvwxyz" + if string(buf.Bytes()) != s { + t.Errorf("WriteString wants %q gets %q", s, string(buf.Bytes())) + } +} + +func TestBufferFull(t *testing.T) { + const longString = "And now, hello, world! It is the time for all good men to come to the aid of their party" + buf := NewReaderSize(strings.NewReader(longString), minReadBufferSize) + line, err := buf.ReadSlice('!') + if string(line) != "And now, hello, " || err != ErrBufferFull { + t.Errorf("first ReadSlice(,) = %q, %v", line, err) + } + line, err = buf.ReadSlice('!') + if string(line) != "world!" || err != nil { + t.Errorf("second ReadSlice(,) = %q, %v", line, err) + } +} + +func TestPeek(t *testing.T) { + p := make([]byte, 10) + // string is 16 (minReadBufferSize) long. + buf := NewReaderSize(strings.NewReader("abcdefghijklmnop"), minReadBufferSize) + if s, err := buf.Peek(1); string(s) != "a" || err != nil { + t.Fatalf("want %q got %q, err=%v", "a", string(s), err) + } + if s, err := buf.Peek(4); string(s) != "abcd" || err != nil { + t.Fatalf("want %q got %q, err=%v", "abcd", string(s), err) + } + if _, err := buf.Peek(-1); err != ErrNegativeCount { + t.Fatalf("want ErrNegativeCount got %v", err) + } + if _, err := buf.Peek(32); err != ErrBufferFull { + t.Fatalf("want ErrBufFull got %v", err) + } + if _, err := buf.Read(p[0:3]); string(p[0:3]) != "abc" || err != nil { + t.Fatalf("want %q got %q, err=%v", "abc", string(p[0:3]), err) + } + if s, err := buf.Peek(1); string(s) != "d" || err != nil { + t.Fatalf("want %q got %q, err=%v", "d", string(s), err) + } + if s, err := buf.Peek(2); string(s) != "de" || err != nil { + t.Fatalf("want %q got %q, err=%v", "de", string(s), err) + } + if _, err := buf.Read(p[0:3]); string(p[0:3]) != "def" || err != nil { + t.Fatalf("want %q got %q, err=%v", "def", string(p[0:3]), err) + } + if s, err := buf.Peek(4); string(s) != "ghij" || err != nil { + t.Fatalf("want %q got %q, err=%v", "ghij", string(s), err) + } + if _, err := buf.Read(p[0:]); string(p[0:]) != "ghijklmnop" || err != nil { + t.Fatalf("want %q got %q, err=%v", "ghijklmnop", string(p[0:minReadBufferSize]), err) + } + if s, err := buf.Peek(0); string(s) != "" || err != nil { + t.Fatalf("want %q got %q, err=%v", "", string(s), err) + } + if _, err := buf.Peek(1); err != io.EOF { + t.Fatalf("want EOF got %v", err) + } + + // Test for issue 3022, not exposing a reader's error on a successful Peek. + buf = NewReaderSize(dataAndEOFReader("abcd"), 32) + if s, err := buf.Peek(2); string(s) != "ab" || err != nil { + t.Errorf(`Peek(2) on "abcd", EOF = %q, %v; want "ab", nil`, string(s), err) + } + if s, err := buf.Peek(4); string(s) != "abcd" || err != nil { + t.Errorf(`Peek(4) on "abcd", EOF = %q, %v; want "abcd", nil`, string(s), err) + } + if n, err := buf.Read(p[0:5]); string(p[0:n]) != "abcd" || err != nil { + t.Fatalf("Read after peek = %q, %v; want abcd, EOF", p[0:n], err) + } + if n, err := buf.Read(p[0:1]); string(p[0:n]) != "" || err != io.EOF { + t.Fatalf(`second Read after peek = %q, %v; want "", EOF`, p[0:n], err) + } +} + +type dataAndEOFReader string + +func (r dataAndEOFReader) Read(p []byte) (int, error) { + return copy(p, r), io.EOF +} + +func TestPeekThenUnreadRune(t *testing.T) { + // This sequence used to cause a crash. + r := NewReader(strings.NewReader("x")) + r.ReadRune() + r.Peek(1) + r.UnreadRune() + r.ReadRune() // Used to panic here +} + +var testOutput = []byte("0123456789abcdefghijklmnopqrstuvwxy") +var testInput = []byte("012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy") +var testInputrn = []byte("012\r\n345\r\n678\r\n9ab\r\ncde\r\nfgh\r\nijk\r\nlmn\r\nopq\r\nrst\r\nuvw\r\nxy\r\n\n\r\n") + +// TestReader wraps a []byte and returns reads of a specific length. +type testReader struct { + data []byte + stride int +} + +func (t *testReader) Read(buf []byte) (n int, err error) { + n = t.stride + if n > len(t.data) { + n = len(t.data) + } + if n > len(buf) { + n = len(buf) + } + copy(buf, t.data) + t.data = t.data[n:] + if len(t.data) == 0 { + err = io.EOF + } + return +} + +func testReadLine(t *testing.T, input []byte) { + //for stride := 1; stride < len(input); stride++ { + for stride := 1; stride < 2; stride++ { + done := 0 + reader := testReader{input, stride} + l := NewReaderSize(&reader, len(input)+1) + for { + line, isPrefix, err := l.ReadLine() + if len(line) > 0 && err != nil { + t.Errorf("ReadLine returned both data and error: %s", err) + } + if isPrefix { + t.Errorf("ReadLine returned prefix") + } + if err != nil { + if err != io.EOF { + t.Fatalf("Got unknown error: %s", err) + } + break + } + if want := testOutput[done : done+len(line)]; !bytes.Equal(want, line) { + t.Errorf("Bad line at stride %d: want: %x got: %x", stride, want, line) + } + done += len(line) + } + if done != len(testOutput) { + t.Errorf("ReadLine didn't return everything: got: %d, want: %d (stride: %d)", done, len(testOutput), stride) + } + } +} + +func TestReadLine(t *testing.T) { + testReadLine(t, testInput) + testReadLine(t, testInputrn) +} + +func TestLineTooLong(t *testing.T) { + data := make([]byte, 0) + for i := 0; i < minReadBufferSize*5/2; i++ { + data = append(data, '0'+byte(i%10)) + } + buf := bytes.NewReader(data) + l := NewReaderSize(buf, minReadBufferSize) + line, isPrefix, err := l.ReadLine() + if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { + t.Errorf("bad result for first line: got %q want %q %v", line, data[:minReadBufferSize], err) + } + data = data[len(line):] + line, isPrefix, err = l.ReadLine() + if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { + t.Errorf("bad result for second line: got %q want %q %v", line, data[:minReadBufferSize], err) + } + data = data[len(line):] + line, isPrefix, err = l.ReadLine() + if isPrefix || !bytes.Equal(line, data[:minReadBufferSize/2]) || err != nil { + t.Errorf("bad result for third line: got %q want %q %v", line, data[:minReadBufferSize/2], err) + } + line, isPrefix, err = l.ReadLine() + if isPrefix || err == nil { + t.Errorf("expected no more lines: %x %s", line, err) + } +} + +func TestReadAfterLines(t *testing.T) { + line1 := "this is line1" + restData := "this is line2\nthis is line 3\n" + inbuf := bytes.NewReader([]byte(line1 + "\n" + restData)) + outbuf := new(bytes.Buffer) + maxLineLength := len(line1) + len(restData)/2 + l := NewReaderSize(inbuf, maxLineLength) + line, isPrefix, err := l.ReadLine() + if isPrefix || err != nil || string(line) != line1 { + t.Errorf("bad result for first line: isPrefix=%v err=%v line=%q", isPrefix, err, string(line)) + } + n, err := io.Copy(outbuf, l) + if int(n) != len(restData) || err != nil { + t.Errorf("bad result for Read: n=%d err=%v", n, err) + } + if outbuf.String() != restData { + t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData) + } +} + +func TestReadEmptyBuffer(t *testing.T) { + l := NewReaderSize(new(bytes.Buffer), minReadBufferSize) + line, isPrefix, err := l.ReadLine() + if err != io.EOF { + t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) + } +} + +func TestLinesAfterRead(t *testing.T) { + l := NewReaderSize(bytes.NewReader([]byte("foo")), minReadBufferSize) + _, err := ioutil.ReadAll(l) + if err != nil { + t.Error(err) + return + } + + line, isPrefix, err := l.ReadLine() + if err != io.EOF { + t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) + } +} + +func TestReadLineNonNilLineOrError(t *testing.T) { + r := NewReader(strings.NewReader("line 1\n")) + for i := 0; i < 2; i++ { + l, _, err := r.ReadLine() + if l != nil && err != nil { + t.Fatalf("on line %d/2; ReadLine=%#v, %v; want non-nil line or Error, but not both", + i+1, l, err) + } + } +} + +type readLineResult struct { + line []byte + isPrefix bool + err error +} + +var readLineNewlinesTests = []struct { + input string + expect []readLineResult +}{ + {"012345678901234\r\n012345678901234\r\n", []readLineResult{ + {[]byte("012345678901234"), true, nil}, + {nil, false, nil}, + {[]byte("012345678901234"), true, nil}, + {nil, false, nil}, + {nil, false, io.EOF}, + }}, + {"0123456789012345\r012345678901234\r", []readLineResult{ + {[]byte("0123456789012345"), true, nil}, + {[]byte("\r012345678901234"), true, nil}, + {[]byte("\r"), false, nil}, + {nil, false, io.EOF}, + }}, +} + +func TestReadLineNewlines(t *testing.T) { + for _, e := range readLineNewlinesTests { + testReadLineNewlines(t, e.input, e.expect) + } +} + +func testReadLineNewlines(t *testing.T, input string, expect []readLineResult) { + b := NewReaderSize(strings.NewReader(input), minReadBufferSize) + for i, e := range expect { + line, isPrefix, err := b.ReadLine() + if !bytes.Equal(line, e.line) { + t.Errorf("%q call %d, line == %q, want %q", input, i, line, e.line) + return + } + if isPrefix != e.isPrefix { + t.Errorf("%q call %d, isPrefix == %v, want %v", input, i, isPrefix, e.isPrefix) + return + } + if err != e.err { + t.Errorf("%q call %d, err == %v, want %v", input, i, err, e.err) + return + } + } +} + +func createTestInput(n int) []byte { + input := make([]byte, n) + for i := range input { + // 101 and 251 are arbitrary prime numbers. + // The idea is to create an input sequence + // which doesn't repeat too frequently. + input[i] = byte(i % 251) + if i%101 == 0 { + input[i] ^= byte(i / 101) + } + } + return input +} + +func TestReaderWriteTo(t *testing.T) { + input := createTestInput(8192) + r := NewReader(onlyReader{bytes.NewReader(input)}) + w := new(bytes.Buffer) + if n, err := r.WriteTo(w); err != nil || n != int64(len(input)) { + t.Fatalf("r.WriteTo(w) = %d, %v, want %d, nil", n, err, len(input)) + } + + for i, val := range w.Bytes() { + if val != input[i] { + t.Errorf("after write: out[%d] = %#x, want %#x", i, val, input[i]) + } + } +} + +type errorWriterToTest struct { + rn, wn int + rerr, werr error + expected error +} + +func (r errorWriterToTest) Read(p []byte) (int, error) { + return len(p) * r.rn, r.rerr +} + +func (w errorWriterToTest) Write(p []byte) (int, error) { + return len(p) * w.wn, w.werr +} + +var errorWriterToTests = []errorWriterToTest{ + {1, 0, nil, io.ErrClosedPipe, io.ErrClosedPipe}, + {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe}, + {0, 0, io.ErrUnexpectedEOF, io.ErrClosedPipe, io.ErrClosedPipe}, + {0, 1, io.EOF, nil, nil}, +} + +func TestReaderWriteToErrors(t *testing.T) { + for i, rw := range errorWriterToTests { + r := NewReader(rw) + if _, err := r.WriteTo(rw); err != rw.expected { + t.Errorf("r.WriteTo(errorWriterToTests[%d]) = _, %v, want _,%v", i, err, rw.expected) + } + } +} + +func TestWriterReadFrom(t *testing.T) { + ws := []func(io.Writer) io.Writer{ + func(w io.Writer) io.Writer { return onlyWriter{w} }, + func(w io.Writer) io.Writer { return w }, + } + + rs := []func(io.Reader) io.Reader{ + iotest.DataErrReader, + func(r io.Reader) io.Reader { return r }, + } + + for ri, rfunc := range rs { + for wi, wfunc := range ws { + input := createTestInput(8192) + b := new(bytes.Buffer) + w := NewWriter(wfunc(b)) + r := rfunc(bytes.NewReader(input)) + if n, err := w.ReadFrom(r); err != nil || n != int64(len(input)) { + t.Errorf("ws[%d],rs[%d]: w.ReadFrom(r) = %d, %v, want %d, nil", wi, ri, n, err, len(input)) + continue + } + if err := w.Flush(); err != nil { + t.Errorf("Flush returned %v", err) + continue + } + if got, want := b.String(), string(input); got != want { + t.Errorf("ws[%d], rs[%d]:\ngot %q\nwant %q\n", wi, ri, got, want) + } + } + } +} + +type errorReaderFromTest struct { + rn, wn int + rerr, werr error + expected error +} + +func (r errorReaderFromTest) Read(p []byte) (int, error) { + return len(p) * r.rn, r.rerr +} + +func (w errorReaderFromTest) Write(p []byte) (int, error) { + return len(p) * w.wn, w.werr +} + +var errorReaderFromTests = []errorReaderFromTest{ + {0, 1, io.EOF, nil, nil}, + {1, 1, io.EOF, nil, nil}, + {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe}, + {0, 0, io.ErrClosedPipe, io.ErrShortWrite, io.ErrClosedPipe}, + {1, 0, nil, io.ErrShortWrite, io.ErrShortWrite}, +} + +func TestWriterReadFromErrors(t *testing.T) { + for i, rw := range errorReaderFromTests { + w := NewWriter(rw) + if _, err := w.ReadFrom(rw); err != rw.expected { + t.Errorf("w.ReadFrom(errorReaderFromTests[%d]) = _, %v, want _,%v", i, err, rw.expected) + } + } +} + +// TestWriterReadFromCounts tests that using io.Copy to copy into a +// bufio.Writer does not prematurely flush the buffer. For example, when +// buffering writes to a network socket, excessive network writes should be +// avoided. +func TestWriterReadFromCounts(t *testing.T) { + var w0 writeCountingDiscard + b0 := NewWriterSize(&w0, 1234) + b0.WriteString(strings.Repeat("x", 1000)) + if w0 != 0 { + t.Fatalf("write 1000 'x's: got %d writes, want 0", w0) + } + b0.WriteString(strings.Repeat("x", 200)) + if w0 != 0 { + t.Fatalf("write 1200 'x's: got %d writes, want 0", w0) + } + io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 30))}) + if w0 != 0 { + t.Fatalf("write 1230 'x's: got %d writes, want 0", w0) + } + io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 9))}) + if w0 != 1 { + t.Fatalf("write 1239 'x's: got %d writes, want 1", w0) + } + + var w1 writeCountingDiscard + b1 := NewWriterSize(&w1, 1234) + b1.WriteString(strings.Repeat("x", 1200)) + b1.Flush() + if w1 != 1 { + t.Fatalf("flush 1200 'x's: got %d writes, want 1", w1) + } + b1.WriteString(strings.Repeat("x", 89)) + if w1 != 1 { + t.Fatalf("write 1200 + 89 'x's: got %d writes, want 1", w1) + } + io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 700))}) + if w1 != 1 { + t.Fatalf("write 1200 + 789 'x's: got %d writes, want 1", w1) + } + io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 600))}) + if w1 != 2 { + t.Fatalf("write 1200 + 1389 'x's: got %d writes, want 2", w1) + } + b1.Flush() + if w1 != 3 { + t.Fatalf("flush 1200 + 1389 'x's: got %d writes, want 3", w1) + } +} + +// A writeCountingDiscard is like ioutil.Discard and counts the number of times +// Write is called on it. +type writeCountingDiscard int + +func (w *writeCountingDiscard) Write(p []byte) (int, error) { + *w++ + return len(p), nil +} + +type negativeReader int + +func (r *negativeReader) Read([]byte) (int, error) { return -1, nil } + +func TestNegativeRead(t *testing.T) { + // should panic with a description pointing at the reader, not at itself. + // (should NOT panic with slice index error, for example.) + b := NewReader(new(negativeReader)) + defer func() { + switch err := recover().(type) { + case nil: + t.Fatal("read did not panic") + case error: + if !strings.Contains(err.Error(), "reader returned negative count from Read") { + t.Fatalf("wrong panic: %v", err) + } + default: + t.Fatalf("unexpected panic value: %T(%v)", err, err) + } + }() + b.Read(make([]byte, 100)) +} + +var errFake = errors.New("fake error") + +type errorThenGoodReader struct { + didErr bool + nread int +} + +func (r *errorThenGoodReader) Read(p []byte) (int, error) { + r.nread++ + if !r.didErr { + r.didErr = true + return 0, errFake + } + return len(p), nil +} + +func TestReaderClearError(t *testing.T) { + r := &errorThenGoodReader{} + b := NewReader(r) + buf := make([]byte, 1) + if _, err := b.Read(nil); err != nil { + t.Fatalf("1st nil Read = %v; want nil", err) + } + if _, err := b.Read(buf); err != errFake { + t.Fatalf("1st Read = %v; want errFake", err) + } + if _, err := b.Read(nil); err != nil { + t.Fatalf("2nd nil Read = %v; want nil", err) + } + if _, err := b.Read(buf); err != nil { + t.Fatalf("3rd Read with buffer = %v; want nil", err) + } + if r.nread != 2 { + t.Errorf("num reads = %d; want 2", r.nread) + } +} + +// Test for golang.org/issue/5947 +func TestWriterReadFromWhileFull(t *testing.T) { + buf := new(bytes.Buffer) + w := NewWriterSize(buf, 10) + + // Fill buffer exactly. + n, err := w.Write([]byte("0123456789")) + if n != 10 || err != nil { + t.Fatalf("Write returned (%v, %v), want (10, nil)", n, err) + } + + // Use ReadFrom to read in some data. + n2, err := w.ReadFrom(strings.NewReader("abcdef")) + if n2 != 6 || err != nil { + t.Fatalf("ReadFrom returned (%v, %v), want (6, nil)", n2, err) + } +} + +type emptyThenNonEmptyReader struct { + r io.Reader + n int +} + +func (r *emptyThenNonEmptyReader) Read(p []byte) (int, error) { + if r.n <= 0 { + return r.r.Read(p) + } + r.n-- + return 0, nil +} + +// Test for golang.org/issue/7611 +func TestWriterReadFromUntilEOF(t *testing.T) { + buf := new(bytes.Buffer) + w := NewWriterSize(buf, 5) + + // Partially fill buffer + n, err := w.Write([]byte("0123")) + if n != 4 || err != nil { + t.Fatalf("Write returned (%v, %v), want (4, nil)", n, err) + } + + // Use ReadFrom to read in some data. + r := &emptyThenNonEmptyReader{r: strings.NewReader("abcd"), n: 3} + n2, err := w.ReadFrom(r) + if n2 != 4 || err != nil { + t.Fatalf("ReadFrom returned (%v, %v), want (4, nil)", n2, err) + } + w.Flush() + if got, want := string(buf.Bytes()), "0123abcd"; got != want { + t.Fatalf("buf.Bytes() returned %q, want %q", got, want) + } +} + +func TestWriterReadFromErrNoProgress(t *testing.T) { + buf := new(bytes.Buffer) + w := NewWriterSize(buf, 5) + + // Partially fill buffer + n, err := w.Write([]byte("0123")) + if n != 4 || err != nil { + t.Fatalf("Write returned (%v, %v), want (4, nil)", n, err) + } + + // Use ReadFrom to read in some data. + r := &emptyThenNonEmptyReader{r: strings.NewReader("abcd"), n: 100} + n2, err := w.ReadFrom(r) + if n2 != 0 || err != io.ErrNoProgress { + t.Fatalf("buf.Bytes() returned (%v, %v), want (0, io.ErrNoProgress)", n2, err) + } +} + +func TestReaderReset(t *testing.T) { + r := NewReader(strings.NewReader("foo foo")) + buf := make([]byte, 3) + r.Read(buf) + if string(buf) != "foo" { + t.Errorf("buf = %q; want foo", buf) + } + r.Reset(strings.NewReader("bar bar")) + all, err := ioutil.ReadAll(r) + if err != nil { + t.Fatal(err) + } + if string(all) != "bar bar" { + t.Errorf("ReadAll = %q; want bar bar", all) + } +} + +func TestWriterReset(t *testing.T) { + var buf1, buf2 bytes.Buffer + w := NewWriter(&buf1) + w.WriteString("foo") + w.Reset(&buf2) // and not flushed + w.WriteString("bar") + w.Flush() + if buf1.String() != "" { + t.Errorf("buf1 = %q; want empty", buf1.String()) + } + if buf2.String() != "bar" { + t.Errorf("buf2 = %q; want bar", buf2.String()) + } +} + +// An onlyReader only implements io.Reader, no matter what other methods the underlying implementation may have. +type onlyReader struct { + io.Reader +} + +// An onlyWriter only implements io.Writer, no matter what other methods the underlying implementation may have. +type onlyWriter struct { + io.Writer +} + +func BenchmarkReaderCopyOptimal(b *testing.B) { + // Optimal case is where the underlying reader implements io.WriterTo + srcBuf := bytes.NewBuffer(make([]byte, 8192)) + src := NewReader(srcBuf) + dstBuf := new(bytes.Buffer) + dst := onlyWriter{dstBuf} + for i := 0; i < b.N; i++ { + srcBuf.Reset() + src.Reset(srcBuf) + dstBuf.Reset() + io.Copy(dst, src) + } +} + +func BenchmarkReaderCopyUnoptimal(b *testing.B) { + // Unoptimal case is where the underlying reader doesn't implement io.WriterTo + srcBuf := bytes.NewBuffer(make([]byte, 8192)) + src := NewReader(onlyReader{srcBuf}) + dstBuf := new(bytes.Buffer) + dst := onlyWriter{dstBuf} + for i := 0; i < b.N; i++ { + srcBuf.Reset() + src.Reset(onlyReader{srcBuf}) + dstBuf.Reset() + io.Copy(dst, src) + } +} + +func BenchmarkReaderCopyNoWriteTo(b *testing.B) { + srcBuf := bytes.NewBuffer(make([]byte, 8192)) + srcReader := NewReader(srcBuf) + src := onlyReader{srcReader} + dstBuf := new(bytes.Buffer) + dst := onlyWriter{dstBuf} + for i := 0; i < b.N; i++ { + srcBuf.Reset() + srcReader.Reset(srcBuf) + dstBuf.Reset() + io.Copy(dst, src) + } +} + +func BenchmarkReaderWriteToOptimal(b *testing.B) { + const bufSize = 16 << 10 + buf := make([]byte, bufSize) + r := bytes.NewReader(buf) + srcReader := NewReaderSize(onlyReader{r}, 1<<10) + if _, ok := ioutil.Discard.(io.ReaderFrom); !ok { + b.Fatal("ioutil.Discard doesn't support ReaderFrom") + } + for i := 0; i < b.N; i++ { + r.Seek(0, 0) + srcReader.Reset(onlyReader{r}) + n, err := srcReader.WriteTo(ioutil.Discard) + if err != nil { + b.Fatal(err) + } + if n != bufSize { + b.Fatalf("n = %d; want %d", n, bufSize) + } + } +} + +func BenchmarkWriterCopyOptimal(b *testing.B) { + // Optimal case is where the underlying writer implements io.ReaderFrom + srcBuf := bytes.NewBuffer(make([]byte, 8192)) + src := onlyReader{srcBuf} + dstBuf := new(bytes.Buffer) + dst := NewWriter(dstBuf) + for i := 0; i < b.N; i++ { + srcBuf.Reset() + dstBuf.Reset() + dst.Reset(dstBuf) + io.Copy(dst, src) + } +} + +func BenchmarkWriterCopyUnoptimal(b *testing.B) { + srcBuf := bytes.NewBuffer(make([]byte, 8192)) + src := onlyReader{srcBuf} + dstBuf := new(bytes.Buffer) + dst := NewWriter(onlyWriter{dstBuf}) + for i := 0; i < b.N; i++ { + srcBuf.Reset() + dstBuf.Reset() + dst.Reset(onlyWriter{dstBuf}) + io.Copy(dst, src) + } +} + +func BenchmarkWriterCopyNoReadFrom(b *testing.B) { + srcBuf := bytes.NewBuffer(make([]byte, 8192)) + src := onlyReader{srcBuf} + dstBuf := new(bytes.Buffer) + dstWriter := NewWriter(dstBuf) + dst := onlyWriter{dstWriter} + for i := 0; i < b.N; i++ { + srcBuf.Reset() + dstBuf.Reset() + dstWriter.Reset(dstBuf) + io.Copy(dst, src) + } +} + +func BenchmarkReaderEmpty(b *testing.B) { + b.ReportAllocs() + str := strings.Repeat("x", 16<<10) + for i := 0; i < b.N; i++ { + br := NewReader(strings.NewReader(str)) + n, err := io.Copy(ioutil.Discard, br) + if err != nil { + b.Fatal(err) + } + if n != int64(len(str)) { + b.Fatal("wrong length") + } + } +} + +func BenchmarkWriterEmpty(b *testing.B) { + b.ReportAllocs() + str := strings.Repeat("x", 1<<10) + bs := []byte(str) + for i := 0; i < b.N; i++ { + bw := NewWriter(ioutil.Discard) + bw.Flush() + bw.WriteByte('a') + bw.Flush() + bw.WriteRune('B') + bw.Flush() + bw.Write(bs) + bw.Flush() + bw.WriteString(str) + bw.Flush() + } +} + +func BenchmarkWriterFlush(b *testing.B) { + b.ReportAllocs() + bw := NewWriter(ioutil.Discard) + str := strings.Repeat("x", 50) + for i := 0; i < b.N; i++ { + bw.WriteString(str) + bw.Flush() + } +} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/export_test.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/export_test.go new file mode 100644 index 0000000..16629d0 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/bufio.v1/export_test.go @@ -0,0 +1,9 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package bufio + +func (b *Buffer) Cap() int { + return cap(b.buf) +} diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/.gitignore b/Godeps/_workspace/src/gopkg.in/ini.v1/.gitignore new file mode 100644 index 0000000..53b6421 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/.gitignore @@ -0,0 +1,3 @@ +testdata/conf_out.ini +ini.sublime-project +ini.sublime-workspace diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/LICENSE b/Godeps/_workspace/src/gopkg.in/ini.v1/LICENSE new file mode 100644 index 0000000..37ec93a --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/LICENSE @@ -0,0 +1,191 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets "[]" replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same "printed page" as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/README.md b/Godeps/_workspace/src/gopkg.in/ini.v1/README.md new file mode 100644 index 0000000..6d77181 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/README.md @@ -0,0 +1,378 @@ +ini [![Build Status](https://drone.io/github.com/go-ini/ini/status.png)](https://drone.io/github.com/go-ini/ini/latest) [![](http://gocover.io/_badge/github.com/go-ini/ini)](http://gocover.io/github.com/go-ini/ini) +=== + +Package ini provides INI file read and write functionality in Go. + +[简体中文](README_ZH.md) + +## Feature + +- Load multiple data sources(`[]byte` or file) with overwrites. +- Read with recursion values. +- Read with parent-child sections. +- Read with auto-increment key names. +- Read with multiple-line values. +- Read with tons of helper methods. +- Read and convert values to Go types. +- Read and **WRITE** comments of sections and keys. +- Manipulate sections, keys and comments with ease. +- Keep sections and keys in order as you parse and save. + +## Installation + + go get gopkg.in/ini.v1 + +## Getting Started + +### Loading from data sources + +A **Data Source** is either raw data in type `[]byte` or a file name with type `string` and you can load **as many as** data sources you want. Passing other types will simply return an error. + +```go +cfg, err := ini.Load([]byte("raw data"), "filename") +``` + +Or start with an empty object: + +```go +cfg := ini.Empty() +``` + +When you cannot decide how many data sources to load at the beginning, you still able to **Append()** them later. + +```go +err := cfg.Append("other file", []byte("other raw data")) +``` + +### Working with sections + +To get a section, you would need to: + +```go +section, err := cfg.GetSection("section name") +``` + +For a shortcut for default section, just give an empty string as name: + +```go +section, err := cfg.GetSection("") +``` + +When you're pretty sure the section exists, following code could make your life easier: + +```go +section := cfg.Section("") +``` + +What happens when the section somehow does not exist? Don't panic, it automatically creates and returns a new section to you. + +To create a new section: + +```go +err := cfg.NewSection("new section") +``` + +To get a list of sections or section names: + +```go +sections := cfg.Sections() +names := cfg.SectionStrings() +``` + +### Working with keys + +To get a key under a section: + +```go +key, err := cfg.Section("").GetKey("key name") +``` + +Same rule applies to key operations: + +```go +key := cfg.Section("").Key("key name") +``` + +To create a new key: + +```go +err := cfg.Section("").NewKey("name", "value") +``` + +To get a list of keys or key names: + +```go +keys := cfg.Section().Keys() +names := cfg.Section().KeyStrings() +``` + +To get a clone hash of keys and corresponding values: + +```go +hash := cfg.GetSection("").KeysHash() +``` + +### Working with values + +To get a string value: + +```go +val := cfg.Section("").Key("key name").String() +``` + +To get value with types: + +```go +// For boolean values: +// true when value is: 1, t, T, TRUE, true, True, YES, yes, Yes, ON, on, On +// false when value is: 0, f, F, FALSE, false, False, NO, no, No, OFF, off, Off +v, err = cfg.Section("").Key("BOOL").Bool() +v, err = cfg.Section("").Key("FLOAT64").Float64() +v, err = cfg.Section("").Key("INT").Int() +v, err = cfg.Section("").Key("INT64").Int64() +v, err = cfg.Section("").Key("TIME").TimeFormat(time.RFC3339) +v, err = cfg.Section("").Key("TIME").Time() // RFC3339 + +v = cfg.Section("").Key("BOOL").MustBool() +v = cfg.Section("").Key("FLOAT64").MustFloat64() +v = cfg.Section("").Key("INT").MustInt() +v = cfg.Section("").Key("INT64").MustInt64() +v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339) +v = cfg.Section("").Key("TIME").MustTime() // RFC3339 + +// Methods start with Must also accept one argument for default value +// when key not found or fail to parse value to given type. +// Except method MustString, which you have to pass a default value. + +v = cfg.Seciont("").Key("String").MustString("default") +v = cfg.Section("").Key("BOOL").MustBool(true) +v = cfg.Section("").Key("FLOAT64").MustFloat64(1.25) +v = cfg.Section("").Key("INT").MustInt(10) +v = cfg.Section("").Key("INT64").MustInt64(99) +v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339, time.Now()) +v = cfg.Section("").Key("TIME").MustTime(time.Now()) // RFC3339 +``` + +What if my value is three-line long? + +```ini +[advance] +ADDRESS = """404 road, +NotFound, State, 5000 +Earth""" +``` + +Not a problem! + +```go +cfg.Section("advance").Key("ADDRESS").String() + +/* --- start --- +404 road, +NotFound, State, 5000 +Earth +------ end --- */ +``` + +That's all? Hmm, no. + +#### Helper methods of working with values + +To get value with given candidates: + +```go +v = cfg.Section("").Key("STRING").In("default", []string{"str", "arr", "types"}) +v = cfg.Section("").Key("FLOAT64").InFloat64(1.1, []float64{1.25, 2.5, 3.75}) +v = cfg.Section("").Key("INT").InInt(5, []int{10, 20, 30}) +v = cfg.Section("").Key("INT64").InInt64(10, []int64{10, 20, 30}) +v = cfg.Section("").Key("TIME").InTimeFormat(time.RFC3339, time.Now(), []time.Time{time1, time2, time3}) +v = cfg.Section("").Key("TIME").InTime(time.Now(), []time.Time{time1, time2, time3}) // RFC3339 +``` + +Default value will be presented if value of key is not in candidates you given, and default value does not need be one of candidates. + +To validate value in a given range: + +```go +vals = cfg.Section("").Key("FLOAT64").RangeFloat64(0.0, 1.1, 2.2) +vals = cfg.Section("").Key("INT").RangeInt(0, 10, 20) +vals = cfg.Section("").Key("INT64").RangeInt64(0, 10, 20) +vals = cfg.Section("").Key("TIME").RangeTimeFormat(time.RFC3339, time.Now(), minTime, maxTime) +vals = cfg.Section("").Key("TIME").RangeTime(time.Now(), minTime, maxTime) // RFC3339 +``` + +To auto-split value into slice: + +```go +vals = cfg.Section("").Key("STRINGS").Strings(",") +vals = cfg.Section("").Key("FLOAT64S").Float64s(",") +vals = cfg.Section("").Key("INTS").Ints(",") +vals = cfg.Section("").Key("INT64S").Int64s(",") +vals = cfg.Section("").Key("TIMES").Times(",") +``` + +## Advanced Usage + +### Recursive Values + +For all value of keys, there is a special syntax `%()s`, where `` is the key name in same section or default section, and `%()s` will be replaced by corresponding value(empty string if key not found). You can use this syntax at most 99 level of recursions. + +```ini +NAME = ini + +[author] +NAME = Unknwon +GITHUB = https://github.com/%(NAME)s + +[package] +FULL_NAME = github.com/go-ini/%(NAME)s +``` + +```go +cfg.Section("author").Key("GITHUB").String() // https://github.com/Unknwon +cfg.Section("package").Key("FULL_NAME").String() // github.com/go-ini/ini +``` + +### Parent-child Sections + +You can use `.` in section name to indicate parent-child relationship between two or more sections. If the key not found in the child section, library will try again on its parent section until there is no parent section. + +```ini +NAME = ini +VERSION = v1 +IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s + +[package] +CLONE_URL = https://%(IMPORT_PATH)s + +[package.sub] +``` + +```go +cfg.Section("package.sub").Key("CLONE_URL").String() // https://gopkg.in/ini.v1 +``` + +### Auto-increment Key Names + +If key name is `-` in data source, then it would be seen as special syntax for auto-increment key name start from 1, and every section is independent on counter. + +```ini +[features] +-: Support read/write comments of keys and sections +-: Support auto-increment of key names +-: Support load multiple files to overwrite key values +``` + +```go +cfg.Section("features").KeyStrings() // []{"#1", "#2", "#3"} +``` + +### Map To Struct + +Want more objective way to play with INI? Cool. + +```ini +Name = Unknwon +age = 21 +Male = true +Born = 1993-01-01T20:17:05Z + +[Note] +Content = Hi is a good man! +Cities = HangZhou, Boston +``` + +```go +type Note struct { + Content string + Cities []string +} + +type Person struct { + Name string + Age int `ini:"age"` + Male bool + Born time.Time + Note + Created time.Time `ini:"-"` +} + +func main() { + cfg, err := ini.Load("path/to/ini") + // ... + p := new(Person) + err = cfg.MapTo(p) + // ... + + // Things can be simpler. + err = ini.MapTo(p, "path/to/ini") + // ... + + // Just map a section? Fine. + n := new(Note) + err = cfg.Section("Note").MapTo(n) + // ... +} +``` + +Can I have default value for field? Absolutely. + +Assign it before you map to struct. It will keep the value as it is if the key is not presented or got wrong type. + +```go +// ... +p := &Person{ + Name: "Joe", +} +// ... +``` + +#### Name Mapper + +To save your time and make your code cleaner, this library supports [`NameMapper`](https://gowalker.org/gopkg.in/ini.v1#NameMapper) between struct field and actual secion and key name. + +There are 2 built-in name mappers: + +- `AllCapsUnderscore`: it converts to format `ALL_CAPS_UNDERSCORE` then match section or key. +- `TitleUnderscore`: it converts to format `title_underscore` then match section or key. + +To use them: + +```go +type Info struct{ + PackageName string +} + +func main() { + err = ini.MapToWithMapper(&Info{}, ini.TitleUnderscore, []byte("packag_name=ini")) + // ... + + cfg, err := ini.Load("PACKAGE_NAME=ini") + // ... + info := new(Info) + cfg.NameMapper = ini.AllCapsUnderscore + err = cfg.MapTo(info) + // ... +} +``` + +## Getting Help + +- [API Documentation](https://gowalker.org/gopkg.in/ini.v1) +- [File An Issue](https://github.com/go-ini/ini/issues/new) + +## FAQs + +### What does `BlockMode` field do? + +By default, library lets you read and write values so we need a locker to make sure your data is safe. But in cases that you are very sure about only reading data through the library, you can set `cfg.BlockMode = false` to speed up read operations about **50-70%** faster. + +### Why another INI library? + +Many people are using my another INI library [goconfig](https://github.com/Unknwon/goconfig), so the reason for this one is I would like to make more Go style code. Also when you set `cfg.BlockMode = false`, this one is about **10-30%** faster. + +To make those changes I have to confirm API broken, so it's safer to keep it in another place and start using `gopkg.in` to version my package at this time.(PS: shorter import path) + +## License + +This project is under Apache v2 License. See the [LICENSE](LICENSE) file for the full license text. diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/README_ZH.md b/Godeps/_workspace/src/gopkg.in/ini.v1/README_ZH.md new file mode 100644 index 0000000..c455cb6 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/README_ZH.md @@ -0,0 +1,367 @@ +本包提供了 Go 语言中读写 INI 文件的功能。 + +## 功能特性 + +- 支持覆盖加载多个数据源(`[]byte` 或文件) +- 支持递归读取键值 +- 支持读取父子分区 +- 支持读取自增键名 +- 支持读取多行的键值 +- 支持大量辅助方法 +- 支持在读取时直接转换为 Go 语言类型 +- 支持读取和 **写入** 分区和键的注释 +- 轻松操作分区、键值和注释 +- 在保存文件时分区和键值会保持原有的顺序 + +## 下载安装 + + go get gopkg.in/ini.v1 + +## 开始使用 + +### 从数据源加载 + +一个 **数据源** 可以是 `[]byte` 类型的原始数据,或 `string` 类型的文件路径。您可以加载 **任意多个** 数据源。如果您传递其它类型的数据源,则会直接返回错误。 + +```go +cfg, err := ini.Load([]byte("raw data"), "filename") +``` + +或者从一个空白的文件开始: + +```go +cfg := ini.Empty() +``` + +当您在一开始无法决定需要加载哪些数据源时,仍可以使用 **Append()** 在需要的时候加载它们。 + +```go +err := cfg.Append("other file", []byte("other raw data")) +``` + +### 操作分区(Section) + +获取指定分区: + +```go +section, err := cfg.GetSection("section name") +``` + +如果您想要获取默认分区,则可以用空字符串代替分区名: + +```go +section, err := cfg.GetSection("") +``` + +当您非常确定某个分区是存在的,可以使用以下简便方法: + +```go +section := cfg.Section("") +``` + +如果不小心判断错了,要获取的分区其实是不存在的,那会发生什么呢?没事的,它会自动创建并返回一个对应的分区对象给您。 + +创建一个分区: + +```go +err := cfg.NewSection("new section") +``` + +获取所有分区对象或名称: + +```go +sections := cfg.Sections() +names := cfg.SectionStrings() +``` + +### 操作键(Key) + +获取某个分区下的键: + +```go +key, err := cfg.Section("").GetKey("key name") +``` + +和分区一样,您也可以直接获取键而忽略错误处理: + +```go +key := cfg.Section("").Key("key name") +``` + +创建一个新的键: + +```go +err := cfg.Section("").NewKey("name", "value") +``` + +获取分区下的所有键或键名: + +```go +keys := cfg.Section().Keys() +names := cfg.Section().KeyStrings() +``` + +获取分区下的所有键值对的克隆: + +```go +hash := cfg.GetSection("").KeysHash() +``` + +### 操作键值(Value) + +获取一个类型为字符串(string)的值: + +```go +val := cfg.Section("").Key("key name").String() +``` + +获取其它类型的值: + +```go +// 布尔值的规则: +// true 当值为:1, t, T, TRUE, true, True, YES, yes, Yes, ON, on, On +// false 当值为:0, f, F, FALSE, false, False, NO, no, No, OFF, off, Off +v, err = cfg.Section("").Key("BOOL").Bool() +v, err = cfg.Section("").Key("FLOAT64").Float64() +v, err = cfg.Section("").Key("INT").Int() +v, err = cfg.Section("").Key("INT64").Int64() +v, err = cfg.Section("").Key("TIME").TimeFormat(time.RFC3339) +v, err = cfg.Section("").Key("TIME").Time() // RFC3339 + +v = cfg.Section("").Key("BOOL").MustBool() +v = cfg.Section("").Key("FLOAT64").MustFloat64() +v = cfg.Section("").Key("INT").MustInt() +v = cfg.Section("").Key("INT64").MustInt64() +v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339) +v = cfg.Section("").Key("TIME").MustTime() // RFC3339 + +// 由 Must 开头的方法名允许接收一个相同类型的参数来作为默认值, +// 当键不存在或者转换失败时,则会直接返回该默认值。 +// 但是,MustString 方法必须传递一个默认值。 + +v = cfg.Seciont("").Key("String").MustString("default") +v = cfg.Section("").Key("BOOL").MustBool(true) +v = cfg.Section("").Key("FLOAT64").MustFloat64(1.25) +v = cfg.Section("").Key("INT").MustInt(10) +v = cfg.Section("").Key("INT64").MustInt64(99) +v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339, time.Now()) +v = cfg.Section("").Key("TIME").MustTime(time.Now()) // RFC3339 +``` + +如果我的值有好多行怎么办? + +```ini +[advance] +ADDRESS = """404 road, +NotFound, State, 5000 +Earth""" +``` + +嗯哼?小 case! + +```go +cfg.Section("advance").Key("ADDRESS").String() + +/* --- start --- +404 road, +NotFound, State, 5000 +Earth +------ end --- */ +``` + +这就是全部了?哈哈,当然不是。 + +#### 操作键值的辅助方法 + +获取键值时设定候选值: + +```go +v = cfg.Section("").Key("STRING").In("default", []string{"str", "arr", "types"}) +v = cfg.Section("").Key("FLOAT64").InFloat64(1.1, []float64{1.25, 2.5, 3.75}) +v = cfg.Section("").Key("INT").InInt(5, []int{10, 20, 30}) +v = cfg.Section("").Key("INT64").InInt64(10, []int64{10, 20, 30}) +v = cfg.Section("").Key("TIME").InTimeFormat(time.RFC3339, time.Now(), []time.Time{time1, time2, time3}) +v = cfg.Section("").Key("TIME").InTime(time.Now(), []time.Time{time1, time2, time3}) // RFC3339 +``` + +如果获取到的值不是候选值的任意一个,则会返回默认值,而默认值不需要是候选值中的一员。 + +验证获取的值是否在指定范围内: + +```go +vals = cfg.Section("").Key("FLOAT64").RangeFloat64(0.0, 1.1, 2.2) +vals = cfg.Section("").Key("INT").RangeInt(0, 10, 20) +vals = cfg.Section("").Key("INT64").RangeInt64(0, 10, 20) +vals = cfg.Section("").Key("TIME").RangeTimeFormat(time.RFC3339, time.Now(), minTime, maxTime) +vals = cfg.Section("").Key("TIME").RangeTime(time.Now(), minTime, maxTime) // RFC3339 +``` + +自动分割键值为切片(slice): + +```go +vals = cfg.Section("").Key("STRINGS").Strings(",") +vals = cfg.Section("").Key("FLOAT64S").Float64s(",") +vals = cfg.Section("").Key("INTS").Ints(",") +vals = cfg.Section("").Key("INT64S").Int64s(",") +vals = cfg.Section("").Key("TIMES").Times(",") +``` + +### 高级用法 + +#### 递归读取键值 + +在获取所有键值的过程中,特殊语法 `%()s` 会被应用,其中 `` 可以是相同分区或者默认分区下的键名。字符串 `%()s` 会被相应的键值所替代,如果指定的键不存在,则会用空字符串替代。您可以最多使用 99 层的递归嵌套。 + +```ini +NAME = ini + +[author] +NAME = Unknwon +GITHUB = https://github.com/%(NAME)s + +[package] +FULL_NAME = github.com/go-ini/%(NAME)s +``` + +```go +cfg.Section("author").Key("GITHUB").String() // https://github.com/Unknwon +cfg.Section("package").Key("FULL_NAME").String() // github.com/go-ini/ini +``` + +#### 读取父子分区 + +您可以在分区名称中使用 `.` 来表示两个或多个分区之间的父子关系。如果某个键在子分区中不存在,则会去它的父分区中再次寻找,直到没有父分区为止。 + +```ini +NAME = ini +VERSION = v1 +IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s + +[package] +CLONE_URL = https://%(IMPORT_PATH)s + +[package.sub] +``` + +```go +cfg.Section("package.sub").Key("CLONE_URL").String() // https://gopkg.in/ini.v1 +``` + +#### 读取自增键名 + +如果数据源中的键名为 `-`,则认为该键使用了自增键名的特殊语法。计数器从 1 开始,并且分区之间是相互独立的。 + +```ini +[features] +-: Support read/write comments of keys and sections +-: Support auto-increment of key names +-: Support load multiple files to overwrite key values +``` + +```go +cfg.Section("features").KeyStrings() // []{"#1", "#2", "#3"} +``` + +### 映射到结构 + +想要使用更加面向对象的方式玩转 INI 吗?好主意。 + +```ini +Name = Unknwon +age = 21 +Male = true +Born = 1993-01-01T20:17:05Z + +[Note] +Content = Hi is a good man! +Cities = HangZhou, Boston +``` + +```go +type Note struct { + Content string + Cities []string +} + +type Person struct { + Name string + Age int `ini:"age"` + Male bool + Born time.Time + Note + Created time.Time `ini:"-"` +} + +func main() { + cfg, err := ini.Load("path/to/ini") + // ... + p := new(Person) + err = cfg.MapTo(p) + // ... + + // 一切竟可以如此的简单。 + err = ini.MapTo(p, "path/to/ini") + // ... + + // 嗯哼?只需要映射一个分区吗? + n := new(Note) + err = cfg.Section("Note").MapTo(n) + // ... +} +``` + +结构的字段怎么设置默认值呢?很简单,只要在映射之前对指定字段进行赋值就可以了。如果键未找到或者类型错误,该值不会发生改变。 + +```go +// ... +p := &Person{ + Name: "Joe", +} +// ... +``` + +#### 名称映射器(Name Mapper) + +为了节省您的时间并简化代码,本库支持类型为 [`NameMapper`](https://gowalker.org/gopkg.in/ini.v1#NameMapper) 的名称映射器,该映射器负责结构字段名与分区名和键名之间的映射。 + +目前有 2 款内置的映射器: + +- `AllCapsUnderscore`:该映射器将字段名转换至格式 `ALL_CAPS_UNDERSCORE` 后再去匹配分区名和键名。 +- `TitleUnderscore`:该映射器将字段名转换至格式 `title_underscore` 后再去匹配分区名和键名。 + +使用方法: + +```go +type Info struct{ + PackageName string +} + +func main() { + err = ini.MapToWithMapper(&Info{}, ini.TitleUnderscore, []byte("packag_name=ini")) + // ... + + cfg, err := ini.Load("PACKAGE_NAME=ini") + // ... + info := new(Info) + cfg.NameMapper = ini.AllCapsUnderscore + err = cfg.MapTo(info) + // ... +} +``` + +## 获取帮助 + +- [API 文档](https://gowalker.org/gopkg.in/ini.v1) +- [创建工单](https://github.com/go-ini/ini/issues/new) + +## 常见问题 + +### 字段 `BlockMode` 是什么? + +默认情况下,本库会在您进行读写操作时采用锁机制来确保数据时间。但在某些情况下,您非常确定只进行读操作。此时,您可以通过设置 `cfg.BlockMode = false` 来将读操作提升大约 **50-70%** 的性能。 + +### 为什么要写另一个 INI 解析库? + +许多人都在使用我的 [goconfig](https://github.com/Unknwon/goconfig) 来完成对 INI 文件的操作,但我希望使用更加 Go 风格的代码。并且当您设置 `cfg.BlockMode = false` 时,会有大约 **10-30%** 的性能提升。 + +为了做出这些改变,我必须对 API 进行破坏,所以新开一个仓库是最安全的做法。除此之外,本库直接使用 `gopkg.in` 来进行版本化发布。(其实真相是导入路径更短了) diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/ini.go b/Godeps/_workspace/src/gopkg.in/ini.v1/ini.go new file mode 100644 index 0000000..6674baf --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/ini.go @@ -0,0 +1,1017 @@ +// Copyright 2014 Unknwon +// +// 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 ini provides INI file read and write functionality in Go. +package ini + +import ( + "bufio" + "bytes" + "errors" + "fmt" + "io" + "os" + "regexp" + "runtime" + "strconv" + "strings" + "sync" + "time" +) + +const ( + DEFAULT_SECTION = "DEFAULT" + // Maximum allowed depth when recursively substituing variable names. + _DEPTH_VALUES = 99 + + _VERSION = "1.2.6" +) + +func Version() string { + return _VERSION +} + +var ( + LineBreak = "\n" + + // Variable regexp pattern: %(variable)s + varPattern = regexp.MustCompile(`%\(([^\)]+)\)s`) + + // Write spaces around "=" to look better. + PrettyFormat = true +) + +func init() { + if runtime.GOOS == "windows" { + LineBreak = "\r\n" + } +} + +func inSlice(str string, s []string) bool { + for _, v := range s { + if str == v { + return true + } + } + return false +} + +// dataSource is a interface that returns file content. +type dataSource interface { + Reader() (io.Reader, error) +} + +type sourceFile struct { + name string +} + +func (s sourceFile) Reader() (io.Reader, error) { + return os.Open(s.name) +} + +type sourceData struct { + data []byte +} + +func (s *sourceData) Reader() (io.Reader, error) { + return bytes.NewReader(s.data), nil +} + +// ____ __. +// | |/ _|____ ___.__. +// | <_/ __ < | | +// | | \ ___/\___ | +// |____|__ \___ > ____| +// \/ \/\/ + +// Key represents a key under a section. +type Key struct { + s *Section + Comment string + name string + value string + isAutoIncr bool +} + +// Name returns name of key. +func (k *Key) Name() string { + return k.name +} + +// Value returns raw value of key for performance purpose. +func (k *Key) Value() string { + return k.value +} + +// String returns string representation of value. +func (k *Key) String() string { + val := k.value + if strings.Index(val, "%") == -1 { + return val + } + + for i := 0; i < _DEPTH_VALUES; i++ { + vr := varPattern.FindString(val) + if len(vr) == 0 { + break + } + + // Take off leading '%(' and trailing ')s'. + noption := strings.TrimLeft(vr, "%(") + noption = strings.TrimRight(noption, ")s") + + // Search in the same section. + nk, err := k.s.GetKey(noption) + if err != nil { + // Search again in default section. + nk, _ = k.s.f.Section("").GetKey(noption) + } + + // Substitute by new value and take off leading '%(' and trailing ')s'. + val = strings.Replace(val, vr, nk.value, -1) + } + return val +} + +// parseBool returns the boolean value represented by the string. +// +// It accepts 1, t, T, TRUE, true, True, YES, yes, Yes, ON, on, On, +// 0, f, F, FALSE, false, False, NO, no, No, OFF, off, Off. +// Any other value returns an error. +func parseBool(str string) (value bool, err error) { + switch str { + case "1", "t", "T", "true", "TRUE", "True", "YES", "yes", "Yes", "ON", "on", "On": + return true, nil + case "0", "f", "F", "false", "FALSE", "False", "NO", "no", "No", "OFF", "off", "Off": + return false, nil + } + return false, fmt.Errorf("parsing \"%s\": invalid syntax", str) +} + +// Bool returns bool type value. +func (k *Key) Bool() (bool, error) { + return parseBool(k.String()) +} + +// Float64 returns float64 type value. +func (k *Key) Float64() (float64, error) { + return strconv.ParseFloat(k.String(), 64) +} + +// Int returns int type value. +func (k *Key) Int() (int, error) { + return strconv.Atoi(k.String()) +} + +// Int64 returns int64 type value. +func (k *Key) Int64() (int64, error) { + return strconv.ParseInt(k.String(), 10, 64) +} + +// TimeFormat parses with given format and returns time.Time type value. +func (k *Key) TimeFormat(format string) (time.Time, error) { + return time.Parse(format, k.String()) +} + +// Time parses with RFC3339 format and returns time.Time type value. +func (k *Key) Time() (time.Time, error) { + return k.TimeFormat(time.RFC3339) +} + +// MustString returns default value if key value is empty. +func (k *Key) MustString(defaultVal string) string { + val := k.String() + if len(val) == 0 { + return defaultVal + } + return val +} + +// MustBool always returns value without error, +// it returns false if error occurs. +func (k *Key) MustBool(defaultVal ...bool) bool { + val, err := k.Bool() + if len(defaultVal) > 0 && err != nil { + return defaultVal[0] + } + return val +} + +// MustFloat64 always returns value without error, +// it returns 0.0 if error occurs. +func (k *Key) MustFloat64(defaultVal ...float64) float64 { + val, err := k.Float64() + if len(defaultVal) > 0 && err != nil { + return defaultVal[0] + } + return val +} + +// MustInt always returns value without error, +// it returns 0 if error occurs. +func (k *Key) MustInt(defaultVal ...int) int { + val, err := k.Int() + if len(defaultVal) > 0 && err != nil { + return defaultVal[0] + } + return val +} + +// MustInt64 always returns value without error, +// it returns 0 if error occurs. +func (k *Key) MustInt64(defaultVal ...int64) int64 { + val, err := k.Int64() + if len(defaultVal) > 0 && err != nil { + return defaultVal[0] + } + return val +} + +// MustTimeFormat always parses with given format and returns value without error, +// it returns zero value if error occurs. +func (k *Key) MustTimeFormat(format string, defaultVal ...time.Time) time.Time { + val, err := k.TimeFormat(format) + if len(defaultVal) > 0 && err != nil { + return defaultVal[0] + } + return val +} + +// MustTime always parses with RFC3339 format and returns value without error, +// it returns zero value if error occurs. +func (k *Key) MustTime(defaultVal ...time.Time) time.Time { + return k.MustTimeFormat(time.RFC3339, defaultVal...) +} + +// In always returns value without error, +// it returns default value if error occurs or doesn't fit into candidates. +func (k *Key) In(defaultVal string, candidates []string) string { + val := k.String() + for _, cand := range candidates { + if val == cand { + return val + } + } + return defaultVal +} + +// InFloat64 always returns value without error, +// it returns default value if error occurs or doesn't fit into candidates. +func (k *Key) InFloat64(defaultVal float64, candidates []float64) float64 { + val := k.MustFloat64() + for _, cand := range candidates { + if val == cand { + return val + } + } + return defaultVal +} + +// InInt always returns value without error, +// it returns default value if error occurs or doesn't fit into candidates. +func (k *Key) InInt(defaultVal int, candidates []int) int { + val := k.MustInt() + for _, cand := range candidates { + if val == cand { + return val + } + } + return defaultVal +} + +// InInt64 always returns value without error, +// it returns default value if error occurs or doesn't fit into candidates. +func (k *Key) InInt64(defaultVal int64, candidates []int64) int64 { + val := k.MustInt64() + for _, cand := range candidates { + if val == cand { + return val + } + } + return defaultVal +} + +// InTimeFormat always parses with given format and returns value without error, +// it returns default value if error occurs or doesn't fit into candidates. +func (k *Key) InTimeFormat(format string, defaultVal time.Time, candidates []time.Time) time.Time { + val := k.MustTimeFormat(format) + for _, cand := range candidates { + if val == cand { + return val + } + } + return defaultVal +} + +// InTime always parses with RFC3339 format and returns value without error, +// it returns default value if error occurs or doesn't fit into candidates. +func (k *Key) InTime(defaultVal time.Time, candidates []time.Time) time.Time { + return k.InTimeFormat(time.RFC3339, defaultVal, candidates) +} + +// RangeFloat64 checks if value is in given range inclusively, +// and returns default value if it's not. +func (k *Key) RangeFloat64(defaultVal, min, max float64) float64 { + val := k.MustFloat64() + if val < min || val > max { + return defaultVal + } + return val +} + +// RangeInt checks if value is in given range inclusively, +// and returns default value if it's not. +func (k *Key) RangeInt(defaultVal, min, max int) int { + val := k.MustInt() + if val < min || val > max { + return defaultVal + } + return val +} + +// RangeInt64 checks if value is in given range inclusively, +// and returns default value if it's not. +func (k *Key) RangeInt64(defaultVal, min, max int64) int64 { + val := k.MustInt64() + if val < min || val > max { + return defaultVal + } + return val +} + +// RangeTimeFormat checks if value with given format is in given range inclusively, +// and returns default value if it's not. +func (k *Key) RangeTimeFormat(format string, defaultVal, min, max time.Time) time.Time { + val := k.MustTimeFormat(format) + if val.Unix() < min.Unix() || val.Unix() > max.Unix() { + return defaultVal + } + return val +} + +// RangeTime checks if value with RFC3339 format is in given range inclusively, +// and returns default value if it's not. +func (k *Key) RangeTime(defaultVal, min, max time.Time) time.Time { + return k.RangeTimeFormat(time.RFC3339, defaultVal, min, max) +} + +// Strings returns list of string devide by given delimiter. +func (k *Key) Strings(delim string) []string { + str := k.String() + if len(str) == 0 { + return []string{} + } + + vals := strings.Split(str, delim) + for i := range vals { + vals[i] = strings.TrimSpace(vals[i]) + } + return vals +} + +// Float64s returns list of float64 devide by given delimiter. +func (k *Key) Float64s(delim string) []float64 { + strs := k.Strings(delim) + vals := make([]float64, len(strs)) + for i := range strs { + vals[i], _ = strconv.ParseFloat(strs[i], 64) + } + return vals +} + +// Ints returns list of int devide by given delimiter. +func (k *Key) Ints(delim string) []int { + strs := k.Strings(delim) + vals := make([]int, len(strs)) + for i := range strs { + vals[i], _ = strconv.Atoi(strs[i]) + } + return vals +} + +// Int64s returns list of int64 devide by given delimiter. +func (k *Key) Int64s(delim string) []int64 { + strs := k.Strings(delim) + vals := make([]int64, len(strs)) + for i := range strs { + vals[i], _ = strconv.ParseInt(strs[i], 10, 64) + } + return vals +} + +// TimesFormat parses with given format and returns list of time.Time devide by given delimiter. +func (k *Key) TimesFormat(format, delim string) []time.Time { + strs := k.Strings(delim) + vals := make([]time.Time, len(strs)) + for i := range strs { + vals[i], _ = time.Parse(format, strs[i]) + } + return vals +} + +// Times parses with RFC3339 format and returns list of time.Time devide by given delimiter. +func (k *Key) Times(delim string) []time.Time { + return k.TimesFormat(time.RFC3339, delim) +} + +// SetValue changes key value. +func (k *Key) SetValue(v string) { + k.value = v +} + +// _________ __ .__ +// / _____/ ____ _____/ |_|__| ____ ____ +// \_____ \_/ __ \_/ ___\ __\ |/ _ \ / \ +// / \ ___/\ \___| | | ( <_> ) | \ +// /_______ /\___ >\___ >__| |__|\____/|___| / +// \/ \/ \/ \/ + +// Section represents a config section. +type Section struct { + f *File + Comment string + name string + keys map[string]*Key + keyList []string + keysHash map[string]string +} + +func newSection(f *File, name string) *Section { + return &Section{f, "", name, make(map[string]*Key), make([]string, 0, 10), make(map[string]string)} +} + +// Name returns name of Section. +func (s *Section) Name() string { + return s.name +} + +// NewKey creates a new key to given section. +func (s *Section) NewKey(name, val string) (*Key, error) { + if len(name) == 0 { + return nil, errors.New("error creating new key: empty key name") + } + + if s.f.BlockMode { + s.f.lock.Lock() + defer s.f.lock.Unlock() + } + + if inSlice(name, s.keyList) { + s.keys[name].value = val + return s.keys[name], nil + } + + s.keyList = append(s.keyList, name) + s.keys[name] = &Key{s, "", name, val, false} + s.keysHash[name] = val + return s.keys[name], nil +} + +// GetKey returns key in section by given name. +func (s *Section) GetKey(name string) (*Key, error) { + // FIXME: change to section level lock? + if s.f.BlockMode { + s.f.lock.RLock() + defer s.f.lock.RUnlock() + } + + key := s.keys[name] + if key == nil { + // Check if it is a child-section. + if i := strings.LastIndex(s.name, "."); i > -1 { + return s.f.Section(s.name[:i]).GetKey(name) + } + return nil, fmt.Errorf("error when getting key of section '%s': key '%s' not exists", s.name, name) + } + return key, nil +} + +// Key assumes named Key exists in section and returns a zero-value when not. +func (s *Section) Key(name string) *Key { + key, err := s.GetKey(name) + if err != nil { + // It's OK here because the only possible error is empty key name, + // but if it's empty, this piece of code won't be executed. + key, _ = s.NewKey(name, "") + return key + } + return key +} + +// Keys returns list of keys of section. +func (s *Section) Keys() []*Key { + keys := make([]*Key, len(s.keyList)) + for i := range s.keyList { + keys[i] = s.Key(s.keyList[i]) + } + return keys +} + +// KeyStrings returns list of key names of section. +func (s *Section) KeyStrings() []string { + list := make([]string, len(s.keyList)) + copy(list, s.keyList) + return list +} + +// KeysHash returns keys hash consisting of names and values. +func (s *Section) KeysHash() map[string]string { + if s.f.BlockMode { + s.f.lock.RLock() + defer s.f.lock.RUnlock() + } + + hash := map[string]string{} + for key, value := range s.keysHash { + hash[key] = value + } + return hash +} + +// DeleteKey deletes a key from section. +func (s *Section) DeleteKey(name string) { + if s.f.BlockMode { + s.f.lock.Lock() + defer s.f.lock.Unlock() + } + + for i, k := range s.keyList { + if k == name { + s.keyList = append(s.keyList[:i], s.keyList[i+1:]...) + delete(s.keys, name) + return + } + } +} + +// ___________.__.__ +// \_ _____/|__| | ____ +// | __) | | | _/ __ \ +// | \ | | |_\ ___/ +// \___ / |__|____/\___ > +// \/ \/ + +// File represents a combination of a or more INI file(s) in memory. +type File struct { + // Should make things safe, but sometimes doesn't matter. + BlockMode bool + // Make sure data is safe in multiple goroutines. + lock sync.RWMutex + + // Allow combination of multiple data sources. + dataSources []dataSource + // Actual data is stored here. + sections map[string]*Section + + // To keep data in order. + sectionList []string + + NameMapper +} + +// newFile initializes File object with given data sources. +func newFile(dataSources []dataSource) *File { + return &File{ + BlockMode: true, + dataSources: dataSources, + sections: make(map[string]*Section), + sectionList: make([]string, 0, 10), + } +} + +func parseDataSource(source interface{}) (dataSource, error) { + switch s := source.(type) { + case string: + return sourceFile{s}, nil + case []byte: + return &sourceData{s}, nil + default: + return nil, fmt.Errorf("error parsing data source: unknown type '%s'", s) + } +} + +// Load loads and parses from INI data sources. +// Arguments can be mixed of file name with string type, or raw data in []byte. +func Load(source interface{}, others ...interface{}) (_ *File, err error) { + sources := make([]dataSource, len(others)+1) + sources[0], err = parseDataSource(source) + if err != nil { + return nil, err + } + for i := range others { + sources[i+1], err = parseDataSource(others[i]) + if err != nil { + return nil, err + } + } + f := newFile(sources) + return f, f.Reload() +} + +// Empty returns an empty file object. +func Empty() *File { + // Ignore error here, we sure our data is good. + f, _ := Load([]byte("")) + return f +} + +// NewSection creates a new section. +func (f *File) NewSection(name string) (*Section, error) { + if len(name) == 0 { + return nil, errors.New("error creating new section: empty section name") + } + + if f.BlockMode { + f.lock.Lock() + defer f.lock.Unlock() + } + + if inSlice(name, f.sectionList) { + return f.sections[name], nil + } + + f.sectionList = append(f.sectionList, name) + f.sections[name] = newSection(f, name) + return f.sections[name], nil +} + +// NewSections creates a list of sections. +func (f *File) NewSections(names ...string) (err error) { + for _, name := range names { + if _, err = f.NewSection(name); err != nil { + return err + } + } + return nil +} + +// GetSection returns section by given name. +func (f *File) GetSection(name string) (*Section, error) { + if len(name) == 0 { + name = DEFAULT_SECTION + } + + if f.BlockMode { + f.lock.RLock() + defer f.lock.RUnlock() + } + + sec := f.sections[name] + if sec == nil { + return nil, fmt.Errorf("error when getting section: section '%s' not exists", name) + } + return sec, nil +} + +// Section assumes named section exists and returns a zero-value when not. +func (f *File) Section(name string) *Section { + sec, err := f.GetSection(name) + if err != nil { + // It's OK here because the only possible error is empty section name, + // but if it's empty, this piece of code won't be executed. + sec, _ = f.NewSection(name) + return sec + } + return sec +} + +// Section returns list of Section. +func (f *File) Sections() []*Section { + sections := make([]*Section, len(f.sectionList)) + for i := range f.sectionList { + sections[i] = f.Section(f.sectionList[i]) + } + return sections +} + +// SectionStrings returns list of section names. +func (f *File) SectionStrings() []string { + list := make([]string, len(f.sectionList)) + copy(list, f.sectionList) + return list +} + +// DeleteSection deletes a section. +func (f *File) DeleteSection(name string) { + if f.BlockMode { + f.lock.Lock() + defer f.lock.Unlock() + } + + if len(name) == 0 { + name = DEFAULT_SECTION + } + + for i, s := range f.sectionList { + if s == name { + f.sectionList = append(f.sectionList[:i], f.sectionList[i+1:]...) + delete(f.sections, name) + return + } + } +} + +func cutComment(str string) string { + i := strings.Index(str, "#") + if i == -1 { + return str + } + return str[:i] +} + +// parse parses data through an io.Reader. +func (f *File) parse(reader io.Reader) error { + buf := bufio.NewReader(reader) + + // Handle BOM-UTF8. + // http://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding + mask, err := buf.Peek(3) + if err == nil && len(mask) >= 3 && mask[0] == 239 && mask[1] == 187 && mask[2] == 191 { + buf.Read(mask) + } + + count := 1 + comments := "" + isEnd := false + + section, err := f.NewSection(DEFAULT_SECTION) + if err != nil { + return err + } + + for { + line, err := buf.ReadString('\n') + line = strings.TrimSpace(line) + length := len(line) + + // Check error and ignore io.EOF just for a moment. + if err != nil { + if err != io.EOF { + return fmt.Errorf("error reading next line: %v", err) + } + // The last line of file could be an empty line. + if length == 0 { + break + } + isEnd = true + } + + // Skip empty lines. + if length == 0 { + continue + } + + switch { + case line[0] == '#' || line[0] == ';': // Comments. + if len(comments) == 0 { + comments = line + } else { + comments += LineBreak + line + } + continue + case line[0] == '[' && line[length-1] == ']': // New sction. + name := strings.TrimSpace(line[1 : length-1]) + section, err = f.NewSection(name) + if err != nil { + return err + } + + if len(comments) > 0 { + section.Comment = comments + comments = "" + } + // Reset counter. + count = 1 + continue + } + + // Other possibilities. + var ( + i int + keyQuote string + kname string + valQuote string + val string + ) + + // Key name surrounded by quotes. + if line[0] == '"' { + if length > 6 && line[0:3] == `"""` { + keyQuote = `"""` + } else { + keyQuote = `"` + } + } else if line[0] == '`' { + keyQuote = "`" + } + if len(keyQuote) > 0 { + qLen := len(keyQuote) + pos := strings.Index(line[qLen:], keyQuote) + if pos == -1 { + return fmt.Errorf("error parsing line: missing closing key quote: %s", line) + } + pos = pos + qLen + i = strings.IndexAny(line[pos:], "=:") + if i < 0 { + return fmt.Errorf("error parsing line: key-value delimiter not found: %s", line) + } else if i == pos { + return fmt.Errorf("error parsing line: key is empty: %s", line) + } + i = i + pos + kname = line[qLen:pos] // Just keep spaces inside quotes. + } else { + i = strings.IndexAny(line, "=:") + if i < 0 { + return fmt.Errorf("error parsing line: key-value delimiter not found: %s", line) + } else if i == 0 { + return fmt.Errorf("error parsing line: key is empty: %s", line) + } + kname = strings.TrimSpace(line[0:i]) + } + + isAutoIncr := false + // Auto increment. + if kname == "-" { + isAutoIncr = true + kname = "#" + fmt.Sprint(count) + count++ + } + + lineRight := strings.TrimSpace(line[i+1:]) + lineRightLength := len(lineRight) + firstChar := "" + if lineRightLength >= 2 { + firstChar = lineRight[0:1] + } + if firstChar == "`" { + valQuote = "`" + } else if lineRightLength >= 6 && lineRight[0:3] == `"""` { + valQuote = `"""` + } + if len(valQuote) > 0 { + qLen := len(valQuote) + pos := strings.LastIndex(lineRight[qLen:], valQuote) + // For multiple lines value. + if pos == -1 { + isEnd := false + val = lineRight[qLen:] + "\n" + for { + next, err := buf.ReadString('\n') + if err != nil { + if err != io.EOF { + return err + } + isEnd = true + } + pos = strings.LastIndex(next, valQuote) + if pos > -1 { + val += next[:pos] + break + } + val += next + if isEnd { + return fmt.Errorf("error parsing line: missing closing key quote from '%s' to '%s'", line, next) + } + } + } else { + val = lineRight[qLen : pos+qLen] + } + } else { + val = strings.TrimSpace(cutComment(lineRight[0:])) + } + + k, err := section.NewKey(kname, val) + if err != nil { + return err + } + k.isAutoIncr = isAutoIncr + if len(comments) > 0 { + k.Comment = comments + comments = "" + } + + if isEnd { + break + } + } + return nil +} + +// Reload reloads and parses all data sources. +func (f *File) Reload() error { + for _, s := range f.dataSources { + r, err := s.Reader() + if err != nil { + return err + } + if err = f.parse(r); err != nil { + return err + } + } + return nil +} + +// Append appends one or more data sources and reloads automatically. +func (f *File) Append(source interface{}, others ...interface{}) error { + ds, err := parseDataSource(source) + if err != nil { + return err + } + f.dataSources = append(f.dataSources, ds) + for _, s := range others { + ds, err = parseDataSource(s) + if err != nil { + return err + } + f.dataSources = append(f.dataSources, ds) + } + return f.Reload() +} + +// SaveTo writes content to filesystem. +func (f *File) SaveTo(filename string) (err error) { + equalSign := "=" + if PrettyFormat { + equalSign = " = " + } + + // Use buffer to make sure target is safe until finish encoding. + buf := bytes.NewBuffer(nil) + for i, sname := range f.sectionList { + sec := f.Section(sname) + if len(sec.Comment) > 0 { + if sec.Comment[0] != '#' && sec.Comment[0] != ';' { + sec.Comment = "; " + sec.Comment + } + if _, err = buf.WriteString(sec.Comment + LineBreak); err != nil { + return err + } + } + + if i > 0 { + if _, err = buf.WriteString("[" + sname + "]" + LineBreak); err != nil { + return err + } + } else { + // Write nothing if default section is empty. + if len(sec.keyList) == 0 { + continue + } + } + + for _, kname := range sec.keyList { + key := sec.Key(kname) + if len(key.Comment) > 0 { + if key.Comment[0] != '#' && key.Comment[0] != ';' { + key.Comment = "; " + key.Comment + } + if _, err = buf.WriteString(key.Comment + LineBreak); err != nil { + return err + } + } + + switch { + case key.isAutoIncr: + kname = "-" + case strings.Contains(kname, "`") || strings.Contains(kname, `"`): + kname = `"""` + kname + `"""` + case strings.Contains(kname, `=`) || strings.Contains(kname, `:`): + kname = "`" + kname + "`" + } + + val := key.value + // In case key value contains "\n", "`" or "\"". + if strings.Contains(val, "\n") || strings.Contains(val, "`") || strings.Contains(val, `"`) { + val = `"""` + val + `"""` + } + if _, err = buf.WriteString(kname + equalSign + val + LineBreak); err != nil { + return err + } + } + + // Put a line between sections. + if _, err = buf.WriteString(LineBreak); err != nil { + return err + } + } + + fw, err := os.Create(filename) + if err != nil { + return err + } + if _, err = buf.WriteTo(fw); err != nil { + return err + } + return fw.Close() +} diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/ini_test.go b/Godeps/_workspace/src/gopkg.in/ini.v1/ini_test.go new file mode 100644 index 0000000..c6daf81 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/ini_test.go @@ -0,0 +1,456 @@ +// Copyright 2014 Unknwon +// +// 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 ini + +import ( + "fmt" + "strings" + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_Version(t *testing.T) { + Convey("Get version", t, func() { + So(Version(), ShouldEqual, _VERSION) + }) +} + +const _CONF_DATA = ` +; Package name +NAME = ini +; Package version +VERSION = v1 +; Package import path +IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s + +# Information about package author +# Bio can be written in multiple lines. +[author] +NAME = Unknwon # Succeeding comment +E-MAIL = fake@localhost +GITHUB = https://github.com/%(NAME)s +BIO = """Gopher. +Coding addict. +Good man. +""" # Succeeding comment + +[package] +CLONE_URL = https://%(IMPORT_PATH)s + +[package.sub] +UNUSED_KEY = should be deleted + +[features] +-: Support read/write comments of keys and sections +-: Support auto-increment of key names +-: Support load multiple files to overwrite key values + +[types] +STRING = str +BOOL = true +BOOL_FALSE = false +FLOAT64 = 1.25 +INT = 10 +TIME = 2015-01-01T20:17:05Z + +[array] +STRINGS = en, zh, de +FLOAT64S = 1.1, 2.2, 3.3 +INTS = 1, 2, 3 +TIMES = 2015-01-01T20:17:05Z,2015-01-01T20:17:05Z,2015-01-01T20:17:05Z + +[note] + +[advance] +true = """"2+3=5"""" +"1+1=2" = true +"""6+1=7""" = true +"""` + "`" + `5+5` + "`" + `""" = 10 +""""6+6"""" = 12 +` + "`" + `7-2=4` + "`" + ` = false +ADDRESS = ` + "`" + `404 road, +NotFound, State, 50000` + "`" + +func Test_Load(t *testing.T) { + Convey("Load from data sources", t, func() { + + Convey("Load with empty data", func() { + So(Empty(), ShouldNotBeNil) + }) + + Convey("Load with multiple data sources", func() { + cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini") + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + }) + }) + + Convey("Bad load process", t, func() { + + Convey("Load from invalid data sources", func() { + _, err := Load(_CONF_DATA) + So(err, ShouldNotBeNil) + + _, err = Load("testdata/404.ini") + So(err, ShouldNotBeNil) + + _, err = Load(1) + So(err, ShouldNotBeNil) + + _, err = Load([]byte(""), 1) + So(err, ShouldNotBeNil) + }) + + Convey("Load with empty section name", func() { + _, err := Load([]byte("[]")) + So(err, ShouldNotBeNil) + }) + + Convey("Load with bad keys", func() { + _, err := Load([]byte(`"""name`)) + So(err, ShouldNotBeNil) + + _, err = Load([]byte(`"""name"""`)) + So(err, ShouldNotBeNil) + + _, err = Load([]byte(`""=1`)) + So(err, ShouldNotBeNil) + + _, err = Load([]byte(`=`)) + So(err, ShouldNotBeNil) + + _, err = Load([]byte(`name`)) + So(err, ShouldNotBeNil) + }) + + Convey("Load with bad values", func() { + _, err := Load([]byte(`name="""Unknwon`)) + So(err, ShouldNotBeNil) + }) + }) +} + +func Test_Values(t *testing.T) { + Convey("Test getting and setting values", t, func() { + cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini") + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + + Convey("Get values in default section", func() { + sec := cfg.Section("") + So(sec, ShouldNotBeNil) + So(sec.Key("NAME").Value(), ShouldEqual, "ini") + So(sec.Key("NAME").String(), ShouldEqual, "ini") + So(sec.Key("NAME").Comment, ShouldEqual, "; Package name") + So(sec.Key("IMPORT_PATH").String(), ShouldEqual, "gopkg.in/ini.v1") + }) + + Convey("Get values in non-default section", func() { + sec := cfg.Section("author") + So(sec, ShouldNotBeNil) + So(sec.Key("NAME").String(), ShouldEqual, "Unknwon") + So(sec.Key("GITHUB").String(), ShouldEqual, "https://github.com/Unknwon") + + sec = cfg.Section("package") + So(sec, ShouldNotBeNil) + So(sec.Key("CLONE_URL").String(), ShouldEqual, "https://gopkg.in/ini.v1") + }) + + Convey("Get auto-increment key names", func() { + keys := cfg.Section("features").Keys() + for i, k := range keys { + So(k.Name(), ShouldEqual, fmt.Sprintf("#%d", i+1)) + } + }) + + Convey("Get overwrite value", func() { + So(cfg.Section("author").Key("E-MAIL").String(), ShouldEqual, "u@gogs.io") + }) + + Convey("Get sections", func() { + sections := cfg.Sections() + for i, name := range []string{DEFAULT_SECTION, "author", "package", "package.sub", "features", "types", "array", "note", "advance"} { + So(sections[i].Name(), ShouldEqual, name) + } + }) + + Convey("Get parent section value", func() { + So(cfg.Section("package.sub").Key("CLONE_URL").String(), ShouldEqual, "https://gopkg.in/ini.v1") + }) + + Convey("Get multiple line value", func() { + So(cfg.Section("author").Key("BIO").String(), ShouldEqual, "Gopher.\nCoding addict.\nGood man.\n") + }) + + Convey("Get values with type", func() { + sec := cfg.Section("types") + v1, err := sec.Key("BOOL").Bool() + So(err, ShouldBeNil) + So(v1, ShouldBeTrue) + + v1, err = sec.Key("BOOL_FALSE").Bool() + So(err, ShouldBeNil) + So(v1, ShouldBeFalse) + + v2, err := sec.Key("FLOAT64").Float64() + So(err, ShouldBeNil) + So(v2, ShouldEqual, 1.25) + + v3, err := sec.Key("INT").Int() + So(err, ShouldBeNil) + So(v3, ShouldEqual, 10) + + v4, err := sec.Key("INT").Int64() + So(err, ShouldBeNil) + So(v4, ShouldEqual, 10) + + t, err := time.Parse(time.RFC3339, "2015-01-01T20:17:05Z") + So(err, ShouldBeNil) + v5, err := sec.Key("TIME").Time() + So(err, ShouldBeNil) + So(v5.String(), ShouldEqual, t.String()) + + Convey("Must get values with type", func() { + So(sec.Key("STRING").MustString("404"), ShouldEqual, "str") + So(sec.Key("BOOL").MustBool(), ShouldBeTrue) + So(sec.Key("FLOAT64").MustFloat64(), ShouldEqual, 1.25) + So(sec.Key("INT").MustInt(), ShouldEqual, 10) + So(sec.Key("INT").MustInt64(), ShouldEqual, 10) + So(sec.Key("TIME").MustTime().String(), ShouldEqual, t.String()) + + Convey("Must get values with default value", func() { + So(sec.Key("STRING_404").MustString("404"), ShouldEqual, "404") + So(sec.Key("BOOL_404").MustBool(true), ShouldBeTrue) + So(sec.Key("FLOAT64_404").MustFloat64(2.5), ShouldEqual, 2.5) + So(sec.Key("INT_404").MustInt(15), ShouldEqual, 15) + So(sec.Key("INT_404").MustInt64(15), ShouldEqual, 15) + + t, err := time.Parse(time.RFC3339, "2014-01-01T20:17:05Z") + So(err, ShouldBeNil) + So(sec.Key("TIME_404").MustTime(t).String(), ShouldEqual, t.String()) + }) + }) + }) + + Convey("Get value with candidates", func() { + sec := cfg.Section("types") + So(sec.Key("STRING").In("", []string{"str", "arr", "types"}), ShouldEqual, "str") + So(sec.Key("FLOAT64").InFloat64(0, []float64{1.25, 2.5, 3.75}), ShouldEqual, 1.25) + So(sec.Key("INT").InInt(0, []int{10, 20, 30}), ShouldEqual, 10) + So(sec.Key("INT").InInt64(0, []int64{10, 20, 30}), ShouldEqual, 10) + + zt, err := time.Parse(time.RFC3339, "0001-01-01T01:00:00Z") + So(err, ShouldBeNil) + t, err := time.Parse(time.RFC3339, "2015-01-01T20:17:05Z") + So(err, ShouldBeNil) + So(sec.Key("TIME").InTime(zt, []time.Time{t, time.Now(), time.Now().Add(1 * time.Second)}).String(), ShouldEqual, t.String()) + + Convey("Get value with candidates and default value", func() { + So(sec.Key("STRING_404").In("str", []string{"str", "arr", "types"}), ShouldEqual, "str") + So(sec.Key("FLOAT64_404").InFloat64(1.25, []float64{1.25, 2.5, 3.75}), ShouldEqual, 1.25) + So(sec.Key("INT_404").InInt(10, []int{10, 20, 30}), ShouldEqual, 10) + So(sec.Key("INT64_404").InInt64(10, []int64{10, 20, 30}), ShouldEqual, 10) + So(sec.Key("TIME_404").InTime(t, []time.Time{time.Now(), time.Now(), time.Now().Add(1 * time.Second)}).String(), ShouldEqual, t.String()) + }) + }) + + Convey("Get values in range", func() { + sec := cfg.Section("types") + So(sec.Key("FLOAT64").RangeFloat64(0, 1, 2), ShouldEqual, 1.25) + So(sec.Key("INT").RangeInt(0, 10, 20), ShouldEqual, 10) + So(sec.Key("INT").RangeInt64(0, 10, 20), ShouldEqual, 10) + + minT, err := time.Parse(time.RFC3339, "0001-01-01T01:00:00Z") + So(err, ShouldBeNil) + midT, err := time.Parse(time.RFC3339, "2013-01-01T01:00:00Z") + So(err, ShouldBeNil) + maxT, err := time.Parse(time.RFC3339, "9999-01-01T01:00:00Z") + So(err, ShouldBeNil) + t, err := time.Parse(time.RFC3339, "2015-01-01T20:17:05Z") + So(err, ShouldBeNil) + So(sec.Key("TIME").RangeTime(t, minT, maxT).String(), ShouldEqual, t.String()) + + Convey("Get value in range with default value", func() { + So(sec.Key("FLOAT64").RangeFloat64(5, 0, 1), ShouldEqual, 5) + So(sec.Key("INT").RangeInt(7, 0, 5), ShouldEqual, 7) + So(sec.Key("INT").RangeInt64(7, 0, 5), ShouldEqual, 7) + So(sec.Key("TIME").RangeTime(t, minT, midT).String(), ShouldEqual, t.String()) + }) + }) + + Convey("Get values into slice", func() { + sec := cfg.Section("array") + So(strings.Join(sec.Key("STRINGS").Strings(","), ","), ShouldEqual, "en,zh,de") + So(len(sec.Key("STRINGS_404").Strings(",")), ShouldEqual, 0) + + vals1 := sec.Key("FLOAT64S").Float64s(",") + for i, v := range []float64{1.1, 2.2, 3.3} { + So(vals1[i], ShouldEqual, v) + } + + vals2 := sec.Key("INTS").Ints(",") + for i, v := range []int{1, 2, 3} { + So(vals2[i], ShouldEqual, v) + } + + vals3 := sec.Key("INTS").Int64s(",") + for i, v := range []int64{1, 2, 3} { + So(vals3[i], ShouldEqual, v) + } + + t, err := time.Parse(time.RFC3339, "2015-01-01T20:17:05Z") + So(err, ShouldBeNil) + vals4 := sec.Key("TIMES").Times(",") + for i, v := range []time.Time{t, t, t} { + So(vals4[i].String(), ShouldEqual, v.String()) + } + }) + + Convey("Get key hash", func() { + cfg.Section("").KeysHash() + }) + + Convey("Set key value", func() { + k := cfg.Section("author").Key("NAME") + k.SetValue("无闻") + So(k.String(), ShouldEqual, "无闻") + }) + + Convey("Get key strings", func() { + So(strings.Join(cfg.Section("types").KeyStrings(), ","), ShouldEqual, "STRING,BOOL,BOOL_FALSE,FLOAT64,INT,TIME") + }) + + Convey("Delete a key", func() { + cfg.Section("package.sub").DeleteKey("UNUSED_KEY") + _, err := cfg.Section("package.sub").GetKey("UNUSED_KEY") + So(err, ShouldNotBeNil) + }) + + Convey("Get section strings", func() { + So(strings.Join(cfg.SectionStrings(), ","), ShouldEqual, "DEFAULT,author,package,package.sub,features,types,array,note,advance") + }) + + Convey("Delete a section", func() { + cfg.DeleteSection("") + So(cfg.SectionStrings()[0], ShouldNotEqual, DEFAULT_SECTION) + }) + + Convey("Create new sections", func() { + cfg.NewSections("test", "test2") + _, err := cfg.GetSection("test") + So(err, ShouldBeNil) + _, err = cfg.GetSection("test2") + So(err, ShouldBeNil) + }) + }) + + Convey("Test getting and setting bad values", t, func() { + cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini") + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + + Convey("Create new key with empty name", func() { + k, err := cfg.Section("").NewKey("", "") + So(err, ShouldNotBeNil) + So(k, ShouldBeNil) + }) + + Convey("Create new section with empty name", func() { + s, err := cfg.NewSection("") + So(err, ShouldNotBeNil) + So(s, ShouldBeNil) + }) + + Convey("Create new sections with empty name", func() { + So(cfg.NewSections(""), ShouldNotBeNil) + }) + + Convey("Get section that not exists", func() { + s, err := cfg.GetSection("404") + So(err, ShouldNotBeNil) + So(s, ShouldBeNil) + + s = cfg.Section("404") + So(s, ShouldNotBeNil) + }) + }) +} + +func Test_File_Append(t *testing.T) { + Convey("Append data sources", t, func() { + cfg, err := Load([]byte("")) + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + + So(cfg.Append([]byte(""), []byte("")), ShouldBeNil) + + Convey("Append bad data sources", func() { + So(cfg.Append(1), ShouldNotBeNil) + So(cfg.Append([]byte(""), 1), ShouldNotBeNil) + }) + }) +} + +func Test_File_SaveTo(t *testing.T) { + Convey("Save file", t, func() { + cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini") + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + + cfg.Section("").Key("NAME").Comment = "Package name" + cfg.Section("author").Comment = `Information about package author +# Bio can be written in multiple lines.` + So(cfg.SaveTo("testdata/conf_out.ini"), ShouldBeNil) + }) +} + +func Benchmark_Key_Value(b *testing.B) { + c, _ := Load([]byte(_CONF_DATA)) + for i := 0; i < b.N; i++ { + c.Section("").Key("NAME").Value() + } +} + +func Benchmark_Key_String(b *testing.B) { + c, _ := Load([]byte(_CONF_DATA)) + for i := 0; i < b.N; i++ { + c.Section("").Key("NAME").String() + } +} + +func Benchmark_Key_Value_NonBlock(b *testing.B) { + c, _ := Load([]byte(_CONF_DATA)) + c.BlockMode = false + for i := 0; i < b.N; i++ { + c.Section("").Key("NAME").Value() + } +} + +func Benchmark_Key_String_NonBlock(b *testing.B) { + c, _ := Load([]byte(_CONF_DATA)) + c.BlockMode = false + for i := 0; i < b.N; i++ { + c.Section("").Key("NAME").String() + } +} + +func Benchmark_Key_SetValue(b *testing.B) { + c, _ := Load([]byte(_CONF_DATA)) + for i := 0; i < b.N; i++ { + c.Section("").Key("NAME").SetValue("10") + } +} diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/struct.go b/Godeps/_workspace/src/gopkg.in/ini.v1/struct.go new file mode 100644 index 0000000..09ea816 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/struct.go @@ -0,0 +1,220 @@ +// Copyright 2014 Unknwon +// +// 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 ini + +import ( + "errors" + "fmt" + "reflect" + "time" + "unicode" +) + +// NameMapper represents a ini tag name mapper. +type NameMapper func(string) string + +// Built-in name getters. +var ( + // AllCapsUnderscore converts to format ALL_CAPS_UNDERSCORE. + AllCapsUnderscore NameMapper = func(raw string) string { + newstr := make([]rune, 0, len(raw)) + for i, chr := range raw { + if isUpper := 'A' <= chr && chr <= 'Z'; isUpper { + if i > 0 { + newstr = append(newstr, '_') + } + } + newstr = append(newstr, unicode.ToUpper(chr)) + } + return string(newstr) + } + // TitleUnderscore converts to format title_underscore. + TitleUnderscore NameMapper = func(raw string) string { + newstr := make([]rune, 0, len(raw)) + for i, chr := range raw { + if isUpper := 'A' <= chr && chr <= 'Z'; isUpper { + if i > 0 { + newstr = append(newstr, '_') + } + chr -= ('A' - 'a') + } + newstr = append(newstr, chr) + } + return string(newstr) + } +) + +func (s *Section) parseFieldName(raw, actual string) string { + if len(actual) > 0 { + return actual + } + if s.f.NameMapper != nil { + return s.f.NameMapper(raw) + } + return raw +} + +func parseDelim(actual string) string { + if len(actual) > 0 { + return actual + } + return "," +} + +var reflectTime = reflect.TypeOf(time.Now()).Kind() + +// setWithProperType sets proper value to field based on its type, +// but it does not return error for failing parsing, +// because we want to use default value that is already assigned to strcut. +func setWithProperType(kind reflect.Kind, key *Key, field reflect.Value, delim string) error { + switch kind { + case reflect.String: + if len(key.String()) == 0 { + return nil + } + field.SetString(key.String()) + case reflect.Bool: + boolVal, err := key.Bool() + if err != nil { + return nil + } + field.SetBool(boolVal) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + intVal, err := key.Int64() + if err != nil { + return nil + } + field.SetInt(intVal) + case reflect.Float64: + floatVal, err := key.Float64() + if err != nil { + return nil + } + field.SetFloat(floatVal) + case reflectTime: + timeVal, err := key.Time() + if err != nil { + return nil + } + field.Set(reflect.ValueOf(timeVal)) + case reflect.Slice: + vals := key.Strings(delim) + numVals := len(vals) + if numVals == 0 { + return nil + } + + sliceOf := field.Type().Elem().Kind() + + var times []time.Time + if sliceOf == reflectTime { + times = key.Times(delim) + } + + slice := reflect.MakeSlice(field.Type(), numVals, numVals) + for i := 0; i < numVals; i++ { + switch sliceOf { + case reflectTime: + slice.Index(i).Set(reflect.ValueOf(times[i])) + default: + slice.Index(i).Set(reflect.ValueOf(vals[i])) + } + } + field.Set(slice) + default: + return fmt.Errorf("unsupported type '%s'", kind) + } + return nil +} + +func (s *Section) mapTo(val reflect.Value) error { + if val.Kind() == reflect.Ptr { + val = val.Elem() + } + typ := val.Type() + + for i := 0; i < typ.NumField(); i++ { + field := val.Field(i) + tpField := typ.Field(i) + + tag := tpField.Tag.Get("ini") + if tag == "-" { + continue + } + + fieldName := s.parseFieldName(tpField.Name, tag) + if len(fieldName) == 0 || !field.CanSet() { + continue + } + + if tpField.Type.Kind() == reflect.Struct { + if sec, err := s.f.GetSection(fieldName); err == nil { + if err = sec.mapTo(field); err != nil { + return fmt.Errorf("error mapping field(%s): %v", fieldName, err) + } + continue + } + } else if tpField.Type.Kind() == reflect.Ptr && tpField.Anonymous { + field.Set(reflect.New(tpField.Type.Elem())) + if sec, err := s.f.GetSection(fieldName); err == nil { + if err = sec.mapTo(field); err != nil { + return fmt.Errorf("error mapping field(%s): %v", fieldName, err) + } + continue + } + } + + if key, err := s.GetKey(fieldName); err == nil { + if err = setWithProperType(tpField.Type.Kind(), key, field, parseDelim(tpField.Tag.Get("delim"))); err != nil { + return fmt.Errorf("error mapping field(%s): %v", fieldName, err) + } + } + } + return nil +} + +// MapTo maps section to given struct. +func (s *Section) MapTo(v interface{}) error { + typ := reflect.TypeOf(v) + val := reflect.ValueOf(v) + if typ.Kind() == reflect.Ptr { + typ = typ.Elem() + val = val.Elem() + } else { + return errors.New("cannot map to non-pointer struct") + } + + return s.mapTo(val) +} + +// MapTo maps file to given struct. +func (f *File) MapTo(v interface{}) error { + return f.Section("").MapTo(v) +} + +// MapTo maps data sources to given struct with name mapper. +func MapToWithMapper(v interface{}, mapper NameMapper, source interface{}, others ...interface{}) error { + cfg, err := Load(source, others...) + if err != nil { + return err + } + cfg.NameMapper = mapper + return cfg.MapTo(v) +} + +// MapTo maps data sources to given struct. +func MapTo(v, source interface{}, others ...interface{}) error { + return MapToWithMapper(v, nil, source, others...) +} diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/struct_test.go b/Godeps/_workspace/src/gopkg.in/ini.v1/struct_test.go new file mode 100644 index 0000000..f6fad19 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/struct_test.go @@ -0,0 +1,181 @@ +// Copyright 2014 Unknwon +// +// 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 ini + +import ( + "strings" + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +type testNested struct { + Cities []string `delim:"|"` + Visits []time.Time + Note string + Unused int `ini:"-"` +} + +type testEmbeded struct { + GPA float64 +} + +type testStruct struct { + Name string `ini:"NAME"` + Age int + Male bool + Money float64 + Born time.Time + Others testNested + *testEmbeded `ini:"grade"` + Unused int `ini:"-"` +} + +const _CONF_DATA_STRUCT = ` +NAME = Unknwon +Age = 21 +Male = true +Money = 1.25 +Born = 1993-10-07T20:17:05Z + +[Others] +Cities = HangZhou|Boston +Visits = 1993-10-07T20:17:05Z, 1993-10-07T20:17:05Z +Note = Hello world! + +[grade] +GPA = 2.8 +` + +type unsupport struct { + Byte byte +} + +type unsupport2 struct { + Others struct { + Cities byte + } +} + +type unsupport3 struct { + Cities byte +} + +type unsupport4 struct { + *unsupport3 `ini:"Others"` +} + +type defaultValue struct { + Name string + Age int + Male bool + Money float64 + Born time.Time + Cities []string +} + +const _INVALID_DATA_CONF_STRUCT = ` +Name = +Age = age +Male = 123 +Money = money +Born = nil +Cities = +` + +func Test_Struct(t *testing.T) { + Convey("Map file to struct", t, func() { + ts := new(testStruct) + So(MapTo(ts, []byte(_CONF_DATA_STRUCT)), ShouldBeNil) + + So(ts.Name, ShouldEqual, "Unknwon") + So(ts.Age, ShouldEqual, 21) + So(ts.Male, ShouldBeTrue) + So(ts.Money, ShouldEqual, 1.25) + + t, err := time.Parse(time.RFC3339, "1993-10-07T20:17:05Z") + So(err, ShouldBeNil) + So(ts.Born.String(), ShouldEqual, t.String()) + + So(strings.Join(ts.Others.Cities, ","), ShouldEqual, "HangZhou,Boston") + So(ts.Others.Visits[0].String(), ShouldEqual, t.String()) + So(ts.Others.Note, ShouldEqual, "Hello world!") + So(ts.testEmbeded.GPA, ShouldEqual, 2.8) + }) + + Convey("Map to non-pointer struct", t, func() { + cfg, err := Load([]byte(_CONF_DATA_STRUCT)) + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + + So(cfg.MapTo(testStruct{}), ShouldNotBeNil) + }) + + Convey("Map to unsupported type", t, func() { + cfg, err := Load([]byte(_CONF_DATA_STRUCT)) + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + + cfg.NameMapper = func(raw string) string { + if raw == "Byte" { + return "NAME" + } + return raw + } + So(cfg.MapTo(&unsupport{}), ShouldNotBeNil) + So(cfg.MapTo(&unsupport2{}), ShouldNotBeNil) + So(cfg.MapTo(&unsupport4{}), ShouldNotBeNil) + }) + + Convey("Map from invalid data source", t, func() { + So(MapTo(&testStruct{}, "hi"), ShouldNotBeNil) + }) + + Convey("Map to wrong types and gain default values", t, func() { + cfg, err := Load([]byte(_INVALID_DATA_CONF_STRUCT)) + So(err, ShouldBeNil) + + t, err := time.Parse(time.RFC3339, "1993-10-07T20:17:05Z") + So(err, ShouldBeNil) + dv := &defaultValue{"Joe", 10, true, 1.25, t, []string{"HangZhou", "Boston"}} + So(cfg.MapTo(dv), ShouldBeNil) + So(dv.Name, ShouldEqual, "Joe") + So(dv.Age, ShouldEqual, 10) + So(dv.Male, ShouldBeTrue) + So(dv.Money, ShouldEqual, 1.25) + So(dv.Born.String(), ShouldEqual, t.String()) + So(strings.Join(dv.Cities, ","), ShouldEqual, "HangZhou,Boston") + }) +} + +type testMapper struct { + PackageName string +} + +func Test_NameGetter(t *testing.T) { + Convey("Test name mappers", t, func() { + So(MapToWithMapper(&testMapper{}, TitleUnderscore, []byte("packag_name=ini")), ShouldBeNil) + + cfg, err := Load([]byte("PACKAGE_NAME=ini")) + So(err, ShouldBeNil) + So(cfg, ShouldNotBeNil) + + cfg.NameMapper = AllCapsUnderscore + tg := new(testMapper) + So(cfg.MapTo(tg), ShouldBeNil) + So(tg.PackageName, ShouldEqual, "ini") + }) +} diff --git a/Godeps/_workspace/src/gopkg.in/ini.v1/testdata/conf.ini b/Godeps/_workspace/src/gopkg.in/ini.v1/testdata/conf.ini new file mode 100644 index 0000000..2ed0ac1 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/ini.v1/testdata/conf.ini @@ -0,0 +1,2 @@ +[author] +E-MAIL = u@gogs.io \ No newline at end of file diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/.travis.yml b/Godeps/_workspace/src/gopkg.in/redis.v2/.travis.yml new file mode 100644 index 0000000..c3cf4b8 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/.travis.yml @@ -0,0 +1,19 @@ +language: go + +services: +- redis-server + +go: + - 1.1 + - 1.2 + - 1.3 + - tip + +install: + - go get gopkg.in/bufio.v1 + - go get gopkg.in/check.v1 + - mkdir -p $HOME/gopath/src/gopkg.in + - ln -s `pwd` $HOME/gopath/src/gopkg.in/redis.v2 + +before_script: + - redis-server testdata/sentinel.conf --sentinel & diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/LICENSE b/Godeps/_workspace/src/gopkg.in/redis.v2/LICENSE new file mode 100644 index 0000000..6855a95 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2012 The Redis Go Client Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/Makefile b/Godeps/_workspace/src/gopkg.in/redis.v2/Makefile new file mode 100644 index 0000000..b250d9b --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/Makefile @@ -0,0 +1,3 @@ +all: + go test gopkg.in/redis.v2 -cpu=1,2,4 + go test gopkg.in/redis.v2 -short -race diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/README.md b/Godeps/_workspace/src/gopkg.in/redis.v2/README.md new file mode 100644 index 0000000..ddf875f --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/README.md @@ -0,0 +1,46 @@ +Redis client for Golang [![Build Status](https://travis-ci.org/go-redis/redis.png?branch=master)](https://travis-ci.org/go-redis/redis) +======================= + +Supports: + +- Redis 2.8 commands except QUIT, MONITOR, SLOWLOG and SYNC. +- Pub/sub. +- Transactions. +- Pipelining. +- Connection pool. +- TLS connections. +- Thread safety. +- Timeouts. +- Redis Sentinel. + +API docs: http://godoc.org/gopkg.in/redis.v2. +Examples: http://godoc.org/gopkg.in/redis.v2#pkg-examples. + +Installation +------------ + +Install: + + go get gopkg.in/redis.v2 + +Look and feel +------------- + +Some corner cases: + + SORT list LIMIT 0 2 ASC + vals, err := client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"}).Result() + + ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2 + vals, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + Min: "-inf", + Max: "+inf", + Offset: 0, + Count: 2, + }).Result() + + ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM + vals, err := client.ZInterStore("out", redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2").Result() + + EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello" + vals, err := client.Eval("return {KEYS[1],ARGV[1]}", []string{"key"}, []string{"hello"}).Result() diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/command.go b/Godeps/_workspace/src/gopkg.in/redis.v2/command.go new file mode 100644 index 0000000..d7c76cf --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/command.go @@ -0,0 +1,597 @@ +package redis + +import ( + "fmt" + "strconv" + "strings" + "time" + + "gopkg.in/bufio.v1" +) + +var ( + _ Cmder = (*Cmd)(nil) + _ Cmder = (*SliceCmd)(nil) + _ Cmder = (*StatusCmd)(nil) + _ Cmder = (*IntCmd)(nil) + _ Cmder = (*DurationCmd)(nil) + _ Cmder = (*BoolCmd)(nil) + _ Cmder = (*StringCmd)(nil) + _ Cmder = (*FloatCmd)(nil) + _ Cmder = (*StringSliceCmd)(nil) + _ Cmder = (*BoolSliceCmd)(nil) + _ Cmder = (*StringStringMapCmd)(nil) + _ Cmder = (*ZSliceCmd)(nil) + _ Cmder = (*ScanCmd)(nil) +) + +type Cmder interface { + args() []string + parseReply(*bufio.Reader) error + setErr(error) + + writeTimeout() *time.Duration + readTimeout() *time.Duration + + Err() error + String() string +} + +func setCmdsErr(cmds []Cmder, e error) { + for _, cmd := range cmds { + cmd.setErr(e) + } +} + +func cmdString(cmd Cmder, val interface{}) string { + s := strings.Join(cmd.args(), " ") + if err := cmd.Err(); err != nil { + return s + ": " + err.Error() + } + if val != nil { + return s + ": " + fmt.Sprint(val) + } + return s + +} + +//------------------------------------------------------------------------------ + +type baseCmd struct { + _args []string + + err error + + _writeTimeout, _readTimeout *time.Duration +} + +func newBaseCmd(args ...string) *baseCmd { + return &baseCmd{ + _args: args, + } +} + +func (cmd *baseCmd) Err() error { + if cmd.err != nil { + return cmd.err + } + return nil +} + +func (cmd *baseCmd) args() []string { + return cmd._args +} + +func (cmd *baseCmd) readTimeout() *time.Duration { + return cmd._readTimeout +} + +func (cmd *baseCmd) setReadTimeout(d time.Duration) { + cmd._readTimeout = &d +} + +func (cmd *baseCmd) writeTimeout() *time.Duration { + return cmd._writeTimeout +} + +func (cmd *baseCmd) setWriteTimeout(d time.Duration) { + cmd._writeTimeout = &d +} + +func (cmd *baseCmd) setErr(e error) { + cmd.err = e +} + +//------------------------------------------------------------------------------ + +type Cmd struct { + *baseCmd + + val interface{} +} + +func NewCmd(args ...string) *Cmd { + return &Cmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *Cmd) Val() interface{} { + return cmd.val +} + +func (cmd *Cmd) Result() (interface{}, error) { + return cmd.val, cmd.err +} + +func (cmd *Cmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *Cmd) parseReply(rd *bufio.Reader) error { + cmd.val, cmd.err = parseReply(rd, parseSlice) + return cmd.err +} + +//------------------------------------------------------------------------------ + +type SliceCmd struct { + *baseCmd + + val []interface{} +} + +func NewSliceCmd(args ...string) *SliceCmd { + return &SliceCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *SliceCmd) Val() []interface{} { + return cmd.val +} + +func (cmd *SliceCmd) Result() ([]interface{}, error) { + return cmd.val, cmd.err +} + +func (cmd *SliceCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *SliceCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, parseSlice) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.([]interface{}) + return nil +} + +//------------------------------------------------------------------------------ + +type StatusCmd struct { + *baseCmd + + val string +} + +func NewStatusCmd(args ...string) *StatusCmd { + return &StatusCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StatusCmd) Val() string { + return cmd.val +} + +func (cmd *StatusCmd) Result() (string, error) { + return cmd.val, cmd.err +} + +func (cmd *StatusCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *StatusCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, nil) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.(string) + return nil +} + +//------------------------------------------------------------------------------ + +type IntCmd struct { + *baseCmd + + val int64 +} + +func NewIntCmd(args ...string) *IntCmd { + return &IntCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *IntCmd) Val() int64 { + return cmd.val +} + +func (cmd *IntCmd) Result() (int64, error) { + return cmd.val, cmd.err +} + +func (cmd *IntCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *IntCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, nil) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.(int64) + return nil +} + +//------------------------------------------------------------------------------ + +type DurationCmd struct { + *baseCmd + + val time.Duration + precision time.Duration +} + +func NewDurationCmd(precision time.Duration, args ...string) *DurationCmd { + return &DurationCmd{ + baseCmd: newBaseCmd(args...), + precision: precision, + } +} + +func (cmd *DurationCmd) Val() time.Duration { + return cmd.val +} + +func (cmd *DurationCmd) Result() (time.Duration, error) { + return cmd.val, cmd.err +} + +func (cmd *DurationCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *DurationCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, nil) + if err != nil { + cmd.err = err + return err + } + cmd.val = time.Duration(v.(int64)) * cmd.precision + return nil +} + +//------------------------------------------------------------------------------ + +type BoolCmd struct { + *baseCmd + + val bool +} + +func NewBoolCmd(args ...string) *BoolCmd { + return &BoolCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *BoolCmd) Val() bool { + return cmd.val +} + +func (cmd *BoolCmd) Result() (bool, error) { + return cmd.val, cmd.err +} + +func (cmd *BoolCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *BoolCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, nil) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.(int64) == 1 + return nil +} + +//------------------------------------------------------------------------------ + +type StringCmd struct { + *baseCmd + + val string +} + +func NewStringCmd(args ...string) *StringCmd { + return &StringCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StringCmd) Val() string { + return cmd.val +} + +func (cmd *StringCmd) Result() (string, error) { + return cmd.val, cmd.err +} + +func (cmd *StringCmd) Int64() (int64, error) { + if cmd.err != nil { + return 0, cmd.err + } + return strconv.ParseInt(cmd.val, 10, 64) +} + +func (cmd *StringCmd) Uint64() (uint64, error) { + if cmd.err != nil { + return 0, cmd.err + } + return strconv.ParseUint(cmd.val, 10, 64) +} + +func (cmd *StringCmd) Float64() (float64, error) { + if cmd.err != nil { + return 0, cmd.err + } + return strconv.ParseFloat(cmd.val, 64) +} + +func (cmd *StringCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *StringCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, nil) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.(string) + return nil +} + +//------------------------------------------------------------------------------ + +type FloatCmd struct { + *baseCmd + + val float64 +} + +func NewFloatCmd(args ...string) *FloatCmd { + return &FloatCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *FloatCmd) Val() float64 { + return cmd.val +} + +func (cmd *FloatCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *FloatCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, nil) + if err != nil { + cmd.err = err + return err + } + cmd.val, cmd.err = strconv.ParseFloat(v.(string), 64) + return cmd.err +} + +//------------------------------------------------------------------------------ + +type StringSliceCmd struct { + *baseCmd + + val []string +} + +func NewStringSliceCmd(args ...string) *StringSliceCmd { + return &StringSliceCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StringSliceCmd) Val() []string { + return cmd.val +} + +func (cmd *StringSliceCmd) Result() ([]string, error) { + return cmd.Val(), cmd.Err() +} + +func (cmd *StringSliceCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *StringSliceCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, parseStringSlice) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.([]string) + return nil +} + +//------------------------------------------------------------------------------ + +type BoolSliceCmd struct { + *baseCmd + + val []bool +} + +func NewBoolSliceCmd(args ...string) *BoolSliceCmd { + return &BoolSliceCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *BoolSliceCmd) Val() []bool { + return cmd.val +} + +func (cmd *BoolSliceCmd) Result() ([]bool, error) { + return cmd.val, cmd.err +} + +func (cmd *BoolSliceCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *BoolSliceCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, parseBoolSlice) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.([]bool) + return nil +} + +//------------------------------------------------------------------------------ + +type StringStringMapCmd struct { + *baseCmd + + val map[string]string +} + +func NewStringStringMapCmd(args ...string) *StringStringMapCmd { + return &StringStringMapCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *StringStringMapCmd) Val() map[string]string { + return cmd.val +} + +func (cmd *StringStringMapCmd) Result() (map[string]string, error) { + return cmd.val, cmd.err +} + +func (cmd *StringStringMapCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *StringStringMapCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, parseStringStringMap) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.(map[string]string) + return nil +} + +//------------------------------------------------------------------------------ + +type ZSliceCmd struct { + *baseCmd + + val []Z +} + +func NewZSliceCmd(args ...string) *ZSliceCmd { + return &ZSliceCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *ZSliceCmd) Val() []Z { + return cmd.val +} + +func (cmd *ZSliceCmd) Result() ([]Z, error) { + return cmd.val, cmd.err +} + +func (cmd *ZSliceCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *ZSliceCmd) parseReply(rd *bufio.Reader) error { + v, err := parseReply(rd, parseZSlice) + if err != nil { + cmd.err = err + return err + } + cmd.val = v.([]Z) + return nil +} + +//------------------------------------------------------------------------------ + +type ScanCmd struct { + *baseCmd + + cursor int64 + keys []string +} + +func NewScanCmd(args ...string) *ScanCmd { + return &ScanCmd{ + baseCmd: newBaseCmd(args...), + } +} + +func (cmd *ScanCmd) Val() (int64, []string) { + return cmd.cursor, cmd.keys +} + +func (cmd *ScanCmd) Result() (int64, []string, error) { + return cmd.cursor, cmd.keys, cmd.err +} + +func (cmd *ScanCmd) String() string { + return cmdString(cmd, cmd.keys) +} + +func (cmd *ScanCmd) parseReply(rd *bufio.Reader) error { + vi, err := parseReply(rd, parseSlice) + if err != nil { + cmd.err = err + return cmd.err + } + v := vi.([]interface{}) + + cmd.cursor, cmd.err = strconv.ParseInt(v[0].(string), 10, 64) + if cmd.err != nil { + return cmd.err + } + + keys := v[1].([]interface{}) + for _, keyi := range keys { + cmd.keys = append(cmd.keys, keyi.(string)) + } + + return nil +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/commands.go b/Godeps/_workspace/src/gopkg.in/redis.v2/commands.go new file mode 100644 index 0000000..6068bab --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/commands.go @@ -0,0 +1,1246 @@ +package redis + +import ( + "io" + "strconv" + "time" +) + +func formatFloat(f float64) string { + return strconv.FormatFloat(f, 'f', -1, 64) +} + +func readTimeout(sec int64) time.Duration { + if sec == 0 { + return 0 + } + return time.Duration(sec+1) * time.Second +} + +//------------------------------------------------------------------------------ + +func (c *Client) Auth(password string) *StatusCmd { + cmd := NewStatusCmd("AUTH", password) + c.Process(cmd) + return cmd +} + +func (c *Client) Echo(message string) *StringCmd { + cmd := NewStringCmd("ECHO", message) + c.Process(cmd) + return cmd +} + +func (c *Client) Ping() *StatusCmd { + cmd := NewStatusCmd("PING") + c.Process(cmd) + return cmd +} + +func (c *Client) Quit() *StatusCmd { + panic("not implemented") +} + +func (c *Client) Select(index int64) *StatusCmd { + cmd := NewStatusCmd("SELECT", strconv.FormatInt(index, 10)) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) Del(keys ...string) *IntCmd { + args := append([]string{"DEL"}, keys...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) Dump(key string) *StringCmd { + cmd := NewStringCmd("DUMP", key) + c.Process(cmd) + return cmd +} + +func (c *Client) Exists(key string) *BoolCmd { + cmd := NewBoolCmd("EXISTS", key) + c.Process(cmd) + return cmd +} + +func (c *Client) Expire(key string, dur time.Duration) *BoolCmd { + cmd := NewBoolCmd("EXPIRE", key, strconv.FormatInt(int64(dur/time.Second), 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) ExpireAt(key string, tm time.Time) *BoolCmd { + cmd := NewBoolCmd("EXPIREAT", key, strconv.FormatInt(tm.Unix(), 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) Keys(pattern string) *StringSliceCmd { + cmd := NewStringSliceCmd("KEYS", pattern) + c.Process(cmd) + return cmd +} + +func (c *Client) Migrate(host, port, key string, db, timeout int64) *StatusCmd { + cmd := NewStatusCmd( + "MIGRATE", + host, + port, + key, + strconv.FormatInt(db, 10), + strconv.FormatInt(timeout, 10), + ) + cmd.setReadTimeout(readTimeout(timeout)) + c.Process(cmd) + return cmd +} + +func (c *Client) Move(key string, db int64) *BoolCmd { + cmd := NewBoolCmd("MOVE", key, strconv.FormatInt(db, 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) ObjectRefCount(keys ...string) *IntCmd { + args := append([]string{"OBJECT", "REFCOUNT"}, keys...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ObjectEncoding(keys ...string) *StringCmd { + args := append([]string{"OBJECT", "ENCODING"}, keys...) + cmd := NewStringCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ObjectIdleTime(keys ...string) *DurationCmd { + args := append([]string{"OBJECT", "IDLETIME"}, keys...) + cmd := NewDurationCmd(time.Second, args...) + c.Process(cmd) + return cmd +} + +func (c *Client) Persist(key string) *BoolCmd { + cmd := NewBoolCmd("PERSIST", key) + c.Process(cmd) + return cmd +} + +func (c *Client) PExpire(key string, dur time.Duration) *BoolCmd { + cmd := NewBoolCmd("PEXPIRE", key, strconv.FormatInt(int64(dur/time.Millisecond), 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) PExpireAt(key string, tm time.Time) *BoolCmd { + cmd := NewBoolCmd( + "PEXPIREAT", + key, + strconv.FormatInt(tm.UnixNano()/int64(time.Millisecond), 10), + ) + c.Process(cmd) + return cmd +} + +func (c *Client) PTTL(key string) *DurationCmd { + cmd := NewDurationCmd(time.Millisecond, "PTTL", key) + c.Process(cmd) + return cmd +} + +func (c *Client) RandomKey() *StringCmd { + cmd := NewStringCmd("RANDOMKEY") + c.Process(cmd) + return cmd +} + +func (c *Client) Rename(key, newkey string) *StatusCmd { + cmd := NewStatusCmd("RENAME", key, newkey) + c.Process(cmd) + return cmd +} + +func (c *Client) RenameNX(key, newkey string) *BoolCmd { + cmd := NewBoolCmd("RENAMENX", key, newkey) + c.Process(cmd) + return cmd +} + +func (c *Client) Restore(key string, ttl int64, value string) *StatusCmd { + cmd := NewStatusCmd( + "RESTORE", + key, + strconv.FormatInt(ttl, 10), + value, + ) + c.Process(cmd) + return cmd +} + +type Sort struct { + By string + Offset, Count float64 + Get []string + Order string + IsAlpha bool + Store string +} + +func (c *Client) Sort(key string, sort Sort) *StringSliceCmd { + args := []string{"SORT", key} + if sort.By != "" { + args = append(args, "BY", sort.By) + } + if sort.Offset != 0 || sort.Count != 0 { + args = append(args, "LIMIT", formatFloat(sort.Offset), formatFloat(sort.Count)) + } + for _, get := range sort.Get { + args = append(args, "GET", get) + } + if sort.Order != "" { + args = append(args, sort.Order) + } + if sort.IsAlpha { + args = append(args, "ALPHA") + } + if sort.Store != "" { + args = append(args, "STORE", sort.Store) + } + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) TTL(key string) *DurationCmd { + cmd := NewDurationCmd(time.Second, "TTL", key) + c.Process(cmd) + return cmd +} + +func (c *Client) Type(key string) *StatusCmd { + cmd := NewStatusCmd("TYPE", key) + c.Process(cmd) + return cmd +} + +func (c *Client) Scan(cursor int64, match string, count int64) *ScanCmd { + args := []string{"SCAN", strconv.FormatInt(cursor, 10)} + if match != "" { + args = append(args, "MATCH", match) + } + if count > 0 { + args = append(args, "COUNT", strconv.FormatInt(count, 10)) + } + cmd := NewScanCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) SScan(key string, cursor int64, match string, count int64) *ScanCmd { + args := []string{"SSCAN", key, strconv.FormatInt(cursor, 10)} + if match != "" { + args = append(args, "MATCH", match) + } + if count > 0 { + args = append(args, "COUNT", strconv.FormatInt(count, 10)) + } + cmd := NewScanCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) HScan(key string, cursor int64, match string, count int64) *ScanCmd { + args := []string{"HSCAN", key, strconv.FormatInt(cursor, 10)} + if match != "" { + args = append(args, "MATCH", match) + } + if count > 0 { + args = append(args, "COUNT", strconv.FormatInt(count, 10)) + } + cmd := NewScanCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZScan(key string, cursor int64, match string, count int64) *ScanCmd { + args := []string{"ZSCAN", key, strconv.FormatInt(cursor, 10)} + if match != "" { + args = append(args, "MATCH", match) + } + if count > 0 { + args = append(args, "COUNT", strconv.FormatInt(count, 10)) + } + cmd := NewScanCmd(args...) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) Append(key, value string) *IntCmd { + cmd := NewIntCmd("APPEND", key, value) + c.Process(cmd) + return cmd +} + +type BitCount struct { + Start, End int64 +} + +func (c *Client) BitCount(key string, bitCount *BitCount) *IntCmd { + args := []string{"BITCOUNT", key} + if bitCount != nil { + args = append( + args, + strconv.FormatInt(bitCount.Start, 10), + strconv.FormatInt(bitCount.End, 10), + ) + } + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) bitOp(op, destKey string, keys ...string) *IntCmd { + args := []string{"BITOP", op, destKey} + args = append(args, keys...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) BitOpAnd(destKey string, keys ...string) *IntCmd { + return c.bitOp("AND", destKey, keys...) +} + +func (c *Client) BitOpOr(destKey string, keys ...string) *IntCmd { + return c.bitOp("OR", destKey, keys...) +} + +func (c *Client) BitOpXor(destKey string, keys ...string) *IntCmd { + return c.bitOp("XOR", destKey, keys...) +} + +func (c *Client) BitOpNot(destKey string, key string) *IntCmd { + return c.bitOp("NOT", destKey, key) +} + +func (c *Client) Decr(key string) *IntCmd { + cmd := NewIntCmd("DECR", key) + c.Process(cmd) + return cmd +} + +func (c *Client) DecrBy(key string, decrement int64) *IntCmd { + cmd := NewIntCmd("DECRBY", key, strconv.FormatInt(decrement, 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) Get(key string) *StringCmd { + cmd := NewStringCmd("GET", key) + c.Process(cmd) + return cmd +} + +func (c *Client) GetBit(key string, offset int64) *IntCmd { + cmd := NewIntCmd("GETBIT", key, strconv.FormatInt(offset, 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) GetRange(key string, start, end int64) *StringCmd { + cmd := NewStringCmd( + "GETRANGE", + key, + strconv.FormatInt(start, 10), + strconv.FormatInt(end, 10), + ) + c.Process(cmd) + return cmd +} + +func (c *Client) GetSet(key, value string) *StringCmd { + cmd := NewStringCmd("GETSET", key, value) + c.Process(cmd) + return cmd +} + +func (c *Client) Incr(key string) *IntCmd { + cmd := NewIntCmd("INCR", key) + c.Process(cmd) + return cmd +} + +func (c *Client) IncrBy(key string, value int64) *IntCmd { + cmd := NewIntCmd("INCRBY", key, strconv.FormatInt(value, 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) IncrByFloat(key string, value float64) *FloatCmd { + cmd := NewFloatCmd("INCRBYFLOAT", key, formatFloat(value)) + c.Process(cmd) + return cmd +} + +func (c *Client) MGet(keys ...string) *SliceCmd { + args := append([]string{"MGET"}, keys...) + cmd := NewSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) MSet(pairs ...string) *StatusCmd { + args := append([]string{"MSET"}, pairs...) + cmd := NewStatusCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) MSetNX(pairs ...string) *BoolCmd { + args := append([]string{"MSETNX"}, pairs...) + cmd := NewBoolCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) PSetEx(key string, dur time.Duration, value string) *StatusCmd { + cmd := NewStatusCmd( + "PSETEX", + key, + strconv.FormatInt(int64(dur/time.Millisecond), 10), + value, + ) + c.Process(cmd) + return cmd +} + +func (c *Client) Set(key, value string) *StatusCmd { + cmd := NewStatusCmd("SET", key, value) + c.Process(cmd) + return cmd +} + +func (c *Client) SetBit(key string, offset int64, value int) *IntCmd { + cmd := NewIntCmd( + "SETBIT", + key, + strconv.FormatInt(offset, 10), + strconv.FormatInt(int64(value), 10), + ) + c.Process(cmd) + return cmd +} + +func (c *Client) SetEx(key string, dur time.Duration, value string) *StatusCmd { + cmd := NewStatusCmd("SETEX", key, strconv.FormatInt(int64(dur/time.Second), 10), value) + c.Process(cmd) + return cmd +} + +func (c *Client) SetNX(key, value string) *BoolCmd { + cmd := NewBoolCmd("SETNX", key, value) + c.Process(cmd) + return cmd +} + +func (c *Client) SetRange(key string, offset int64, value string) *IntCmd { + cmd := NewIntCmd("SETRANGE", key, strconv.FormatInt(offset, 10), value) + c.Process(cmd) + return cmd +} + +func (c *Client) StrLen(key string) *IntCmd { + cmd := NewIntCmd("STRLEN", key) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) HDel(key string, fields ...string) *IntCmd { + args := append([]string{"HDEL", key}, fields...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) HExists(key, field string) *BoolCmd { + cmd := NewBoolCmd("HEXISTS", key, field) + c.Process(cmd) + return cmd +} + +func (c *Client) HGet(key, field string) *StringCmd { + cmd := NewStringCmd("HGET", key, field) + c.Process(cmd) + return cmd +} + +func (c *Client) HGetAll(key string) *StringSliceCmd { + cmd := NewStringSliceCmd("HGETALL", key) + c.Process(cmd) + return cmd +} + +func (c *Client) HGetAllMap(key string) *StringStringMapCmd { + cmd := NewStringStringMapCmd("HGETALL", key) + c.Process(cmd) + return cmd +} + +func (c *Client) HIncrBy(key, field string, incr int64) *IntCmd { + cmd := NewIntCmd("HINCRBY", key, field, strconv.FormatInt(incr, 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) HIncrByFloat(key, field string, incr float64) *FloatCmd { + cmd := NewFloatCmd("HINCRBYFLOAT", key, field, formatFloat(incr)) + c.Process(cmd) + return cmd +} + +func (c *Client) HKeys(key string) *StringSliceCmd { + cmd := NewStringSliceCmd("HKEYS", key) + c.Process(cmd) + return cmd +} + +func (c *Client) HLen(key string) *IntCmd { + cmd := NewIntCmd("HLEN", key) + c.Process(cmd) + return cmd +} + +func (c *Client) HMGet(key string, fields ...string) *SliceCmd { + args := append([]string{"HMGET", key}, fields...) + cmd := NewSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) HMSet(key, field, value string, pairs ...string) *StatusCmd { + args := append([]string{"HMSET", key, field, value}, pairs...) + cmd := NewStatusCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) HSet(key, field, value string) *BoolCmd { + cmd := NewBoolCmd("HSET", key, field, value) + c.Process(cmd) + return cmd +} + +func (c *Client) HSetNX(key, field, value string) *BoolCmd { + cmd := NewBoolCmd("HSETNX", key, field, value) + c.Process(cmd) + return cmd +} + +func (c *Client) HVals(key string) *StringSliceCmd { + cmd := NewStringSliceCmd("HVALS", key) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) BLPop(timeout int64, keys ...string) *StringSliceCmd { + args := append([]string{"BLPOP"}, keys...) + args = append(args, strconv.FormatInt(timeout, 10)) + cmd := NewStringSliceCmd(args...) + cmd.setReadTimeout(readTimeout(timeout)) + c.Process(cmd) + return cmd +} + +func (c *Client) BRPop(timeout int64, keys ...string) *StringSliceCmd { + args := append([]string{"BRPOP"}, keys...) + args = append(args, strconv.FormatInt(timeout, 10)) + cmd := NewStringSliceCmd(args...) + cmd.setReadTimeout(readTimeout(timeout)) + c.Process(cmd) + return cmd +} + +func (c *Client) BRPopLPush(source, destination string, timeout int64) *StringCmd { + cmd := NewStringCmd( + "BRPOPLPUSH", + source, + destination, + strconv.FormatInt(timeout, 10), + ) + cmd.setReadTimeout(readTimeout(timeout)) + c.Process(cmd) + return cmd +} + +func (c *Client) LIndex(key string, index int64) *StringCmd { + cmd := NewStringCmd("LINDEX", key, strconv.FormatInt(index, 10)) + c.Process(cmd) + return cmd +} + +func (c *Client) LInsert(key, op, pivot, value string) *IntCmd { + cmd := NewIntCmd("LINSERT", key, op, pivot, value) + c.Process(cmd) + return cmd +} + +func (c *Client) LLen(key string) *IntCmd { + cmd := NewIntCmd("LLEN", key) + c.Process(cmd) + return cmd +} + +func (c *Client) LPop(key string) *StringCmd { + cmd := NewStringCmd("LPOP", key) + c.Process(cmd) + return cmd +} + +func (c *Client) LPush(key string, values ...string) *IntCmd { + args := append([]string{"LPUSH", key}, values...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) LPushX(key, value string) *IntCmd { + cmd := NewIntCmd("LPUSHX", key, value) + c.Process(cmd) + return cmd +} + +func (c *Client) LRange(key string, start, stop int64) *StringSliceCmd { + cmd := NewStringSliceCmd( + "LRANGE", + key, + strconv.FormatInt(start, 10), + strconv.FormatInt(stop, 10), + ) + c.Process(cmd) + return cmd +} + +func (c *Client) LRem(key string, count int64, value string) *IntCmd { + cmd := NewIntCmd("LREM", key, strconv.FormatInt(count, 10), value) + c.Process(cmd) + return cmd +} + +func (c *Client) LSet(key string, index int64, value string) *StatusCmd { + cmd := NewStatusCmd("LSET", key, strconv.FormatInt(index, 10), value) + c.Process(cmd) + return cmd +} + +func (c *Client) LTrim(key string, start, stop int64) *StatusCmd { + cmd := NewStatusCmd( + "LTRIM", + key, + strconv.FormatInt(start, 10), + strconv.FormatInt(stop, 10), + ) + c.Process(cmd) + return cmd +} + +func (c *Client) RPop(key string) *StringCmd { + cmd := NewStringCmd("RPOP", key) + c.Process(cmd) + return cmd +} + +func (c *Client) RPopLPush(source, destination string) *StringCmd { + cmd := NewStringCmd("RPOPLPUSH", source, destination) + c.Process(cmd) + return cmd +} + +func (c *Client) RPush(key string, values ...string) *IntCmd { + args := append([]string{"RPUSH", key}, values...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) RPushX(key string, value string) *IntCmd { + cmd := NewIntCmd("RPUSHX", key, value) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) SAdd(key string, members ...string) *IntCmd { + args := append([]string{"SADD", key}, members...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) SCard(key string) *IntCmd { + cmd := NewIntCmd("SCARD", key) + c.Process(cmd) + return cmd +} + +func (c *Client) SDiff(keys ...string) *StringSliceCmd { + args := append([]string{"SDIFF"}, keys...) + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) SDiffStore(destination string, keys ...string) *IntCmd { + args := append([]string{"SDIFFSTORE", destination}, keys...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) SInter(keys ...string) *StringSliceCmd { + args := append([]string{"SINTER"}, keys...) + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) SInterStore(destination string, keys ...string) *IntCmd { + args := append([]string{"SINTERSTORE", destination}, keys...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) SIsMember(key, member string) *BoolCmd { + cmd := NewBoolCmd("SISMEMBER", key, member) + c.Process(cmd) + return cmd +} + +func (c *Client) SMembers(key string) *StringSliceCmd { + cmd := NewStringSliceCmd("SMEMBERS", key) + c.Process(cmd) + return cmd +} + +func (c *Client) SMove(source, destination, member string) *BoolCmd { + cmd := NewBoolCmd("SMOVE", source, destination, member) + c.Process(cmd) + return cmd +} + +func (c *Client) SPop(key string) *StringCmd { + cmd := NewStringCmd("SPOP", key) + c.Process(cmd) + return cmd +} + +func (c *Client) SRandMember(key string) *StringCmd { + cmd := NewStringCmd("SRANDMEMBER", key) + c.Process(cmd) + return cmd +} + +func (c *Client) SRem(key string, members ...string) *IntCmd { + args := append([]string{"SREM", key}, members...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) SUnion(keys ...string) *StringSliceCmd { + args := append([]string{"SUNION"}, keys...) + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) SUnionStore(destination string, keys ...string) *IntCmd { + args := append([]string{"SUNIONSTORE", destination}, keys...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +type Z struct { + Score float64 + Member string +} + +type ZStore struct { + Weights []int64 + Aggregate string +} + +func (c *Client) ZAdd(key string, members ...Z) *IntCmd { + args := []string{"ZADD", key} + for _, m := range members { + args = append(args, formatFloat(m.Score), m.Member) + } + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZCard(key string) *IntCmd { + cmd := NewIntCmd("ZCARD", key) + c.Process(cmd) + return cmd +} + +func (c *Client) ZCount(key, min, max string) *IntCmd { + cmd := NewIntCmd("ZCOUNT", key, min, max) + c.Process(cmd) + return cmd +} + +func (c *Client) ZIncrBy(key string, increment float64, member string) *FloatCmd { + cmd := NewFloatCmd("ZINCRBY", key, formatFloat(increment), member) + c.Process(cmd) + return cmd +} + +func (c *Client) ZInterStore( + destination string, + store ZStore, + keys ...string, +) *IntCmd { + args := []string{"ZINTERSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)} + args = append(args, keys...) + if len(store.Weights) > 0 { + args = append(args, "WEIGHTS") + for _, weight := range store.Weights { + args = append(args, strconv.FormatInt(weight, 10)) + } + } + if store.Aggregate != "" { + args = append(args, "AGGREGATE", store.Aggregate) + } + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd { + args := []string{ + "ZRANGE", + key, + strconv.FormatInt(start, 10), + strconv.FormatInt(stop, 10), + } + if withScores { + args = append(args, "WITHSCORES") + } + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRange(key string, start, stop int64) *StringSliceCmd { + return c.zRange(key, start, stop, false) +} + +func (c *Client) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd { + args := []string{ + "ZRANGE", + key, + strconv.FormatInt(start, 10), + strconv.FormatInt(stop, 10), + "WITHSCORES", + } + cmd := NewZSliceCmd(args...) + c.Process(cmd) + return cmd +} + +type ZRangeByScore struct { + Min, Max string + + Offset, Count int64 +} + +func (c *Client) zRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd { + args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max} + if withScores { + args = append(args, "WITHSCORES") + } + if opt.Offset != 0 || opt.Count != 0 { + args = append( + args, + "LIMIT", + strconv.FormatInt(opt.Offset, 10), + strconv.FormatInt(opt.Count, 10), + ) + } + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd { + return c.zRangeByScore(key, opt, false) +} + +func (c *Client) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd { + args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"} + if opt.Offset != 0 || opt.Count != 0 { + args = append( + args, + "LIMIT", + strconv.FormatInt(opt.Offset, 10), + strconv.FormatInt(opt.Count, 10), + ) + } + cmd := NewZSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRank(key, member string) *IntCmd { + cmd := NewIntCmd("ZRANK", key, member) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRem(key string, members ...string) *IntCmd { + args := append([]string{"ZREM", key}, members...) + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRemRangeByRank(key string, start, stop int64) *IntCmd { + cmd := NewIntCmd( + "ZREMRANGEBYRANK", + key, + strconv.FormatInt(start, 10), + strconv.FormatInt(stop, 10), + ) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRemRangeByScore(key, min, max string) *IntCmd { + cmd := NewIntCmd("ZREMRANGEBYSCORE", key, min, max) + c.Process(cmd) + return cmd +} + +func (c *Client) zRevRange(key, start, stop string, withScores bool) *StringSliceCmd { + args := []string{"ZREVRANGE", key, start, stop} + if withScores { + args = append(args, "WITHSCORES") + } + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRevRange(key, start, stop string) *StringSliceCmd { + return c.zRevRange(key, start, stop, false) +} + +func (c *Client) ZRevRangeWithScores(key, start, stop string) *ZSliceCmd { + args := []string{"ZREVRANGE", key, start, stop, "WITHSCORES"} + cmd := NewZSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) zRevRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd { + args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min} + if withScores { + args = append(args, "WITHSCORES") + } + if opt.Offset != 0 || opt.Count != 0 { + args = append( + args, + "LIMIT", + strconv.FormatInt(opt.Offset, 10), + strconv.FormatInt(opt.Count, 10), + ) + } + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd { + return c.zRevRangeByScore(key, opt, false) +} + +func (c *Client) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd { + args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"} + if opt.Offset != 0 || opt.Count != 0 { + args = append( + args, + "LIMIT", + strconv.FormatInt(opt.Offset, 10), + strconv.FormatInt(opt.Count, 10), + ) + } + cmd := NewZSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ZRevRank(key, member string) *IntCmd { + cmd := NewIntCmd("ZREVRANK", key, member) + c.Process(cmd) + return cmd +} + +func (c *Client) ZScore(key, member string) *FloatCmd { + cmd := NewFloatCmd("ZSCORE", key, member) + c.Process(cmd) + return cmd +} + +func (c *Client) ZUnionStore( + destination string, + store ZStore, + keys ...string, +) *IntCmd { + args := []string{"ZUNIONSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)} + args = append(args, keys...) + if len(store.Weights) > 0 { + args = append(args, "WEIGHTS") + for _, weight := range store.Weights { + args = append(args, strconv.FormatInt(weight, 10)) + } + } + if store.Aggregate != "" { + args = append(args, "AGGREGATE", store.Aggregate) + } + cmd := NewIntCmd(args...) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) BgRewriteAOF() *StatusCmd { + cmd := NewStatusCmd("BGREWRITEAOF") + c.Process(cmd) + return cmd +} + +func (c *Client) BgSave() *StatusCmd { + cmd := NewStatusCmd("BGSAVE") + c.Process(cmd) + return cmd +} + +func (c *Client) ClientKill(ipPort string) *StatusCmd { + cmd := NewStatusCmd("CLIENT", "KILL", ipPort) + c.Process(cmd) + return cmd +} + +func (c *Client) ClientList() *StringCmd { + cmd := NewStringCmd("CLIENT", "LIST") + c.Process(cmd) + return cmd +} + +func (c *Client) ConfigGet(parameter string) *SliceCmd { + cmd := NewSliceCmd("CONFIG", "GET", parameter) + c.Process(cmd) + return cmd +} + +func (c *Client) ConfigResetStat() *StatusCmd { + cmd := NewStatusCmd("CONFIG", "RESETSTAT") + c.Process(cmd) + return cmd +} + +func (c *Client) ConfigSet(parameter, value string) *StatusCmd { + cmd := NewStatusCmd("CONFIG", "SET", parameter, value) + c.Process(cmd) + return cmd +} + +func (c *Client) DbSize() *IntCmd { + cmd := NewIntCmd("DBSIZE") + c.Process(cmd) + return cmd +} + +func (c *Client) FlushAll() *StatusCmd { + cmd := NewStatusCmd("FLUSHALL") + c.Process(cmd) + return cmd +} + +func (c *Client) FlushDb() *StatusCmd { + cmd := NewStatusCmd("FLUSHDB") + c.Process(cmd) + return cmd +} + +func (c *Client) Info() *StringCmd { + cmd := NewStringCmd("INFO") + c.Process(cmd) + return cmd +} + +func (c *Client) LastSave() *IntCmd { + cmd := NewIntCmd("LASTSAVE") + c.Process(cmd) + return cmd +} + +func (c *Client) Save() *StatusCmd { + cmd := NewStatusCmd("SAVE") + c.Process(cmd) + return cmd +} + +func (c *Client) shutdown(modifier string) *StatusCmd { + var args []string + if modifier == "" { + args = []string{"SHUTDOWN"} + } else { + args = []string{"SHUTDOWN", modifier} + } + cmd := NewStatusCmd(args...) + c.Process(cmd) + if err := cmd.Err(); err != nil { + if err == io.EOF { + // Server quit as expected. + cmd.err = nil + } + } else { + // Server did not quit. String reply contains the reason. + cmd.err = errorf(cmd.val) + cmd.val = "" + } + return cmd +} + +func (c *Client) Shutdown() *StatusCmd { + return c.shutdown("") +} + +func (c *Client) ShutdownSave() *StatusCmd { + return c.shutdown("SAVE") +} + +func (c *Client) ShutdownNoSave() *StatusCmd { + return c.shutdown("NOSAVE") +} + +func (c *Client) SlaveOf(host, port string) *StatusCmd { + cmd := NewStatusCmd("SLAVEOF", host, port) + c.Process(cmd) + return cmd +} + +func (c *Client) SlowLog() { + panic("not implemented") +} + +func (c *Client) Sync() { + panic("not implemented") +} + +func (c *Client) Time() *StringSliceCmd { + cmd := NewStringSliceCmd("TIME") + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) Eval(script string, keys []string, args []string) *Cmd { + cmdArgs := []string{"EVAL", script, strconv.FormatInt(int64(len(keys)), 10)} + cmdArgs = append(cmdArgs, keys...) + cmdArgs = append(cmdArgs, args...) + cmd := NewCmd(cmdArgs...) + c.Process(cmd) + return cmd +} + +func (c *Client) EvalSha(sha1 string, keys []string, args []string) *Cmd { + cmdArgs := []string{"EVALSHA", sha1, strconv.FormatInt(int64(len(keys)), 10)} + cmdArgs = append(cmdArgs, keys...) + cmdArgs = append(cmdArgs, args...) + cmd := NewCmd(cmdArgs...) + c.Process(cmd) + return cmd +} + +func (c *Client) ScriptExists(scripts ...string) *BoolSliceCmd { + args := append([]string{"SCRIPT", "EXISTS"}, scripts...) + cmd := NewBoolSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) ScriptFlush() *StatusCmd { + cmd := NewStatusCmd("SCRIPT", "FLUSH") + c.Process(cmd) + return cmd +} + +func (c *Client) ScriptKill() *StatusCmd { + cmd := NewStatusCmd("SCRIPT", "KILL") + c.Process(cmd) + return cmd +} + +func (c *Client) ScriptLoad(script string) *StringCmd { + cmd := NewStringCmd("SCRIPT", "LOAD", script) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) DebugObject(key string) *StringCmd { + cmd := NewStringCmd("DEBUG", "OBJECT", key) + c.Process(cmd) + return cmd +} + +//------------------------------------------------------------------------------ + +func (c *Client) PubSubChannels(pattern string) *StringSliceCmd { + args := []string{"PUBSUB", "CHANNELS"} + if pattern != "*" { + args = append(args, pattern) + } + cmd := NewStringSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) PubSubNumSub(channels ...string) *SliceCmd { + args := []string{"PUBSUB", "NUMSUB"} + args = append(args, channels...) + cmd := NewSliceCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Client) PubSubNumPat() *IntCmd { + cmd := NewIntCmd("PUBSUB", "NUMPAT") + c.Process(cmd) + return cmd +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/doc.go b/Godeps/_workspace/src/gopkg.in/redis.v2/doc.go new file mode 100644 index 0000000..5526253 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/doc.go @@ -0,0 +1,4 @@ +/* +Package redis implements a Redis client. +*/ +package redis diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/error.go b/Godeps/_workspace/src/gopkg.in/redis.v2/error.go new file mode 100644 index 0000000..667fffd --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/error.go @@ -0,0 +1,23 @@ +package redis + +import ( + "fmt" +) + +// Redis nil reply. +var Nil = errorf("redis: nil") + +// Redis transaction failed. +var TxFailedErr = errorf("redis: transaction failed") + +type redisError struct { + s string +} + +func errorf(s string, args ...interface{}) redisError { + return redisError{s: fmt.Sprintf(s, args...)} +} + +func (err redisError) Error() string { + return err.s +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/example_test.go b/Godeps/_workspace/src/gopkg.in/redis.v2/example_test.go new file mode 100644 index 0000000..dbc9513 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/example_test.go @@ -0,0 +1,180 @@ +package redis_test + +import ( + "fmt" + "strconv" + + "gopkg.in/redis.v2" +) + +var client *redis.Client + +func init() { + client = redis.NewTCPClient(&redis.Options{ + Addr: ":6379", + }) + client.FlushDb() +} + +func ExampleNewTCPClient() { + client := redis.NewTCPClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password set + DB: 0, // use default DB + }) + + pong, err := client.Ping().Result() + fmt.Println(pong, err) + // Output: PONG +} + +func ExampleNewFailoverClient() { + client := redis.NewFailoverClient(&redis.FailoverOptions{ + MasterName: "master", + SentinelAddrs: []string{":26379"}, + }) + + pong, err := client.Ping().Result() + fmt.Println(pong, err) + // Output: PONG +} + +func ExampleClient() { + if err := client.Set("foo", "bar").Err(); err != nil { + panic(err) + } + + v, err := client.Get("hello").Result() + fmt.Printf("%q %q %v", v, err, err == redis.Nil) + // Output: "" "redis: nil" true +} + +func ExampleClient_Incr() { + if err := client.Incr("counter").Err(); err != nil { + panic(err) + } + + n, err := client.Get("counter").Int64() + fmt.Println(n, err) + // Output: 1 +} + +func ExampleClient_Pipelined() { + cmds, err := client.Pipelined(func(c *redis.Pipeline) error { + c.Set("key1", "hello1") + c.Get("key1") + return nil + }) + fmt.Println(err) + set := cmds[0].(*redis.StatusCmd) + fmt.Println(set) + get := cmds[1].(*redis.StringCmd) + fmt.Println(get) + // Output: + // SET key1 hello1: OK + // GET key1: hello1 +} + +func ExamplePipeline() { + pipeline := client.Pipeline() + set := pipeline.Set("key1", "hello1") + get := pipeline.Get("key1") + cmds, err := pipeline.Exec() + fmt.Println(cmds, err) + fmt.Println(set) + fmt.Println(get) + // Output: [SET key1 hello1: OK GET key1: hello1] + // SET key1 hello1: OK + // GET key1: hello1 +} + +func ExampleMulti() { + incr := func(tx *redis.Multi) ([]redis.Cmder, error) { + s, err := tx.Get("key").Result() + if err != nil && err != redis.Nil { + return nil, err + } + n, _ := strconv.ParseInt(s, 10, 64) + + return tx.Exec(func() error { + tx.Set("key", strconv.FormatInt(n+1, 10)) + return nil + }) + } + + client.Del("key") + + tx := client.Multi() + defer tx.Close() + + watch := tx.Watch("key") + _ = watch.Err() + + for { + cmds, err := incr(tx) + if err == redis.TxFailedErr { + continue + } else if err != nil { + panic(err) + } + fmt.Println(cmds, err) + break + } + + // Output: [SET key 1: OK] +} + +func ExamplePubSub() { + pubsub := client.PubSub() + defer pubsub.Close() + + err := pubsub.Subscribe("mychannel") + _ = err + + msg, err := pubsub.Receive() + fmt.Println(msg, err) + + pub := client.Publish("mychannel", "hello") + _ = pub.Err() + + msg, err = pubsub.Receive() + fmt.Println(msg, err) + + // Output: subscribe: mychannel + // Message +} + +func ExampleScript() { + setnx := redis.NewScript(` + if redis.call("get", KEYS[1]) == false then + redis.call("set", KEYS[1], ARGV[1]) + return 1 + end + return 0 + `) + + v1, err := setnx.Run(client, []string{"keynx"}, []string{"foo"}).Result() + fmt.Println(v1.(int64), err) + + v2, err := setnx.Run(client, []string{"keynx"}, []string{"bar"}).Result() + fmt.Println(v2.(int64), err) + + get := client.Get("keynx") + fmt.Println(get) + + // Output: 1 + // 0 + // GET keynx: foo +} + +func Example_customCommand() { + Get := func(client *redis.Client, key string) *redis.StringCmd { + cmd := redis.NewStringCmd("GET", key) + client.Process(cmd) + return cmd + } + + v, err := Get(client, "key_does_not_exist").Result() + fmt.Printf("%q %s", v, err) + // Output: "" redis: nil +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/export_test.go b/Godeps/_workspace/src/gopkg.in/redis.v2/export_test.go new file mode 100644 index 0000000..7f7fa67 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/export_test.go @@ -0,0 +1,5 @@ +package redis + +func (c *baseClient) Pool() pool { + return c.connPool +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/multi.go b/Godeps/_workspace/src/gopkg.in/redis.v2/multi.go new file mode 100644 index 0000000..bff38df --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/multi.go @@ -0,0 +1,138 @@ +package redis + +import ( + "errors" + "fmt" +) + +var errDiscard = errors.New("redis: Discard can be used only inside Exec") + +// Not thread-safe. +type Multi struct { + *Client +} + +func (c *Client) Multi() *Multi { + return &Multi{ + Client: &Client{ + baseClient: &baseClient{ + opt: c.opt, + connPool: newSingleConnPool(c.connPool, true), + }, + }, + } +} + +func (c *Multi) Close() error { + if err := c.Unwatch().Err(); err != nil { + return err + } + return c.Client.Close() +} + +func (c *Multi) Watch(keys ...string) *StatusCmd { + args := append([]string{"WATCH"}, keys...) + cmd := NewStatusCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Multi) Unwatch(keys ...string) *StatusCmd { + args := append([]string{"UNWATCH"}, keys...) + cmd := NewStatusCmd(args...) + c.Process(cmd) + return cmd +} + +func (c *Multi) Discard() error { + if c.cmds == nil { + return errDiscard + } + c.cmds = c.cmds[:1] + return nil +} + +// Exec always returns list of commands. If transaction fails +// TxFailedErr is returned. Otherwise Exec returns error of the first +// failed command or nil. +func (c *Multi) Exec(f func() error) ([]Cmder, error) { + c.cmds = []Cmder{NewStatusCmd("MULTI")} + if err := f(); err != nil { + return nil, err + } + c.cmds = append(c.cmds, NewSliceCmd("EXEC")) + + cmds := c.cmds + c.cmds = nil + + if len(cmds) == 2 { + return []Cmder{}, nil + } + + cn, err := c.conn() + if err != nil { + setCmdsErr(cmds[1:len(cmds)-1], err) + return cmds[1 : len(cmds)-1], err + } + + err = c.execCmds(cn, cmds) + if err != nil { + c.freeConn(cn, err) + return cmds[1 : len(cmds)-1], err + } + + c.putConn(cn) + return cmds[1 : len(cmds)-1], nil +} + +func (c *Multi) execCmds(cn *conn, cmds []Cmder) error { + err := c.writeCmd(cn, cmds...) + if err != nil { + setCmdsErr(cmds[1:len(cmds)-1], err) + return err + } + + statusCmd := NewStatusCmd() + + // Omit last command (EXEC). + cmdsLen := len(cmds) - 1 + + // Parse queued replies. + for i := 0; i < cmdsLen; i++ { + if err := statusCmd.parseReply(cn.rd); err != nil { + setCmdsErr(cmds[1:len(cmds)-1], err) + return err + } + } + + // Parse number of replies. + line, err := readLine(cn.rd) + if err != nil { + setCmdsErr(cmds[1:len(cmds)-1], err) + return err + } + if line[0] != '*' { + err := fmt.Errorf("redis: expected '*', but got line %q", line) + setCmdsErr(cmds[1:len(cmds)-1], err) + return err + } + if len(line) == 3 && line[1] == '-' && line[2] == '1' { + setCmdsErr(cmds[1:len(cmds)-1], TxFailedErr) + return TxFailedErr + } + + var firstCmdErr error + + // Parse replies. + // Loop starts from 1 to omit MULTI cmd. + for i := 1; i < cmdsLen; i++ { + cmd := cmds[i] + if err := cmd.parseReply(cn.rd); err != nil { + if firstCmdErr == nil { + firstCmdErr = err + } + } + } + + return firstCmdErr +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/parser.go b/Godeps/_workspace/src/gopkg.in/redis.v2/parser.go new file mode 100644 index 0000000..b4c380c --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/parser.go @@ -0,0 +1,262 @@ +package redis + +import ( + "errors" + "fmt" + "strconv" + + "gopkg.in/bufio.v1" +) + +type multiBulkParser func(rd *bufio.Reader, n int64) (interface{}, error) + +var ( + errReaderTooSmall = errors.New("redis: reader is too small") +) + +//------------------------------------------------------------------------------ + +func appendArgs(buf []byte, args []string) []byte { + buf = append(buf, '*') + buf = strconv.AppendUint(buf, uint64(len(args)), 10) + buf = append(buf, '\r', '\n') + for _, arg := range args { + buf = append(buf, '$') + buf = strconv.AppendUint(buf, uint64(len(arg)), 10) + buf = append(buf, '\r', '\n') + buf = append(buf, arg...) + buf = append(buf, '\r', '\n') + } + return buf +} + +//------------------------------------------------------------------------------ + +func readLine(rd *bufio.Reader) ([]byte, error) { + line, isPrefix, err := rd.ReadLine() + if err != nil { + return line, err + } + if isPrefix { + return line, errReaderTooSmall + } + return line, nil +} + +func readN(rd *bufio.Reader, n int) ([]byte, error) { + b, err := rd.ReadN(n) + if err == bufio.ErrBufferFull { + tmp := make([]byte, n) + r := copy(tmp, b) + b = tmp + + for { + nn, err := rd.Read(b[r:]) + r += nn + if r >= n { + // Ignore error if we read enough. + break + } + if err != nil { + return nil, err + } + } + } else if err != nil { + return nil, err + } + return b, nil +} + +//------------------------------------------------------------------------------ + +func parseReq(rd *bufio.Reader) ([]string, error) { + line, err := readLine(rd) + if err != nil { + return nil, err + } + + if line[0] != '*' { + return []string{string(line)}, nil + } + numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64) + if err != nil { + return nil, err + } + + args := make([]string, 0, numReplies) + for i := int64(0); i < numReplies; i++ { + line, err = readLine(rd) + if err != nil { + return nil, err + } + if line[0] != '$' { + return nil, fmt.Errorf("redis: expected '$', but got %q", line) + } + + argLen, err := strconv.ParseInt(string(line[1:]), 10, 32) + if err != nil { + return nil, err + } + + arg, err := readN(rd, int(argLen)+2) + if err != nil { + return nil, err + } + args = append(args, string(arg[:argLen])) + } + return args, nil +} + +//------------------------------------------------------------------------------ + +func parseReply(rd *bufio.Reader, p multiBulkParser) (interface{}, error) { + line, err := readLine(rd) + if err != nil { + return nil, err + } + + switch line[0] { + case '-': + return nil, errorf(string(line[1:])) + case '+': + return string(line[1:]), nil + case ':': + v, err := strconv.ParseInt(string(line[1:]), 10, 64) + if err != nil { + return nil, err + } + return v, nil + case '$': + if len(line) == 3 && line[1] == '-' && line[2] == '1' { + return nil, Nil + } + + replyLen, err := strconv.Atoi(string(line[1:])) + if err != nil { + return nil, err + } + + b, err := readN(rd, replyLen+2) + if err != nil { + return nil, err + } + return string(b[:replyLen]), nil + case '*': + if len(line) == 3 && line[1] == '-' && line[2] == '1' { + return nil, Nil + } + + repliesNum, err := strconv.ParseInt(string(line[1:]), 10, 64) + if err != nil { + return nil, err + } + + return p(rd, repliesNum) + } + return nil, fmt.Errorf("redis: can't parse %q", line) +} + +func parseSlice(rd *bufio.Reader, n int64) (interface{}, error) { + vals := make([]interface{}, 0, n) + for i := int64(0); i < n; i++ { + v, err := parseReply(rd, parseSlice) + if err == Nil { + vals = append(vals, nil) + } else if err != nil { + return nil, err + } else { + vals = append(vals, v) + } + } + return vals, nil +} + +func parseStringSlice(rd *bufio.Reader, n int64) (interface{}, error) { + vals := make([]string, 0, n) + for i := int64(0); i < n; i++ { + viface, err := parseReply(rd, nil) + if err != nil { + return nil, err + } + v, ok := viface.(string) + if !ok { + return nil, fmt.Errorf("got %T, expected string", viface) + } + vals = append(vals, v) + } + return vals, nil +} + +func parseBoolSlice(rd *bufio.Reader, n int64) (interface{}, error) { + vals := make([]bool, 0, n) + for i := int64(0); i < n; i++ { + viface, err := parseReply(rd, nil) + if err != nil { + return nil, err + } + v, ok := viface.(int64) + if !ok { + return nil, fmt.Errorf("got %T, expected int64", viface) + } + vals = append(vals, v == 1) + } + return vals, nil +} + +func parseStringStringMap(rd *bufio.Reader, n int64) (interface{}, error) { + m := make(map[string]string, n/2) + for i := int64(0); i < n; i += 2 { + keyiface, err := parseReply(rd, nil) + if err != nil { + return nil, err + } + key, ok := keyiface.(string) + if !ok { + return nil, fmt.Errorf("got %T, expected string", keyiface) + } + + valueiface, err := parseReply(rd, nil) + if err != nil { + return nil, err + } + value, ok := valueiface.(string) + if !ok { + return nil, fmt.Errorf("got %T, expected string", valueiface) + } + + m[key] = value + } + return m, nil +} + +func parseZSlice(rd *bufio.Reader, n int64) (interface{}, error) { + zz := make([]Z, n/2) + for i := int64(0); i < n; i += 2 { + z := &zz[i/2] + + memberiface, err := parseReply(rd, nil) + if err != nil { + return nil, err + } + member, ok := memberiface.(string) + if !ok { + return nil, fmt.Errorf("got %T, expected string", memberiface) + } + z.Member = member + + scoreiface, err := parseReply(rd, nil) + if err != nil { + return nil, err + } + scorestr, ok := scoreiface.(string) + if !ok { + return nil, fmt.Errorf("got %T, expected string", scoreiface) + } + score, err := strconv.ParseFloat(scorestr, 64) + if err != nil { + return nil, err + } + z.Score = score + } + return zz, nil +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/parser_test.go b/Godeps/_workspace/src/gopkg.in/redis.v2/parser_test.go new file mode 100644 index 0000000..1b9e158 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/parser_test.go @@ -0,0 +1,54 @@ +package redis + +import ( + "testing" + + "gopkg.in/bufio.v1" +) + +func BenchmarkParseReplyStatus(b *testing.B) { + benchmarkParseReply(b, "+OK\r\n", nil, false) +} + +func BenchmarkParseReplyInt(b *testing.B) { + benchmarkParseReply(b, ":1\r\n", nil, false) +} + +func BenchmarkParseReplyError(b *testing.B) { + benchmarkParseReply(b, "-Error message\r\n", nil, true) +} + +func BenchmarkParseReplyString(b *testing.B) { + benchmarkParseReply(b, "$5\r\nhello\r\n", nil, false) +} + +func BenchmarkParseReplySlice(b *testing.B) { + benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", parseSlice, false) +} + +func benchmarkParseReply(b *testing.B, reply string, p multiBulkParser, wanterr bool) { + b.StopTimer() + + buf := &bufio.Buffer{} + rd := bufio.NewReader(buf) + for i := 0; i < b.N; i++ { + buf.WriteString(reply) + } + + b.StartTimer() + + for i := 0; i < b.N; i++ { + _, err := parseReply(rd, p) + if !wanterr && err != nil { + panic(err) + } + } +} + +func BenchmarkAppendArgs(b *testing.B) { + buf := make([]byte, 0, 64) + args := []string{"hello", "world", "foo", "bar"} + for i := 0; i < b.N; i++ { + appendArgs(buf, args) + } +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/pipeline.go b/Godeps/_workspace/src/gopkg.in/redis.v2/pipeline.go new file mode 100644 index 0000000..540d6c5 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/pipeline.go @@ -0,0 +1,91 @@ +package redis + +// Not thread-safe. +type Pipeline struct { + *Client + + closed bool +} + +func (c *Client) Pipeline() *Pipeline { + return &Pipeline{ + Client: &Client{ + baseClient: &baseClient{ + opt: c.opt, + connPool: c.connPool, + + cmds: make([]Cmder, 0), + }, + }, + } +} + +func (c *Client) Pipelined(f func(*Pipeline) error) ([]Cmder, error) { + pc := c.Pipeline() + if err := f(pc); err != nil { + return nil, err + } + cmds, err := pc.Exec() + pc.Close() + return cmds, err +} + +func (c *Pipeline) Close() error { + c.closed = true + return nil +} + +func (c *Pipeline) Discard() error { + if c.closed { + return errClosed + } + c.cmds = c.cmds[:0] + return nil +} + +// Exec always returns list of commands and error of the first failed +// command if any. +func (c *Pipeline) Exec() ([]Cmder, error) { + if c.closed { + return nil, errClosed + } + + cmds := c.cmds + c.cmds = make([]Cmder, 0) + + if len(cmds) == 0 { + return []Cmder{}, nil + } + + cn, err := c.conn() + if err != nil { + setCmdsErr(cmds, err) + return cmds, err + } + + if err := c.execCmds(cn, cmds); err != nil { + c.freeConn(cn, err) + return cmds, err + } + + c.putConn(cn) + return cmds, nil +} + +func (c *Pipeline) execCmds(cn *conn, cmds []Cmder) error { + if err := c.writeCmd(cn, cmds...); err != nil { + setCmdsErr(cmds, err) + return err + } + + var firstCmdErr error + for _, cmd := range cmds { + if err := cmd.parseReply(cn.rd); err != nil { + if firstCmdErr == nil { + firstCmdErr = err + } + } + } + + return firstCmdErr +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/pool.go b/Godeps/_workspace/src/gopkg.in/redis.v2/pool.go new file mode 100644 index 0000000..bca4d19 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/pool.go @@ -0,0 +1,405 @@ +package redis + +import ( + "container/list" + "errors" + "log" + "net" + "sync" + "time" + + "gopkg.in/bufio.v1" +) + +var ( + errClosed = errors.New("redis: client is closed") + errRateLimited = errors.New("redis: you open connections too fast") +) + +var ( + zeroTime = time.Time{} +) + +type pool interface { + Get() (*conn, bool, error) + Put(*conn) error + Remove(*conn) error + Len() int + Size() int + Close() error + Filter(func(*conn) bool) +} + +//------------------------------------------------------------------------------ + +type conn struct { + netcn net.Conn + rd *bufio.Reader + buf []byte + + inUse bool + usedAt time.Time + + readTimeout time.Duration + writeTimeout time.Duration + + elem *list.Element +} + +func newConnFunc(dial func() (net.Conn, error)) func() (*conn, error) { + return func() (*conn, error) { + netcn, err := dial() + if err != nil { + return nil, err + } + cn := &conn{ + netcn: netcn, + buf: make([]byte, 0, 64), + } + cn.rd = bufio.NewReader(cn) + return cn, nil + } +} + +func (cn *conn) Read(b []byte) (int, error) { + if cn.readTimeout != 0 { + cn.netcn.SetReadDeadline(time.Now().Add(cn.readTimeout)) + } else { + cn.netcn.SetReadDeadline(zeroTime) + } + return cn.netcn.Read(b) +} + +func (cn *conn) Write(b []byte) (int, error) { + if cn.writeTimeout != 0 { + cn.netcn.SetWriteDeadline(time.Now().Add(cn.writeTimeout)) + } else { + cn.netcn.SetWriteDeadline(zeroTime) + } + return cn.netcn.Write(b) +} + +func (cn *conn) RemoteAddr() net.Addr { + return cn.netcn.RemoteAddr() +} + +func (cn *conn) Close() error { + return cn.netcn.Close() +} + +//------------------------------------------------------------------------------ + +type connPool struct { + dial func() (*conn, error) + rl *rateLimiter + + opt *options + + cond *sync.Cond + conns *list.List + + idleNum int + closed bool +} + +func newConnPool(dial func() (*conn, error), opt *options) *connPool { + return &connPool{ + dial: dial, + rl: newRateLimiter(time.Second, 2*opt.PoolSize), + + opt: opt, + + cond: sync.NewCond(&sync.Mutex{}), + conns: list.New(), + } +} + +func (p *connPool) new() (*conn, error) { + if !p.rl.Check() { + return nil, errRateLimited + } + return p.dial() +} + +func (p *connPool) Get() (*conn, bool, error) { + p.cond.L.Lock() + + if p.closed { + p.cond.L.Unlock() + return nil, false, errClosed + } + + if p.opt.IdleTimeout > 0 { + for el := p.conns.Front(); el != nil; el = el.Next() { + cn := el.Value.(*conn) + if cn.inUse { + break + } + if time.Since(cn.usedAt) > p.opt.IdleTimeout { + if err := p.remove(cn); err != nil { + log.Printf("remove failed: %s", err) + } + } + } + } + + for p.conns.Len() >= p.opt.PoolSize && p.idleNum == 0 { + p.cond.Wait() + } + + if p.idleNum > 0 { + elem := p.conns.Front() + cn := elem.Value.(*conn) + if cn.inUse { + panic("pool: precondition failed") + } + cn.inUse = true + p.conns.MoveToBack(elem) + p.idleNum-- + + p.cond.L.Unlock() + return cn, false, nil + } + + if p.conns.Len() < p.opt.PoolSize { + cn, err := p.new() + if err != nil { + p.cond.L.Unlock() + return nil, false, err + } + + cn.inUse = true + cn.elem = p.conns.PushBack(cn) + + p.cond.L.Unlock() + return cn, true, nil + } + + panic("not reached") +} + +func (p *connPool) Put(cn *conn) error { + if cn.rd.Buffered() != 0 { + b, _ := cn.rd.ReadN(cn.rd.Buffered()) + log.Printf("redis: connection has unread data: %q", b) + return p.Remove(cn) + } + + if p.opt.IdleTimeout > 0 { + cn.usedAt = time.Now() + } + + p.cond.L.Lock() + if p.closed { + p.cond.L.Unlock() + return errClosed + } + cn.inUse = false + p.conns.MoveToFront(cn.elem) + p.idleNum++ + p.cond.Signal() + p.cond.L.Unlock() + + return nil +} + +func (p *connPool) Remove(cn *conn) error { + p.cond.L.Lock() + if p.closed { + // Noop, connection is already closed. + p.cond.L.Unlock() + return nil + } + err := p.remove(cn) + p.cond.Signal() + p.cond.L.Unlock() + return err +} + +func (p *connPool) remove(cn *conn) error { + p.conns.Remove(cn.elem) + cn.elem = nil + if !cn.inUse { + p.idleNum-- + } + return cn.Close() +} + +// Len returns number of idle connections. +func (p *connPool) Len() int { + defer p.cond.L.Unlock() + p.cond.L.Lock() + return p.idleNum +} + +// Size returns number of connections in the pool. +func (p *connPool) Size() int { + defer p.cond.L.Unlock() + p.cond.L.Lock() + return p.conns.Len() +} + +func (p *connPool) Filter(f func(*conn) bool) { + p.cond.L.Lock() + for el, next := p.conns.Front(), p.conns.Front(); el != nil; el = next { + next = el.Next() + cn := el.Value.(*conn) + if !f(cn) { + p.remove(cn) + } + } + p.cond.L.Unlock() +} + +func (p *connPool) Close() error { + defer p.cond.L.Unlock() + p.cond.L.Lock() + if p.closed { + return nil + } + p.closed = true + p.rl.Close() + var retErr error + for { + e := p.conns.Front() + if e == nil { + break + } + if err := p.remove(e.Value.(*conn)); err != nil { + log.Printf("cn.Close failed: %s", err) + retErr = err + } + } + return retErr +} + +//------------------------------------------------------------------------------ + +type singleConnPool struct { + pool pool + + cnMtx sync.Mutex + cn *conn + + reusable bool + + closed bool +} + +func newSingleConnPool(pool pool, reusable bool) *singleConnPool { + return &singleConnPool{ + pool: pool, + reusable: reusable, + } +} + +func (p *singleConnPool) SetConn(cn *conn) { + p.cnMtx.Lock() + p.cn = cn + p.cnMtx.Unlock() +} + +func (p *singleConnPool) Get() (*conn, bool, error) { + defer p.cnMtx.Unlock() + p.cnMtx.Lock() + + if p.closed { + return nil, false, errClosed + } + if p.cn != nil { + return p.cn, false, nil + } + + cn, isNew, err := p.pool.Get() + if err != nil { + return nil, false, err + } + p.cn = cn + + return p.cn, isNew, nil +} + +func (p *singleConnPool) Put(cn *conn) error { + defer p.cnMtx.Unlock() + p.cnMtx.Lock() + if p.cn != cn { + panic("p.cn != cn") + } + if p.closed { + return errClosed + } + return nil +} + +func (p *singleConnPool) put() error { + err := p.pool.Put(p.cn) + p.cn = nil + return err +} + +func (p *singleConnPool) Remove(cn *conn) error { + defer p.cnMtx.Unlock() + p.cnMtx.Lock() + if p.cn == nil { + panic("p.cn == nil") + } + if p.cn != cn { + panic("p.cn != cn") + } + if p.closed { + return errClosed + } + return p.remove() +} + +func (p *singleConnPool) remove() error { + err := p.pool.Remove(p.cn) + p.cn = nil + return err +} + +func (p *singleConnPool) Len() int { + defer p.cnMtx.Unlock() + p.cnMtx.Lock() + if p.cn == nil { + return 0 + } + return 1 +} + +func (p *singleConnPool) Size() int { + defer p.cnMtx.Unlock() + p.cnMtx.Lock() + if p.cn == nil { + return 0 + } + return 1 +} + +func (p *singleConnPool) Filter(f func(*conn) bool) { + p.cnMtx.Lock() + if p.cn != nil { + if !f(p.cn) { + p.remove() + } + } + p.cnMtx.Unlock() +} + +func (p *singleConnPool) Close() error { + defer p.cnMtx.Unlock() + p.cnMtx.Lock() + if p.closed { + return nil + } + p.closed = true + var err error + if p.cn != nil { + if p.reusable { + err = p.put() + } else { + err = p.remove() + } + } + return err +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/pubsub.go b/Godeps/_workspace/src/gopkg.in/redis.v2/pubsub.go new file mode 100644 index 0000000..6ac130b --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/pubsub.go @@ -0,0 +1,134 @@ +package redis + +import ( + "fmt" + "time" +) + +// Not thread-safe. +type PubSub struct { + *baseClient +} + +func (c *Client) PubSub() *PubSub { + return &PubSub{ + baseClient: &baseClient{ + opt: c.opt, + connPool: newSingleConnPool(c.connPool, false), + }, + } +} + +func (c *Client) Publish(channel, message string) *IntCmd { + req := NewIntCmd("PUBLISH", channel, message) + c.Process(req) + return req +} + +type Message struct { + Channel string + Payload string +} + +func (m *Message) String() string { + return fmt.Sprintf("Message<%s: %s>", m.Channel, m.Payload) +} + +type PMessage struct { + Channel string + Pattern string + Payload string +} + +func (m *PMessage) String() string { + return fmt.Sprintf("PMessage<%s: %s>", m.Channel, m.Payload) +} + +type Subscription struct { + Kind string + Channel string + Count int +} + +func (m *Subscription) String() string { + return fmt.Sprintf("%s: %s", m.Kind, m.Channel) +} + +func (c *PubSub) Receive() (interface{}, error) { + return c.ReceiveTimeout(0) +} + +func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error) { + cn, err := c.conn() + if err != nil { + return nil, err + } + cn.readTimeout = timeout + + cmd := NewSliceCmd() + if err := cmd.parseReply(cn.rd); err != nil { + return nil, err + } + + reply := cmd.Val() + + msgName := reply[0].(string) + switch msgName { + case "subscribe", "unsubscribe", "psubscribe", "punsubscribe": + return &Subscription{ + Kind: msgName, + Channel: reply[1].(string), + Count: int(reply[2].(int64)), + }, nil + case "message": + return &Message{ + Channel: reply[1].(string), + Payload: reply[2].(string), + }, nil + case "pmessage": + return &PMessage{ + Pattern: reply[1].(string), + Channel: reply[2].(string), + Payload: reply[3].(string), + }, nil + } + return nil, fmt.Errorf("redis: unsupported message name: %q", msgName) +} + +func (c *PubSub) subscribe(cmd string, channels ...string) error { + cn, err := c.conn() + if err != nil { + return err + } + + args := append([]string{cmd}, channels...) + req := NewSliceCmd(args...) + return c.writeCmd(cn, req) +} + +func (c *PubSub) Subscribe(channels ...string) error { + return c.subscribe("SUBSCRIBE", channels...) +} + +func (c *PubSub) PSubscribe(patterns ...string) error { + return c.subscribe("PSUBSCRIBE", patterns...) +} + +func (c *PubSub) unsubscribe(cmd string, channels ...string) error { + cn, err := c.conn() + if err != nil { + return err + } + + args := append([]string{cmd}, channels...) + req := NewSliceCmd(args...) + return c.writeCmd(cn, req) +} + +func (c *PubSub) Unsubscribe(channels ...string) error { + return c.unsubscribe("UNSUBSCRIBE", channels...) +} + +func (c *PubSub) PUnsubscribe(patterns ...string) error { + return c.unsubscribe("PUNSUBSCRIBE", patterns...) +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/rate_limit.go b/Godeps/_workspace/src/gopkg.in/redis.v2/rate_limit.go new file mode 100644 index 0000000..20d8512 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/rate_limit.go @@ -0,0 +1,53 @@ +package redis + +import ( + "sync/atomic" + "time" +) + +type rateLimiter struct { + v int64 + + _closed int64 +} + +func newRateLimiter(limit time.Duration, bucketSize int) *rateLimiter { + rl := &rateLimiter{ + v: int64(bucketSize), + } + go rl.loop(limit, int64(bucketSize)) + return rl +} + +func (rl *rateLimiter) loop(limit time.Duration, bucketSize int64) { + for { + if rl.closed() { + break + } + if v := atomic.LoadInt64(&rl.v); v < bucketSize { + atomic.AddInt64(&rl.v, 1) + } + time.Sleep(limit) + } +} + +func (rl *rateLimiter) Check() bool { + for { + if v := atomic.LoadInt64(&rl.v); v > 0 { + if atomic.CompareAndSwapInt64(&rl.v, v, v-1) { + return true + } + } else { + return false + } + } +} + +func (rl *rateLimiter) Close() error { + atomic.StoreInt64(&rl._closed, 1) + return nil +} + +func (rl *rateLimiter) closed() bool { + return atomic.LoadInt64(&rl._closed) == 1 +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/rate_limit_test.go b/Godeps/_workspace/src/gopkg.in/redis.v2/rate_limit_test.go new file mode 100644 index 0000000..2f0d41a --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/rate_limit_test.go @@ -0,0 +1,31 @@ +package redis + +import ( + "sync" + "testing" + "time" +) + +func TestRateLimiter(t *testing.T) { + var n = 100000 + if testing.Short() { + n = 1000 + } + rl := newRateLimiter(time.Minute, n) + + wg := &sync.WaitGroup{} + for i := 0; i < n; i++ { + wg.Add(1) + go func() { + if !rl.Check() { + panic("check failed") + } + wg.Done() + }() + } + wg.Wait() + + if rl.Check() && rl.Check() { + t.Fatal("check passed") + } +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/redis.go b/Godeps/_workspace/src/gopkg.in/redis.v2/redis.go new file mode 100644 index 0000000..0d15dc8 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/redis.go @@ -0,0 +1,231 @@ +package redis + +import ( + "log" + "net" + "time" +) + +type baseClient struct { + connPool pool + opt *options + cmds []Cmder +} + +func (c *baseClient) writeCmd(cn *conn, cmds ...Cmder) error { + buf := cn.buf[:0] + for _, cmd := range cmds { + buf = appendArgs(buf, cmd.args()) + } + + _, err := cn.Write(buf) + return err +} + +func (c *baseClient) conn() (*conn, error) { + cn, isNew, err := c.connPool.Get() + if err != nil { + return nil, err + } + + if isNew { + if err := c.initConn(cn); err != nil { + c.removeConn(cn) + return nil, err + } + } + + return cn, nil +} + +func (c *baseClient) initConn(cn *conn) error { + if c.opt.Password == "" && c.opt.DB == 0 { + return nil + } + + pool := newSingleConnPool(c.connPool, false) + pool.SetConn(cn) + + // Client is not closed because we want to reuse underlying connection. + client := &Client{ + baseClient: &baseClient{ + opt: c.opt, + connPool: pool, + }, + } + + if c.opt.Password != "" { + if err := client.Auth(c.opt.Password).Err(); err != nil { + return err + } + } + + if c.opt.DB > 0 { + if err := client.Select(c.opt.DB).Err(); err != nil { + return err + } + } + + return nil +} + +func (c *baseClient) freeConn(cn *conn, ei error) error { + if cn.rd.Buffered() > 0 { + return c.connPool.Remove(cn) + } + if _, ok := ei.(redisError); ok { + return c.connPool.Put(cn) + } + return c.connPool.Remove(cn) +} + +func (c *baseClient) removeConn(cn *conn) { + if err := c.connPool.Remove(cn); err != nil { + log.Printf("pool.Remove failed: %s", err) + } +} + +func (c *baseClient) putConn(cn *conn) { + if err := c.connPool.Put(cn); err != nil { + log.Printf("pool.Put failed: %s", err) + } +} + +func (c *baseClient) Process(cmd Cmder) { + if c.cmds == nil { + c.run(cmd) + } else { + c.cmds = append(c.cmds, cmd) + } +} + +func (c *baseClient) run(cmd Cmder) { + cn, err := c.conn() + if err != nil { + cmd.setErr(err) + return + } + + if timeout := cmd.writeTimeout(); timeout != nil { + cn.writeTimeout = *timeout + } else { + cn.writeTimeout = c.opt.WriteTimeout + } + + if timeout := cmd.readTimeout(); timeout != nil { + cn.readTimeout = *timeout + } else { + cn.readTimeout = c.opt.ReadTimeout + } + + if err := c.writeCmd(cn, cmd); err != nil { + c.freeConn(cn, err) + cmd.setErr(err) + return + } + + if err := cmd.parseReply(cn.rd); err != nil { + c.freeConn(cn, err) + return + } + + c.putConn(cn) +} + +// Close closes the client, releasing any open resources. +func (c *baseClient) Close() error { + return c.connPool.Close() +} + +//------------------------------------------------------------------------------ + +type options struct { + Password string + DB int64 + + DialTimeout time.Duration + ReadTimeout time.Duration + WriteTimeout time.Duration + + PoolSize int + IdleTimeout time.Duration +} + +type Options struct { + Network string + Addr string + + // Dialer creates new network connection and has priority over + // Network and Addr options. + Dialer func() (net.Conn, error) + + Password string + DB int64 + + DialTimeout time.Duration + ReadTimeout time.Duration + WriteTimeout time.Duration + + PoolSize int + IdleTimeout time.Duration +} + +func (opt *Options) getPoolSize() int { + if opt.PoolSize == 0 { + return 10 + } + return opt.PoolSize +} + +func (opt *Options) getDialTimeout() time.Duration { + if opt.DialTimeout == 0 { + return 5 * time.Second + } + return opt.DialTimeout +} + +func (opt *Options) options() *options { + return &options{ + DB: opt.DB, + Password: opt.Password, + + DialTimeout: opt.getDialTimeout(), + ReadTimeout: opt.ReadTimeout, + WriteTimeout: opt.WriteTimeout, + + PoolSize: opt.getPoolSize(), + IdleTimeout: opt.IdleTimeout, + } +} + +type Client struct { + *baseClient +} + +func NewClient(clOpt *Options) *Client { + opt := clOpt.options() + dialer := clOpt.Dialer + if dialer == nil { + dialer = func() (net.Conn, error) { + return net.DialTimeout(clOpt.Network, clOpt.Addr, opt.DialTimeout) + } + } + return &Client{ + baseClient: &baseClient{ + opt: opt, + connPool: newConnPool(newConnFunc(dialer), opt), + }, + } +} + +// Deprecated. Use NewClient instead. +func NewTCPClient(opt *Options) *Client { + opt.Network = "tcp" + return NewClient(opt) +} + +// Deprecated. Use NewClient instead. +func NewUnixClient(opt *Options) *Client { + opt.Network = "unix" + return NewClient(opt) +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/redis_test.go b/Godeps/_workspace/src/gopkg.in/redis.v2/redis_test.go new file mode 100644 index 0000000..49f84d0 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/redis_test.go @@ -0,0 +1,3333 @@ +package redis_test + +import ( + "bytes" + "fmt" + "io" + "net" + "sort" + "strconv" + "sync" + "testing" + "time" + + "gopkg.in/redis.v2" + + . "gopkg.in/check.v1" +) + +const redisAddr = ":6379" + +//------------------------------------------------------------------------------ + +func sortStrings(slice []string) []string { + sort.Strings(slice) + return slice +} + +//------------------------------------------------------------------------------ + +type RedisConnectorTest struct{} + +var _ = Suite(&RedisConnectorTest{}) + +func (t *RedisConnectorTest) TestShutdown(c *C) { + c.Skip("shutdowns server") + + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + + shutdown := client.Shutdown() + c.Check(shutdown.Err(), Equals, io.EOF) + c.Check(shutdown.Val(), Equals, "") + + ping := client.Ping() + c.Check(ping.Err(), ErrorMatches, "dial tcp :[0-9]+: connection refused") + c.Check(ping.Val(), Equals, "") +} + +func (t *RedisConnectorTest) TestNewTCPClient(c *C) { + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + ping := client.Ping() + c.Check(ping.Err(), IsNil) + c.Check(ping.Val(), Equals, "PONG") + c.Assert(client.Close(), IsNil) +} + +func (t *RedisConnectorTest) TestNewUnixClient(c *C) { + c.Skip("not available on Travis CI") + + client := redis.NewUnixClient(&redis.Options{ + Addr: "/tmp/redis.sock", + }) + ping := client.Ping() + c.Check(ping.Err(), IsNil) + c.Check(ping.Val(), Equals, "PONG") + c.Assert(client.Close(), IsNil) +} + +func (t *RedisConnectorTest) TestDialer(c *C) { + client := redis.NewClient(&redis.Options{ + Dialer: func() (net.Conn, error) { + return net.Dial("tcp", redisAddr) + }, + }) + ping := client.Ping() + c.Check(ping.Err(), IsNil) + c.Check(ping.Val(), Equals, "PONG") + c.Assert(client.Close(), IsNil) +} + +func (t *RedisConnectorTest) TestClose(c *C) { + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + c.Assert(client.Close(), IsNil) + + ping := client.Ping() + c.Assert(ping.Err(), Not(IsNil)) + c.Assert(ping.Err().Error(), Equals, "redis: client is closed") + + c.Assert(client.Close(), IsNil) +} + +func (t *RedisConnectorTest) TestPubSubClose(c *C) { + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + + pubsub := client.PubSub() + c.Assert(pubsub.Close(), IsNil) + + _, err := pubsub.Receive() + c.Assert(err, Not(IsNil)) + c.Assert(err.Error(), Equals, "redis: client is closed") + + ping := client.Ping() + c.Assert(ping.Err(), IsNil) + + c.Assert(client.Close(), IsNil) +} + +func (t *RedisConnectorTest) TestMultiClose(c *C) { + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + + multi := client.Multi() + c.Assert(multi.Close(), IsNil) + + _, err := multi.Exec(func() error { + multi.Ping() + return nil + }) + c.Assert(err, Not(IsNil)) + c.Assert(err.Error(), Equals, "redis: client is closed") + + ping := client.Ping() + c.Assert(ping.Err(), IsNil) + + c.Assert(client.Close(), IsNil) +} + +func (t *RedisConnectorTest) TestPipelineClose(c *C) { + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + + _, err := client.Pipelined(func(pipeline *redis.Pipeline) error { + c.Assert(pipeline.Close(), IsNil) + pipeline.Ping() + return nil + }) + c.Assert(err, Not(IsNil)) + c.Assert(err.Error(), Equals, "redis: client is closed") + + ping := client.Ping() + c.Assert(ping.Err(), IsNil) + + c.Assert(client.Close(), IsNil) +} + +func (t *RedisConnectorTest) TestIdleTimeout(c *C) { + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + IdleTimeout: time.Nanosecond, + }) + for i := 0; i < 10; i++ { + c.Assert(client.Ping().Err(), IsNil) + } +} + +func (t *RedisConnectorTest) TestSelectDb(c *C) { + client1 := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + DB: 1, + }) + c.Assert(client1.Set("key", "db1").Err(), IsNil) + + client2 := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + DB: 2, + }) + c.Assert(client2.Get("key").Err(), Equals, redis.Nil) +} + +//------------------------------------------------------------------------------ + +type RedisConnPoolTest struct { + client *redis.Client +} + +var _ = Suite(&RedisConnPoolTest{}) + +func (t *RedisConnPoolTest) SetUpTest(c *C) { + t.client = redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) +} + +func (t *RedisConnPoolTest) TearDownTest(c *C) { + c.Assert(t.client.FlushDb().Err(), IsNil) + c.Assert(t.client.Close(), IsNil) +} + +func (t *RedisConnPoolTest) TestConnPoolMaxSize(c *C) { + wg := &sync.WaitGroup{} + for i := 0; i < 1000; i++ { + wg.Add(1) + go func() { + ping := t.client.Ping() + c.Assert(ping.Err(), IsNil) + c.Assert(ping.Val(), Equals, "PONG") + wg.Done() + }() + } + wg.Wait() + + c.Assert(t.client.Pool().Size(), Equals, 10) + c.Assert(t.client.Pool().Len(), Equals, 10) +} + +func (t *RedisConnPoolTest) TestConnPoolMaxSizeOnPipelineClient(c *C) { + const N = 1000 + + wg := &sync.WaitGroup{} + wg.Add(N) + for i := 0; i < N; i++ { + go func() { + pipeline := t.client.Pipeline() + ping := pipeline.Ping() + cmds, err := pipeline.Exec() + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 1) + c.Assert(ping.Err(), IsNil) + c.Assert(ping.Val(), Equals, "PONG") + + c.Assert(pipeline.Close(), IsNil) + + wg.Done() + }() + } + wg.Wait() + + c.Assert(t.client.Pool().Size(), Equals, 10) + c.Assert(t.client.Pool().Len(), Equals, 10) +} + +func (t *RedisConnPoolTest) TestConnPoolMaxSizeOnMultiClient(c *C) { + const N = 1000 + + wg := &sync.WaitGroup{} + wg.Add(N) + for i := 0; i < N; i++ { + go func() { + multi := t.client.Multi() + var ping *redis.StatusCmd + cmds, err := multi.Exec(func() error { + ping = multi.Ping() + return nil + }) + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 1) + c.Assert(ping.Err(), IsNil) + c.Assert(ping.Val(), Equals, "PONG") + + c.Assert(multi.Close(), IsNil) + + wg.Done() + }() + } + wg.Wait() + + c.Assert(t.client.Pool().Size(), Equals, 10) + c.Assert(t.client.Pool().Len(), Equals, 10) +} + +func (t *RedisConnPoolTest) TestConnPoolMaxSizeOnPubSub(c *C) { + const N = 10 + + wg := &sync.WaitGroup{} + wg.Add(N) + for i := 0; i < N; i++ { + go func() { + defer wg.Done() + pubsub := t.client.PubSub() + c.Assert(pubsub.Subscribe(), IsNil) + c.Assert(pubsub.Close(), IsNil) + }() + } + wg.Wait() + + c.Assert(t.client.Pool().Size(), Equals, 0) + c.Assert(t.client.Pool().Len(), Equals, 0) +} + +func (t *RedisConnPoolTest) TestConnPoolRemovesBrokenConn(c *C) { + cn, _, err := t.client.Pool().Get() + c.Assert(err, IsNil) + c.Assert(cn.Close(), IsNil) + c.Assert(t.client.Pool().Put(cn), IsNil) + + ping := t.client.Ping() + c.Assert(ping.Err().Error(), Equals, "use of closed network connection") + c.Assert(ping.Val(), Equals, "") + + ping = t.client.Ping() + c.Assert(ping.Err(), IsNil) + c.Assert(ping.Val(), Equals, "PONG") + + c.Assert(t.client.Pool().Size(), Equals, 1) + c.Assert(t.client.Pool().Len(), Equals, 1) +} + +func (t *RedisConnPoolTest) TestConnPoolReusesConn(c *C) { + for i := 0; i < 1000; i++ { + ping := t.client.Ping() + c.Assert(ping.Err(), IsNil) + c.Assert(ping.Val(), Equals, "PONG") + } + + c.Assert(t.client.Pool().Size(), Equals, 1) + c.Assert(t.client.Pool().Len(), Equals, 1) +} + +//------------------------------------------------------------------------------ + +type RedisTest struct { + client *redis.Client +} + +var _ = Suite(&RedisTest{}) + +func Test(t *testing.T) { TestingT(t) } + +func (t *RedisTest) SetUpTest(c *C) { + t.client = redis.NewTCPClient(&redis.Options{ + Addr: ":6379", + }) + + // This is much faster than Flushall. + c.Assert(t.client.Select(1).Err(), IsNil) + c.Assert(t.client.FlushDb().Err(), IsNil) + c.Assert(t.client.Select(0).Err(), IsNil) + c.Assert(t.client.FlushDb().Err(), IsNil) +} + +func (t *RedisTest) TearDownTest(c *C) { + c.Assert(t.client.Close(), IsNil) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestCmdStringMethod(c *C) { + set := t.client.Set("foo", "bar") + c.Assert(set.String(), Equals, "SET foo bar: OK") + + get := t.client.Get("foo") + c.Assert(get.String(), Equals, "GET foo: bar") +} + +func (t *RedisTest) TestCmdStringMethodError(c *C) { + get2 := t.client.Get("key_does_not_exists") + c.Assert(get2.String(), Equals, "GET key_does_not_exists: redis: nil") +} + +func (t *RedisTest) TestRunWithouthCheckingErrVal(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") + + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") +} + +func (t *RedisTest) TestGetSpecChars(c *C) { + set := t.client.Set("key", "hello1\r\nhello2\r\n") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello1\r\nhello2\r\n") +} + +func (t *RedisTest) TestGetBigVal(c *C) { + val := string(bytes.Repeat([]byte{'*'}, 1<<16)) + + set := t.client.Set("key", val) + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, val) +} + +func (t *RedisTest) TestManyKeys(c *C) { + var n = 100000 + + for i := 0; i < n; i++ { + t.client.Set("keys.key"+strconv.Itoa(i), "hello"+strconv.Itoa(i)) + } + keys := t.client.Keys("keys.*") + c.Assert(keys.Err(), IsNil) + c.Assert(len(keys.Val()), Equals, n) +} + +func (t *RedisTest) TestManyKeys2(c *C) { + var n = 100000 + + keys := []string{"non-existent-key"} + for i := 0; i < n; i++ { + key := "keys.key" + strconv.Itoa(i) + t.client.Set(key, "hello"+strconv.Itoa(i)) + keys = append(keys, key) + } + keys = append(keys, "non-existent-key") + + mget := t.client.MGet(keys...) + c.Assert(mget.Err(), IsNil) + c.Assert(len(mget.Val()), Equals, n+2) + vals := mget.Val() + for i := 0; i < n; i++ { + c.Assert(vals[i+1], Equals, "hello"+strconv.Itoa(i)) + } + c.Assert(vals[0], Equals, nil) + c.Assert(vals[n+1], Equals, nil) +} + +func (t *RedisTest) TestStringCmdHelpers(c *C) { + set := t.client.Set("key", "10") + c.Assert(set.Err(), IsNil) + + n, err := t.client.Get("key").Int64() + c.Assert(err, IsNil) + c.Assert(n, Equals, int64(10)) + + un, err := t.client.Get("key").Uint64() + c.Assert(err, IsNil) + c.Assert(un, Equals, uint64(10)) + + f, err := t.client.Get("key").Float64() + c.Assert(err, IsNil) + c.Assert(f, Equals, float64(10)) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestAuth(c *C) { + auth := t.client.Auth("password") + c.Assert(auth.Err(), ErrorMatches, "ERR Client sent AUTH, but no password is set") + c.Assert(auth.Val(), Equals, "") +} + +func (t *RedisTest) TestEcho(c *C) { + echo := t.client.Echo("hello") + c.Assert(echo.Err(), IsNil) + c.Assert(echo.Val(), Equals, "hello") +} + +func (t *RedisTest) TestPing(c *C) { + ping := t.client.Ping() + c.Assert(ping.Err(), IsNil) + c.Assert(ping.Val(), Equals, "PONG") +} + +func (t *RedisTest) TestSelect(c *C) { + sel := t.client.Select(1) + c.Assert(sel.Err(), IsNil) + c.Assert(sel.Val(), Equals, "OK") +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestCmdKeysDel(c *C) { + set := t.client.Set("key1", "Hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + set = t.client.Set("key2", "World") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + del := t.client.Del("key1", "key2", "key3") + c.Assert(del.Err(), IsNil) + c.Assert(del.Val(), Equals, int64(2)) +} + +func (t *RedisTest) TestCmdKeysDump(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + dump := t.client.Dump("key") + c.Assert(dump.Err(), IsNil) + c.Assert(dump.Val(), Equals, "\x00\x05hello\x06\x00\xf5\x9f\xb7\xf6\x90a\x1c\x99") +} + +func (t *RedisTest) TestCmdKeysExists(c *C) { + set := t.client.Set("key1", "Hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + exists := t.client.Exists("key1") + c.Assert(exists.Err(), IsNil) + c.Assert(exists.Val(), Equals, true) + + exists = t.client.Exists("key2") + c.Assert(exists.Err(), IsNil) + c.Assert(exists.Val(), Equals, false) +} + +func (t *RedisTest) TestCmdKeysExpire(c *C) { + set := t.client.Set("key", "Hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + expire := t.client.Expire("key", 10*time.Second) + c.Assert(expire.Err(), IsNil) + c.Assert(expire.Val(), Equals, true) + + ttl := t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val(), Equals, 10*time.Second) + + set = t.client.Set("key", "Hello World") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + ttl = t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val() < 0, Equals, true) +} + +func (t *RedisTest) TestCmdKeysExpireAt(c *C) { + set := t.client.Set("key", "Hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + exists := t.client.Exists("key") + c.Assert(exists.Err(), IsNil) + c.Assert(exists.Val(), Equals, true) + + expireAt := t.client.ExpireAt("key", time.Now().Add(-time.Hour)) + c.Assert(expireAt.Err(), IsNil) + c.Assert(expireAt.Val(), Equals, true) + + exists = t.client.Exists("key") + c.Assert(exists.Err(), IsNil) + c.Assert(exists.Val(), Equals, false) +} + +func (t *RedisTest) TestCmdKeysKeys(c *C) { + mset := t.client.MSet("one", "1", "two", "2", "three", "3", "four", "4") + c.Assert(mset.Err(), IsNil) + c.Assert(mset.Val(), Equals, "OK") + + keys := t.client.Keys("*o*") + c.Assert(keys.Err(), IsNil) + c.Assert(sortStrings(keys.Val()), DeepEquals, []string{"four", "one", "two"}) + + keys = t.client.Keys("t??") + c.Assert(keys.Err(), IsNil) + c.Assert(keys.Val(), DeepEquals, []string{"two"}) + + keys = t.client.Keys("*") + c.Assert(keys.Err(), IsNil) + c.Assert( + sortStrings(keys.Val()), + DeepEquals, + []string{"four", "one", "three", "two"}, + ) +} + +func (t *RedisTest) TestCmdKeysMigrate(c *C) { + migrate := t.client.Migrate("localhost", "6380", "key", 0, 0) + c.Assert(migrate.Err(), IsNil) + c.Assert(migrate.Val(), Equals, "NOKEY") + + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + migrate = t.client.Migrate("localhost", "6380", "key", 0, 0) + c.Assert(migrate.Err(), ErrorMatches, "IOERR error or timeout writing to target instance") + c.Assert(migrate.Val(), Equals, "") +} + +func (t *RedisTest) TestCmdKeysMove(c *C) { + move := t.client.Move("key", 1) + c.Assert(move.Err(), IsNil) + c.Assert(move.Val(), Equals, false) + + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + move = t.client.Move("key", 1) + c.Assert(move.Err(), IsNil) + c.Assert(move.Val(), Equals, true) + + get := t.client.Get("key") + c.Assert(get.Err(), Equals, redis.Nil) + c.Assert(get.Val(), Equals, "") + + sel := t.client.Select(1) + c.Assert(sel.Err(), IsNil) + c.Assert(sel.Val(), Equals, "OK") + + get = t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") +} + +func (t *RedisTest) TestCmdKeysObject(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + refCount := t.client.ObjectRefCount("key") + c.Assert(refCount.Err(), IsNil) + c.Assert(refCount.Val(), Equals, int64(1)) + + enc := t.client.ObjectEncoding("key") + c.Assert(enc.Err(), IsNil) + c.Assert(enc.Val(), Equals, "raw") + + idleTime := t.client.ObjectIdleTime("key") + c.Assert(idleTime.Err(), IsNil) + c.Assert(idleTime.Val(), Equals, time.Duration(0)) +} + +func (t *RedisTest) TestCmdKeysPersist(c *C) { + set := t.client.Set("key", "Hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + expire := t.client.Expire("key", 10*time.Second) + c.Assert(expire.Err(), IsNil) + c.Assert(expire.Val(), Equals, true) + + ttl := t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val(), Equals, 10*time.Second) + + persist := t.client.Persist("key") + c.Assert(persist.Err(), IsNil) + c.Assert(persist.Val(), Equals, true) + + ttl = t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val() < 0, Equals, true) +} + +func (t *RedisTest) TestCmdKeysPExpire(c *C) { + set := t.client.Set("key", "Hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + expiration := 900 * time.Millisecond + pexpire := t.client.PExpire("key", expiration) + c.Assert(pexpire.Err(), IsNil) + c.Assert(pexpire.Val(), Equals, true) + + ttl := t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val(), Equals, time.Second) + + pttl := t.client.PTTL("key") + c.Assert(pttl.Err(), IsNil) + c.Assert(pttl.Val() <= expiration, Equals, true) + c.Assert(pttl.Val() >= expiration-time.Millisecond, Equals, true) +} + +func (t *RedisTest) TestCmdKeysPExpireAt(c *C) { + set := t.client.Set("key", "Hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + expiration := 900 * time.Millisecond + pexpireat := t.client.PExpireAt("key", time.Now().Add(expiration)) + c.Assert(pexpireat.Err(), IsNil) + c.Assert(pexpireat.Val(), Equals, true) + + ttl := t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val(), Equals, time.Second) + + pttl := t.client.PTTL("key") + c.Assert(pttl.Err(), IsNil) + c.Assert(pttl.Val() <= expiration, Equals, true) + c.Assert(pttl.Val() >= expiration-time.Millisecond, Equals, true) +} + +func (t *RedisTest) TestCmdKeysPTTL(c *C) { + set := t.client.Set("key", "Hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + expiration := time.Second + expire := t.client.Expire("key", expiration) + c.Assert(expire.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + pttl := t.client.PTTL("key") + c.Assert(pttl.Err(), IsNil) + c.Assert(pttl.Val() <= expiration, Equals, true) + c.Assert(pttl.Val() >= expiration-time.Millisecond, Equals, true) +} + +func (t *RedisTest) TestCmdKeysRandomKey(c *C) { + randomKey := t.client.RandomKey() + c.Assert(randomKey.Err(), Equals, redis.Nil) + c.Assert(randomKey.Val(), Equals, "") + + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + randomKey = t.client.RandomKey() + c.Assert(randomKey.Err(), IsNil) + c.Assert(randomKey.Val(), Equals, "key") +} + +func (t *RedisTest) TestCmdKeysRename(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + status := t.client.Rename("key", "key1") + c.Assert(status.Err(), IsNil) + c.Assert(status.Val(), Equals, "OK") + + get := t.client.Get("key1") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") +} + +func (t *RedisTest) TestCmdKeysRenameNX(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + renameNX := t.client.RenameNX("key", "key1") + c.Assert(renameNX.Err(), IsNil) + c.Assert(renameNX.Val(), Equals, true) + + get := t.client.Get("key1") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") +} + +func (t *RedisTest) TestCmdKeysRestore(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + dump := t.client.Dump("key") + c.Assert(dump.Err(), IsNil) + + del := t.client.Del("key") + c.Assert(del.Err(), IsNil) + + restore := t.client.Restore("key", 0, dump.Val()) + c.Assert(restore.Err(), IsNil) + c.Assert(restore.Val(), Equals, "OK") + + type_ := t.client.Type("key") + c.Assert(type_.Err(), IsNil) + c.Assert(type_.Val(), Equals, "string") + + lRange := t.client.Get("key") + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), Equals, "hello") +} + +func (t *RedisTest) TestCmdKeysSort(c *C) { + lPush := t.client.LPush("list", "1") + c.Assert(lPush.Err(), IsNil) + c.Assert(lPush.Val(), Equals, int64(1)) + lPush = t.client.LPush("list", "3") + c.Assert(lPush.Err(), IsNil) + c.Assert(lPush.Val(), Equals, int64(2)) + lPush = t.client.LPush("list", "2") + c.Assert(lPush.Err(), IsNil) + c.Assert(lPush.Val(), Equals, int64(3)) + + sort := t.client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"}) + c.Assert(sort.Err(), IsNil) + c.Assert(sort.Val(), DeepEquals, []string{"1", "2"}) +} + +func (t *RedisTest) TestCmdKeysSortBy(c *C) { + lPush := t.client.LPush("list", "1") + c.Assert(lPush.Err(), IsNil) + c.Assert(lPush.Val(), Equals, int64(1)) + lPush = t.client.LPush("list", "3") + c.Assert(lPush.Err(), IsNil) + c.Assert(lPush.Val(), Equals, int64(2)) + lPush = t.client.LPush("list", "2") + c.Assert(lPush.Err(), IsNil) + c.Assert(lPush.Val(), Equals, int64(3)) + + set := t.client.Set("weight_1", "5") + c.Assert(set.Err(), IsNil) + set = t.client.Set("weight_2", "2") + c.Assert(set.Err(), IsNil) + set = t.client.Set("weight_3", "8") + c.Assert(set.Err(), IsNil) + + sort := t.client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC", By: "weight_*"}) + c.Assert(sort.Err(), IsNil) + c.Assert(sort.Val(), DeepEquals, []string{"2", "1"}) +} + +func (t *RedisTest) TestCmdKeysTTL(c *C) { + ttl := t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val() < 0, Equals, true) + + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + expire := t.client.Expire("key", 60*time.Second) + c.Assert(expire.Err(), IsNil) + c.Assert(expire.Val(), Equals, true) + + ttl = t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val(), Equals, 60*time.Second) +} + +func (t *RedisTest) TestCmdKeysType(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + type_ := t.client.Type("key") + c.Assert(type_.Err(), IsNil) + c.Assert(type_.Val(), Equals, "string") +} + +func (t *RedisTest) TestCmdScan(c *C) { + for i := 0; i < 1000; i++ { + set := t.client.Set(fmt.Sprintf("key%d", i), "hello") + c.Assert(set.Err(), IsNil) + } + + cursor, keys, err := t.client.Scan(0, "", 0).Result() + c.Assert(err, IsNil) + c.Assert(cursor > 0, Equals, true) + c.Assert(len(keys) > 0, Equals, true) +} + +func (t *RedisTest) TestCmdSScan(c *C) { + for i := 0; i < 1000; i++ { + sadd := t.client.SAdd("myset", fmt.Sprintf("member%d", i)) + c.Assert(sadd.Err(), IsNil) + } + + cursor, keys, err := t.client.SScan("myset", 0, "", 0).Result() + c.Assert(err, IsNil) + c.Assert(cursor > 0, Equals, true) + c.Assert(len(keys) > 0, Equals, true) +} + +func (t *RedisTest) TestCmdHScan(c *C) { + for i := 0; i < 1000; i++ { + sadd := t.client.HSet("myhash", fmt.Sprintf("key%d", i), "hello") + c.Assert(sadd.Err(), IsNil) + } + + cursor, keys, err := t.client.HScan("myhash", 0, "", 0).Result() + c.Assert(err, IsNil) + c.Assert(cursor > 0, Equals, true) + c.Assert(len(keys) > 0, Equals, true) +} + +func (t *RedisTest) TestCmdZScan(c *C) { + for i := 0; i < 1000; i++ { + sadd := t.client.ZAdd("myset", redis.Z{float64(i), fmt.Sprintf("member%d", i)}) + c.Assert(sadd.Err(), IsNil) + } + + cursor, keys, err := t.client.ZScan("myset", 0, "", 0).Result() + c.Assert(err, IsNil) + c.Assert(cursor > 0, Equals, true) + c.Assert(len(keys) > 0, Equals, true) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestStringsAppend(c *C) { + exists := t.client.Exists("key") + c.Assert(exists.Err(), IsNil) + c.Assert(exists.Val(), Equals, false) + + append := t.client.Append("key", "Hello") + c.Assert(append.Err(), IsNil) + c.Assert(append.Val(), Equals, int64(5)) + + append = t.client.Append("key", " World") + c.Assert(append.Err(), IsNil) + c.Assert(append.Val(), Equals, int64(11)) + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "Hello World") +} + +func (t *RedisTest) TestStringsBitCount(c *C) { + set := t.client.Set("key", "foobar") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + bitCount := t.client.BitCount("key", nil) + c.Assert(bitCount.Err(), IsNil) + c.Assert(bitCount.Val(), Equals, int64(26)) + + bitCount = t.client.BitCount("key", &redis.BitCount{0, 0}) + c.Assert(bitCount.Err(), IsNil) + c.Assert(bitCount.Val(), Equals, int64(4)) + + bitCount = t.client.BitCount("key", &redis.BitCount{1, 1}) + c.Assert(bitCount.Err(), IsNil) + c.Assert(bitCount.Val(), Equals, int64(6)) +} + +func (t *RedisTest) TestStringsBitOpAnd(c *C) { + set := t.client.Set("key1", "1") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + set = t.client.Set("key2", "0") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + bitOpAnd := t.client.BitOpAnd("dest", "key1", "key2") + c.Assert(bitOpAnd.Err(), IsNil) + c.Assert(bitOpAnd.Val(), Equals, int64(1)) + + get := t.client.Get("dest") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "0") +} + +func (t *RedisTest) TestStringsBitOpOr(c *C) { + set := t.client.Set("key1", "1") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + set = t.client.Set("key2", "0") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + bitOpOr := t.client.BitOpOr("dest", "key1", "key2") + c.Assert(bitOpOr.Err(), IsNil) + c.Assert(bitOpOr.Val(), Equals, int64(1)) + + get := t.client.Get("dest") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "1") +} + +func (t *RedisTest) TestStringsBitOpXor(c *C) { + set := t.client.Set("key1", "\xff") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + set = t.client.Set("key2", "\x0f") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + bitOpXor := t.client.BitOpXor("dest", "key1", "key2") + c.Assert(bitOpXor.Err(), IsNil) + c.Assert(bitOpXor.Val(), Equals, int64(1)) + + get := t.client.Get("dest") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "\xf0") +} + +func (t *RedisTest) TestStringsBitOpNot(c *C) { + set := t.client.Set("key1", "\x00") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + bitOpNot := t.client.BitOpNot("dest", "key1") + c.Assert(bitOpNot.Err(), IsNil) + c.Assert(bitOpNot.Val(), Equals, int64(1)) + + get := t.client.Get("dest") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "\xff") +} + +func (t *RedisTest) TestStringsDecr(c *C) { + set := t.client.Set("key", "10") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + decr := t.client.Decr("key") + c.Assert(decr.Err(), IsNil) + c.Assert(decr.Val(), Equals, int64(9)) + + set = t.client.Set("key", "234293482390480948029348230948") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + decr = t.client.Decr("key") + c.Assert(decr.Err(), ErrorMatches, "ERR value is not an integer or out of range") + c.Assert(decr.Val(), Equals, int64(0)) +} + +func (t *RedisTest) TestStringsDecrBy(c *C) { + set := t.client.Set("key", "10") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + decrBy := t.client.DecrBy("key", 5) + c.Assert(decrBy.Err(), IsNil) + c.Assert(decrBy.Val(), Equals, int64(5)) +} + +func (t *RedisTest) TestStringsGet(c *C) { + get := t.client.Get("_") + c.Assert(get.Err(), Equals, redis.Nil) + c.Assert(get.Val(), Equals, "") + + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + get = t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") +} + +func (t *RedisTest) TestStringsGetBit(c *C) { + setBit := t.client.SetBit("key", 7, 1) + c.Assert(setBit.Err(), IsNil) + c.Assert(setBit.Val(), Equals, int64(0)) + + getBit := t.client.GetBit("key", 0) + c.Assert(getBit.Err(), IsNil) + c.Assert(getBit.Val(), Equals, int64(0)) + + getBit = t.client.GetBit("key", 7) + c.Assert(getBit.Err(), IsNil) + c.Assert(getBit.Val(), Equals, int64(1)) + + getBit = t.client.GetBit("key", 100) + c.Assert(getBit.Err(), IsNil) + c.Assert(getBit.Val(), Equals, int64(0)) +} + +func (t *RedisTest) TestStringsGetRange(c *C) { + set := t.client.Set("key", "This is a string") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + getRange := t.client.GetRange("key", 0, 3) + c.Assert(getRange.Err(), IsNil) + c.Assert(getRange.Val(), Equals, "This") + + getRange = t.client.GetRange("key", -3, -1) + c.Assert(getRange.Err(), IsNil) + c.Assert(getRange.Val(), Equals, "ing") + + getRange = t.client.GetRange("key", 0, -1) + c.Assert(getRange.Err(), IsNil) + c.Assert(getRange.Val(), Equals, "This is a string") + + getRange = t.client.GetRange("key", 10, 100) + c.Assert(getRange.Err(), IsNil) + c.Assert(getRange.Val(), Equals, "string") +} + +func (t *RedisTest) TestStringsGetSet(c *C) { + incr := t.client.Incr("key") + c.Assert(incr.Err(), IsNil) + c.Assert(incr.Val(), Equals, int64(1)) + + getSet := t.client.GetSet("key", "0") + c.Assert(getSet.Err(), IsNil) + c.Assert(getSet.Val(), Equals, "1") + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "0") +} + +func (t *RedisTest) TestStringsIncr(c *C) { + set := t.client.Set("key", "10") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + incr := t.client.Incr("key") + c.Assert(incr.Err(), IsNil) + c.Assert(incr.Val(), Equals, int64(11)) + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "11") +} + +func (t *RedisTest) TestStringsIncrBy(c *C) { + set := t.client.Set("key", "10") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + incrBy := t.client.IncrBy("key", 5) + c.Assert(incrBy.Err(), IsNil) + c.Assert(incrBy.Val(), Equals, int64(15)) +} + +func (t *RedisTest) TestIncrByFloat(c *C) { + set := t.client.Set("key", "10.50") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + incrByFloat := t.client.IncrByFloat("key", 0.1) + c.Assert(incrByFloat.Err(), IsNil) + c.Assert(incrByFloat.Val(), Equals, 10.6) + + set = t.client.Set("key", "5.0e3") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + incrByFloat = t.client.IncrByFloat("key", 2.0e2) + c.Assert(incrByFloat.Err(), IsNil) + c.Assert(incrByFloat.Val(), Equals, float64(5200)) +} + +func (t *RedisTest) TestIncrByFloatOverflow(c *C) { + incrByFloat := t.client.IncrByFloat("key", 996945661) + c.Assert(incrByFloat.Err(), IsNil) + c.Assert(incrByFloat.Val(), Equals, float64(996945661)) +} + +func (t *RedisTest) TestStringsMSetMGet(c *C) { + mSet := t.client.MSet("key1", "hello1", "key2", "hello2") + c.Assert(mSet.Err(), IsNil) + c.Assert(mSet.Val(), Equals, "OK") + + mGet := t.client.MGet("key1", "key2", "_") + c.Assert(mGet.Err(), IsNil) + c.Assert(mGet.Val(), DeepEquals, []interface{}{"hello1", "hello2", nil}) +} + +func (t *RedisTest) TestStringsMSetNX(c *C) { + mSetNX := t.client.MSetNX("key1", "hello1", "key2", "hello2") + c.Assert(mSetNX.Err(), IsNil) + c.Assert(mSetNX.Val(), Equals, true) + + mSetNX = t.client.MSetNX("key2", "hello1", "key3", "hello2") + c.Assert(mSetNX.Err(), IsNil) + c.Assert(mSetNX.Val(), Equals, false) +} + +func (t *RedisTest) TestStringsPSetEx(c *C) { + expiration := 50 * time.Millisecond + psetex := t.client.PSetEx("key", expiration, "hello") + c.Assert(psetex.Err(), IsNil) + c.Assert(psetex.Val(), Equals, "OK") + + pttl := t.client.PTTL("key") + c.Assert(pttl.Err(), IsNil) + c.Assert(pttl.Val() <= expiration, Equals, true) + c.Assert(pttl.Val() >= expiration-time.Millisecond, Equals, true) + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") +} + +func (t *RedisTest) TestStringsSetGet(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") +} + +func (t *RedisTest) TestStringsSetEx(c *C) { + setEx := t.client.SetEx("key", 10*time.Second, "hello") + c.Assert(setEx.Err(), IsNil) + c.Assert(setEx.Val(), Equals, "OK") + + ttl := t.client.TTL("key") + c.Assert(ttl.Err(), IsNil) + c.Assert(ttl.Val(), Equals, 10*time.Second) +} + +func (t *RedisTest) TestStringsSetNX(c *C) { + setNX := t.client.SetNX("key", "hello") + c.Assert(setNX.Err(), IsNil) + c.Assert(setNX.Val(), Equals, true) + + setNX = t.client.SetNX("key", "hello2") + c.Assert(setNX.Err(), IsNil) + c.Assert(setNX.Val(), Equals, false) + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") +} + +func (t *RedisTest) TestStringsSetRange(c *C) { + set := t.client.Set("key", "Hello World") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + range_ := t.client.SetRange("key", 6, "Redis") + c.Assert(range_.Err(), IsNil) + c.Assert(range_.Val(), Equals, int64(11)) + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "Hello Redis") +} + +func (t *RedisTest) TestStringsStrLen(c *C) { + set := t.client.Set("key", "hello") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + strLen := t.client.StrLen("key") + c.Assert(strLen.Err(), IsNil) + c.Assert(strLen.Val(), Equals, int64(5)) + + strLen = t.client.StrLen("_") + c.Assert(strLen.Err(), IsNil) + c.Assert(strLen.Val(), Equals, int64(0)) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestCmdHDel(c *C) { + hSet := t.client.HSet("hash", "key", "hello") + c.Assert(hSet.Err(), IsNil) + + hDel := t.client.HDel("hash", "key") + c.Assert(hDel.Err(), IsNil) + c.Assert(hDel.Val(), Equals, int64(1)) + + hDel = t.client.HDel("hash", "key") + c.Assert(hDel.Err(), IsNil) + c.Assert(hDel.Val(), Equals, int64(0)) +} + +func (t *RedisTest) TestCmdHExists(c *C) { + hSet := t.client.HSet("hash", "key", "hello") + c.Assert(hSet.Err(), IsNil) + + hExists := t.client.HExists("hash", "key") + c.Assert(hExists.Err(), IsNil) + c.Assert(hExists.Val(), Equals, true) + + hExists = t.client.HExists("hash", "key1") + c.Assert(hExists.Err(), IsNil) + c.Assert(hExists.Val(), Equals, false) +} + +func (t *RedisTest) TestCmdHGet(c *C) { + hSet := t.client.HSet("hash", "key", "hello") + c.Assert(hSet.Err(), IsNil) + + hGet := t.client.HGet("hash", "key") + c.Assert(hGet.Err(), IsNil) + c.Assert(hGet.Val(), Equals, "hello") + + hGet = t.client.HGet("hash", "key1") + c.Assert(hGet.Err(), Equals, redis.Nil) + c.Assert(hGet.Val(), Equals, "") +} + +func (t *RedisTest) TestCmdHGetAll(c *C) { + hSet := t.client.HSet("hash", "key1", "hello1") + c.Assert(hSet.Err(), IsNil) + hSet = t.client.HSet("hash", "key2", "hello2") + c.Assert(hSet.Err(), IsNil) + + hGetAll := t.client.HGetAll("hash") + c.Assert(hGetAll.Err(), IsNil) + c.Assert(hGetAll.Val(), DeepEquals, []string{"key1", "hello1", "key2", "hello2"}) +} + +func (t *RedisTest) TestCmdHGetAllMap(c *C) { + hSet := t.client.HSet("hash", "key1", "hello1") + c.Assert(hSet.Err(), IsNil) + hSet = t.client.HSet("hash", "key2", "hello2") + c.Assert(hSet.Err(), IsNil) + + hGetAll := t.client.HGetAllMap("hash") + c.Assert(hGetAll.Err(), IsNil) + c.Assert(hGetAll.Val(), DeepEquals, map[string]string{"key1": "hello1", "key2": "hello2"}) +} + +func (t *RedisTest) TestCmdHIncrBy(c *C) { + hSet := t.client.HSet("hash", "key", "5") + c.Assert(hSet.Err(), IsNil) + + hIncrBy := t.client.HIncrBy("hash", "key", 1) + c.Assert(hIncrBy.Err(), IsNil) + c.Assert(hIncrBy.Val(), Equals, int64(6)) + + hIncrBy = t.client.HIncrBy("hash", "key", -1) + c.Assert(hIncrBy.Err(), IsNil) + c.Assert(hIncrBy.Val(), Equals, int64(5)) + + hIncrBy = t.client.HIncrBy("hash", "key", -10) + c.Assert(hIncrBy.Err(), IsNil) + c.Assert(hIncrBy.Val(), Equals, int64(-5)) +} + +func (t *RedisTest) TestCmdHIncrByFloat(c *C) { + hSet := t.client.HSet("hash", "field", "10.50") + c.Assert(hSet.Err(), IsNil) + c.Assert(hSet.Val(), Equals, true) + + hIncrByFloat := t.client.HIncrByFloat("hash", "field", 0.1) + c.Assert(hIncrByFloat.Err(), IsNil) + c.Assert(hIncrByFloat.Val(), Equals, 10.6) + + hSet = t.client.HSet("hash", "field", "5.0e3") + c.Assert(hSet.Err(), IsNil) + c.Assert(hSet.Val(), Equals, false) + + hIncrByFloat = t.client.HIncrByFloat("hash", "field", 2.0e2) + c.Assert(hIncrByFloat.Err(), IsNil) + c.Assert(hIncrByFloat.Val(), Equals, float64(5200)) +} + +func (t *RedisTest) TestCmdHKeys(c *C) { + hkeys := t.client.HKeys("hash") + c.Assert(hkeys.Err(), IsNil) + c.Assert(hkeys.Val(), DeepEquals, []string{}) + + hset := t.client.HSet("hash", "key1", "hello1") + c.Assert(hset.Err(), IsNil) + hset = t.client.HSet("hash", "key2", "hello2") + c.Assert(hset.Err(), IsNil) + + hkeys = t.client.HKeys("hash") + c.Assert(hkeys.Err(), IsNil) + c.Assert(hkeys.Val(), DeepEquals, []string{"key1", "key2"}) +} + +func (t *RedisTest) TestCmdHLen(c *C) { + hSet := t.client.HSet("hash", "key1", "hello1") + c.Assert(hSet.Err(), IsNil) + hSet = t.client.HSet("hash", "key2", "hello2") + c.Assert(hSet.Err(), IsNil) + + hLen := t.client.HLen("hash") + c.Assert(hLen.Err(), IsNil) + c.Assert(hLen.Val(), Equals, int64(2)) +} + +func (t *RedisTest) TestCmdHMGet(c *C) { + hSet := t.client.HSet("hash", "key1", "hello1") + c.Assert(hSet.Err(), IsNil) + hSet = t.client.HSet("hash", "key2", "hello2") + c.Assert(hSet.Err(), IsNil) + + hMGet := t.client.HMGet("hash", "key1", "key2", "_") + c.Assert(hMGet.Err(), IsNil) + c.Assert(hMGet.Val(), DeepEquals, []interface{}{"hello1", "hello2", nil}) +} + +func (t *RedisTest) TestCmdHMSet(c *C) { + hMSet := t.client.HMSet("hash", "key1", "hello1", "key2", "hello2") + c.Assert(hMSet.Err(), IsNil) + c.Assert(hMSet.Val(), Equals, "OK") + + hGet := t.client.HGet("hash", "key1") + c.Assert(hGet.Err(), IsNil) + c.Assert(hGet.Val(), Equals, "hello1") + + hGet = t.client.HGet("hash", "key2") + c.Assert(hGet.Err(), IsNil) + c.Assert(hGet.Val(), Equals, "hello2") +} + +func (t *RedisTest) TestCmdHSet(c *C) { + hSet := t.client.HSet("hash", "key", "hello") + c.Assert(hSet.Err(), IsNil) + c.Assert(hSet.Val(), Equals, true) + + hGet := t.client.HGet("hash", "key") + c.Assert(hGet.Err(), IsNil) + c.Assert(hGet.Val(), Equals, "hello") +} + +func (t *RedisTest) TestCmdHSetNX(c *C) { + hSetNX := t.client.HSetNX("hash", "key", "hello") + c.Assert(hSetNX.Err(), IsNil) + c.Assert(hSetNX.Val(), Equals, true) + + hSetNX = t.client.HSetNX("hash", "key", "hello") + c.Assert(hSetNX.Err(), IsNil) + c.Assert(hSetNX.Val(), Equals, false) + + hGet := t.client.HGet("hash", "key") + c.Assert(hGet.Err(), IsNil) + c.Assert(hGet.Val(), Equals, "hello") +} + +func (t *RedisTest) TestCmdHVals(c *C) { + hSet := t.client.HSet("hash", "key1", "hello1") + c.Assert(hSet.Err(), IsNil) + hSet = t.client.HSet("hash", "key2", "hello2") + c.Assert(hSet.Err(), IsNil) + + hVals := t.client.HVals("hash") + c.Assert(hVals.Err(), IsNil) + c.Assert(hVals.Val(), DeepEquals, []string{"hello1", "hello2"}) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestCmdListsBLPop(c *C) { + rPush := t.client.RPush("list1", "a", "b", "c") + c.Assert(rPush.Err(), IsNil) + + bLPop := t.client.BLPop(0, "list1", "list2") + c.Assert(bLPop.Err(), IsNil) + c.Assert(bLPop.Val(), DeepEquals, []string{"list1", "a"}) +} + +func (t *RedisTest) TestCmdListsBLPopBlocks(c *C) { + started := make(chan bool) + done := make(chan bool) + go func() { + started <- true + bLPop := t.client.BLPop(0, "list") + c.Assert(bLPop.Err(), IsNil) + c.Assert(bLPop.Val(), DeepEquals, []string{"list", "a"}) + done <- true + }() + <-started + + select { + case <-done: + c.Error("BLPop is not blocked") + case <-time.After(time.Second): + // ok + } + + rPush := t.client.RPush("list", "a") + c.Assert(rPush.Err(), IsNil) + + select { + case <-done: + // ok + case <-time.After(time.Second): + c.Error("BLPop is still blocked") + // ok + } +} + +func (t *RedisTest) TestCmdListsBLPopTimeout(c *C) { + bLPop := t.client.BLPop(1, "list1") + c.Assert(bLPop.Err(), Equals, redis.Nil) + c.Assert(bLPop.Val(), IsNil) +} + +func (t *RedisTest) TestCmdListsBRPop(c *C) { + rPush := t.client.RPush("list1", "a", "b", "c") + c.Assert(rPush.Err(), IsNil) + + bRPop := t.client.BRPop(0, "list1", "list2") + c.Assert(bRPop.Err(), IsNil) + c.Assert(bRPop.Val(), DeepEquals, []string{"list1", "c"}) +} + +func (t *RedisTest) TestCmdListsBRPopBlocks(c *C) { + started := make(chan bool) + done := make(chan bool) + go func() { + started <- true + brpop := t.client.BRPop(0, "list") + c.Assert(brpop.Err(), IsNil) + c.Assert(brpop.Val(), DeepEquals, []string{"list", "a"}) + done <- true + }() + <-started + + select { + case <-done: + c.Error("BRPop is not blocked") + case <-time.After(time.Second): + // ok + } + + rPush := t.client.RPush("list", "a") + c.Assert(rPush.Err(), IsNil) + + select { + case <-done: + // ok + case <-time.After(time.Second): + c.Error("BRPop is still blocked") + // ok + } +} + +func (t *RedisTest) TestCmdListsBRPopLPush(c *C) { + rPush := t.client.RPush("list1", "a", "b", "c") + c.Assert(rPush.Err(), IsNil) + + bRPopLPush := t.client.BRPopLPush("list1", "list2", 0) + c.Assert(bRPopLPush.Err(), IsNil) + c.Assert(bRPopLPush.Val(), Equals, "c") +} + +func (t *RedisTest) TestCmdListsLIndex(c *C) { + lPush := t.client.LPush("list", "World") + c.Assert(lPush.Err(), IsNil) + lPush = t.client.LPush("list", "Hello") + c.Assert(lPush.Err(), IsNil) + + lIndex := t.client.LIndex("list", 0) + c.Assert(lIndex.Err(), IsNil) + c.Assert(lIndex.Val(), Equals, "Hello") + + lIndex = t.client.LIndex("list", -1) + c.Assert(lIndex.Err(), IsNil) + c.Assert(lIndex.Val(), Equals, "World") + + lIndex = t.client.LIndex("list", 3) + c.Assert(lIndex.Err(), Equals, redis.Nil) + c.Assert(lIndex.Val(), Equals, "") +} + +func (t *RedisTest) TestCmdListsLInsert(c *C) { + rPush := t.client.RPush("list", "Hello") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "World") + c.Assert(rPush.Err(), IsNil) + + lInsert := t.client.LInsert("list", "BEFORE", "World", "There") + c.Assert(lInsert.Err(), IsNil) + c.Assert(lInsert.Val(), Equals, int64(3)) + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"Hello", "There", "World"}) +} + +func (t *RedisTest) TestCmdListsLLen(c *C) { + lPush := t.client.LPush("list", "World") + c.Assert(lPush.Err(), IsNil) + lPush = t.client.LPush("list", "Hello") + c.Assert(lPush.Err(), IsNil) + + lLen := t.client.LLen("list") + c.Assert(lLen.Err(), IsNil) + c.Assert(lLen.Val(), Equals, int64(2)) +} + +func (t *RedisTest) TestCmdListsLPop(c *C) { + rPush := t.client.RPush("list", "one") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "two") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "three") + c.Assert(rPush.Err(), IsNil) + + lPop := t.client.LPop("list") + c.Assert(lPop.Err(), IsNil) + c.Assert(lPop.Val(), Equals, "one") + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"two", "three"}) +} + +func (t *RedisTest) TestCmdListsLPush(c *C) { + lPush := t.client.LPush("list", "World") + c.Assert(lPush.Err(), IsNil) + lPush = t.client.LPush("list", "Hello") + c.Assert(lPush.Err(), IsNil) + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"Hello", "World"}) +} + +func (t *RedisTest) TestCmdListsLPushX(c *C) { + lPush := t.client.LPush("list", "World") + c.Assert(lPush.Err(), IsNil) + + lPushX := t.client.LPushX("list", "Hello") + c.Assert(lPushX.Err(), IsNil) + c.Assert(lPushX.Val(), Equals, int64(2)) + + lPushX = t.client.LPushX("list2", "Hello") + c.Assert(lPushX.Err(), IsNil) + c.Assert(lPushX.Val(), Equals, int64(0)) + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"Hello", "World"}) + + lRange = t.client.LRange("list2", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{}) +} + +func (t *RedisTest) TestCmdListsLRange(c *C) { + rPush := t.client.RPush("list", "one") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "two") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "three") + c.Assert(rPush.Err(), IsNil) + + lRange := t.client.LRange("list", 0, 0) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"one"}) + + lRange = t.client.LRange("list", -3, 2) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"one", "two", "three"}) + + lRange = t.client.LRange("list", -100, 100) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"one", "two", "three"}) + + lRange = t.client.LRange("list", 5, 10) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{}) +} + +func (t *RedisTest) TestCmdListsLRem(c *C) { + rPush := t.client.RPush("list", "hello") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "hello") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "key") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "hello") + c.Assert(rPush.Err(), IsNil) + + lRem := t.client.LRem("list", -2, "hello") + c.Assert(lRem.Err(), IsNil) + c.Assert(lRem.Val(), Equals, int64(2)) + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"hello", "key"}) +} + +func (t *RedisTest) TestCmdListsLSet(c *C) { + rPush := t.client.RPush("list", "one") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "two") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "three") + c.Assert(rPush.Err(), IsNil) + + lSet := t.client.LSet("list", 0, "four") + c.Assert(lSet.Err(), IsNil) + c.Assert(lSet.Val(), Equals, "OK") + + lSet = t.client.LSet("list", -2, "five") + c.Assert(lSet.Err(), IsNil) + c.Assert(lSet.Val(), Equals, "OK") + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"four", "five", "three"}) +} + +func (t *RedisTest) TestCmdListsLTrim(c *C) { + rPush := t.client.RPush("list", "one") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "two") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "three") + c.Assert(rPush.Err(), IsNil) + + lTrim := t.client.LTrim("list", 1, -1) + c.Assert(lTrim.Err(), IsNil) + c.Assert(lTrim.Val(), Equals, "OK") + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"two", "three"}) +} + +func (t *RedisTest) TestCmdListsRPop(c *C) { + rPush := t.client.RPush("list", "one") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "two") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "three") + c.Assert(rPush.Err(), IsNil) + + rPop := t.client.RPop("list") + c.Assert(rPop.Err(), IsNil) + c.Assert(rPop.Val(), Equals, "three") + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"one", "two"}) +} + +func (t *RedisTest) TestCmdListsRPopLPush(c *C) { + rPush := t.client.RPush("list", "one") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "two") + c.Assert(rPush.Err(), IsNil) + rPush = t.client.RPush("list", "three") + c.Assert(rPush.Err(), IsNil) + + rPopLPush := t.client.RPopLPush("list", "list2") + c.Assert(rPopLPush.Err(), IsNil) + c.Assert(rPopLPush.Val(), Equals, "three") + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"one", "two"}) + + lRange = t.client.LRange("list2", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"three"}) +} + +func (t *RedisTest) TestCmdListsRPush(c *C) { + rPush := t.client.RPush("list", "Hello") + c.Assert(rPush.Err(), IsNil) + c.Assert(rPush.Val(), Equals, int64(1)) + + rPush = t.client.RPush("list", "World") + c.Assert(rPush.Err(), IsNil) + c.Assert(rPush.Val(), Equals, int64(2)) + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"Hello", "World"}) +} + +func (t *RedisTest) TestCmdListsRPushX(c *C) { + rPush := t.client.RPush("list", "Hello") + c.Assert(rPush.Err(), IsNil) + c.Assert(rPush.Val(), Equals, int64(1)) + + rPushX := t.client.RPushX("list", "World") + c.Assert(rPushX.Err(), IsNil) + c.Assert(rPushX.Val(), Equals, int64(2)) + + rPushX = t.client.RPushX("list2", "World") + c.Assert(rPushX.Err(), IsNil) + c.Assert(rPushX.Val(), Equals, int64(0)) + + lRange := t.client.LRange("list", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{"Hello", "World"}) + + lRange = t.client.LRange("list2", 0, -1) + c.Assert(lRange.Err(), IsNil) + c.Assert(lRange.Val(), DeepEquals, []string{}) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestSAdd(c *C) { + sAdd := t.client.SAdd("set", "Hello") + c.Assert(sAdd.Err(), IsNil) + c.Assert(sAdd.Val(), Equals, int64(1)) + + sAdd = t.client.SAdd("set", "World") + c.Assert(sAdd.Err(), IsNil) + c.Assert(sAdd.Val(), Equals, int64(1)) + + sAdd = t.client.SAdd("set", "World") + c.Assert(sAdd.Err(), IsNil) + c.Assert(sAdd.Val(), Equals, int64(0)) + + sMembers := t.client.SMembers("set") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sortStrings(sMembers.Val()), DeepEquals, []string{"Hello", "World"}) +} + +func (t *RedisTest) TestSCard(c *C) { + sAdd := t.client.SAdd("set", "Hello") + c.Assert(sAdd.Err(), IsNil) + c.Assert(sAdd.Val(), Equals, int64(1)) + + sAdd = t.client.SAdd("set", "World") + c.Assert(sAdd.Err(), IsNil) + c.Assert(sAdd.Val(), Equals, int64(1)) + + sCard := t.client.SCard("set") + c.Assert(sCard.Err(), IsNil) + c.Assert(sCard.Val(), Equals, int64(2)) +} + +func (t *RedisTest) TestSDiff(c *C) { + sAdd := t.client.SAdd("set1", "a") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "b") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "c") + c.Assert(sAdd.Err(), IsNil) + + sAdd = t.client.SAdd("set2", "c") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "d") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "e") + c.Assert(sAdd.Err(), IsNil) + + sDiff := t.client.SDiff("set1", "set2") + c.Assert(sDiff.Err(), IsNil) + c.Assert(sortStrings(sDiff.Val()), DeepEquals, []string{"a", "b"}) +} + +func (t *RedisTest) TestSDiffStore(c *C) { + sAdd := t.client.SAdd("set1", "a") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "b") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "c") + c.Assert(sAdd.Err(), IsNil) + + sAdd = t.client.SAdd("set2", "c") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "d") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "e") + c.Assert(sAdd.Err(), IsNil) + + sDiffStore := t.client.SDiffStore("set", "set1", "set2") + c.Assert(sDiffStore.Err(), IsNil) + c.Assert(sDiffStore.Val(), Equals, int64(2)) + + sMembers := t.client.SMembers("set") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sortStrings(sMembers.Val()), DeepEquals, []string{"a", "b"}) +} + +func (t *RedisTest) TestSInter(c *C) { + sAdd := t.client.SAdd("set1", "a") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "b") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "c") + c.Assert(sAdd.Err(), IsNil) + + sAdd = t.client.SAdd("set2", "c") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "d") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "e") + c.Assert(sAdd.Err(), IsNil) + + sInter := t.client.SInter("set1", "set2") + c.Assert(sInter.Err(), IsNil) + c.Assert(sInter.Val(), DeepEquals, []string{"c"}) +} + +func (t *RedisTest) TestSInterStore(c *C) { + sAdd := t.client.SAdd("set1", "a") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "b") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "c") + c.Assert(sAdd.Err(), IsNil) + + sAdd = t.client.SAdd("set2", "c") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "d") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "e") + c.Assert(sAdd.Err(), IsNil) + + sInterStore := t.client.SInterStore("set", "set1", "set2") + c.Assert(sInterStore.Err(), IsNil) + c.Assert(sInterStore.Val(), Equals, int64(1)) + + sMembers := t.client.SMembers("set") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sMembers.Val(), DeepEquals, []string{"c"}) +} + +func (t *RedisTest) TestIsMember(c *C) { + sAdd := t.client.SAdd("set", "one") + c.Assert(sAdd.Err(), IsNil) + + sIsMember := t.client.SIsMember("set", "one") + c.Assert(sIsMember.Err(), IsNil) + c.Assert(sIsMember.Val(), Equals, true) + + sIsMember = t.client.SIsMember("set", "two") + c.Assert(sIsMember.Err(), IsNil) + c.Assert(sIsMember.Val(), Equals, false) +} + +func (t *RedisTest) TestSMembers(c *C) { + sAdd := t.client.SAdd("set", "Hello") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set", "World") + c.Assert(sAdd.Err(), IsNil) + + sMembers := t.client.SMembers("set") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sortStrings(sMembers.Val()), DeepEquals, []string{"Hello", "World"}) +} + +func (t *RedisTest) TestSMove(c *C) { + sAdd := t.client.SAdd("set1", "one") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "two") + c.Assert(sAdd.Err(), IsNil) + + sAdd = t.client.SAdd("set2", "three") + c.Assert(sAdd.Err(), IsNil) + + sMove := t.client.SMove("set1", "set2", "two") + c.Assert(sMove.Err(), IsNil) + c.Assert(sMove.Val(), Equals, true) + + sMembers := t.client.SMembers("set1") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sMembers.Val(), DeepEquals, []string{"one"}) + + sMembers = t.client.SMembers("set2") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sortStrings(sMembers.Val()), DeepEquals, []string{"three", "two"}) +} + +func (t *RedisTest) TestSPop(c *C) { + sAdd := t.client.SAdd("set", "one") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set", "two") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set", "three") + c.Assert(sAdd.Err(), IsNil) + + sPop := t.client.SPop("set") + c.Assert(sPop.Err(), IsNil) + c.Assert(sPop.Val(), Not(Equals), "") + + sMembers := t.client.SMembers("set") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sMembers.Val(), HasLen, 2) +} + +func (t *RedisTest) TestSRandMember(c *C) { + sAdd := t.client.SAdd("set", "one") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set", "two") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set", "three") + c.Assert(sAdd.Err(), IsNil) + + sRandMember := t.client.SRandMember("set") + c.Assert(sRandMember.Err(), IsNil) + c.Assert(sRandMember.Val(), Not(Equals), "") + + sMembers := t.client.SMembers("set") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sMembers.Val(), HasLen, 3) +} + +func (t *RedisTest) TestSRem(c *C) { + sAdd := t.client.SAdd("set", "one") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set", "two") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set", "three") + c.Assert(sAdd.Err(), IsNil) + + sRem := t.client.SRem("set", "one") + c.Assert(sRem.Err(), IsNil) + c.Assert(sRem.Val(), Equals, int64(1)) + + sRem = t.client.SRem("set", "four") + c.Assert(sRem.Err(), IsNil) + c.Assert(sRem.Val(), Equals, int64(0)) + + sMembers := t.client.SMembers("set") + c.Assert(sMembers.Err(), IsNil) + c.Assert( + sortStrings(sMembers.Val()), + DeepEquals, + []string{"three", "two"}, + ) +} + +func (t *RedisTest) TestSUnion(c *C) { + sAdd := t.client.SAdd("set1", "a") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "b") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "c") + c.Assert(sAdd.Err(), IsNil) + + sAdd = t.client.SAdd("set2", "c") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "d") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "e") + c.Assert(sAdd.Err(), IsNil) + + sUnion := t.client.SUnion("set1", "set2") + c.Assert(sUnion.Err(), IsNil) + c.Assert(sUnion.Val(), HasLen, 5) +} + +func (t *RedisTest) TestSUnionStore(c *C) { + sAdd := t.client.SAdd("set1", "a") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "b") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set1", "c") + c.Assert(sAdd.Err(), IsNil) + + sAdd = t.client.SAdd("set2", "c") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "d") + c.Assert(sAdd.Err(), IsNil) + sAdd = t.client.SAdd("set2", "e") + c.Assert(sAdd.Err(), IsNil) + + sUnionStore := t.client.SUnionStore("set", "set1", "set2") + c.Assert(sUnionStore.Err(), IsNil) + c.Assert(sUnionStore.Val(), Equals, int64(5)) + + sMembers := t.client.SMembers("set") + c.Assert(sMembers.Err(), IsNil) + c.Assert(sMembers.Val(), HasLen, 5) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestZAdd(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + c.Assert(zAdd.Val(), Equals, int64(1)) + + zAdd = t.client.ZAdd("zset", redis.Z{1, "uno"}) + c.Assert(zAdd.Err(), IsNil) + c.Assert(zAdd.Val(), Equals, int64(1)) + + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + c.Assert(zAdd.Val(), Equals, int64(1)) + + zAdd = t.client.ZAdd("zset", redis.Z{3, "two"}) + c.Assert(zAdd.Err(), IsNil) + c.Assert(zAdd.Val(), Equals, int64(0)) + + val, err := t.client.ZRangeWithScores("zset", 0, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{1, "one"}, {1, "uno"}, {3, "two"}}) +} + +func (t *RedisTest) TestZCard(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + + zCard := t.client.ZCard("zset") + c.Assert(zCard.Err(), IsNil) + c.Assert(zCard.Val(), Equals, int64(2)) +} + +func (t *RedisTest) TestZCount(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zCount := t.client.ZCount("zset", "-inf", "+inf") + c.Assert(zCount.Err(), IsNil) + c.Assert(zCount.Val(), Equals, int64(3)) + + zCount = t.client.ZCount("zset", "(1", "3") + c.Assert(zCount.Err(), IsNil) + c.Assert(zCount.Val(), Equals, int64(2)) +} + +func (t *RedisTest) TestZIncrBy(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + + zIncrBy := t.client.ZIncrBy("zset", 2, "one") + c.Assert(zIncrBy.Err(), IsNil) + c.Assert(zIncrBy.Val(), Equals, float64(3)) + + val, err := t.client.ZRangeWithScores("zset", 0, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{2, "two"}, {3, "one"}}) +} + +func (t *RedisTest) TestZInterStore(c *C) { + zAdd := t.client.ZAdd("zset1", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset1", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + + zAdd = t.client.ZAdd("zset2", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset2", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset3", redis.Z{3, "two"}) + c.Assert(zAdd.Err(), IsNil) + + zInterStore := t.client.ZInterStore( + "out", redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2") + c.Assert(zInterStore.Err(), IsNil) + c.Assert(zInterStore.Val(), Equals, int64(2)) + + val, err := t.client.ZRangeWithScores("out", 0, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{5, "one"}, {10, "two"}}) +} + +func (t *RedisTest) TestZRange(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zRange := t.client.ZRange("zset", 0, -1) + c.Assert(zRange.Err(), IsNil) + c.Assert(zRange.Val(), DeepEquals, []string{"one", "two", "three"}) + + zRange = t.client.ZRange("zset", 2, 3) + c.Assert(zRange.Err(), IsNil) + c.Assert(zRange.Val(), DeepEquals, []string{"three"}) + + zRange = t.client.ZRange("zset", -2, -1) + c.Assert(zRange.Err(), IsNil) + c.Assert(zRange.Val(), DeepEquals, []string{"two", "three"}) +} + +func (t *RedisTest) TestZRangeWithScores(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + val, err := t.client.ZRangeWithScores("zset", 0, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{1, "one"}, {2, "two"}, {3, "three"}}) + + val, err = t.client.ZRangeWithScores("zset", 2, 3).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{3, "three"}}) + + val, err = t.client.ZRangeWithScores("zset", -2, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{2, "two"}, {3, "three"}}) +} + +func (t *RedisTest) TestZRangeByScore(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zRangeByScore := t.client.ZRangeByScore("zset", redis.ZRangeByScore{ + Min: "-inf", + Max: "+inf", + }) + c.Assert(zRangeByScore.Err(), IsNil) + c.Assert(zRangeByScore.Val(), DeepEquals, []string{"one", "two", "three"}) + + zRangeByScore = t.client.ZRangeByScore("zset", redis.ZRangeByScore{ + Min: "1", + Max: "2", + }) + c.Assert(zRangeByScore.Err(), IsNil) + c.Assert(zRangeByScore.Val(), DeepEquals, []string{"one", "two"}) + + zRangeByScore = t.client.ZRangeByScore("zset", redis.ZRangeByScore{ + Min: "(1", + Max: "2", + }) + c.Assert(zRangeByScore.Err(), IsNil) + c.Assert(zRangeByScore.Val(), DeepEquals, []string{"two"}) + + zRangeByScore = t.client.ZRangeByScore("zset", redis.ZRangeByScore{ + Min: "(1", + Max: "(2", + }) + c.Assert(zRangeByScore.Err(), IsNil) + c.Assert(zRangeByScore.Val(), DeepEquals, []string{}) +} + +func (t *RedisTest) TestZRangeByScoreWithScoresMap(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + val, err := t.client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + Min: "-inf", + Max: "+inf", + }).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{1, "one"}, {2, "two"}, {3, "three"}}) + + val, err = t.client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + Min: "1", + Max: "2", + }).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{1, "one"}, {2, "two"}}) + + val, err = t.client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + Min: "(1", + Max: "2", + }).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{2, "two"}}) + + val, err = t.client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + Min: "(1", + Max: "(2", + }).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{}) +} + +func (t *RedisTest) TestZRank(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zRank := t.client.ZRank("zset", "three") + c.Assert(zRank.Err(), IsNil) + c.Assert(zRank.Val(), Equals, int64(2)) + + zRank = t.client.ZRank("zset", "four") + c.Assert(zRank.Err(), Equals, redis.Nil) + c.Assert(zRank.Val(), Equals, int64(0)) +} + +func (t *RedisTest) TestZRem(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zRem := t.client.ZRem("zset", "two") + c.Assert(zRem.Err(), IsNil) + c.Assert(zRem.Val(), Equals, int64(1)) + + val, err := t.client.ZRangeWithScores("zset", 0, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{1, "one"}, {3, "three"}}) +} + +func (t *RedisTest) TestZRemRangeByRank(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zRemRangeByRank := t.client.ZRemRangeByRank("zset", 0, 1) + c.Assert(zRemRangeByRank.Err(), IsNil) + c.Assert(zRemRangeByRank.Val(), Equals, int64(2)) + + val, err := t.client.ZRangeWithScores("zset", 0, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{3, "three"}}) +} + +func (t *RedisTest) TestZRemRangeByScore(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zRemRangeByScore := t.client.ZRemRangeByScore("zset", "-inf", "(2") + c.Assert(zRemRangeByScore.Err(), IsNil) + c.Assert(zRemRangeByScore.Val(), Equals, int64(1)) + + val, err := t.client.ZRangeWithScores("zset", 0, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{2, "two"}, {3, "three"}}) +} + +func (t *RedisTest) TestZRevRange(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zRevRange := t.client.ZRevRange("zset", "0", "-1") + c.Assert(zRevRange.Err(), IsNil) + c.Assert(zRevRange.Val(), DeepEquals, []string{"three", "two", "one"}) + + zRevRange = t.client.ZRevRange("zset", "2", "3") + c.Assert(zRevRange.Err(), IsNil) + c.Assert(zRevRange.Val(), DeepEquals, []string{"one"}) + + zRevRange = t.client.ZRevRange("zset", "-2", "-1") + c.Assert(zRevRange.Err(), IsNil) + c.Assert(zRevRange.Val(), DeepEquals, []string{"two", "one"}) +} + +func (t *RedisTest) TestZRevRangeWithScoresMap(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + val, err := t.client.ZRevRangeWithScores("zset", "0", "-1").Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}) + + val, err = t.client.ZRevRangeWithScores("zset", "2", "3").Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{1, "one"}}) + + val, err = t.client.ZRevRangeWithScores("zset", "-2", "-1").Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{2, "two"}, {1, "one"}}) +} + +func (t *RedisTest) TestZRevRangeByScore(c *C) { + zadd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zadd.Err(), IsNil) + zadd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zadd.Err(), IsNil) + zadd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zadd.Err(), IsNil) + + vals, err := t.client.ZRevRangeByScore( + "zset", redis.ZRangeByScore{Max: "+inf", Min: "-inf"}).Result() + c.Assert(err, IsNil) + c.Assert(vals, DeepEquals, []string{"three", "two", "one"}) + + vals, err = t.client.ZRevRangeByScore( + "zset", redis.ZRangeByScore{Max: "2", Min: "(1"}).Result() + c.Assert(err, IsNil) + c.Assert(vals, DeepEquals, []string{"two"}) + + vals, err = t.client.ZRevRangeByScore( + "zset", redis.ZRangeByScore{Max: "(2", Min: "(1"}).Result() + c.Assert(err, IsNil) + c.Assert(vals, DeepEquals, []string{}) +} + +func (t *RedisTest) TestZRevRangeByScoreWithScores(c *C) { + zadd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zadd.Err(), IsNil) + zadd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zadd.Err(), IsNil) + zadd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zadd.Err(), IsNil) + + vals, err := t.client.ZRevRangeByScoreWithScores( + "zset", redis.ZRangeByScore{Max: "+inf", Min: "-inf"}).Result() + c.Assert(err, IsNil) + c.Assert(vals, DeepEquals, []redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}) +} + +func (t *RedisTest) TestZRevRangeByScoreWithScoresMap(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + val, err := t.client.ZRevRangeByScoreWithScores( + "zset", redis.ZRangeByScore{Max: "+inf", Min: "-inf"}).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}) + + val, err = t.client.ZRevRangeByScoreWithScores( + "zset", redis.ZRangeByScore{Max: "2", Min: "(1"}).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{2, "two"}}) + + val, err = t.client.ZRevRangeByScoreWithScores( + "zset", redis.ZRangeByScore{Max: "(2", Min: "(1"}).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{}) +} + +func (t *RedisTest) TestZRevRank(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zRevRank := t.client.ZRevRank("zset", "one") + c.Assert(zRevRank.Err(), IsNil) + c.Assert(zRevRank.Val(), Equals, int64(2)) + + zRevRank = t.client.ZRevRank("zset", "four") + c.Assert(zRevRank.Err(), Equals, redis.Nil) + c.Assert(zRevRank.Val(), Equals, int64(0)) +} + +func (t *RedisTest) TestZScore(c *C) { + zAdd := t.client.ZAdd("zset", redis.Z{1.001, "one"}) + c.Assert(zAdd.Err(), IsNil) + + zScore := t.client.ZScore("zset", "one") + c.Assert(zScore.Err(), IsNil) + c.Assert(zScore.Val(), Equals, float64(1.001)) +} + +func (t *RedisTest) TestZUnionStore(c *C) { + zAdd := t.client.ZAdd("zset1", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset1", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + + zAdd = t.client.ZAdd("zset2", redis.Z{1, "one"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset2", redis.Z{2, "two"}) + c.Assert(zAdd.Err(), IsNil) + zAdd = t.client.ZAdd("zset2", redis.Z{3, "three"}) + c.Assert(zAdd.Err(), IsNil) + + zUnionStore := t.client.ZUnionStore( + "out", redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2") + c.Assert(zUnionStore.Err(), IsNil) + c.Assert(zUnionStore.Val(), Equals, int64(3)) + + val, err := t.client.ZRangeWithScores("out", 0, -1).Result() + c.Assert(err, IsNil) + c.Assert(val, DeepEquals, []redis.Z{{5, "one"}, {9, "three"}, {10, "two"}}) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestPatternPubSub(c *C) { + pubsub := t.client.PubSub() + defer func() { + c.Assert(pubsub.Close(), IsNil) + }() + + c.Assert(pubsub.PSubscribe("mychannel*"), IsNil) + + pub := t.client.Publish("mychannel1", "hello") + c.Assert(pub.Err(), IsNil) + c.Assert(pub.Val(), Equals, int64(1)) + + c.Assert(pubsub.PUnsubscribe("mychannel*"), IsNil) + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + subscr := msgi.(*redis.Subscription) + c.Assert(subscr.Kind, Equals, "psubscribe") + c.Assert(subscr.Channel, Equals, "mychannel*") + c.Assert(subscr.Count, Equals, 1) + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + subscr := msgi.(*redis.PMessage) + c.Assert(subscr.Channel, Equals, "mychannel1") + c.Assert(subscr.Pattern, Equals, "mychannel*") + c.Assert(subscr.Payload, Equals, "hello") + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + subscr := msgi.(*redis.Subscription) + c.Assert(subscr.Kind, Equals, "punsubscribe") + c.Assert(subscr.Channel, Equals, "mychannel*") + c.Assert(subscr.Count, Equals, 0) + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err.(net.Error).Timeout(), Equals, true) + c.Assert(msgi, IsNil) + } +} + +func (t *RedisTest) TestPubSub(c *C) { + pubsub := t.client.PubSub() + defer func() { + c.Assert(pubsub.Close(), IsNil) + }() + + c.Assert(pubsub.Subscribe("mychannel", "mychannel2"), IsNil) + + pub := t.client.Publish("mychannel", "hello") + c.Assert(pub.Err(), IsNil) + c.Assert(pub.Val(), Equals, int64(1)) + + pub = t.client.Publish("mychannel2", "hello2") + c.Assert(pub.Err(), IsNil) + c.Assert(pub.Val(), Equals, int64(1)) + + c.Assert(pubsub.Unsubscribe("mychannel", "mychannel2"), IsNil) + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + subscr := msgi.(*redis.Subscription) + c.Assert(subscr.Kind, Equals, "subscribe") + c.Assert(subscr.Channel, Equals, "mychannel") + c.Assert(subscr.Count, Equals, 1) + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + subscr := msgi.(*redis.Subscription) + c.Assert(subscr.Kind, Equals, "subscribe") + c.Assert(subscr.Channel, Equals, "mychannel2") + c.Assert(subscr.Count, Equals, 2) + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + subscr := msgi.(*redis.Message) + c.Assert(subscr.Channel, Equals, "mychannel") + c.Assert(subscr.Payload, Equals, "hello") + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + msg := msgi.(*redis.Message) + c.Assert(msg.Channel, Equals, "mychannel2") + c.Assert(msg.Payload, Equals, "hello2") + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + subscr := msgi.(*redis.Subscription) + c.Assert(subscr.Kind, Equals, "unsubscribe") + c.Assert(subscr.Channel, Equals, "mychannel") + c.Assert(subscr.Count, Equals, 1) + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err, IsNil) + subscr := msgi.(*redis.Subscription) + c.Assert(subscr.Kind, Equals, "unsubscribe") + c.Assert(subscr.Channel, Equals, "mychannel2") + c.Assert(subscr.Count, Equals, 0) + } + + { + msgi, err := pubsub.ReceiveTimeout(time.Second) + c.Assert(err.(net.Error).Timeout(), Equals, true) + c.Assert(msgi, IsNil) + } +} + +func (t *RedisTest) TestPubSubChannels(c *C) { + channels, err := t.client.PubSubChannels("mychannel*").Result() + c.Assert(err, IsNil) + c.Assert(channels, HasLen, 0) + c.Assert(channels, Not(IsNil)) + + pubsub := t.client.PubSub() + defer pubsub.Close() + + c.Assert(pubsub.Subscribe("mychannel", "mychannel2"), IsNil) + + channels, err = t.client.PubSubChannels("mychannel*").Result() + c.Assert(err, IsNil) + c.Assert(sortStrings(channels), DeepEquals, []string{"mychannel", "mychannel2"}) + + channels, err = t.client.PubSubChannels("").Result() + c.Assert(err, IsNil) + c.Assert(channels, HasLen, 0) + + channels, err = t.client.PubSubChannels("*").Result() + c.Assert(err, IsNil) + c.Assert(len(channels) >= 2, Equals, true) +} + +func (t *RedisTest) TestPubSubNumSub(c *C) { + pubsub := t.client.PubSub() + defer pubsub.Close() + + c.Assert(pubsub.Subscribe("mychannel", "mychannel2"), IsNil) + + channels, err := t.client.PubSubNumSub("mychannel", "mychannel2", "mychannel3").Result() + c.Assert(err, IsNil) + c.Assert( + channels, + DeepEquals, + []interface{}{"mychannel", int64(1), "mychannel2", int64(1), "mychannel3", int64(0)}, + ) +} + +func (t *RedisTest) TestPubSubNumPat(c *C) { + num, err := t.client.PubSubNumPat().Result() + c.Assert(err, IsNil) + c.Assert(num, Equals, int64(0)) + + pubsub := t.client.PubSub() + defer pubsub.Close() + + c.Assert(pubsub.PSubscribe("mychannel*"), IsNil) + + num, err = t.client.PubSubNumPat().Result() + c.Assert(err, IsNil) + c.Assert(num, Equals, int64(1)) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestPipeline(c *C) { + set := t.client.Set("key2", "hello2") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + pipeline := t.client.Pipeline() + defer func() { + c.Assert(pipeline.Close(), IsNil) + }() + + set = pipeline.Set("key1", "hello1") + get := pipeline.Get("key2") + incr := pipeline.Incr("key3") + getNil := pipeline.Get("key4") + + cmds, err := pipeline.Exec() + c.Assert(err, Equals, redis.Nil) + c.Assert(cmds, HasLen, 4) + + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello2") + + c.Assert(incr.Err(), IsNil) + c.Assert(incr.Val(), Equals, int64(1)) + + c.Assert(getNil.Err(), Equals, redis.Nil) + c.Assert(getNil.Val(), Equals, "") +} + +func (t *RedisTest) TestPipelineDiscardQueued(c *C) { + pipeline := t.client.Pipeline() + + pipeline.Get("key") + pipeline.Discard() + cmds, err := pipeline.Exec() + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 0) + + c.Assert(pipeline.Close(), IsNil) +} + +func (t *RedisTest) TestPipelined(c *C) { + var get *redis.StringCmd + cmds, err := t.client.Pipelined(func(pipe *redis.Pipeline) error { + get = pipe.Get("foo") + return nil + }) + c.Assert(err, Equals, redis.Nil) + c.Assert(cmds, HasLen, 1) + c.Assert(cmds[0], Equals, get) + c.Assert(get.Err(), Equals, redis.Nil) + c.Assert(get.Val(), Equals, "") +} + +func (t *RedisTest) TestPipelineErrValNotSet(c *C) { + pipeline := t.client.Pipeline() + defer func() { + c.Assert(pipeline.Close(), IsNil) + }() + + get := pipeline.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "") +} + +func (t *RedisTest) TestPipelineRunQueuedOnEmptyQueue(c *C) { + pipeline := t.client.Pipeline() + defer func() { + c.Assert(pipeline.Close(), IsNil) + }() + + cmds, err := pipeline.Exec() + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 0) +} + +// TODO: make thread safe? +func (t *RedisTest) TestPipelineIncr(c *C) { + const N = 20000 + key := "TestPipelineIncr" + + pipeline := t.client.Pipeline() + + wg := &sync.WaitGroup{} + wg.Add(N) + for i := 0; i < N; i++ { + pipeline.Incr(key) + wg.Done() + } + wg.Wait() + + cmds, err := pipeline.Exec() + c.Assert(err, IsNil) + c.Assert(len(cmds), Equals, 20000) + for _, cmd := range cmds { + if cmd.Err() != nil { + c.Errorf("got %v, expected nil", cmd.Err()) + } + } + + get := t.client.Get(key) + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, strconv.Itoa(N)) + + c.Assert(pipeline.Close(), IsNil) +} + +func (t *RedisTest) TestPipelineEcho(c *C) { + const N = 1000 + + wg := &sync.WaitGroup{} + wg.Add(N) + for i := 0; i < N; i++ { + go func(i int) { + pipeline := t.client.Pipeline() + + msg1 := "echo" + strconv.Itoa(i) + msg2 := "echo" + strconv.Itoa(i+1) + + echo1 := pipeline.Echo(msg1) + echo2 := pipeline.Echo(msg2) + + cmds, err := pipeline.Exec() + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 2) + + c.Assert(echo1.Err(), IsNil) + c.Assert(echo1.Val(), Equals, msg1) + + c.Assert(echo2.Err(), IsNil) + c.Assert(echo2.Val(), Equals, msg2) + + c.Assert(pipeline.Close(), IsNil) + + wg.Done() + }(i) + } + wg.Wait() +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestMultiExec(c *C) { + multi := t.client.Multi() + defer func() { + c.Assert(multi.Close(), IsNil) + }() + + var ( + set *redis.StatusCmd + get *redis.StringCmd + ) + cmds, err := multi.Exec(func() error { + set = multi.Set("key", "hello") + get = multi.Get("key") + return nil + }) + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 2) + + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello") +} + +func (t *RedisTest) TestMultiExecDiscard(c *C) { + multi := t.client.Multi() + defer func() { + c.Assert(multi.Close(), IsNil) + }() + + cmds, err := multi.Exec(func() error { + multi.Set("key1", "hello1") + multi.Discard() + multi.Set("key2", "hello2") + return nil + }) + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 1) + + get := t.client.Get("key1") + c.Assert(get.Err(), Equals, redis.Nil) + c.Assert(get.Val(), Equals, "") + + get = t.client.Get("key2") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "hello2") +} + +func (t *RedisTest) TestMultiExecEmpty(c *C) { + multi := t.client.Multi() + defer func() { + c.Assert(multi.Close(), IsNil) + }() + + cmds, err := multi.Exec(func() error { return nil }) + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 0) + + ping := multi.Ping() + c.Check(ping.Err(), IsNil) + c.Check(ping.Val(), Equals, "PONG") +} + +func (t *RedisTest) TestMultiExecOnEmptyQueue(c *C) { + multi := t.client.Multi() + defer func() { + c.Assert(multi.Close(), IsNil) + }() + + cmds, err := multi.Exec(func() error { return nil }) + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 0) +} + +func (t *RedisTest) TestMultiExecIncr(c *C) { + multi := t.client.Multi() + defer func() { + c.Assert(multi.Close(), IsNil) + }() + + cmds, err := multi.Exec(func() error { + for i := int64(0); i < 20000; i++ { + multi.Incr("key") + } + return nil + }) + c.Assert(err, IsNil) + c.Assert(len(cmds), Equals, 20000) + for _, cmd := range cmds { + if cmd.Err() != nil { + c.Errorf("got %v, expected nil", cmd.Err()) + } + } + + get := t.client.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Equals, "20000") +} + +func (t *RedisTest) transactionalIncr(c *C) ([]redis.Cmder, error) { + multi := t.client.Multi() + defer func() { + c.Assert(multi.Close(), IsNil) + }() + + watch := multi.Watch("key") + c.Assert(watch.Err(), IsNil) + c.Assert(watch.Val(), Equals, "OK") + + get := multi.Get("key") + c.Assert(get.Err(), IsNil) + c.Assert(get.Val(), Not(Equals), redis.Nil) + + v, err := strconv.ParseInt(get.Val(), 10, 64) + c.Assert(err, IsNil) + + return multi.Exec(func() error { + multi.Set("key", strconv.FormatInt(v+1, 10)) + return nil + }) +} + +func (t *RedisTest) TestWatchUnwatch(c *C) { + var n = 10000 + if testing.Short() { + n = 1000 + } + + set := t.client.Set("key", "0") + c.Assert(set.Err(), IsNil) + + wg := &sync.WaitGroup{} + for i := 0; i < n; i++ { + wg.Add(1) + go func() { + defer wg.Done() + for { + cmds, err := t.transactionalIncr(c) + if err == redis.TxFailedErr { + continue + } + c.Assert(err, IsNil) + c.Assert(cmds, HasLen, 1) + c.Assert(cmds[0].Err(), IsNil) + break + } + }() + } + wg.Wait() + + val, err := t.client.Get("key").Int64() + c.Assert(err, IsNil) + c.Assert(val, Equals, int64(n)) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestRaceEcho(c *C) { + var n = 10000 + if testing.Short() { + n = 1000 + } + + wg := &sync.WaitGroup{} + wg.Add(n) + for i := 0; i < n; i++ { + go func(i int) { + msg := "echo" + strconv.Itoa(i) + echo := t.client.Echo(msg) + c.Assert(echo.Err(), IsNil) + c.Assert(echo.Val(), Equals, msg) + wg.Done() + }(i) + } + wg.Wait() +} + +func (t *RedisTest) TestRaceIncr(c *C) { + var n = 10000 + if testing.Short() { + n = 1000 + } + + wg := &sync.WaitGroup{} + wg.Add(n) + for i := 0; i < n; i++ { + go func() { + incr := t.client.Incr("TestRaceIncr") + if err := incr.Err(); err != nil { + panic(err) + } + wg.Done() + }() + } + wg.Wait() + + val, err := t.client.Get("TestRaceIncr").Result() + c.Assert(err, IsNil) + c.Assert(val, Equals, strconv.Itoa(n)) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestCmdBgRewriteAOF(c *C) { + r := t.client.BgRewriteAOF() + c.Assert(r.Err(), IsNil) + c.Assert(r.Val(), Equals, "Background append only file rewriting started") +} + +func (t *RedisTest) TestCmdBgSave(c *C) { + // workaround for "ERR Can't BGSAVE while AOF log rewriting is in progress" + time.Sleep(time.Second) + + r := t.client.BgSave() + c.Assert(r.Err(), IsNil) + c.Assert(r.Val(), Equals, "Background saving started") +} + +func (t *RedisTest) TestCmdClientKill(c *C) { + r := t.client.ClientKill("1.1.1.1:1111") + c.Assert(r.Err(), ErrorMatches, "ERR No such client") + c.Assert(r.Val(), Equals, "") +} + +func (t *RedisTest) TestCmdConfigGet(c *C) { + r := t.client.ConfigGet("*") + c.Assert(r.Err(), IsNil) + c.Assert(len(r.Val()) > 0, Equals, true) +} + +func (t *RedisTest) TestCmdConfigResetStat(c *C) { + r := t.client.ConfigResetStat() + c.Assert(r.Err(), IsNil) + c.Assert(r.Val(), Equals, "OK") +} + +func (t *RedisTest) TestCmdConfigSet(c *C) { + configGet := t.client.ConfigGet("maxmemory") + c.Assert(configGet.Err(), IsNil) + c.Assert(configGet.Val(), HasLen, 2) + c.Assert(configGet.Val()[0], Equals, "maxmemory") + + configSet := t.client.ConfigSet("maxmemory", configGet.Val()[1].(string)) + c.Assert(configSet.Err(), IsNil) + c.Assert(configSet.Val(), Equals, "OK") +} + +func (t *RedisTest) TestCmdDbSize(c *C) { + dbSize := t.client.DbSize() + c.Assert(dbSize.Err(), IsNil) + c.Assert(dbSize.Val(), Equals, int64(0)) +} + +func (t *RedisTest) TestCmdFlushAll(c *C) { + // TODO +} + +func (t *RedisTest) TestCmdFlushDb(c *C) { + // TODO +} + +func (t *RedisTest) TestCmdInfo(c *C) { + info := t.client.Info() + c.Assert(info.Err(), IsNil) + c.Assert(info.Val(), Not(Equals), "") +} + +func (t *RedisTest) TestCmdLastSave(c *C) { + lastSave := t.client.LastSave() + c.Assert(lastSave.Err(), IsNil) + c.Assert(lastSave.Val(), Not(Equals), 0) +} + +func (t *RedisTest) TestCmdSave(c *C) { + save := t.client.Save() + c.Assert(save.Err(), IsNil) + c.Assert(save.Val(), Equals, "OK") +} + +func (t *RedisTest) TestSlaveOf(c *C) { + slaveOf := t.client.SlaveOf("localhost", "8888") + c.Assert(slaveOf.Err(), IsNil) + c.Assert(slaveOf.Val(), Equals, "OK") + + slaveOf = t.client.SlaveOf("NO", "ONE") + c.Assert(slaveOf.Err(), IsNil) + c.Assert(slaveOf.Val(), Equals, "OK") +} + +func (t *RedisTest) TestTime(c *C) { + time := t.client.Time() + c.Assert(time.Err(), IsNil) + c.Assert(time.Val(), HasLen, 2) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestScriptingEval(c *C) { + eval := t.client.Eval( + "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", + []string{"key1", "key2"}, + []string{"first", "second"}, + ) + c.Assert(eval.Err(), IsNil) + c.Assert(eval.Val(), DeepEquals, []interface{}{"key1", "key2", "first", "second"}) + + eval = t.client.Eval( + "return redis.call('set',KEYS[1],'bar')", + []string{"foo"}, + []string{}, + ) + c.Assert(eval.Err(), IsNil) + c.Assert(eval.Val(), Equals, "OK") + + eval = t.client.Eval("return 10", []string{}, []string{}) + c.Assert(eval.Err(), IsNil) + c.Assert(eval.Val(), Equals, int64(10)) + + eval = t.client.Eval("return {1,2,{3,'Hello World!'}}", []string{}, []string{}) + c.Assert(eval.Err(), IsNil) + // DeepEquals can't compare nested slices. + c.Assert( + fmt.Sprintf("%#v", eval.Val()), + Equals, + `[]interface {}{1, 2, []interface {}{3, "Hello World!"}}`, + ) +} + +func (t *RedisTest) TestScriptingEvalSha(c *C) { + set := t.client.Set("foo", "bar") + c.Assert(set.Err(), IsNil) + c.Assert(set.Val(), Equals, "OK") + + eval := t.client.Eval("return redis.call('get','foo')", nil, nil) + c.Assert(eval.Err(), IsNil) + c.Assert(eval.Val(), Equals, "bar") + + evalSha := t.client.EvalSha("6b1bf486c81ceb7edf3c093f4c48582e38c0e791", nil, nil) + c.Assert(evalSha.Err(), IsNil) + c.Assert(evalSha.Val(), Equals, "bar") + + evalSha = t.client.EvalSha("ffffffffffffffffffffffffffffffffffffffff", nil, nil) + c.Assert(evalSha.Err(), ErrorMatches, "NOSCRIPT No matching script. Please use EVAL.") + c.Assert(evalSha.Val(), Equals, nil) +} + +func (t *RedisTest) TestScriptingScriptExists(c *C) { + scriptLoad := t.client.ScriptLoad("return 1") + c.Assert(scriptLoad.Err(), IsNil) + c.Assert(scriptLoad.Val(), Equals, "e0e1f9fabfc9d4800c877a703b823ac0578ff8db") + + scriptExists := t.client.ScriptExists( + "e0e1f9fabfc9d4800c877a703b823ac0578ff8db", + "ffffffffffffffffffffffffffffffffffffffff", + ) + c.Assert(scriptExists.Err(), IsNil) + c.Assert(scriptExists.Val(), DeepEquals, []bool{true, false}) +} + +func (t *RedisTest) TestScriptingScriptFlush(c *C) { + scriptFlush := t.client.ScriptFlush() + c.Assert(scriptFlush.Err(), IsNil) + c.Assert(scriptFlush.Val(), Equals, "OK") +} + +func (t *RedisTest) TestScriptingScriptKill(c *C) { + scriptKill := t.client.ScriptKill() + c.Assert(scriptKill.Err(), ErrorMatches, ".*No scripts in execution right now.") + c.Assert(scriptKill.Val(), Equals, "") +} + +func (t *RedisTest) TestScriptingScriptLoad(c *C) { + scriptLoad := t.client.ScriptLoad("return redis.call('get','foo')") + c.Assert(scriptLoad.Err(), IsNil) + c.Assert(scriptLoad.Val(), Equals, "6b1bf486c81ceb7edf3c093f4c48582e38c0e791") +} + +func (t *RedisTest) TestScriptingNewScript(c *C) { + s := redis.NewScript("return 1") + run := s.Run(t.client, nil, nil) + c.Assert(run.Err(), IsNil) + c.Assert(run.Val(), Equals, int64(1)) +} + +func (t *RedisTest) TestScriptingEvalAndPipeline(c *C) { + pipeline := t.client.Pipeline() + s := redis.NewScript("return 1") + run := s.Eval(pipeline, nil, nil) + _, err := pipeline.Exec() + c.Assert(err, IsNil) + c.Assert(run.Err(), IsNil) + c.Assert(run.Val(), Equals, int64(1)) +} + +func (t *RedisTest) TestScriptingEvalShaAndPipeline(c *C) { + s := redis.NewScript("return 1") + c.Assert(s.Load(t.client).Err(), IsNil) + + pipeline := t.client.Pipeline() + run := s.Eval(pipeline, nil, nil) + _, err := pipeline.Exec() + c.Assert(err, IsNil) + c.Assert(run.Err(), IsNil) + c.Assert(run.Val(), Equals, int64(1)) +} + +//------------------------------------------------------------------------------ + +func (t *RedisTest) TestCmdDebugObject(c *C) { + { + debug := t.client.DebugObject("foo") + c.Assert(debug.Err(), Not(IsNil)) + c.Assert(debug.Err().Error(), Equals, "ERR no such key") + } + + { + t.client.Set("foo", "bar") + debug := t.client.DebugObject("foo") + c.Assert(debug.Err(), IsNil) + c.Assert(debug.Val(), FitsTypeOf, "") + c.Assert(debug.Val(), Not(Equals), "") + } +} + +//------------------------------------------------------------------------------ + +func BenchmarkRedisPing(b *testing.B) { + b.StopTimer() + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + b.StartTimer() + + for i := 0; i < b.N; i++ { + if err := client.Ping().Err(); err != nil { + panic(err) + } + } +} + +func BenchmarkRedisSet(b *testing.B) { + b.StopTimer() + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + b.StartTimer() + + for i := 0; i < b.N; i++ { + if err := client.Set("key", "hello").Err(); err != nil { + panic(err) + } + } +} + +func BenchmarkRedisGetNil(b *testing.B) { + b.StopTimer() + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + if err := client.FlushDb().Err(); err != nil { + b.Fatal(err) + } + b.StartTimer() + + for i := 0; i < b.N; i++ { + if err := client.Get("key").Err(); err != redis.Nil { + b.Fatal(err) + } + } +} + +func BenchmarkRedisGet(b *testing.B) { + b.StopTimer() + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + if err := client.Set("key", "hello").Err(); err != nil { + b.Fatal(err) + } + b.StartTimer() + + for i := 0; i < b.N; i++ { + if err := client.Get("key").Err(); err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkRedisMGet(b *testing.B) { + b.StopTimer() + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + if err := client.MSet("key1", "hello1", "key2", "hello2").Err(); err != nil { + b.Fatal(err) + } + b.StartTimer() + + for i := 0; i < b.N; i++ { + if err := client.MGet("key1", "key2").Err(); err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkSetExpire(b *testing.B) { + b.StopTimer() + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + b.StartTimer() + + for i := 0; i < b.N; i++ { + if err := client.Set("key", "hello").Err(); err != nil { + b.Fatal(err) + } + if err := client.Expire("key", time.Second).Err(); err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkPipeline(b *testing.B) { + b.StopTimer() + client := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + }) + b.StartTimer() + + for i := 0; i < b.N; i++ { + _, err := client.Pipelined(func(pipe *redis.Pipeline) error { + pipe.Set("key", "hello") + pipe.Expire("key", time.Second) + return nil + }) + if err != nil { + b.Fatal(err) + } + } +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/script.go b/Godeps/_workspace/src/gopkg.in/redis.v2/script.go new file mode 100644 index 0000000..96c35f5 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/script.go @@ -0,0 +1,52 @@ +package redis + +import ( + "crypto/sha1" + "encoding/hex" + "io" + "strings" +) + +type scripter interface { + Eval(script string, keys []string, args []string) *Cmd + EvalSha(sha1 string, keys []string, args []string) *Cmd + ScriptExists(scripts ...string) *BoolSliceCmd + ScriptLoad(script string) *StringCmd +} + +type Script struct { + src, hash string +} + +func NewScript(src string) *Script { + h := sha1.New() + io.WriteString(h, src) + return &Script{ + src: src, + hash: hex.EncodeToString(h.Sum(nil)), + } +} + +func (s *Script) Load(c scripter) *StringCmd { + return c.ScriptLoad(s.src) +} + +func (s *Script) Exists(c scripter) *BoolSliceCmd { + return c.ScriptExists(s.src) +} + +func (s *Script) Eval(c scripter, keys []string, args []string) *Cmd { + return c.Eval(s.src, keys, args) +} + +func (s *Script) EvalSha(c scripter, keys []string, args []string) *Cmd { + return c.EvalSha(s.hash, keys, args) +} + +func (s *Script) Run(c *Client, keys []string, args []string) *Cmd { + r := s.EvalSha(c, keys, args) + if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") { + return s.Eval(c, keys, args) + } + return r +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/sentinel.go b/Godeps/_workspace/src/gopkg.in/redis.v2/sentinel.go new file mode 100644 index 0000000..d3ffeca --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/sentinel.go @@ -0,0 +1,291 @@ +package redis + +import ( + "errors" + "log" + "net" + "strings" + "sync" + "time" +) + +//------------------------------------------------------------------------------ + +type FailoverOptions struct { + MasterName string + SentinelAddrs []string + + Password string + DB int64 + + PoolSize int + + DialTimeout time.Duration + ReadTimeout time.Duration + WriteTimeout time.Duration + IdleTimeout time.Duration +} + +func (opt *FailoverOptions) getPoolSize() int { + if opt.PoolSize == 0 { + return 10 + } + return opt.PoolSize +} + +func (opt *FailoverOptions) getDialTimeout() time.Duration { + if opt.DialTimeout == 0 { + return 5 * time.Second + } + return opt.DialTimeout +} + +func (opt *FailoverOptions) options() *options { + return &options{ + DB: opt.DB, + Password: opt.Password, + + DialTimeout: opt.getDialTimeout(), + ReadTimeout: opt.ReadTimeout, + WriteTimeout: opt.WriteTimeout, + + PoolSize: opt.getPoolSize(), + IdleTimeout: opt.IdleTimeout, + } +} + +func NewFailoverClient(failoverOpt *FailoverOptions) *Client { + opt := failoverOpt.options() + failover := &sentinelFailover{ + masterName: failoverOpt.MasterName, + sentinelAddrs: failoverOpt.SentinelAddrs, + + opt: opt, + } + return &Client{ + baseClient: &baseClient{ + opt: opt, + connPool: failover.Pool(), + }, + } +} + +//------------------------------------------------------------------------------ + +type sentinelClient struct { + *baseClient +} + +func newSentinel(clOpt *Options) *sentinelClient { + opt := clOpt.options() + opt.Password = "" + opt.DB = 0 + dialer := func() (net.Conn, error) { + return net.DialTimeout("tcp", clOpt.Addr, opt.DialTimeout) + } + return &sentinelClient{ + baseClient: &baseClient{ + opt: opt, + connPool: newConnPool(newConnFunc(dialer), opt), + }, + } +} + +func (c *sentinelClient) PubSub() *PubSub { + return &PubSub{ + baseClient: &baseClient{ + opt: c.opt, + connPool: newSingleConnPool(c.connPool, false), + }, + } +} + +func (c *sentinelClient) GetMasterAddrByName(name string) *StringSliceCmd { + cmd := NewStringSliceCmd("SENTINEL", "get-master-addr-by-name", name) + c.Process(cmd) + return cmd +} + +func (c *sentinelClient) Sentinels(name string) *SliceCmd { + cmd := NewSliceCmd("SENTINEL", "sentinels", name) + c.Process(cmd) + return cmd +} + +type sentinelFailover struct { + masterName string + sentinelAddrs []string + + opt *options + + pool pool + poolOnce sync.Once + + lock sync.RWMutex + _sentinel *sentinelClient +} + +func (d *sentinelFailover) dial() (net.Conn, error) { + addr, err := d.MasterAddr() + if err != nil { + return nil, err + } + return net.DialTimeout("tcp", addr, d.opt.DialTimeout) +} + +func (d *sentinelFailover) Pool() pool { + d.poolOnce.Do(func() { + d.pool = newConnPool(newConnFunc(d.dial), d.opt) + }) + return d.pool +} + +func (d *sentinelFailover) MasterAddr() (string, error) { + defer d.lock.Unlock() + d.lock.Lock() + + // Try last working sentinel. + if d._sentinel != nil { + addr, err := d._sentinel.GetMasterAddrByName(d.masterName).Result() + if err != nil { + log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) + d.resetSentinel() + } else { + addr := net.JoinHostPort(addr[0], addr[1]) + log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr) + return addr, nil + } + } + + for i, sentinelAddr := range d.sentinelAddrs { + sentinel := newSentinel(&Options{ + Addr: sentinelAddr, + + DB: d.opt.DB, + Password: d.opt.Password, + + DialTimeout: d.opt.DialTimeout, + ReadTimeout: d.opt.ReadTimeout, + WriteTimeout: d.opt.WriteTimeout, + + PoolSize: d.opt.PoolSize, + IdleTimeout: d.opt.IdleTimeout, + }) + masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result() + if err != nil { + log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) + sentinel.Close() + continue + } + + // Push working sentinel to the top. + d.sentinelAddrs[0], d.sentinelAddrs[i] = d.sentinelAddrs[i], d.sentinelAddrs[0] + + d.setSentinel(sentinel) + addr := net.JoinHostPort(masterAddr[0], masterAddr[1]) + log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr) + return addr, nil + } + + return "", errors.New("redis: all sentinels are unreachable") +} + +func (d *sentinelFailover) setSentinel(sentinel *sentinelClient) { + d.discoverSentinels(sentinel) + d._sentinel = sentinel + go d.listen() +} + +func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) { + sentinels, err := sentinel.Sentinels(d.masterName).Result() + if err != nil { + log.Printf("redis-sentinel: Sentinels %q failed: %s", d.masterName, err) + return + } + for _, sentinel := range sentinels { + vals := sentinel.([]interface{}) + for i := 0; i < len(vals); i += 2 { + key := vals[i].(string) + if key == "name" { + sentinelAddr := vals[i+1].(string) + if !contains(d.sentinelAddrs, sentinelAddr) { + log.Printf( + "redis-sentinel: discovered new %q sentinel: %s", + d.masterName, sentinelAddr, + ) + d.sentinelAddrs = append(d.sentinelAddrs, sentinelAddr) + } + } + } + } +} + +func (d *sentinelFailover) listen() { + var pubsub *PubSub + for { + if pubsub == nil { + pubsub = d._sentinel.PubSub() + if err := pubsub.Subscribe("+switch-master"); err != nil { + log.Printf("redis-sentinel: Subscribe failed: %s", err) + d.lock.Lock() + d.resetSentinel() + d.lock.Unlock() + return + } + } + + msgIface, err := pubsub.Receive() + if err != nil { + log.Printf("redis-sentinel: Receive failed: %s", err) + pubsub.Close() + return + } + + switch msg := msgIface.(type) { + case *Message: + switch msg.Channel { + case "+switch-master": + parts := strings.Split(msg.Payload, " ") + if parts[0] != d.masterName { + log.Printf("redis-sentinel: ignore new %s addr", parts[0]) + continue + } + addr := net.JoinHostPort(parts[3], parts[4]) + log.Printf( + "redis-sentinel: new %q addr is %s", + d.masterName, addr, + ) + d.pool.Filter(func(cn *conn) bool { + if cn.RemoteAddr().String() != addr { + log.Printf( + "redis-sentinel: closing connection to old master %s", + cn.RemoteAddr(), + ) + return false + } + return true + }) + default: + log.Printf("redis-sentinel: unsupported message: %s", msg) + } + case *Subscription: + // Ignore. + default: + log.Printf("redis-sentinel: unsupported message: %s", msgIface) + } + } +} + +func (d *sentinelFailover) resetSentinel() { + d._sentinel.Close() + d._sentinel = nil +} + +func contains(slice []string, str string) bool { + for _, s := range slice { + if s == str { + return true + } + } + return false +} diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/sentinel_test.go b/Godeps/_workspace/src/gopkg.in/redis.v2/sentinel_test.go new file mode 100644 index 0000000..ede59bd --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/sentinel_test.go @@ -0,0 +1,185 @@ +package redis_test + +import ( + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "testing" + "text/template" + "time" + + "gopkg.in/redis.v2" +) + +func startRedis(port string) (*exec.Cmd, error) { + cmd := exec.Command("redis-server", "--port", port) + if false { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + } + if err := cmd.Start(); err != nil { + return nil, err + } + return cmd, nil +} + +func startRedisSlave(port, slave string) (*exec.Cmd, error) { + cmd := exec.Command("redis-server", "--port", port, "--slaveof", "127.0.0.1", slave) + if false { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + } + if err := cmd.Start(); err != nil { + return nil, err + } + return cmd, nil +} + +func startRedisSentinel(port, masterName, masterPort string) (*exec.Cmd, error) { + dir, err := ioutil.TempDir("", "sentinel") + if err != nil { + return nil, err + } + + sentinelConfFilepath := filepath.Join(dir, "sentinel.conf") + tpl, err := template.New("sentinel.conf").Parse(sentinelConf) + if err != nil { + return nil, err + } + + data := struct { + Port string + MasterName string + MasterPort string + }{ + Port: port, + MasterName: masterName, + MasterPort: masterPort, + } + if err := writeTemplateToFile(sentinelConfFilepath, tpl, data); err != nil { + return nil, err + } + + cmd := exec.Command("redis-server", sentinelConfFilepath, "--sentinel") + if true { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + } + if err := cmd.Start(); err != nil { + return nil, err + } + + return cmd, nil +} + +func writeTemplateToFile(path string, t *template.Template, data interface{}) error { + f, err := os.Create(path) + if err != nil { + return err + } + defer f.Close() + return t.Execute(f, data) +} + +func TestSentinel(t *testing.T) { + masterName := "mymaster" + masterPort := "8123" + slavePort := "8124" + sentinelPort := "8125" + + masterCmd, err := startRedis(masterPort) + if err != nil { + t.Fatal(err) + } + defer masterCmd.Process.Kill() + + // Wait for master to start. + time.Sleep(200 * time.Millisecond) + + master := redis.NewTCPClient(&redis.Options{ + Addr: ":" + masterPort, + }) + if err := master.Ping().Err(); err != nil { + t.Fatal(err) + } + + slaveCmd, err := startRedisSlave(slavePort, masterPort) + if err != nil { + t.Fatal(err) + } + defer slaveCmd.Process.Kill() + + // Wait for slave to start. + time.Sleep(200 * time.Millisecond) + + slave := redis.NewTCPClient(&redis.Options{ + Addr: ":" + slavePort, + }) + if err := slave.Ping().Err(); err != nil { + t.Fatal(err) + } + + sentinelCmd, err := startRedisSentinel(sentinelPort, masterName, masterPort) + if err != nil { + t.Fatal(err) + } + defer sentinelCmd.Process.Kill() + + // Wait for sentinel to start. + time.Sleep(200 * time.Millisecond) + + sentinel := redis.NewTCPClient(&redis.Options{ + Addr: ":" + sentinelPort, + }) + if err := sentinel.Ping().Err(); err != nil { + t.Fatal(err) + } + defer sentinel.Shutdown() + + client := redis.NewFailoverClient(&redis.FailoverOptions{ + MasterName: masterName, + SentinelAddrs: []string{":" + sentinelPort}, + }) + + if err := client.Set("foo", "master").Err(); err != nil { + t.Fatal(err) + } + + val, err := master.Get("foo").Result() + if err != nil { + t.Fatal(err) + } + if val != "master" { + t.Fatalf(`got %q, expected "master"`, val) + } + + // Kill Redis master. + if err := masterCmd.Process.Kill(); err != nil { + t.Fatal(err) + } + if err := master.Ping().Err(); err == nil { + t.Fatalf("master was not killed") + } + + // Wait for Redis sentinel to elect new master. + time.Sleep(5 * time.Second) + + // Check that client picked up new master. + val, err = client.Get("foo").Result() + if err != nil { + t.Fatal(err) + } + if val != "master" { + t.Fatalf(`got %q, expected "master"`, val) + } +} + +var sentinelConf = ` +port {{ .Port }} + +sentinel monitor {{ .MasterName }} 127.0.0.1 {{ .MasterPort }} 1 +sentinel down-after-milliseconds {{ .MasterName }} 1000 +sentinel failover-timeout {{ .MasterName }} 2000 +sentinel parallel-syncs {{ .MasterName }} 1 +` diff --git a/Godeps/_workspace/src/gopkg.in/redis.v2/testdata/sentinel.conf b/Godeps/_workspace/src/gopkg.in/redis.v2/testdata/sentinel.conf new file mode 100644 index 0000000..3da90b3 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/redis.v2/testdata/sentinel.conf @@ -0,0 +1,6 @@ +port 26379 + +sentinel monitor master 127.0.0.1 6379 1 +sentinel down-after-milliseconds master 2000 +sentinel failover-timeout master 5000 +sentinel parallel-syncs master 4