add vendor
This commit is contained in:
parent
a45b1f3603
commit
4fd4c7018e
@ -22,8 +22,18 @@ func GetAllAccount() ([]Account, error) {
|
|||||||
return accs, err
|
return accs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get - get account info
|
// GetAccount -
|
||||||
func (c *Account) Get() error {
|
func GetAccount(account string) (acc *Account, err error) {
|
||||||
_, err := x.Table("account").Get(c)
|
acc = &Account{
|
||||||
return err
|
Account: account,
|
||||||
|
}
|
||||||
|
ok, err := x.Table("account").Get(acc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
@ -8,28 +8,49 @@ import (
|
|||||||
// Context custom patch context
|
// Context custom patch context
|
||||||
type Context struct {
|
type Context struct {
|
||||||
*gin.Context
|
*gin.Context
|
||||||
|
C map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CustomMiddle func
|
||||||
type CustomMiddle func(*Context)
|
type CustomMiddle func(*Context)
|
||||||
|
|
||||||
|
// PatchContext func
|
||||||
func PatchContext(handler CustomMiddle) gin.HandlerFunc {
|
func PatchContext(handler CustomMiddle) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
ctx := &Context{
|
ctx := &Context{
|
||||||
Context: c,
|
Context: c,
|
||||||
}
|
}
|
||||||
|
ctx.C = make(map[string]interface{})
|
||||||
handler(ctx)
|
handler(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFound response
|
// NotFound response
|
||||||
func (c *Context) NotFound(msg string) {
|
func (c *Context) NotFound(msg interface{}) {
|
||||||
obj := apimsg.GetRes("NotFound", msg)
|
obj := apimsg.GetRes("NotFound", msg)
|
||||||
// c.JSON(obj.Status, obj.Obj)
|
c.AbortWithStatusJSON(obj.Status, obj.Obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DataFormat error response
|
||||||
|
func (c *Context) DataFormat(msg interface{}) {
|
||||||
|
obj := apimsg.GetRes("DataFormat", msg)
|
||||||
c.AbortWithStatusJSON(obj.Status, obj.Obj)
|
c.AbortWithStatusJSON(obj.Status, obj.Obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Success response
|
// Success response
|
||||||
func (c *Context) Success(msg interface{}) {
|
func (c *Context) Success(msg interface{}) {
|
||||||
obj := apimsg.GetRes("Success", msg)
|
obj := apimsg.GetRes("Success", msg)
|
||||||
c.JSON(obj.Status, obj.Obj)
|
c.AbortWithStatusJSON(obj.Status, obj.Obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServerError response
|
||||||
|
func (c *Context) ServerError(msg interface{}) {
|
||||||
|
obj := apimsg.GetRes("InternalError", msg)
|
||||||
|
c.AbortWithStatusJSON(obj.Status, obj.Obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CustomRes send othen response
|
||||||
|
func (c *Context) CustomRes(name string, msg interface{}) {
|
||||||
|
obj := apimsg.GetRes(name, msg)
|
||||||
|
c.AbortWithStatusJSON(obj.Status, obj.Obj)
|
||||||
}
|
}
|
||||||
|
17
modules/utils/utils.go
Normal file
17
modules/utils/utils.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
// ToMap struct to map[string]interface{}
|
||||||
|
func ToMap(ss interface{}) map[string]interface{} {
|
||||||
|
t := reflect.ValueOf(ss).Elem()
|
||||||
|
|
||||||
|
smap := make(map[string]interface{})
|
||||||
|
|
||||||
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
f := t.Field(i)
|
||||||
|
smap[t.Type().Field(i).Name] = f.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
return smap
|
||||||
|
}
|
@ -1,7 +1,11 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"git.trj.tw/golang/go-gallery/models"
|
||||||
"git.trj.tw/golang/go-gallery/modules/context"
|
"git.trj.tw/golang/go-gallery/modules/context"
|
||||||
|
"git.trj.tw/golang/go-gallery/modules/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserLogin route
|
// UserLogin route
|
||||||
@ -15,9 +19,21 @@ func UserLogin(c *context.Context) {
|
|||||||
}
|
}
|
||||||
err := c.ShouldBind(&loginArg)
|
err := c.ShouldBind(&loginArg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.NotFound("body not found")
|
c.DataFormat(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Success("api success")
|
acc, err := models.GetAccount(loginArg.Account)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
c.ServerError(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if acc == nil {
|
||||||
|
c.NotFound("User not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Success(utils.ToMap(acc))
|
||||||
}
|
}
|
||||||
|
21
vendor/github.com/gin-contrib/sse/LICENSE
generated
vendored
Normal file
21
vendor/github.com/gin-contrib/sse/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Manuel Martínez-Almeida
|
||||||
|
|
||||||
|
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.
|
58
vendor/github.com/gin-contrib/sse/README.md
generated
vendored
Normal file
58
vendor/github.com/gin-contrib/sse/README.md
generated
vendored
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
# Server-Sent Events
|
||||||
|
|
||||||
|
[![GoDoc](https://godoc.org/github.com/gin-contrib/sse?status.svg)](https://godoc.org/github.com/gin-contrib/sse)
|
||||||
|
[![Build Status](https://travis-ci.org/gin-contrib/sse.svg)](https://travis-ci.org/gin-contrib/sse)
|
||||||
|
[![codecov](https://codecov.io/gh/gin-contrib/sse/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/sse)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/sse)](https://goreportcard.com/report/github.com/gin-contrib/sse)
|
||||||
|
|
||||||
|
Server-sent events (SSE) is a technology where a browser receives automatic updates from a server via HTTP connection. The Server-Sent Events EventSource API is [standardized as part of HTML5[1] by the W3C](http://www.w3.org/TR/2009/WD-eventsource-20091029/).
|
||||||
|
|
||||||
|
- [Read this great SSE introduction by the HTML5Rocks guys](http://www.html5rocks.com/en/tutorials/eventsource/basics/)
|
||||||
|
- [Browser support](http://caniuse.com/#feat=eventsource)
|
||||||
|
|
||||||
|
## Sample code
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/gin-contrib/sse"
|
||||||
|
|
||||||
|
func httpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
// data can be a primitive like a string, an integer or a float
|
||||||
|
sse.Encode(w, sse.Event{
|
||||||
|
Event: "message",
|
||||||
|
Data: "some data\nmore data",
|
||||||
|
})
|
||||||
|
|
||||||
|
// also a complex type, like a map, a struct or a slice
|
||||||
|
sse.Encode(w, sse.Event{
|
||||||
|
Id: "124",
|
||||||
|
Event: "message",
|
||||||
|
Data: map[string]interface{}{
|
||||||
|
"user": "manu",
|
||||||
|
"date": time.Now().Unix(),
|
||||||
|
"content": "hi!",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```
|
||||||
|
event: message
|
||||||
|
data: some data\\nmore data
|
||||||
|
|
||||||
|
id: 124
|
||||||
|
event: message
|
||||||
|
data: {"content":"hi!","date":1431540810,"user":"manu"}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Content-Type
|
||||||
|
|
||||||
|
```go
|
||||||
|
fmt.Println(sse.ContentType)
|
||||||
|
```
|
||||||
|
```
|
||||||
|
text/event-stream
|
||||||
|
```
|
||||||
|
|
||||||
|
## Decoding support
|
||||||
|
|
||||||
|
There is a client-side implementation of SSE coming soon.
|
116
vendor/github.com/gin-contrib/sse/sse-decoder.go
generated
vendored
Normal file
116
vendor/github.com/gin-contrib/sse/sse-decoder.go
generated
vendored
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package sse
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
type decoder struct {
|
||||||
|
events []Event
|
||||||
|
}
|
||||||
|
|
||||||
|
func Decode(r io.Reader) ([]Event, error) {
|
||||||
|
var dec decoder
|
||||||
|
return dec.decode(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *decoder) dispatchEvent(event Event, data string) {
|
||||||
|
dataLength := len(data)
|
||||||
|
if dataLength > 0 {
|
||||||
|
//If the data buffer's last character is a U+000A LINE FEED (LF) character, then remove the last character from the data buffer.
|
||||||
|
data = data[:dataLength-1]
|
||||||
|
dataLength--
|
||||||
|
}
|
||||||
|
if dataLength == 0 && event.Event == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if event.Event == "" {
|
||||||
|
event.Event = "message"
|
||||||
|
}
|
||||||
|
event.Data = data
|
||||||
|
d.events = append(d.events, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *decoder) decode(r io.Reader) ([]Event, error) {
|
||||||
|
buf, err := ioutil.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentEvent Event
|
||||||
|
var dataBuffer *bytes.Buffer = new(bytes.Buffer)
|
||||||
|
// TODO (and unit tests)
|
||||||
|
// Lines must be separated by either a U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pair,
|
||||||
|
// a single U+000A LINE FEED (LF) character,
|
||||||
|
// or a single U+000D CARRIAGE RETURN (CR) character.
|
||||||
|
lines := bytes.Split(buf, []byte{'\n'})
|
||||||
|
for _, line := range lines {
|
||||||
|
if len(line) == 0 {
|
||||||
|
// If the line is empty (a blank line). Dispatch the event.
|
||||||
|
d.dispatchEvent(currentEvent, dataBuffer.String())
|
||||||
|
|
||||||
|
// reset current event and data buffer
|
||||||
|
currentEvent = Event{}
|
||||||
|
dataBuffer.Reset()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if line[0] == byte(':') {
|
||||||
|
// If the line starts with a U+003A COLON character (:), ignore the line.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var field, value []byte
|
||||||
|
colonIndex := bytes.IndexRune(line, ':')
|
||||||
|
if colonIndex != -1 {
|
||||||
|
// If the line contains a U+003A COLON character character (:)
|
||||||
|
// Collect the characters on the line before the first U+003A COLON character (:),
|
||||||
|
// and let field be that string.
|
||||||
|
field = line[:colonIndex]
|
||||||
|
// Collect the characters on the line after the first U+003A COLON character (:),
|
||||||
|
// and let value be that string.
|
||||||
|
value = line[colonIndex+1:]
|
||||||
|
// If value starts with a single U+0020 SPACE character, remove it from value.
|
||||||
|
if len(value) > 0 && value[0] == ' ' {
|
||||||
|
value = value[1:]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Otherwise, the string is not empty but does not contain a U+003A COLON character character (:)
|
||||||
|
// Use the whole line as the field name, and the empty string as the field value.
|
||||||
|
field = line
|
||||||
|
value = []byte{}
|
||||||
|
}
|
||||||
|
// The steps to process the field given a field name and a field value depend on the field name,
|
||||||
|
// as given in the following list. Field names must be compared literally,
|
||||||
|
// with no case folding performed.
|
||||||
|
switch string(field) {
|
||||||
|
case "event":
|
||||||
|
// Set the event name buffer to field value.
|
||||||
|
currentEvent.Event = string(value)
|
||||||
|
case "id":
|
||||||
|
// Set the event stream's last event ID to the field value.
|
||||||
|
currentEvent.Id = string(value)
|
||||||
|
case "retry":
|
||||||
|
// If the field value consists of only characters in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9),
|
||||||
|
// then interpret the field value as an integer in base ten, and set the event stream's reconnection time to that integer.
|
||||||
|
// Otherwise, ignore the field.
|
||||||
|
currentEvent.Id = string(value)
|
||||||
|
case "data":
|
||||||
|
// Append the field value to the data buffer,
|
||||||
|
dataBuffer.Write(value)
|
||||||
|
// then append a single U+000A LINE FEED (LF) character to the data buffer.
|
||||||
|
dataBuffer.WriteString("\n")
|
||||||
|
default:
|
||||||
|
//Otherwise. The field is ignored.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Once the end of the file is reached, the user agent must dispatch the event one final time.
|
||||||
|
d.dispatchEvent(currentEvent, dataBuffer.String())
|
||||||
|
|
||||||
|
return d.events, nil
|
||||||
|
}
|
110
vendor/github.com/gin-contrib/sse/sse-encoder.go
generated
vendored
Normal file
110
vendor/github.com/gin-contrib/sse/sse-encoder.go
generated
vendored
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package sse
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Server-Sent Events
|
||||||
|
// W3C Working Draft 29 October 2009
|
||||||
|
// http://www.w3.org/TR/2009/WD-eventsource-20091029/
|
||||||
|
|
||||||
|
const ContentType = "text/event-stream"
|
||||||
|
|
||||||
|
var contentType = []string{ContentType}
|
||||||
|
var noCache = []string{"no-cache"}
|
||||||
|
|
||||||
|
var fieldReplacer = strings.NewReplacer(
|
||||||
|
"\n", "\\n",
|
||||||
|
"\r", "\\r")
|
||||||
|
|
||||||
|
var dataReplacer = strings.NewReplacer(
|
||||||
|
"\n", "\ndata:",
|
||||||
|
"\r", "\\r")
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
Event string
|
||||||
|
Id string
|
||||||
|
Retry uint
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Encode(writer io.Writer, event Event) error {
|
||||||
|
w := checkWriter(writer)
|
||||||
|
writeId(w, event.Id)
|
||||||
|
writeEvent(w, event.Event)
|
||||||
|
writeRetry(w, event.Retry)
|
||||||
|
return writeData(w, event.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeId(w stringWriter, id string) {
|
||||||
|
if len(id) > 0 {
|
||||||
|
w.WriteString("id:")
|
||||||
|
fieldReplacer.WriteString(w, id)
|
||||||
|
w.WriteString("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeEvent(w stringWriter, event string) {
|
||||||
|
if len(event) > 0 {
|
||||||
|
w.WriteString("event:")
|
||||||
|
fieldReplacer.WriteString(w, event)
|
||||||
|
w.WriteString("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeRetry(w stringWriter, retry uint) {
|
||||||
|
if retry > 0 {
|
||||||
|
w.WriteString("retry:")
|
||||||
|
w.WriteString(strconv.FormatUint(uint64(retry), 10))
|
||||||
|
w.WriteString("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeData(w stringWriter, data interface{}) error {
|
||||||
|
w.WriteString("data:")
|
||||||
|
switch kindOfData(data) {
|
||||||
|
case reflect.Struct, reflect.Slice, reflect.Map:
|
||||||
|
err := json.NewEncoder(w).Encode(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.WriteString("\n")
|
||||||
|
default:
|
||||||
|
dataReplacer.WriteString(w, fmt.Sprint(data))
|
||||||
|
w.WriteString("\n\n")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Event) Render(w http.ResponseWriter) error {
|
||||||
|
r.WriteContentType(w)
|
||||||
|
return Encode(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Event) WriteContentType(w http.ResponseWriter) {
|
||||||
|
header := w.Header()
|
||||||
|
header["Content-Type"] = contentType
|
||||||
|
|
||||||
|
if _, exist := header["Cache-Control"]; !exist {
|
||||||
|
header["Cache-Control"] = noCache
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func kindOfData(data interface{}) reflect.Kind {
|
||||||
|
value := reflect.ValueOf(data)
|
||||||
|
valueType := value.Kind()
|
||||||
|
if valueType == reflect.Ptr {
|
||||||
|
valueType = value.Elem().Kind()
|
||||||
|
}
|
||||||
|
return valueType
|
||||||
|
}
|
24
vendor/github.com/gin-contrib/sse/writer.go
generated
vendored
Normal file
24
vendor/github.com/gin-contrib/sse/writer.go
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package sse
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
type stringWriter interface {
|
||||||
|
io.Writer
|
||||||
|
WriteString(string) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type stringWrapper struct {
|
||||||
|
io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w stringWrapper) WriteString(str string) (int, error) {
|
||||||
|
return w.Writer.Write([]byte(str))
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkWriter(writer io.Writer) stringWriter {
|
||||||
|
if w, ok := writer.(stringWriter); ok {
|
||||||
|
return w
|
||||||
|
} else {
|
||||||
|
return stringWrapper{writer}
|
||||||
|
}
|
||||||
|
}
|
227
vendor/github.com/gin-gonic/gin/AUTHORS.md
generated
vendored
Normal file
227
vendor/github.com/gin-gonic/gin/AUTHORS.md
generated
vendored
Normal file
@ -0,0 +1,227 @@
|
|||||||
|
List of all the awesome people working to make Gin the best Web Framework in Go.
|
||||||
|
|
||||||
|
## gin 0.x series authors
|
||||||
|
|
||||||
|
**Maintainer:** Manu Martinez-Almeida (@manucorporat), Javier Provecho (@javierprovecho)
|
||||||
|
|
||||||
|
People and companies, who have contributed, in alphabetical order.
|
||||||
|
|
||||||
|
**@858806258 (杰哥)**
|
||||||
|
- Fix typo in example
|
||||||
|
|
||||||
|
|
||||||
|
**@achedeuzot (Klemen Sever)**
|
||||||
|
- Fix newline debug printing
|
||||||
|
|
||||||
|
|
||||||
|
**@adammck (Adam Mckaig)**
|
||||||
|
- Add MIT license
|
||||||
|
|
||||||
|
|
||||||
|
**@AlexanderChen1989 (Alexander)**
|
||||||
|
- Typos in README
|
||||||
|
|
||||||
|
|
||||||
|
**@alexanderdidenko (Aleksandr Didenko)**
|
||||||
|
- Add support multipart/form-data
|
||||||
|
|
||||||
|
|
||||||
|
**@alexandernyquist (Alexander Nyquist)**
|
||||||
|
- Using template.Must to fix multiple return issue
|
||||||
|
- ★ Added support for OPTIONS verb
|
||||||
|
- ★ Setting response headers before calling WriteHeader
|
||||||
|
- Improved documentation for model binding
|
||||||
|
- ★ Added Content.Redirect()
|
||||||
|
- ★ Added tons of Unit tests
|
||||||
|
|
||||||
|
|
||||||
|
**@austinheap (Austin Heap)**
|
||||||
|
- Added travis CI integration
|
||||||
|
|
||||||
|
|
||||||
|
**@andredublin (Andre Dublin)**
|
||||||
|
- Fix typo in comment
|
||||||
|
|
||||||
|
|
||||||
|
**@bredov (Ludwig Valda Vasquez)**
|
||||||
|
- Fix html templating in debug mode
|
||||||
|
|
||||||
|
|
||||||
|
**@bluele (Jun Kimura)**
|
||||||
|
- Fixes code examples in README
|
||||||
|
|
||||||
|
|
||||||
|
**@chad-russell**
|
||||||
|
- ★ Support for serializing gin.H into XML
|
||||||
|
|
||||||
|
|
||||||
|
**@dickeyxxx (Jeff Dickey)**
|
||||||
|
- Typos in README
|
||||||
|
- Add example about serving static files
|
||||||
|
|
||||||
|
|
||||||
|
**@donileo (Adonis)**
|
||||||
|
- Add NoMethod handler
|
||||||
|
|
||||||
|
|
||||||
|
**@dutchcoders (DutchCoders)**
|
||||||
|
- ★ Fix security bug that allows client to spoof ip
|
||||||
|
- Fix typo. r.HTMLTemplates -> SetHTMLTemplate
|
||||||
|
|
||||||
|
|
||||||
|
**@el3ctro- (Joshua Loper)**
|
||||||
|
- Fix typo in example
|
||||||
|
|
||||||
|
|
||||||
|
**@ethankan (Ethan Kan)**
|
||||||
|
- Unsigned integers in binding
|
||||||
|
|
||||||
|
|
||||||
|
**(Evgeny Persienko)**
|
||||||
|
- Validate sub structures
|
||||||
|
|
||||||
|
|
||||||
|
**@frankbille (Frank Bille)**
|
||||||
|
- Add support for HTTP Realm Auth
|
||||||
|
|
||||||
|
|
||||||
|
**@fmd (Fareed Dudhia)**
|
||||||
|
- Fix typo. SetHTTPTemplate -> SetHTMLTemplate
|
||||||
|
|
||||||
|
|
||||||
|
**@ironiridis (Christopher Harrington)**
|
||||||
|
- Remove old reference
|
||||||
|
|
||||||
|
|
||||||
|
**@jammie-stackhouse (Jamie Stackhouse)**
|
||||||
|
- Add more shortcuts for router methods
|
||||||
|
|
||||||
|
|
||||||
|
**@jasonrhansen**
|
||||||
|
- Fix spelling and grammar errors in documentation
|
||||||
|
|
||||||
|
|
||||||
|
**@JasonSoft (Jason Lee)**
|
||||||
|
- Fix typo in comment
|
||||||
|
|
||||||
|
|
||||||
|
**@joiggama (Ignacio Galindo)**
|
||||||
|
- Add utf-8 charset header on renders
|
||||||
|
|
||||||
|
|
||||||
|
**@julienschmidt (Julien Schmidt)**
|
||||||
|
- gofmt the code examples
|
||||||
|
|
||||||
|
|
||||||
|
**@kelcecil (Kel Cecil)**
|
||||||
|
- Fix readme typo
|
||||||
|
|
||||||
|
|
||||||
|
**@kyledinh (Kyle Dinh)**
|
||||||
|
- Adds RunTLS()
|
||||||
|
|
||||||
|
|
||||||
|
**@LinusU (Linus Unnebäck)**
|
||||||
|
- Small fixes in README
|
||||||
|
|
||||||
|
|
||||||
|
**@loongmxbt (Saint Asky)**
|
||||||
|
- Fix typo in example
|
||||||
|
|
||||||
|
|
||||||
|
**@lucas-clemente (Lucas Clemente)**
|
||||||
|
- ★ work around path.Join removing trailing slashes from routes
|
||||||
|
|
||||||
|
|
||||||
|
**@mattn (Yasuhiro Matsumoto)**
|
||||||
|
- Improve color logger
|
||||||
|
|
||||||
|
|
||||||
|
**@mdigger (Dmitry Sedykh)**
|
||||||
|
- Fixes Form binding when content-type is x-www-form-urlencoded
|
||||||
|
- No repeat call c.Writer.Status() in gin.Logger
|
||||||
|
- Fixes Content-Type for json render
|
||||||
|
|
||||||
|
|
||||||
|
**@mirzac (Mirza Ceric)**
|
||||||
|
- Fix debug printing
|
||||||
|
|
||||||
|
|
||||||
|
**@mopemope (Yutaka Matsubara)**
|
||||||
|
- ★ Adds Godep support (Dependencies Manager)
|
||||||
|
- Fix variadic parameter in the flexible render API
|
||||||
|
- Fix Corrupted plain render
|
||||||
|
- Add Pluggable View Renderer Example
|
||||||
|
|
||||||
|
|
||||||
|
**@msemenistyi (Mykyta Semenistyi)**
|
||||||
|
- update Readme.md. Add code to String method
|
||||||
|
|
||||||
|
|
||||||
|
**@msoedov (Sasha Myasoedov)**
|
||||||
|
- ★ Adds tons of unit tests.
|
||||||
|
|
||||||
|
|
||||||
|
**@ngerakines (Nick Gerakines)**
|
||||||
|
- ★ Improves API, c.GET() doesn't panic
|
||||||
|
- Adds MustGet() method
|
||||||
|
|
||||||
|
|
||||||
|
**@r8k (Rajiv Kilaparti)**
|
||||||
|
- Fix Port usage in README.
|
||||||
|
|
||||||
|
|
||||||
|
**@rayrod2030 (Ray Rodriguez)**
|
||||||
|
- Fix typo in example
|
||||||
|
|
||||||
|
|
||||||
|
**@rns**
|
||||||
|
- Fix typo in example
|
||||||
|
|
||||||
|
|
||||||
|
**@RobAWilkinson (Robert Wilkinson)**
|
||||||
|
- Add example of forms and params
|
||||||
|
|
||||||
|
|
||||||
|
**@rogierlommers (Rogier Lommers)**
|
||||||
|
- Add updated static serve example
|
||||||
|
|
||||||
|
|
||||||
|
**@se77en (Damon Zhao)**
|
||||||
|
- Improve color logging
|
||||||
|
|
||||||
|
|
||||||
|
**@silasb (Silas Baronda)**
|
||||||
|
- Fixing quotes in README
|
||||||
|
|
||||||
|
|
||||||
|
**@SkuliOskarsson (Skuli Oskarsson)**
|
||||||
|
- Fixes some texts in README II
|
||||||
|
|
||||||
|
|
||||||
|
**@slimmy (Jimmy Pettersson)**
|
||||||
|
- Added messages for required bindings
|
||||||
|
|
||||||
|
|
||||||
|
**@smira (Andrey Smirnov)**
|
||||||
|
- Add support for ignored/unexported fields in binding
|
||||||
|
|
||||||
|
|
||||||
|
**@superalsrk (SRK.Lyu)**
|
||||||
|
- Update httprouter godeps
|
||||||
|
|
||||||
|
|
||||||
|
**@tebeka (Miki Tebeka)**
|
||||||
|
- Use net/http constants instead of numeric values
|
||||||
|
|
||||||
|
|
||||||
|
**@techjanitor**
|
||||||
|
- Update context.go reserved IPs
|
||||||
|
|
||||||
|
|
||||||
|
**@yosssi (Keiji Yoshida)**
|
||||||
|
- Fix link in README
|
||||||
|
|
||||||
|
|
||||||
|
**@yuyabee**
|
||||||
|
- Fixed README
|
298
vendor/github.com/gin-gonic/gin/BENCHMARKS.md
generated
vendored
Normal file
298
vendor/github.com/gin-gonic/gin/BENCHMARKS.md
generated
vendored
Normal file
@ -0,0 +1,298 @@
|
|||||||
|
**Machine:** intel i7 ivy bridge quad-core. 8GB RAM.
|
||||||
|
**Date:** June 4th, 2015
|
||||||
|
[https://github.com/gin-gonic/go-http-routing-benchmark](https://github.com/gin-gonic/go-http-routing-benchmark)
|
||||||
|
|
||||||
|
```
|
||||||
|
BenchmarkAce_Param 5000000 372 ns/op 32 B/op 1 allocs/op
|
||||||
|
BenchmarkBear_Param 1000000 1165 ns/op 424 B/op 5 allocs/op
|
||||||
|
BenchmarkBeego_Param 1000000 2440 ns/op 720 B/op 10 allocs/op
|
||||||
|
BenchmarkBone_Param 1000000 1067 ns/op 384 B/op 3 allocs/op
|
||||||
|
BenchmarkDenco_Param 5000000 240 ns/op 32 B/op 1 allocs/op
|
||||||
|
BenchmarkEcho_Param 10000000 130 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_Param 10000000 133 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_Param 1000000 1826 ns/op 656 B/op 9 allocs/op
|
||||||
|
BenchmarkGoji_Param 2000000 957 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkGoJsonRest_Param 1000000 2021 ns/op 657 B/op 14 allocs/op
|
||||||
|
BenchmarkGoRestful_Param 200000 8825 ns/op 2496 B/op 31 allocs/op
|
||||||
|
BenchmarkGorillaMux_Param 500000 3340 ns/op 784 B/op 9 allocs/op
|
||||||
|
BenchmarkHttpRouter_Param 10000000 152 ns/op 32 B/op 1 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_Param 2000000 717 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkKocha_Param 3000000 423 ns/op 56 B/op 3 allocs/op
|
||||||
|
BenchmarkMacaron_Param 1000000 3410 ns/op 1104 B/op 11 allocs/op
|
||||||
|
BenchmarkMartini_Param 200000 7101 ns/op 1152 B/op 12 allocs/op
|
||||||
|
BenchmarkPat_Param 1000000 2040 ns/op 656 B/op 14 allocs/op
|
||||||
|
BenchmarkPossum_Param 1000000 2048 ns/op 624 B/op 7 allocs/op
|
||||||
|
BenchmarkR2router_Param 1000000 1144 ns/op 432 B/op 6 allocs/op
|
||||||
|
BenchmarkRevel_Param 200000 6725 ns/op 1672 B/op 28 allocs/op
|
||||||
|
BenchmarkRivet_Param 1000000 1121 ns/op 464 B/op 5 allocs/op
|
||||||
|
BenchmarkTango_Param 1000000 1479 ns/op 256 B/op 10 allocs/op
|
||||||
|
BenchmarkTigerTonic_Param 1000000 3393 ns/op 992 B/op 19 allocs/op
|
||||||
|
BenchmarkTraffic_Param 300000 5525 ns/op 1984 B/op 23 allocs/op
|
||||||
|
BenchmarkVulcan_Param 2000000 924 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_Param 1000000 1084 ns/op 368 B/op 3 allocs/op
|
||||||
|
BenchmarkAce_Param5 3000000 614 ns/op 160 B/op 1 allocs/op
|
||||||
|
BenchmarkBear_Param5 1000000 1617 ns/op 469 B/op 5 allocs/op
|
||||||
|
BenchmarkBeego_Param5 1000000 3373 ns/op 992 B/op 13 allocs/op
|
||||||
|
BenchmarkBone_Param5 1000000 1478 ns/op 432 B/op 3 allocs/op
|
||||||
|
BenchmarkDenco_Param5 3000000 570 ns/op 160 B/op 1 allocs/op
|
||||||
|
BenchmarkEcho_Param5 5000000 256 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_Param5 10000000 222 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_Param5 1000000 2789 ns/op 928 B/op 12 allocs/op
|
||||||
|
BenchmarkGoji_Param5 1000000 1287 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkGoJsonRest_Param5 1000000 3670 ns/op 1105 B/op 17 allocs/op
|
||||||
|
BenchmarkGoRestful_Param5 200000 10756 ns/op 2672 B/op 31 allocs/op
|
||||||
|
BenchmarkGorillaMux_Param5 300000 5543 ns/op 912 B/op 9 allocs/op
|
||||||
|
BenchmarkHttpRouter_Param5 5000000 403 ns/op 160 B/op 1 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_Param5 1000000 1089 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkKocha_Param5 1000000 1682 ns/op 440 B/op 10 allocs/op
|
||||||
|
BenchmarkMacaron_Param5 300000 4596 ns/op 1376 B/op 14 allocs/op
|
||||||
|
BenchmarkMartini_Param5 100000 15703 ns/op 1280 B/op 12 allocs/op
|
||||||
|
BenchmarkPat_Param5 300000 5320 ns/op 1008 B/op 42 allocs/op
|
||||||
|
BenchmarkPossum_Param5 1000000 2155 ns/op 624 B/op 7 allocs/op
|
||||||
|
BenchmarkR2router_Param5 1000000 1559 ns/op 432 B/op 6 allocs/op
|
||||||
|
BenchmarkRevel_Param5 200000 8184 ns/op 2024 B/op 35 allocs/op
|
||||||
|
BenchmarkRivet_Param5 1000000 1914 ns/op 528 B/op 9 allocs/op
|
||||||
|
BenchmarkTango_Param5 1000000 3280 ns/op 944 B/op 18 allocs/op
|
||||||
|
BenchmarkTigerTonic_Param5 200000 11638 ns/op 2519 B/op 53 allocs/op
|
||||||
|
BenchmarkTraffic_Param5 200000 8941 ns/op 2280 B/op 31 allocs/op
|
||||||
|
BenchmarkVulcan_Param5 1000000 1279 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_Param5 1000000 1574 ns/op 416 B/op 3 allocs/op
|
||||||
|
BenchmarkAce_Param20 1000000 1528 ns/op 640 B/op 1 allocs/op
|
||||||
|
BenchmarkBear_Param20 300000 4906 ns/op 1633 B/op 5 allocs/op
|
||||||
|
BenchmarkBeego_Param20 200000 10529 ns/op 3868 B/op 17 allocs/op
|
||||||
|
BenchmarkBone_Param20 300000 7362 ns/op 2539 B/op 5 allocs/op
|
||||||
|
BenchmarkDenco_Param20 1000000 1884 ns/op 640 B/op 1 allocs/op
|
||||||
|
BenchmarkEcho_Param20 2000000 689 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_Param20 3000000 545 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_Param20 200000 9437 ns/op 3804 B/op 16 allocs/op
|
||||||
|
BenchmarkGoji_Param20 500000 3987 ns/op 1246 B/op 2 allocs/op
|
||||||
|
BenchmarkGoJsonRest_Param20 100000 12799 ns/op 4492 B/op 21 allocs/op
|
||||||
|
BenchmarkGoRestful_Param20 100000 19451 ns/op 5244 B/op 33 allocs/op
|
||||||
|
BenchmarkGorillaMux_Param20 100000 12456 ns/op 3275 B/op 11 allocs/op
|
||||||
|
BenchmarkHttpRouter_Param20 1000000 1333 ns/op 640 B/op 1 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_Param20 300000 6490 ns/op 2187 B/op 4 allocs/op
|
||||||
|
BenchmarkKocha_Param20 300000 5335 ns/op 1808 B/op 27 allocs/op
|
||||||
|
BenchmarkMacaron_Param20 200000 11325 ns/op 4252 B/op 18 allocs/op
|
||||||
|
BenchmarkMartini_Param20 20000 64419 ns/op 3644 B/op 14 allocs/op
|
||||||
|
BenchmarkPat_Param20 50000 24672 ns/op 4888 B/op 151 allocs/op
|
||||||
|
BenchmarkPossum_Param20 1000000 2085 ns/op 624 B/op 7 allocs/op
|
||||||
|
BenchmarkR2router_Param20 300000 6809 ns/op 2283 B/op 8 allocs/op
|
||||||
|
BenchmarkRevel_Param20 100000 16600 ns/op 5551 B/op 54 allocs/op
|
||||||
|
BenchmarkRivet_Param20 200000 8428 ns/op 2620 B/op 26 allocs/op
|
||||||
|
BenchmarkTango_Param20 100000 16302 ns/op 8224 B/op 48 allocs/op
|
||||||
|
BenchmarkTigerTonic_Param20 30000 46828 ns/op 10538 B/op 178 allocs/op
|
||||||
|
BenchmarkTraffic_Param20 50000 28871 ns/op 7998 B/op 66 allocs/op
|
||||||
|
BenchmarkVulcan_Param20 1000000 2267 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_Param20 300000 6828 ns/op 2507 B/op 5 allocs/op
|
||||||
|
BenchmarkAce_ParamWrite 3000000 502 ns/op 40 B/op 2 allocs/op
|
||||||
|
BenchmarkBear_ParamWrite 1000000 1303 ns/op 424 B/op 5 allocs/op
|
||||||
|
BenchmarkBeego_ParamWrite 1000000 2489 ns/op 728 B/op 11 allocs/op
|
||||||
|
BenchmarkBone_ParamWrite 1000000 1181 ns/op 384 B/op 3 allocs/op
|
||||||
|
BenchmarkDenco_ParamWrite 5000000 315 ns/op 32 B/op 1 allocs/op
|
||||||
|
BenchmarkEcho_ParamWrite 10000000 237 ns/op 8 B/op 1 allocs/op
|
||||||
|
BenchmarkGin_ParamWrite 5000000 336 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_ParamWrite 1000000 2079 ns/op 664 B/op 10 allocs/op
|
||||||
|
BenchmarkGoji_ParamWrite 1000000 1092 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkGoJsonRest_ParamWrite 1000000 3329 ns/op 1136 B/op 19 allocs/op
|
||||||
|
BenchmarkGoRestful_ParamWrite 200000 9273 ns/op 2504 B/op 32 allocs/op
|
||||||
|
BenchmarkGorillaMux_ParamWrite 500000 3919 ns/op 792 B/op 10 allocs/op
|
||||||
|
BenchmarkHttpRouter_ParamWrite 10000000 223 ns/op 32 B/op 1 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_ParamWrite 2000000 788 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkKocha_ParamWrite 3000000 549 ns/op 56 B/op 3 allocs/op
|
||||||
|
BenchmarkMacaron_ParamWrite 500000 4558 ns/op 1216 B/op 16 allocs/op
|
||||||
|
BenchmarkMartini_ParamWrite 200000 8850 ns/op 1256 B/op 16 allocs/op
|
||||||
|
BenchmarkPat_ParamWrite 500000 3679 ns/op 1088 B/op 19 allocs/op
|
||||||
|
BenchmarkPossum_ParamWrite 1000000 2114 ns/op 624 B/op 7 allocs/op
|
||||||
|
BenchmarkR2router_ParamWrite 1000000 1320 ns/op 432 B/op 6 allocs/op
|
||||||
|
BenchmarkRevel_ParamWrite 200000 8048 ns/op 2128 B/op 33 allocs/op
|
||||||
|
BenchmarkRivet_ParamWrite 1000000 1393 ns/op 472 B/op 6 allocs/op
|
||||||
|
BenchmarkTango_ParamWrite 2000000 819 ns/op 136 B/op 5 allocs/op
|
||||||
|
BenchmarkTigerTonic_ParamWrite 300000 5860 ns/op 1440 B/op 25 allocs/op
|
||||||
|
BenchmarkTraffic_ParamWrite 200000 7429 ns/op 2400 B/op 27 allocs/op
|
||||||
|
BenchmarkVulcan_ParamWrite 2000000 972 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_ParamWrite 1000000 1226 ns/op 368 B/op 3 allocs/op
|
||||||
|
BenchmarkAce_GithubStatic 5000000 294 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkBear_GithubStatic 3000000 575 ns/op 88 B/op 3 allocs/op
|
||||||
|
BenchmarkBeego_GithubStatic 1000000 1561 ns/op 368 B/op 7 allocs/op
|
||||||
|
BenchmarkBone_GithubStatic 200000 12301 ns/op 2880 B/op 60 allocs/op
|
||||||
|
BenchmarkDenco_GithubStatic 20000000 74.6 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkEcho_GithubStatic 10000000 176 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_GithubStatic 10000000 159 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_GithubStatic 1000000 1116 ns/op 304 B/op 6 allocs/op
|
||||||
|
BenchmarkGoji_GithubStatic 5000000 413 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGoRestful_GithubStatic 30000 55200 ns/op 3520 B/op 36 allocs/op
|
||||||
|
BenchmarkGoJsonRest_GithubStatic 1000000 1504 ns/op 337 B/op 12 allocs/op
|
||||||
|
BenchmarkGorillaMux_GithubStatic 100000 23620 ns/op 464 B/op 8 allocs/op
|
||||||
|
BenchmarkHttpRouter_GithubStatic 20000000 78.3 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_GithubStatic 20000000 84.9 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkKocha_GithubStatic 20000000 111 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkMacaron_GithubStatic 1000000 2686 ns/op 752 B/op 8 allocs/op
|
||||||
|
BenchmarkMartini_GithubStatic 100000 22244 ns/op 832 B/op 11 allocs/op
|
||||||
|
BenchmarkPat_GithubStatic 100000 13278 ns/op 3648 B/op 76 allocs/op
|
||||||
|
BenchmarkPossum_GithubStatic 1000000 1429 ns/op 480 B/op 4 allocs/op
|
||||||
|
BenchmarkR2router_GithubStatic 2000000 726 ns/op 144 B/op 5 allocs/op
|
||||||
|
BenchmarkRevel_GithubStatic 300000 6271 ns/op 1288 B/op 25 allocs/op
|
||||||
|
BenchmarkRivet_GithubStatic 3000000 474 ns/op 112 B/op 2 allocs/op
|
||||||
|
BenchmarkTango_GithubStatic 1000000 1842 ns/op 256 B/op 10 allocs/op
|
||||||
|
BenchmarkTigerTonic_GithubStatic 5000000 361 ns/op 48 B/op 1 allocs/op
|
||||||
|
BenchmarkTraffic_GithubStatic 30000 47197 ns/op 18920 B/op 149 allocs/op
|
||||||
|
BenchmarkVulcan_GithubStatic 1000000 1415 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_GithubStatic 1000000 2522 ns/op 512 B/op 11 allocs/op
|
||||||
|
BenchmarkAce_GithubParam 3000000 578 ns/op 96 B/op 1 allocs/op
|
||||||
|
BenchmarkBear_GithubParam 1000000 1592 ns/op 464 B/op 5 allocs/op
|
||||||
|
BenchmarkBeego_GithubParam 1000000 2891 ns/op 784 B/op 11 allocs/op
|
||||||
|
BenchmarkBone_GithubParam 300000 6440 ns/op 1456 B/op 16 allocs/op
|
||||||
|
BenchmarkDenco_GithubParam 3000000 514 ns/op 128 B/op 1 allocs/op
|
||||||
|
BenchmarkEcho_GithubParam 5000000 292 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_GithubParam 10000000 242 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_GithubParam 1000000 2343 ns/op 720 B/op 10 allocs/op
|
||||||
|
BenchmarkGoji_GithubParam 1000000 1566 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkGoJsonRest_GithubParam 1000000 2828 ns/op 721 B/op 15 allocs/op
|
||||||
|
BenchmarkGoRestful_GithubParam 10000 177711 ns/op 2816 B/op 35 allocs/op
|
||||||
|
BenchmarkGorillaMux_GithubParam 100000 13591 ns/op 816 B/op 9 allocs/op
|
||||||
|
BenchmarkHttpRouter_GithubParam 5000000 352 ns/op 96 B/op 1 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_GithubParam 2000000 973 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkKocha_GithubParam 2000000 889 ns/op 128 B/op 5 allocs/op
|
||||||
|
BenchmarkMacaron_GithubParam 500000 4047 ns/op 1168 B/op 12 allocs/op
|
||||||
|
BenchmarkMartini_GithubParam 50000 28982 ns/op 1184 B/op 12 allocs/op
|
||||||
|
BenchmarkPat_GithubParam 200000 8747 ns/op 2480 B/op 56 allocs/op
|
||||||
|
BenchmarkPossum_GithubParam 1000000 2158 ns/op 624 B/op 7 allocs/op
|
||||||
|
BenchmarkR2router_GithubParam 1000000 1352 ns/op 432 B/op 6 allocs/op
|
||||||
|
BenchmarkRevel_GithubParam 200000 7673 ns/op 1784 B/op 30 allocs/op
|
||||||
|
BenchmarkRivet_GithubParam 1000000 1573 ns/op 480 B/op 6 allocs/op
|
||||||
|
BenchmarkTango_GithubParam 1000000 2418 ns/op 480 B/op 13 allocs/op
|
||||||
|
BenchmarkTigerTonic_GithubParam 300000 6048 ns/op 1440 B/op 28 allocs/op
|
||||||
|
BenchmarkTraffic_GithubParam 100000 20143 ns/op 6024 B/op 55 allocs/op
|
||||||
|
BenchmarkVulcan_GithubParam 1000000 2224 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_GithubParam 500000 4156 ns/op 1312 B/op 12 allocs/op
|
||||||
|
BenchmarkAce_GithubAll 10000 109482 ns/op 13792 B/op 167 allocs/op
|
||||||
|
BenchmarkBear_GithubAll 10000 287490 ns/op 79952 B/op 943 allocs/op
|
||||||
|
BenchmarkBeego_GithubAll 3000 562184 ns/op 146272 B/op 2092 allocs/op
|
||||||
|
BenchmarkBone_GithubAll 500 2578716 ns/op 648016 B/op 8119 allocs/op
|
||||||
|
BenchmarkDenco_GithubAll 20000 94955 ns/op 20224 B/op 167 allocs/op
|
||||||
|
BenchmarkEcho_GithubAll 30000 58705 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_GithubAll 30000 50991 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_GithubAll 5000 449648 ns/op 133280 B/op 1889 allocs/op
|
||||||
|
BenchmarkGoji_GithubAll 2000 689748 ns/op 56113 B/op 334 allocs/op
|
||||||
|
BenchmarkGoJsonRest_GithubAll 5000 537769 ns/op 135995 B/op 2940 allocs/op
|
||||||
|
BenchmarkGoRestful_GithubAll 100 18410628 ns/op 797236 B/op 7725 allocs/op
|
||||||
|
BenchmarkGorillaMux_GithubAll 200 8036360 ns/op 153137 B/op 1791 allocs/op
|
||||||
|
BenchmarkHttpRouter_GithubAll 20000 63506 ns/op 13792 B/op 167 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_GithubAll 10000 165927 ns/op 56112 B/op 334 allocs/op
|
||||||
|
BenchmarkKocha_GithubAll 10000 171362 ns/op 23304 B/op 843 allocs/op
|
||||||
|
BenchmarkMacaron_GithubAll 2000 817008 ns/op 224960 B/op 2315 allocs/op
|
||||||
|
BenchmarkMartini_GithubAll 100 12609209 ns/op 237952 B/op 2686 allocs/op
|
||||||
|
BenchmarkPat_GithubAll 300 4830398 ns/op 1504101 B/op 32222 allocs/op
|
||||||
|
BenchmarkPossum_GithubAll 10000 301716 ns/op 97440 B/op 812 allocs/op
|
||||||
|
BenchmarkR2router_GithubAll 10000 270691 ns/op 77328 B/op 1182 allocs/op
|
||||||
|
BenchmarkRevel_GithubAll 1000 1491919 ns/op 345553 B/op 5918 allocs/op
|
||||||
|
BenchmarkRivet_GithubAll 10000 283860 ns/op 84272 B/op 1079 allocs/op
|
||||||
|
BenchmarkTango_GithubAll 5000 473821 ns/op 87078 B/op 2470 allocs/op
|
||||||
|
BenchmarkTigerTonic_GithubAll 2000 1120131 ns/op 241088 B/op 6052 allocs/op
|
||||||
|
BenchmarkTraffic_GithubAll 200 8708979 ns/op 2664762 B/op 22390 allocs/op
|
||||||
|
BenchmarkVulcan_GithubAll 5000 353392 ns/op 19894 B/op 609 allocs/op
|
||||||
|
BenchmarkZeus_GithubAll 2000 944234 ns/op 300688 B/op 2648 allocs/op
|
||||||
|
BenchmarkAce_GPlusStatic 5000000 251 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkBear_GPlusStatic 3000000 415 ns/op 72 B/op 3 allocs/op
|
||||||
|
BenchmarkBeego_GPlusStatic 1000000 1416 ns/op 352 B/op 7 allocs/op
|
||||||
|
BenchmarkBone_GPlusStatic 10000000 192 ns/op 32 B/op 1 allocs/op
|
||||||
|
BenchmarkDenco_GPlusStatic 30000000 47.6 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkEcho_GPlusStatic 10000000 131 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_GPlusStatic 10000000 131 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_GPlusStatic 1000000 1035 ns/op 288 B/op 6 allocs/op
|
||||||
|
BenchmarkGoji_GPlusStatic 5000000 304 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGoJsonRest_GPlusStatic 1000000 1286 ns/op 337 B/op 12 allocs/op
|
||||||
|
BenchmarkGoRestful_GPlusStatic 200000 9649 ns/op 2160 B/op 30 allocs/op
|
||||||
|
BenchmarkGorillaMux_GPlusStatic 1000000 2346 ns/op 464 B/op 8 allocs/op
|
||||||
|
BenchmarkHttpRouter_GPlusStatic 30000000 42.7 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_GPlusStatic 30000000 49.5 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkKocha_GPlusStatic 20000000 74.8 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkMacaron_GPlusStatic 1000000 2520 ns/op 736 B/op 8 allocs/op
|
||||||
|
BenchmarkMartini_GPlusStatic 300000 5310 ns/op 832 B/op 11 allocs/op
|
||||||
|
BenchmarkPat_GPlusStatic 5000000 398 ns/op 96 B/op 2 allocs/op
|
||||||
|
BenchmarkPossum_GPlusStatic 1000000 1434 ns/op 480 B/op 4 allocs/op
|
||||||
|
BenchmarkR2router_GPlusStatic 2000000 646 ns/op 144 B/op 5 allocs/op
|
||||||
|
BenchmarkRevel_GPlusStatic 300000 6172 ns/op 1272 B/op 25 allocs/op
|
||||||
|
BenchmarkRivet_GPlusStatic 3000000 444 ns/op 112 B/op 2 allocs/op
|
||||||
|
BenchmarkTango_GPlusStatic 1000000 1400 ns/op 208 B/op 10 allocs/op
|
||||||
|
BenchmarkTigerTonic_GPlusStatic 10000000 213 ns/op 32 B/op 1 allocs/op
|
||||||
|
BenchmarkTraffic_GPlusStatic 1000000 3091 ns/op 1208 B/op 16 allocs/op
|
||||||
|
BenchmarkVulcan_GPlusStatic 2000000 863 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_GPlusStatic 10000000 237 ns/op 16 B/op 1 allocs/op
|
||||||
|
BenchmarkAce_GPlusParam 3000000 435 ns/op 64 B/op 1 allocs/op
|
||||||
|
BenchmarkBear_GPlusParam 1000000 1205 ns/op 448 B/op 5 allocs/op
|
||||||
|
BenchmarkBeego_GPlusParam 1000000 2494 ns/op 720 B/op 10 allocs/op
|
||||||
|
BenchmarkBone_GPlusParam 1000000 1126 ns/op 384 B/op 3 allocs/op
|
||||||
|
BenchmarkDenco_GPlusParam 5000000 325 ns/op 64 B/op 1 allocs/op
|
||||||
|
BenchmarkEcho_GPlusParam 10000000 168 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_GPlusParam 10000000 170 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_GPlusParam 1000000 1895 ns/op 656 B/op 9 allocs/op
|
||||||
|
BenchmarkGoji_GPlusParam 1000000 1071 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkGoJsonRest_GPlusParam 1000000 2282 ns/op 657 B/op 14 allocs/op
|
||||||
|
BenchmarkGoRestful_GPlusParam 100000 19400 ns/op 2560 B/op 33 allocs/op
|
||||||
|
BenchmarkGorillaMux_GPlusParam 500000 5001 ns/op 784 B/op 9 allocs/op
|
||||||
|
BenchmarkHttpRouter_GPlusParam 10000000 240 ns/op 64 B/op 1 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_GPlusParam 2000000 797 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkKocha_GPlusParam 3000000 505 ns/op 56 B/op 3 allocs/op
|
||||||
|
BenchmarkMacaron_GPlusParam 1000000 3668 ns/op 1104 B/op 11 allocs/op
|
||||||
|
BenchmarkMartini_GPlusParam 200000 10672 ns/op 1152 B/op 12 allocs/op
|
||||||
|
BenchmarkPat_GPlusParam 1000000 2376 ns/op 704 B/op 14 allocs/op
|
||||||
|
BenchmarkPossum_GPlusParam 1000000 2090 ns/op 624 B/op 7 allocs/op
|
||||||
|
BenchmarkR2router_GPlusParam 1000000 1233 ns/op 432 B/op 6 allocs/op
|
||||||
|
BenchmarkRevel_GPlusParam 200000 6778 ns/op 1704 B/op 28 allocs/op
|
||||||
|
BenchmarkRivet_GPlusParam 1000000 1279 ns/op 464 B/op 5 allocs/op
|
||||||
|
BenchmarkTango_GPlusParam 1000000 1981 ns/op 272 B/op 10 allocs/op
|
||||||
|
BenchmarkTigerTonic_GPlusParam 500000 3893 ns/op 1064 B/op 19 allocs/op
|
||||||
|
BenchmarkTraffic_GPlusParam 200000 6585 ns/op 2000 B/op 23 allocs/op
|
||||||
|
BenchmarkVulcan_GPlusParam 1000000 1233 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_GPlusParam 1000000 1350 ns/op 368 B/op 3 allocs/op
|
||||||
|
BenchmarkAce_GPlus2Params 3000000 512 ns/op 64 B/op 1 allocs/op
|
||||||
|
BenchmarkBear_GPlus2Params 1000000 1564 ns/op 464 B/op 5 allocs/op
|
||||||
|
BenchmarkBeego_GPlus2Params 1000000 3043 ns/op 784 B/op 11 allocs/op
|
||||||
|
BenchmarkBone_GPlus2Params 1000000 3152 ns/op 736 B/op 7 allocs/op
|
||||||
|
BenchmarkDenco_GPlus2Params 3000000 431 ns/op 64 B/op 1 allocs/op
|
||||||
|
BenchmarkEcho_GPlus2Params 5000000 247 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_GPlus2Params 10000000 219 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_GPlus2Params 1000000 2363 ns/op 720 B/op 10 allocs/op
|
||||||
|
BenchmarkGoji_GPlus2Params 1000000 1540 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkGoJsonRest_GPlus2Params 1000000 2872 ns/op 721 B/op 15 allocs/op
|
||||||
|
BenchmarkGoRestful_GPlus2Params 100000 23030 ns/op 2720 B/op 35 allocs/op
|
||||||
|
BenchmarkGorillaMux_GPlus2Params 200000 10516 ns/op 816 B/op 9 allocs/op
|
||||||
|
BenchmarkHttpRouter_GPlus2Params 5000000 273 ns/op 64 B/op 1 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_GPlus2Params 2000000 939 ns/op 336 B/op 2 allocs/op
|
||||||
|
BenchmarkKocha_GPlus2Params 2000000 844 ns/op 128 B/op 5 allocs/op
|
||||||
|
BenchmarkMacaron_GPlus2Params 500000 3914 ns/op 1168 B/op 12 allocs/op
|
||||||
|
BenchmarkMartini_GPlus2Params 50000 35759 ns/op 1280 B/op 16 allocs/op
|
||||||
|
BenchmarkPat_GPlus2Params 200000 7089 ns/op 2304 B/op 41 allocs/op
|
||||||
|
BenchmarkPossum_GPlus2Params 1000000 2093 ns/op 624 B/op 7 allocs/op
|
||||||
|
BenchmarkR2router_GPlus2Params 1000000 1320 ns/op 432 B/op 6 allocs/op
|
||||||
|
BenchmarkRevel_GPlus2Params 200000 7351 ns/op 1800 B/op 30 allocs/op
|
||||||
|
BenchmarkRivet_GPlus2Params 1000000 1485 ns/op 480 B/op 6 allocs/op
|
||||||
|
BenchmarkTango_GPlus2Params 1000000 2111 ns/op 448 B/op 12 allocs/op
|
||||||
|
BenchmarkTigerTonic_GPlus2Params 300000 6271 ns/op 1528 B/op 28 allocs/op
|
||||||
|
BenchmarkTraffic_GPlus2Params 100000 14886 ns/op 3312 B/op 34 allocs/op
|
||||||
|
BenchmarkVulcan_GPlus2Params 1000000 1883 ns/op 98 B/op 3 allocs/op
|
||||||
|
BenchmarkZeus_GPlus2Params 1000000 2686 ns/op 784 B/op 6 allocs/op
|
||||||
|
BenchmarkAce_GPlusAll 300000 5912 ns/op 640 B/op 11 allocs/op
|
||||||
|
BenchmarkBear_GPlusAll 100000 16448 ns/op 5072 B/op 61 allocs/op
|
||||||
|
BenchmarkBeego_GPlusAll 50000 32916 ns/op 8976 B/op 129 allocs/op
|
||||||
|
BenchmarkBone_GPlusAll 50000 25836 ns/op 6992 B/op 76 allocs/op
|
||||||
|
BenchmarkDenco_GPlusAll 500000 4462 ns/op 672 B/op 11 allocs/op
|
||||||
|
BenchmarkEcho_GPlusAll 500000 2806 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGin_GPlusAll 500000 2579 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkGocraftWeb_GPlusAll 50000 25223 ns/op 8144 B/op 116 allocs/op
|
||||||
|
BenchmarkGoji_GPlusAll 100000 14237 ns/op 3696 B/op 22 allocs/op
|
||||||
|
BenchmarkGoJsonRest_GPlusAll 50000 29227 ns/op 8221 B/op 183 allocs/op
|
||||||
|
BenchmarkGoRestful_GPlusAll 10000 203144 ns/op 36064 B/op 441 allocs/op
|
||||||
|
BenchmarkGorillaMux_GPlusAll 20000 80906 ns/op 9712 B/op 115 allocs/op
|
||||||
|
BenchmarkHttpRouter_GPlusAll 500000 3040 ns/op 640 B/op 11 allocs/op
|
||||||
|
BenchmarkHttpTreeMux_GPlusAll 200000 9627 ns/op 3696 B/op 22 allocs/op
|
||||||
|
BenchmarkKocha_GPlusAll 200000 8108 ns/op 976 B/op 43 allocs/op
|
||||||
|
BenchmarkMacaron_GPlusAll 30000 48083 ns/op 13968 B/op 142 allocs/op
|
||||||
|
BenchmarkMartini_GPlusAll 10000 196978 ns/op 15072 B/op 178 allocs/op
|
||||||
|
BenchmarkPat_GPlusAll 30000 58865 ns/op 16880 B/op 343 allocs/op
|
||||||
|
BenchmarkPossum_GPlusAll 100000 19685 ns/op 6240 B/op 52 allocs/op
|
||||||
|
BenchmarkR2router_GPlusAll 100000 16251 ns/op 5040 B/op 76 allocs/op
|
||||||
|
BenchmarkRevel_GPlusAll 20000 93489 ns/op 21656 B/op 368 allocs/op
|
||||||
|
BenchmarkRivet_GPlusAll 100000 16907 ns/op 5408 B/op 64 allocs/op
|
||||||
|
```
|
191
vendor/github.com/gin-gonic/gin/CHANGELOG.md
generated
vendored
Normal file
191
vendor/github.com/gin-gonic/gin/CHANGELOG.md
generated
vendored
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
# CHANGELOG
|
||||||
|
|
||||||
|
### Gin 1.2
|
||||||
|
|
||||||
|
- [NEW] Switch from godeps to govendor
|
||||||
|
- [NEW] Add support for Let's Encrypt via gin-gonic/autotls
|
||||||
|
- [NEW] Improve README examples and add extra at examples folder
|
||||||
|
- [NEW] Improved support with App Engine
|
||||||
|
- [NEW] Add custom template delimiters, see #860
|
||||||
|
- [NEW] Add Template Func Maps, see #962
|
||||||
|
- [NEW] Add \*context.Handler(), see #928
|
||||||
|
- [NEW] Add \*context.GetRawData()
|
||||||
|
- [NEW] Add \*context.GetHeader() (request)
|
||||||
|
- [NEW] Add \*context.AbortWithStatusJSON() (JSON content type)
|
||||||
|
- [NEW] Add \*context.Keys type cast helpers
|
||||||
|
- [NEW] Add \*context.ShouldBindWith()
|
||||||
|
- [NEW] Add \*context.MustBindWith()
|
||||||
|
- [NEW] Add \*engine.SetFuncMap()
|
||||||
|
- [DEPRECATE] On next release: \*context.BindWith(), see #855
|
||||||
|
- [FIX] Refactor render
|
||||||
|
- [FIX] Reworked tests
|
||||||
|
- [FIX] logger now supports cygwin
|
||||||
|
- [FIX] Use X-Forwarded-For before X-Real-Ip
|
||||||
|
- [FIX] time.Time binding (#904)
|
||||||
|
|
||||||
|
### Gin 1.1.4
|
||||||
|
|
||||||
|
- [NEW] Support google appengine for IsTerminal func
|
||||||
|
|
||||||
|
### Gin 1.1.3
|
||||||
|
|
||||||
|
- [FIX] Reverted Logger: skip ANSI color commands
|
||||||
|
|
||||||
|
### Gin 1.1
|
||||||
|
|
||||||
|
- [NEW] Implement QueryArray and PostArray methods
|
||||||
|
- [NEW] Refactor GetQuery and GetPostForm
|
||||||
|
- [NEW] Add contribution guide
|
||||||
|
- [FIX] Corrected typos in README
|
||||||
|
- [FIX] Removed additional Iota
|
||||||
|
- [FIX] Changed imports to gopkg instead of github in README (#733)
|
||||||
|
- [FIX] Logger: skip ANSI color commands if output is not a tty
|
||||||
|
|
||||||
|
### Gin 1.0rc2 (...)
|
||||||
|
|
||||||
|
- [PERFORMANCE] Fast path for writing Content-Type.
|
||||||
|
- [PERFORMANCE] Much faster 404 routing
|
||||||
|
- [PERFORMANCE] Allocation optimizations
|
||||||
|
- [PERFORMANCE] Faster root tree lookup
|
||||||
|
- [PERFORMANCE] Zero overhead, String() and JSON() rendering.
|
||||||
|
- [PERFORMANCE] Faster ClientIP parsing
|
||||||
|
- [PERFORMANCE] Much faster SSE implementation
|
||||||
|
- [NEW] Benchmarks suite
|
||||||
|
- [NEW] Bind validation can be disabled and replaced with custom validators.
|
||||||
|
- [NEW] More flexible HTML render
|
||||||
|
- [NEW] Multipart and PostForm bindings
|
||||||
|
- [NEW] Adds method to return all the registered routes
|
||||||
|
- [NEW] Context.HandlerName() returns the main handler's name
|
||||||
|
- [NEW] Adds Error.IsType() helper
|
||||||
|
- [FIX] Binding multipart form
|
||||||
|
- [FIX] Integration tests
|
||||||
|
- [FIX] Crash when binding non struct object in Context.
|
||||||
|
- [FIX] RunTLS() implementation
|
||||||
|
- [FIX] Logger() unit tests
|
||||||
|
- [FIX] Adds SetHTMLTemplate() warning
|
||||||
|
- [FIX] Context.IsAborted()
|
||||||
|
- [FIX] More unit tests
|
||||||
|
- [FIX] JSON, XML, HTML renders accept custom content-types
|
||||||
|
- [FIX] gin.AbortIndex is unexported
|
||||||
|
- [FIX] Better approach to avoid directory listing in StaticFS()
|
||||||
|
- [FIX] Context.ClientIP() always returns the IP with trimmed spaces.
|
||||||
|
- [FIX] Better warning when running in debug mode.
|
||||||
|
- [FIX] Google App Engine integration. debugPrint does not use os.Stdout
|
||||||
|
- [FIX] Fixes integer overflow in error type
|
||||||
|
- [FIX] Error implements the json.Marshaller interface
|
||||||
|
- [FIX] MIT license in every file
|
||||||
|
|
||||||
|
|
||||||
|
### Gin 1.0rc1 (May 22, 2015)
|
||||||
|
|
||||||
|
- [PERFORMANCE] Zero allocation router
|
||||||
|
- [PERFORMANCE] Faster JSON, XML and text rendering
|
||||||
|
- [PERFORMANCE] Custom hand optimized HttpRouter for Gin
|
||||||
|
- [PERFORMANCE] Misc code optimizations. Inlining, tail call optimizations
|
||||||
|
- [NEW] Built-in support for golang.org/x/net/context
|
||||||
|
- [NEW] Any(path, handler). Create a route that matches any path
|
||||||
|
- [NEW] Refactored rendering pipeline (faster and static typeded)
|
||||||
|
- [NEW] Refactored errors API
|
||||||
|
- [NEW] IndentedJSON() prints pretty JSON
|
||||||
|
- [NEW] Added gin.DefaultWriter
|
||||||
|
- [NEW] UNIX socket support
|
||||||
|
- [NEW] RouterGroup.BasePath is exposed
|
||||||
|
- [NEW] JSON validation using go-validate-yourself (very powerful options)
|
||||||
|
- [NEW] Completed suite of unit tests
|
||||||
|
- [NEW] HTTP streaming with c.Stream()
|
||||||
|
- [NEW] StaticFile() creates a router for serving just one file.
|
||||||
|
- [NEW] StaticFS() has an option to disable directory listing.
|
||||||
|
- [NEW] StaticFS() for serving static files through virtual filesystems
|
||||||
|
- [NEW] Server-Sent Events native support
|
||||||
|
- [NEW] WrapF() and WrapH() helpers for wrapping http.HandlerFunc and http.Handler
|
||||||
|
- [NEW] Added LoggerWithWriter() middleware
|
||||||
|
- [NEW] Added RecoveryWithWriter() middleware
|
||||||
|
- [NEW] Added DefaultPostFormValue()
|
||||||
|
- [NEW] Added DefaultFormValue()
|
||||||
|
- [NEW] Added DefaultParamValue()
|
||||||
|
- [FIX] BasicAuth() when using custom realm
|
||||||
|
- [FIX] Bug when serving static files in nested routing group
|
||||||
|
- [FIX] Redirect using built-in http.Redirect()
|
||||||
|
- [FIX] Logger when printing the requested path
|
||||||
|
- [FIX] Documentation typos
|
||||||
|
- [FIX] Context.Engine renamed to Context.engine
|
||||||
|
- [FIX] Better debugging messages
|
||||||
|
- [FIX] ErrorLogger
|
||||||
|
- [FIX] Debug HTTP render
|
||||||
|
- [FIX] Refactored binding and render modules
|
||||||
|
- [FIX] Refactored Context initialization
|
||||||
|
- [FIX] Refactored BasicAuth()
|
||||||
|
- [FIX] NoMethod/NoRoute handlers
|
||||||
|
- [FIX] Hijacking http
|
||||||
|
- [FIX] Better support for Google App Engine (using log instead of fmt)
|
||||||
|
|
||||||
|
|
||||||
|
### Gin 0.6 (Mar 9, 2015)
|
||||||
|
|
||||||
|
- [NEW] Support multipart/form-data
|
||||||
|
- [NEW] NoMethod handler
|
||||||
|
- [NEW] Validate sub structures
|
||||||
|
- [NEW] Support for HTTP Realm Auth
|
||||||
|
- [FIX] Unsigned integers in binding
|
||||||
|
- [FIX] Improve color logger
|
||||||
|
|
||||||
|
|
||||||
|
### Gin 0.5 (Feb 7, 2015)
|
||||||
|
|
||||||
|
- [NEW] Content Negotiation
|
||||||
|
- [FIX] Solved security bug that allow a client to spoof ip
|
||||||
|
- [FIX] Fix unexported/ignored fields in binding
|
||||||
|
|
||||||
|
|
||||||
|
### Gin 0.4 (Aug 21, 2014)
|
||||||
|
|
||||||
|
- [NEW] Development mode
|
||||||
|
- [NEW] Unit tests
|
||||||
|
- [NEW] Add Content.Redirect()
|
||||||
|
- [FIX] Deferring WriteHeader()
|
||||||
|
- [FIX] Improved documentation for model binding
|
||||||
|
|
||||||
|
|
||||||
|
### Gin 0.3 (Jul 18, 2014)
|
||||||
|
|
||||||
|
- [PERFORMANCE] Normal log and error log are printed in the same call.
|
||||||
|
- [PERFORMANCE] Improve performance of NoRouter()
|
||||||
|
- [PERFORMANCE] Improve context's memory locality, reduce CPU cache faults.
|
||||||
|
- [NEW] Flexible rendering API
|
||||||
|
- [NEW] Add Context.File()
|
||||||
|
- [NEW] Add shorcut RunTLS() for http.ListenAndServeTLS
|
||||||
|
- [FIX] Rename NotFound404() to NoRoute()
|
||||||
|
- [FIX] Errors in context are purged
|
||||||
|
- [FIX] Adds HEAD method in Static file serving
|
||||||
|
- [FIX] Refactors Static() file serving
|
||||||
|
- [FIX] Using keyed initialization to fix app-engine integration
|
||||||
|
- [FIX] Can't unmarshal JSON array, #63
|
||||||
|
- [FIX] Renaming Context.Req to Context.Request
|
||||||
|
- [FIX] Check application/x-www-form-urlencoded when parsing form
|
||||||
|
|
||||||
|
|
||||||
|
### Gin 0.2b (Jul 08, 2014)
|
||||||
|
- [PERFORMANCE] Using sync.Pool to allocatio/gc overhead
|
||||||
|
- [NEW] Travis CI integration
|
||||||
|
- [NEW] Completely new logger
|
||||||
|
- [NEW] New API for serving static files. gin.Static()
|
||||||
|
- [NEW] gin.H() can be serialized into XML
|
||||||
|
- [NEW] Typed errors. Errors can be typed. Internet/external/custom.
|
||||||
|
- [NEW] Support for Godeps
|
||||||
|
- [NEW] Travis/Godocs badges in README
|
||||||
|
- [NEW] New Bind() and BindWith() methods for parsing request body.
|
||||||
|
- [NEW] Add Content.Copy()
|
||||||
|
- [NEW] Add context.LastError()
|
||||||
|
- [NEW] Add shorcut for OPTIONS HTTP method
|
||||||
|
- [FIX] Tons of README fixes
|
||||||
|
- [FIX] Header is written before body
|
||||||
|
- [FIX] BasicAuth() and changes API a little bit
|
||||||
|
- [FIX] Recovery() middleware only prints panics
|
||||||
|
- [FIX] Context.Get() does not panic anymore. Use MustGet() instead.
|
||||||
|
- [FIX] Multiple http.WriteHeader() in NotFound handlers
|
||||||
|
- [FIX] Engine.Run() panics if http server can't be setted up
|
||||||
|
- [FIX] Crash when route path doesn't start with '/'
|
||||||
|
- [FIX] Do not update header when status code is negative
|
||||||
|
- [FIX] Setting response headers before calling WriteHeader in context.String()
|
||||||
|
- [FIX] Add MIT license
|
||||||
|
- [FIX] Changes behaviour of ErrorLogger() and Logger()
|
21
vendor/github.com/gin-gonic/gin/LICENSE
generated
vendored
Normal file
21
vendor/github.com/gin-gonic/gin/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Manuel Martínez-Almeida
|
||||||
|
|
||||||
|
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.
|
61
vendor/github.com/gin-gonic/gin/Makefile
generated
vendored
Normal file
61
vendor/github.com/gin-gonic/gin/Makefile
generated
vendored
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
GOFMT ?= gofmt "-s"
|
||||||
|
PACKAGES ?= $(shell go list ./... | grep -v /vendor/)
|
||||||
|
GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*")
|
||||||
|
|
||||||
|
all: build
|
||||||
|
|
||||||
|
install: deps
|
||||||
|
govendor sync
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test:
|
||||||
|
go test -v -covermode=count -coverprofile=coverage.out
|
||||||
|
|
||||||
|
.PHONY: fmt
|
||||||
|
fmt:
|
||||||
|
$(GOFMT) -w $(GOFILES)
|
||||||
|
|
||||||
|
.PHONY: fmt-check
|
||||||
|
fmt-check:
|
||||||
|
# get all go files and run go fmt on them
|
||||||
|
@diff=$$($(GOFMT) -d $(GOFILES)); \
|
||||||
|
if [ -n "$$diff" ]; then \
|
||||||
|
echo "Please run 'make fmt' and commit the result:"; \
|
||||||
|
echo "$${diff}"; \
|
||||||
|
exit 1; \
|
||||||
|
fi;
|
||||||
|
|
||||||
|
vet:
|
||||||
|
go vet $(PACKAGES)
|
||||||
|
|
||||||
|
deps:
|
||||||
|
@hash govendor > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
|
go get -u github.com/kardianos/govendor; \
|
||||||
|
fi
|
||||||
|
@hash embedmd > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
|
go get -u github.com/campoy/embedmd; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
embedmd:
|
||||||
|
embedmd -d *.md
|
||||||
|
|
||||||
|
.PHONY: lint
|
||||||
|
lint:
|
||||||
|
@hash golint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
|
go get -u github.com/golang/lint/golint; \
|
||||||
|
fi
|
||||||
|
for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done;
|
||||||
|
|
||||||
|
.PHONY: misspell-check
|
||||||
|
misspell-check:
|
||||||
|
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
|
go get -u github.com/client9/misspell/cmd/misspell; \
|
||||||
|
fi
|
||||||
|
misspell -error $(GOFILES)
|
||||||
|
|
||||||
|
.PHONY: misspell
|
||||||
|
misspell:
|
||||||
|
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
|
go get -u github.com/client9/misspell/cmd/misspell; \
|
||||||
|
fi
|
||||||
|
misspell -w $(GOFILES)
|
977
vendor/github.com/gin-gonic/gin/README.md
generated
vendored
Normal file
977
vendor/github.com/gin-gonic/gin/README.md
generated
vendored
Normal file
@ -0,0 +1,977 @@
|
|||||||
|
# Gin Web Framework
|
||||||
|
|
||||||
|
<img align="right" src="https://raw.githubusercontent.com/gin-gonic/gin/master/logo.jpg">
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/gin-gonic/gin.svg)](https://travis-ci.org/gin-gonic/gin)
|
||||||
|
[![codecov](https://codecov.io/gh/gin-gonic/gin/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-gonic/gin)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-gonic/gin)](https://goreportcard.com/report/github.com/gin-gonic/gin)
|
||||||
|
[![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.svg)](https://godoc.org/github.com/gin-gonic/gin)
|
||||||
|
[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
Gin is a web framework written in Go (Golang). It features a martini-like API with much better performance, up to 40 times faster thanks to [httprouter](https://github.com/julienschmidt/httprouter). If you need performance and good productivity, you will love Gin.
|
||||||
|
|
||||||
|
![Gin console logger](https://gin-gonic.github.io/gin/other/console.png)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ cat test.go
|
||||||
|
```
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
r.GET("/ping", func(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"message": "pong",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
r.Run() // listen and serve on 0.0.0.0:8080
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Benchmarks
|
||||||
|
|
||||||
|
Gin uses a custom version of [HttpRouter](https://github.com/julienschmidt/httprouter)
|
||||||
|
|
||||||
|
[See all benchmarks](/BENCHMARKS.md)
|
||||||
|
|
||||||
|
|
||||||
|
Benchmark name | (1) | (2) | (3) | (4)
|
||||||
|
--------------------------------|----------:|----------:|----------:|------:
|
||||||
|
BenchmarkAce_GithubAll | 10000 | 109482 | 13792 | 167
|
||||||
|
BenchmarkBear_GithubAll | 10000 | 287490 | 79952 | 943
|
||||||
|
BenchmarkBeego_GithubAll | 3000 | 562184 | 146272 | 2092
|
||||||
|
BenchmarkBone_GithubAll | 500 | 2578716 | 648016 | 8119
|
||||||
|
BenchmarkDenco_GithubAll | 20000 | 94955 | 20224 | 167
|
||||||
|
BenchmarkEcho_GithubAll | 30000 | 58705 | 0 | 0
|
||||||
|
**BenchmarkGin_GithubAll** | **30000** | **50991** | **0** | **0**
|
||||||
|
BenchmarkGocraftWeb_GithubAll | 5000 | 449648 | 133280 | 1889
|
||||||
|
BenchmarkGoji_GithubAll | 2000 | 689748 | 56113 | 334
|
||||||
|
BenchmarkGoJsonRest_GithubAll | 5000 | 537769 | 135995 | 2940
|
||||||
|
BenchmarkGoRestful_GithubAll | 100 | 18410628 | 797236 | 7725
|
||||||
|
BenchmarkGorillaMux_GithubAll | 200 | 8036360 | 153137 | 1791
|
||||||
|
BenchmarkHttpRouter_GithubAll | 20000 | 63506 | 13792 | 167
|
||||||
|
BenchmarkHttpTreeMux_GithubAll | 10000 | 165927 | 56112 | 334
|
||||||
|
BenchmarkKocha_GithubAll | 10000 | 171362 | 23304 | 843
|
||||||
|
BenchmarkMacaron_GithubAll | 2000 | 817008 | 224960 | 2315
|
||||||
|
BenchmarkMartini_GithubAll | 100 | 12609209 | 237952 | 2686
|
||||||
|
BenchmarkPat_GithubAll | 300 | 4830398 | 1504101 | 32222
|
||||||
|
BenchmarkPossum_GithubAll | 10000 | 301716 | 97440 | 812
|
||||||
|
BenchmarkR2router_GithubAll | 10000 | 270691 | 77328 | 1182
|
||||||
|
BenchmarkRevel_GithubAll | 1000 | 1491919 | 345553 | 5918
|
||||||
|
BenchmarkRivet_GithubAll | 10000 | 283860 | 84272 | 1079
|
||||||
|
BenchmarkTango_GithubAll | 5000 | 473821 | 87078 | 2470
|
||||||
|
BenchmarkTigerTonic_GithubAll | 2000 | 1120131 | 241088 | 6052
|
||||||
|
BenchmarkTraffic_GithubAll | 200 | 8708979 | 2664762 | 22390
|
||||||
|
BenchmarkVulcan_GithubAll | 5000 | 353392 | 19894 | 609
|
||||||
|
BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648
|
||||||
|
|
||||||
|
(1): Total Repetitions
|
||||||
|
(2): Single Repetition Duration (ns/op)
|
||||||
|
(3): Heap Memory (B/op)
|
||||||
|
(4): Average Allocations per Repetition (allocs/op)
|
||||||
|
|
||||||
|
## Gin v1. stable
|
||||||
|
|
||||||
|
- [x] Zero allocation router.
|
||||||
|
- [x] Still the fastest http router and framework. From routing to writing.
|
||||||
|
- [x] Complete suite of unit tests
|
||||||
|
- [x] Battle tested
|
||||||
|
- [x] API frozen, new releases will not break your code.
|
||||||
|
|
||||||
|
|
||||||
|
## Start using it
|
||||||
|
|
||||||
|
1. Download and install it:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ go get github.com/gin-gonic/gin
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Import it in your code:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
```
|
||||||
|
|
||||||
|
3. (Optional) Import `net/http`. This is required for example if using constants such as `http.StatusOK`.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "net/http"
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Examples
|
||||||
|
|
||||||
|
### Using GET, POST, PUT, PATCH, DELETE and OPTIONS
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
// Disable Console Color
|
||||||
|
// gin.DisableConsoleColor()
|
||||||
|
|
||||||
|
// Creates a gin router with default middleware:
|
||||||
|
// logger and recovery (crash-free) middleware
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
router.GET("/someGet", getting)
|
||||||
|
router.POST("/somePost", posting)
|
||||||
|
router.PUT("/somePut", putting)
|
||||||
|
router.DELETE("/someDelete", deleting)
|
||||||
|
router.PATCH("/somePatch", patching)
|
||||||
|
router.HEAD("/someHead", head)
|
||||||
|
router.OPTIONS("/someOptions", options)
|
||||||
|
|
||||||
|
// By default it serves on :8080 unless a
|
||||||
|
// PORT environment variable was defined.
|
||||||
|
router.Run()
|
||||||
|
// router.Run(":3000") for a hard coded port
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Parameters in path
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
// This handler will match /user/john but will not match neither /user/ or /user
|
||||||
|
router.GET("/user/:name", func(c *gin.Context) {
|
||||||
|
name := c.Param("name")
|
||||||
|
c.String(http.StatusOK, "Hello %s", name)
|
||||||
|
})
|
||||||
|
|
||||||
|
// However, this one will match /user/john/ and also /user/john/send
|
||||||
|
// If no other routers match /user/john, it will redirect to /user/john/
|
||||||
|
router.GET("/user/:name/*action", func(c *gin.Context) {
|
||||||
|
name := c.Param("name")
|
||||||
|
action := c.Param("action")
|
||||||
|
message := name + " is " + action
|
||||||
|
c.String(http.StatusOK, message)
|
||||||
|
})
|
||||||
|
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Querystring parameters
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
// Query string parameters are parsed using the existing underlying request object.
|
||||||
|
// The request responds to a url matching: /welcome?firstname=Jane&lastname=Doe
|
||||||
|
router.GET("/welcome", func(c *gin.Context) {
|
||||||
|
firstname := c.DefaultQuery("firstname", "Guest")
|
||||||
|
lastname := c.Query("lastname") // shortcut for c.Request.URL.Query().Get("lastname")
|
||||||
|
|
||||||
|
c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Multipart/Urlencoded Form
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
router.POST("/form_post", func(c *gin.Context) {
|
||||||
|
message := c.PostForm("message")
|
||||||
|
nick := c.DefaultPostForm("nick", "anonymous")
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"status": "posted",
|
||||||
|
"message": message,
|
||||||
|
"nick": nick,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Another example: query + post form
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /post?id=1234&page=1 HTTP/1.1
|
||||||
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
name=manu&message=this_is_great
|
||||||
|
```
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
router.POST("/post", func(c *gin.Context) {
|
||||||
|
|
||||||
|
id := c.Query("id")
|
||||||
|
page := c.DefaultQuery("page", "0")
|
||||||
|
name := c.PostForm("name")
|
||||||
|
message := c.PostForm("message")
|
||||||
|
|
||||||
|
fmt.Printf("id: %s; page: %s; name: %s; message: %s", id, page, name, message)
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
id: 1234; page: 1; name: manu; message: this_is_great
|
||||||
|
```
|
||||||
|
|
||||||
|
### Upload files
|
||||||
|
|
||||||
|
#### Single file
|
||||||
|
|
||||||
|
References issue [#774](https://github.com/gin-gonic/gin/issues/774) and detail [example code](examples/upload-file/single).
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
router.POST("/upload", func(c *gin.Context) {
|
||||||
|
// single file
|
||||||
|
file, _ := c.FormFile("file")
|
||||||
|
log.Println(file.Filename)
|
||||||
|
|
||||||
|
c.String(http.StatusOK, fmt.Printf("'%s' uploaded!", file.Filename))
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
How to `curl`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X POST http://localhost:8080/upload \
|
||||||
|
-F "file=@/Users/appleboy/test.zip" \
|
||||||
|
-H "Content-Type: multipart/form-data"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Multiple files
|
||||||
|
|
||||||
|
See the detail [example code](examples/upload-file/multiple).
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
router.POST("/upload", func(c *gin.Context) {
|
||||||
|
// Multipart form
|
||||||
|
form, _ := c.MultipartForm()
|
||||||
|
files := form.File["upload[]"]
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
log.Println(file.Filename)
|
||||||
|
}
|
||||||
|
c.String(http.StatusOK, fmt.Printf("%d files uploaded!", len(files)))
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
How to `curl`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X POST http://localhost:8080/upload \
|
||||||
|
-F "upload[]=@/Users/appleboy/test1.zip" \
|
||||||
|
-F "upload[]=@/Users/appleboy/test2.zip" \
|
||||||
|
-H "Content-Type: multipart/form-data"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Grouping routes
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
// Simple group: v1
|
||||||
|
v1 := router.Group("/v1")
|
||||||
|
{
|
||||||
|
v1.POST("/login", loginEndpoint)
|
||||||
|
v1.POST("/submit", submitEndpoint)
|
||||||
|
v1.POST("/read", readEndpoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple group: v2
|
||||||
|
v2 := router.Group("/v2")
|
||||||
|
{
|
||||||
|
v2.POST("/login", loginEndpoint)
|
||||||
|
v2.POST("/submit", submitEndpoint)
|
||||||
|
v2.POST("/read", readEndpoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Blank Gin without middleware by default
|
||||||
|
|
||||||
|
Use
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := gin.New()
|
||||||
|
```
|
||||||
|
|
||||||
|
instead of
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := gin.Default()
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Using middleware
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
// Creates a router without any middleware by default
|
||||||
|
r := gin.New()
|
||||||
|
|
||||||
|
// Global middleware
|
||||||
|
r.Use(gin.Logger())
|
||||||
|
r.Use(gin.Recovery())
|
||||||
|
|
||||||
|
// Per route middleware, you can add as many as you desire.
|
||||||
|
r.GET("/benchmark", MyBenchLogger(), benchEndpoint)
|
||||||
|
|
||||||
|
// Authorization group
|
||||||
|
// authorized := r.Group("/", AuthRequired())
|
||||||
|
// exactly the same as:
|
||||||
|
authorized := r.Group("/")
|
||||||
|
// per group middleware! in this case we use the custom created
|
||||||
|
// AuthRequired() middleware just in the "authorized" group.
|
||||||
|
authorized.Use(AuthRequired())
|
||||||
|
{
|
||||||
|
authorized.POST("/login", loginEndpoint)
|
||||||
|
authorized.POST("/submit", submitEndpoint)
|
||||||
|
authorized.POST("/read", readEndpoint)
|
||||||
|
|
||||||
|
// nested group
|
||||||
|
testing := authorized.Group("testing")
|
||||||
|
testing.GET("/analytics", analyticsEndpoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen and serve on 0.0.0.0:8080
|
||||||
|
r.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Model binding and validation
|
||||||
|
|
||||||
|
To bind a request body into a type, use model binding. We currently support binding of JSON, XML and standard form values (foo=bar&boo=baz).
|
||||||
|
|
||||||
|
Note that you need to set the corresponding binding tag on all fields you want to bind. For example, when binding from JSON, set `json:"fieldname"`.
|
||||||
|
|
||||||
|
When using the Bind-method, Gin tries to infer the binder depending on the Content-Type header. If you are sure what you are binding, you can use BindWith.
|
||||||
|
|
||||||
|
You can also specify that specific fields are required. If a field is decorated with `binding:"required"` and has a empty value when binding, the current request will fail with an error.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Binding from JSON
|
||||||
|
type Login struct {
|
||||||
|
User string `form:"user" json:"user" binding:"required"`
|
||||||
|
Password string `form:"password" json:"password" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
// Example for binding JSON ({"user": "manu", "password": "123"})
|
||||||
|
router.POST("/loginJSON", func(c *gin.Context) {
|
||||||
|
var json Login
|
||||||
|
if c.BindJSON(&json) == nil {
|
||||||
|
if json.User == "manu" && json.Password == "123" {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Example for binding a HTML form (user=manu&password=123)
|
||||||
|
router.POST("/loginForm", func(c *gin.Context) {
|
||||||
|
var form Login
|
||||||
|
// This will infer what binder to use depending on the content-type header.
|
||||||
|
if c.Bind(&form) == nil {
|
||||||
|
if form.User == "manu" && form.Password == "123" {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Listen and serve on 0.0.0.0:8080
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bind Query String
|
||||||
|
|
||||||
|
See the [detail information](https://github.com/gin-gonic/gin/issues/742#issuecomment-264681292).
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Name string `form:"name"`
|
||||||
|
Address string `form:"address"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
route := gin.Default()
|
||||||
|
route.GET("/testing", startPage)
|
||||||
|
route.Run(":8085")
|
||||||
|
}
|
||||||
|
|
||||||
|
func startPage(c *gin.Context) {
|
||||||
|
var person Person
|
||||||
|
// If `GET`, only `Form` binding engine (`query`) used.
|
||||||
|
// If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`).
|
||||||
|
// See more at https://github.com/gin-gonic/gin/blob/develop/binding/binding.go#L45
|
||||||
|
if c.Bind(&person) == nil {
|
||||||
|
log.Println(person.Name)
|
||||||
|
log.Println(person.Address)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.String(200, "Success")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Multipart/Urlencoded binding
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LoginForm struct {
|
||||||
|
User string `form:"user" binding:"required"`
|
||||||
|
Password string `form:"password" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
router.POST("/login", func(c *gin.Context) {
|
||||||
|
// you can bind multipart form with explicit binding declaration:
|
||||||
|
// c.MustBindWith(&form, binding.Form)
|
||||||
|
// or you can simply use autobinding with Bind method:
|
||||||
|
var form LoginForm
|
||||||
|
// in this case proper binding will be automatically selected
|
||||||
|
if c.Bind(&form) == nil {
|
||||||
|
if form.User == "user" && form.Password == "password" {
|
||||||
|
c.JSON(200, gin.H{"status": "you are logged in"})
|
||||||
|
} else {
|
||||||
|
c.JSON(401, gin.H{"status": "unauthorized"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Test it with:
|
||||||
|
```sh
|
||||||
|
$ curl -v --form user=user --form password=password http://localhost:8080/login
|
||||||
|
```
|
||||||
|
|
||||||
|
### XML, JSON and YAML rendering
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
|
// gin.H is a shortcut for map[string]interface{}
|
||||||
|
r.GET("/someJSON", func(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/moreJSON", func(c *gin.Context) {
|
||||||
|
// You also can use a struct
|
||||||
|
var msg struct {
|
||||||
|
Name string `json:"user"`
|
||||||
|
Message string
|
||||||
|
Number int
|
||||||
|
}
|
||||||
|
msg.Name = "Lena"
|
||||||
|
msg.Message = "hey"
|
||||||
|
msg.Number = 123
|
||||||
|
// Note that msg.Name becomes "user" in the JSON
|
||||||
|
// Will output : {"user": "Lena", "Message": "hey", "Number": 123}
|
||||||
|
c.JSON(http.StatusOK, msg)
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/someXML", func(c *gin.Context) {
|
||||||
|
c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/someYAML", func(c *gin.Context) {
|
||||||
|
c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Listen and serve on 0.0.0.0:8080
|
||||||
|
r.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Serving static files
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
router.Static("/assets", "./assets")
|
||||||
|
router.StaticFS("/more_static", http.Dir("my_file_system"))
|
||||||
|
router.StaticFile("/favicon.ico", "./resources/favicon.ico")
|
||||||
|
|
||||||
|
// Listen and serve on 0.0.0.0:8080
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### HTML rendering
|
||||||
|
|
||||||
|
Using LoadHTMLGlob() or LoadHTMLFiles()
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
router.LoadHTMLGlob("templates/*")
|
||||||
|
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
|
||||||
|
router.GET("/index", func(c *gin.Context) {
|
||||||
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
||||||
|
"title": "Main website",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
templates/index.tmpl
|
||||||
|
|
||||||
|
```html
|
||||||
|
<html>
|
||||||
|
<h1>
|
||||||
|
{{ .title }}
|
||||||
|
</h1>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
Using templates with same name in different directories
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
router.LoadHTMLGlob("templates/**/*")
|
||||||
|
router.GET("/posts/index", func(c *gin.Context) {
|
||||||
|
c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{
|
||||||
|
"title": "Posts",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
router.GET("/users/index", func(c *gin.Context) {
|
||||||
|
c.HTML(http.StatusOK, "users/index.tmpl", gin.H{
|
||||||
|
"title": "Users",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
templates/posts/index.tmpl
|
||||||
|
|
||||||
|
```html
|
||||||
|
{{ define "posts/index.tmpl" }}
|
||||||
|
<html><h1>
|
||||||
|
{{ .title }}
|
||||||
|
</h1>
|
||||||
|
<p>Using posts/index.tmpl</p>
|
||||||
|
</html>
|
||||||
|
{{ end }}
|
||||||
|
```
|
||||||
|
|
||||||
|
templates/users/index.tmpl
|
||||||
|
|
||||||
|
```html
|
||||||
|
{{ define "users/index.tmpl" }}
|
||||||
|
<html><h1>
|
||||||
|
{{ .title }}
|
||||||
|
</h1>
|
||||||
|
<p>Using users/index.tmpl</p>
|
||||||
|
</html>
|
||||||
|
{{ end }}
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also use your own html template render
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "html/template"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
html := template.Must(template.ParseFiles("file1", "file2"))
|
||||||
|
router.SetHTMLTemplate(html)
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You may use custom delims
|
||||||
|
|
||||||
|
```go
|
||||||
|
r := gin.Default()
|
||||||
|
r.Delims("{[{", "}]}")
|
||||||
|
r.LoadHTMLGlob("/path/to/templates"))
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Add custom template funcs
|
||||||
|
|
||||||
|
main.go
|
||||||
|
|
||||||
|
```go
|
||||||
|
...
|
||||||
|
|
||||||
|
func formatAsDate(t time.Time) string {
|
||||||
|
year, month, day := t.Date()
|
||||||
|
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
|
||||||
|
}
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
router.SetFuncMap(template.FuncMap{
|
||||||
|
"formatAsDate": formatAsDate,
|
||||||
|
})
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
router.GET("/raw", func(c *Context) {
|
||||||
|
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
|
||||||
|
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
raw.tmpl
|
||||||
|
|
||||||
|
```html
|
||||||
|
Date: {[{.now | formatAsDate}]}
|
||||||
|
```
|
||||||
|
|
||||||
|
Result:
|
||||||
|
```
|
||||||
|
Date: 2017/07/01
|
||||||
|
```
|
||||||
|
|
||||||
|
### Multitemplate
|
||||||
|
|
||||||
|
Gin allow by default use only one html.Template. Check [a multitemplate render](https://github.com/gin-contrib/multitemplate) for using features like go 1.6 `block template`.
|
||||||
|
|
||||||
|
### Redirects
|
||||||
|
|
||||||
|
Issuing a HTTP redirect is easy:
|
||||||
|
|
||||||
|
```go
|
||||||
|
r.GET("/test", func(c *gin.Context) {
|
||||||
|
c.Redirect(http.StatusMovedPermanently, "http://www.google.com/")
|
||||||
|
})
|
||||||
|
```
|
||||||
|
Both internal and external locations are supported.
|
||||||
|
|
||||||
|
|
||||||
|
### Custom Middleware
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Logger() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
t := time.Now()
|
||||||
|
|
||||||
|
// Set example variable
|
||||||
|
c.Set("example", "12345")
|
||||||
|
|
||||||
|
// before request
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
// after request
|
||||||
|
latency := time.Since(t)
|
||||||
|
log.Print(latency)
|
||||||
|
|
||||||
|
// access the status we are sending
|
||||||
|
status := c.Writer.Status()
|
||||||
|
log.Println(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.New()
|
||||||
|
r.Use(Logger())
|
||||||
|
|
||||||
|
r.GET("/test", func(c *gin.Context) {
|
||||||
|
example := c.MustGet("example").(string)
|
||||||
|
|
||||||
|
// it would print: "12345"
|
||||||
|
log.Println(example)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Listen and serve on 0.0.0.0:8080
|
||||||
|
r.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using BasicAuth() middleware
|
||||||
|
|
||||||
|
```go
|
||||||
|
// simulate some private data
|
||||||
|
var secrets = gin.H{
|
||||||
|
"foo": gin.H{"email": "foo@bar.com", "phone": "123433"},
|
||||||
|
"austin": gin.H{"email": "austin@example.com", "phone": "666"},
|
||||||
|
"lena": gin.H{"email": "lena@guapa.com", "phone": "523443"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
|
// Group using gin.BasicAuth() middleware
|
||||||
|
// gin.Accounts is a shortcut for map[string]string
|
||||||
|
authorized := r.Group("/admin", gin.BasicAuth(gin.Accounts{
|
||||||
|
"foo": "bar",
|
||||||
|
"austin": "1234",
|
||||||
|
"lena": "hello2",
|
||||||
|
"manu": "4321",
|
||||||
|
}))
|
||||||
|
|
||||||
|
// /admin/secrets endpoint
|
||||||
|
// hit "localhost:8080/admin/secrets
|
||||||
|
authorized.GET("/secrets", func(c *gin.Context) {
|
||||||
|
// get user, it was set by the BasicAuth middleware
|
||||||
|
user := c.MustGet(gin.AuthUserKey).(string)
|
||||||
|
if secret, ok := secrets[user]; ok {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"user": user, "secret": secret})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"user": user, "secret": "NO SECRET :("})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Listen and serve on 0.0.0.0:8080
|
||||||
|
r.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Goroutines inside a middleware
|
||||||
|
|
||||||
|
When starting inside a middleware or handler, you **SHOULD NOT** use the original context inside it, you have to use a read-only copy.
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
|
r.GET("/long_async", func(c *gin.Context) {
|
||||||
|
// create copy to be used inside the goroutine
|
||||||
|
cCp := c.Copy()
|
||||||
|
go func() {
|
||||||
|
// simulate a long task with time.Sleep(). 5 seconds
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
|
// note that you are using the copied context "cCp", IMPORTANT
|
||||||
|
log.Println("Done! in path " + cCp.Request.URL.Path)
|
||||||
|
}()
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/long_sync", func(c *gin.Context) {
|
||||||
|
// simulate a long task with time.Sleep(). 5 seconds
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
|
// since we are NOT using a goroutine, we do not have to copy the context
|
||||||
|
log.Println("Done! in path " + c.Request.URL.Path)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Listen and serve on 0.0.0.0:8080
|
||||||
|
r.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Custom HTTP configuration
|
||||||
|
|
||||||
|
Use `http.ListenAndServe()` directly, like this:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
http.ListenAndServe(":8080", router)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
or
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
s := &http.Server{
|
||||||
|
Addr: ":8080",
|
||||||
|
Handler: router,
|
||||||
|
ReadTimeout: 10 * time.Second,
|
||||||
|
WriteTimeout: 10 * time.Second,
|
||||||
|
MaxHeaderBytes: 1 << 20,
|
||||||
|
}
|
||||||
|
s.ListenAndServe()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Support Let's Encrypt
|
||||||
|
|
||||||
|
example for 1-line LetsEncrypt HTTPS servers.
|
||||||
|
|
||||||
|
[embedmd]:# (examples/auto-tls/example1.go go)
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/autotls"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
|
// Ping handler
|
||||||
|
r.GET("/ping", func(c *gin.Context) {
|
||||||
|
c.String(200, "pong")
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Fatal(autotls.Run(r, "example1.com", "example2.com"))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
example for custom autocert manager.
|
||||||
|
|
||||||
|
[embedmd]:# (examples/auto-tls/example2.go go)
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/autotls"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"golang.org/x/crypto/acme/autocert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
|
// Ping handler
|
||||||
|
r.GET("/ping", func(c *gin.Context) {
|
||||||
|
c.String(200, "pong")
|
||||||
|
})
|
||||||
|
|
||||||
|
m := autocert.Manager{
|
||||||
|
Prompt: autocert.AcceptTOS,
|
||||||
|
HostPolicy: autocert.HostWhitelist("example1.com", "example2.com"),
|
||||||
|
Cache: autocert.DirCache("/var/www/.cache"),
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatal(autotls.RunWithManager(r, m))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Graceful restart or stop
|
||||||
|
|
||||||
|
Do you want to graceful restart or stop your web server?
|
||||||
|
There are some ways this can be done.
|
||||||
|
|
||||||
|
We can use [fvbock/endless](https://github.com/fvbock/endless) to replace the default `ListenAndServe`. Refer issue [#296](https://github.com/gin-gonic/gin/issues/296) for more details.
|
||||||
|
|
||||||
|
```go
|
||||||
|
router := gin.Default()
|
||||||
|
router.GET("/", handler)
|
||||||
|
// [...]
|
||||||
|
endless.ListenAndServe(":4242", router)
|
||||||
|
```
|
||||||
|
|
||||||
|
An alternative to endless:
|
||||||
|
|
||||||
|
* [manners](https://github.com/braintree/manners): A polite Go HTTP server that shuts down gracefully.
|
||||||
|
* [graceful](https://github.com/tylerb/graceful): Graceful is a Go package enabling graceful shutdown of an http.Handler server.
|
||||||
|
* [grace](https://github.com/facebookgo/grace): Graceful restart & zero downtime deploy for Go servers.
|
||||||
|
|
||||||
|
If you are using Go 1.8, you may not need to use this library! Consider using http.Server's built-in [Shutdown()](https://golang.org/pkg/net/http/#Server.Shutdown) method for graceful shutdowns. See the full [graceful-shutdown](./examples/graceful-shutdown) example with gin.
|
||||||
|
|
||||||
|
[embedmd]:# (examples/graceful-shutdown/graceful-shutdown/server.go go)
|
||||||
|
```go
|
||||||
|
// +build go1.8
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
router.GET("/", func(c *gin.Context) {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
c.String(http.StatusOK, "Welcome Gin Server")
|
||||||
|
})
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: ":8080",
|
||||||
|
Handler: router,
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
// service connections
|
||||||
|
if err := srv.ListenAndServe(); err != nil {
|
||||||
|
log.Printf("listen: %s\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for interrupt signal to gracefully shutdown the server with
|
||||||
|
// a timeout of 5 seconds.
|
||||||
|
quit := make(chan os.Signal)
|
||||||
|
signal.Notify(quit, os.Interrupt)
|
||||||
|
<-quit
|
||||||
|
log.Println("Shutdown Server ...")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := srv.Shutdown(ctx); err != nil {
|
||||||
|
log.Fatal("Server Shutdown:", err)
|
||||||
|
}
|
||||||
|
log.Println("Server exist")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
- With issues:
|
||||||
|
- Use the search tool before opening a new issue.
|
||||||
|
- Please provide source code and commit sha if you found a bug.
|
||||||
|
- Review existing issues and provide feedback or react to them.
|
||||||
|
- With pull requests:
|
||||||
|
- Open your pull request against develop
|
||||||
|
- Your pull request should have no more than two commits, if not you should squash them.
|
||||||
|
- It should pass all tests in the available continuous integrations systems such as TravisCI.
|
||||||
|
- You should add/modify tests to cover your proposed code changes.
|
||||||
|
- If your pull request contains a new feature, please document it on the README.
|
||||||
|
|
||||||
|
## Users
|
||||||
|
|
||||||
|
Awesome project lists using [Gin](https://github.com/gin-gonic/gin) web framework.
|
||||||
|
|
||||||
|
* [drone](https://github.com/drone/drone): Drone is a Continuous Delivery platform built on Docker, written in Go
|
||||||
|
* [gorush](https://github.com/appleboy/gorush): A push notification server written in Go.
|
92
vendor/github.com/gin-gonic/gin/auth.go
generated
vendored
Normal file
92
vendor/github.com/gin-gonic/gin/auth.go
generated
vendored
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/subtle"
|
||||||
|
"encoding/base64"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const AuthUserKey = "user"
|
||||||
|
|
||||||
|
type (
|
||||||
|
Accounts map[string]string
|
||||||
|
authPair struct {
|
||||||
|
Value string
|
||||||
|
User string
|
||||||
|
}
|
||||||
|
authPairs []authPair
|
||||||
|
)
|
||||||
|
|
||||||
|
func (a authPairs) searchCredential(authValue string) (string, bool) {
|
||||||
|
if len(authValue) == 0 {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
for _, pair := range a {
|
||||||
|
if pair.Value == authValue {
|
||||||
|
return pair.User, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where
|
||||||
|
// the key is the user name and the value is the password, as well as the name of the Realm.
|
||||||
|
// If the realm is empty, "Authorization Required" will be used by default.
|
||||||
|
// (see http://tools.ietf.org/html/rfc2617#section-1.2)
|
||||||
|
func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc {
|
||||||
|
if realm == "" {
|
||||||
|
realm = "Authorization Required"
|
||||||
|
}
|
||||||
|
realm = "Basic realm=" + strconv.Quote(realm)
|
||||||
|
pairs := processAccounts(accounts)
|
||||||
|
return func(c *Context) {
|
||||||
|
// Search user in the slice of allowed credentials
|
||||||
|
user, found := pairs.searchCredential(c.Request.Header.Get("Authorization"))
|
||||||
|
if !found {
|
||||||
|
// Credentials doesn't match, we return 401 and abort handlers chain.
|
||||||
|
c.Header("WWW-Authenticate", realm)
|
||||||
|
c.AbortWithStatus(401)
|
||||||
|
} else {
|
||||||
|
// The user credentials was found, set user's id to key AuthUserKey in this context, the userId can be read later using
|
||||||
|
// c.MustGet(gin.AuthUserKey)
|
||||||
|
c.Set(AuthUserKey, user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BasicAuth returns a Basic HTTP Authorization middleware. It takes as argument a map[string]string where
|
||||||
|
// the key is the user name and the value is the password.
|
||||||
|
func BasicAuth(accounts Accounts) HandlerFunc {
|
||||||
|
return BasicAuthForRealm(accounts, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func processAccounts(accounts Accounts) authPairs {
|
||||||
|
assert1(len(accounts) > 0, "Empty list of authorized credentials")
|
||||||
|
pairs := make(authPairs, 0, len(accounts))
|
||||||
|
for user, password := range accounts {
|
||||||
|
assert1(len(user) > 0, "User can not be empty")
|
||||||
|
value := authorizationHeader(user, password)
|
||||||
|
pairs = append(pairs, authPair{
|
||||||
|
Value: value,
|
||||||
|
User: user,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return pairs
|
||||||
|
}
|
||||||
|
|
||||||
|
func authorizationHeader(user, password string) string {
|
||||||
|
base := user + ":" + password
|
||||||
|
return "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
|
||||||
|
}
|
||||||
|
|
||||||
|
func secureCompare(given, actual string) bool {
|
||||||
|
if subtle.ConstantTimeEq(int32(len(given)), int32(len(actual))) == 1 {
|
||||||
|
return subtle.ConstantTimeCompare([]byte(given), []byte(actual)) == 1
|
||||||
|
}
|
||||||
|
/* Securely compare actual to itself to keep constant time, but always return false */
|
||||||
|
return subtle.ConstantTimeCompare([]byte(actual), []byte(actual)) == 1 && false
|
||||||
|
}
|
72
vendor/github.com/gin-gonic/gin/binding/binding.go
generated
vendored
Normal file
72
vendor/github.com/gin-gonic/gin/binding/binding.go
generated
vendored
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
const (
|
||||||
|
MIMEJSON = "application/json"
|
||||||
|
MIMEHTML = "text/html"
|
||||||
|
MIMEXML = "application/xml"
|
||||||
|
MIMEXML2 = "text/xml"
|
||||||
|
MIMEPlain = "text/plain"
|
||||||
|
MIMEPOSTForm = "application/x-www-form-urlencoded"
|
||||||
|
MIMEMultipartPOSTForm = "multipart/form-data"
|
||||||
|
MIMEPROTOBUF = "application/x-protobuf"
|
||||||
|
MIMEMSGPACK = "application/x-msgpack"
|
||||||
|
MIMEMSGPACK2 = "application/msgpack"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Binding interface {
|
||||||
|
Name() string
|
||||||
|
Bind(*http.Request, interface{}) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type StructValidator interface {
|
||||||
|
// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
|
||||||
|
// If the received type is not a struct, any validation should be skipped and nil must be returned.
|
||||||
|
// If the received type is a struct or pointer to a struct, the validation should be performed.
|
||||||
|
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
|
||||||
|
// Otherwise nil must be returned.
|
||||||
|
ValidateStruct(interface{}) error
|
||||||
|
}
|
||||||
|
|
||||||
|
var Validator StructValidator = &defaultValidator{}
|
||||||
|
|
||||||
|
var (
|
||||||
|
JSON = jsonBinding{}
|
||||||
|
XML = xmlBinding{}
|
||||||
|
Form = formBinding{}
|
||||||
|
FormPost = formPostBinding{}
|
||||||
|
FormMultipart = formMultipartBinding{}
|
||||||
|
ProtoBuf = protobufBinding{}
|
||||||
|
MsgPack = msgpackBinding{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func Default(method, contentType string) Binding {
|
||||||
|
if method == "GET" {
|
||||||
|
return Form
|
||||||
|
}
|
||||||
|
|
||||||
|
switch contentType {
|
||||||
|
case MIMEJSON:
|
||||||
|
return JSON
|
||||||
|
case MIMEXML, MIMEXML2:
|
||||||
|
return XML
|
||||||
|
case MIMEPROTOBUF:
|
||||||
|
return ProtoBuf
|
||||||
|
case MIMEMSGPACK, MIMEMSGPACK2:
|
||||||
|
return MsgPack
|
||||||
|
default: //case MIMEPOSTForm, MIMEMultipartPOSTForm:
|
||||||
|
return Form
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validate(obj interface{}) error {
|
||||||
|
if Validator == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return Validator.ValidateStruct(obj)
|
||||||
|
}
|
45
vendor/github.com/gin-gonic/gin/binding/default_validator.go
generated
vendored
Normal file
45
vendor/github.com/gin-gonic/gin/binding/default_validator.go
generated
vendored
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"gopkg.in/go-playground/validator.v8"
|
||||||
|
)
|
||||||
|
|
||||||
|
type defaultValidator struct {
|
||||||
|
once sync.Once
|
||||||
|
validate *validator.Validate
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ StructValidator = &defaultValidator{}
|
||||||
|
|
||||||
|
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
|
||||||
|
if kindOfData(obj) == reflect.Struct {
|
||||||
|
v.lazyinit()
|
||||||
|
if err := v.validate.Struct(obj); err != nil {
|
||||||
|
return error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *defaultValidator) lazyinit() {
|
||||||
|
v.once.Do(func() {
|
||||||
|
config := &validator.Config{TagName: "binding"}
|
||||||
|
v.validate = validator.New(config)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func kindOfData(data interface{}) reflect.Kind {
|
||||||
|
value := reflect.ValueOf(data)
|
||||||
|
valueType := value.Kind()
|
||||||
|
if valueType == reflect.Ptr {
|
||||||
|
valueType = value.Elem().Kind()
|
||||||
|
}
|
||||||
|
return valueType
|
||||||
|
}
|
54
vendor/github.com/gin-gonic/gin/binding/form.go
generated
vendored
Normal file
54
vendor/github.com/gin-gonic/gin/binding/form.go
generated
vendored
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type formBinding struct{}
|
||||||
|
type formPostBinding struct{}
|
||||||
|
type formMultipartBinding struct{}
|
||||||
|
|
||||||
|
func (formBinding) Name() string {
|
||||||
|
return "form"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (formBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
if err := req.ParseForm(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.ParseMultipartForm(32 << 10) // 32 MB
|
||||||
|
if err := mapForm(obj, req.Form); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return validate(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (formPostBinding) Name() string {
|
||||||
|
return "form-urlencoded"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
if err := req.ParseForm(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := mapForm(obj, req.PostForm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return validate(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (formMultipartBinding) Name() string {
|
||||||
|
return "multipart/form-data"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
if err := req.ParseMultipartForm(32 << 10); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := mapForm(obj, req.MultipartForm.Value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return validate(obj)
|
||||||
|
}
|
182
vendor/github.com/gin-gonic/gin/binding/form_mapping.go
generated
vendored
Normal file
182
vendor/github.com/gin-gonic/gin/binding/form_mapping.go
generated
vendored
Normal file
@ -0,0 +1,182 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func mapForm(ptr interface{}, form map[string][]string) error {
|
||||||
|
typ := reflect.TypeOf(ptr).Elem()
|
||||||
|
val := reflect.ValueOf(ptr).Elem()
|
||||||
|
for i := 0; i < typ.NumField(); i++ {
|
||||||
|
typeField := typ.Field(i)
|
||||||
|
structField := val.Field(i)
|
||||||
|
if !structField.CanSet() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
structFieldKind := structField.Kind()
|
||||||
|
inputFieldName := typeField.Tag.Get("form")
|
||||||
|
if inputFieldName == "" {
|
||||||
|
inputFieldName = typeField.Name
|
||||||
|
|
||||||
|
// if "form" tag is nil, we inspect if the field is a struct.
|
||||||
|
// this would not make sense for JSON parsing but it does for a form
|
||||||
|
// since data is flatten
|
||||||
|
if structFieldKind == reflect.Struct {
|
||||||
|
err := mapForm(structField.Addr().Interface(), form)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inputValue, exists := form[inputFieldName]
|
||||||
|
if !exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
numElems := len(inputValue)
|
||||||
|
if structFieldKind == reflect.Slice && numElems > 0 {
|
||||||
|
sliceOf := structField.Type().Elem().Kind()
|
||||||
|
slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
|
||||||
|
for i := 0; i < numElems; i++ {
|
||||||
|
if err := setWithProperType(sliceOf, inputValue[i], slice.Index(i)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val.Field(i).Set(slice)
|
||||||
|
} else {
|
||||||
|
if _, isTime := structField.Interface().(time.Time); isTime {
|
||||||
|
if err := setTimeField(inputValue[0], typeField, structField); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := setWithProperType(typeField.Type.Kind(), inputValue[0], structField); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
|
||||||
|
switch valueKind {
|
||||||
|
case reflect.Int:
|
||||||
|
return setIntField(val, 0, structField)
|
||||||
|
case reflect.Int8:
|
||||||
|
return setIntField(val, 8, structField)
|
||||||
|
case reflect.Int16:
|
||||||
|
return setIntField(val, 16, structField)
|
||||||
|
case reflect.Int32:
|
||||||
|
return setIntField(val, 32, structField)
|
||||||
|
case reflect.Int64:
|
||||||
|
return setIntField(val, 64, structField)
|
||||||
|
case reflect.Uint:
|
||||||
|
return setUintField(val, 0, structField)
|
||||||
|
case reflect.Uint8:
|
||||||
|
return setUintField(val, 8, structField)
|
||||||
|
case reflect.Uint16:
|
||||||
|
return setUintField(val, 16, structField)
|
||||||
|
case reflect.Uint32:
|
||||||
|
return setUintField(val, 32, structField)
|
||||||
|
case reflect.Uint64:
|
||||||
|
return setUintField(val, 64, structField)
|
||||||
|
case reflect.Bool:
|
||||||
|
return setBoolField(val, structField)
|
||||||
|
case reflect.Float32:
|
||||||
|
return setFloatField(val, 32, structField)
|
||||||
|
case reflect.Float64:
|
||||||
|
return setFloatField(val, 64, structField)
|
||||||
|
case reflect.String:
|
||||||
|
structField.SetString(val)
|
||||||
|
default:
|
||||||
|
return errors.New("Unknown type")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setIntField(val string, bitSize int, field reflect.Value) error {
|
||||||
|
if val == "" {
|
||||||
|
val = "0"
|
||||||
|
}
|
||||||
|
intVal, err := strconv.ParseInt(val, 10, bitSize)
|
||||||
|
if err == nil {
|
||||||
|
field.SetInt(intVal)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func setUintField(val string, bitSize int, field reflect.Value) error {
|
||||||
|
if val == "" {
|
||||||
|
val = "0"
|
||||||
|
}
|
||||||
|
uintVal, err := strconv.ParseUint(val, 10, bitSize)
|
||||||
|
if err == nil {
|
||||||
|
field.SetUint(uintVal)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func setBoolField(val string, field reflect.Value) error {
|
||||||
|
if val == "" {
|
||||||
|
val = "false"
|
||||||
|
}
|
||||||
|
boolVal, err := strconv.ParseBool(val)
|
||||||
|
if err == nil {
|
||||||
|
field.SetBool(boolVal)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setFloatField(val string, bitSize int, field reflect.Value) error {
|
||||||
|
if val == "" {
|
||||||
|
val = "0.0"
|
||||||
|
}
|
||||||
|
floatVal, err := strconv.ParseFloat(val, bitSize)
|
||||||
|
if err == nil {
|
||||||
|
field.SetFloat(floatVal)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func setTimeField(val string, structField reflect.StructField, value reflect.Value) error {
|
||||||
|
timeFormat := structField.Tag.Get("time_format")
|
||||||
|
if timeFormat == "" {
|
||||||
|
return errors.New("Blank time format")
|
||||||
|
}
|
||||||
|
|
||||||
|
if val == "" {
|
||||||
|
value.Set(reflect.ValueOf(time.Time{}))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
l := time.Local
|
||||||
|
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
|
||||||
|
l = time.UTC
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := time.ParseInLocation(timeFormat, val, l)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
value.Set(reflect.ValueOf(t))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't pass in pointers to bind to. Can lead to bugs. See:
|
||||||
|
// https://github.com/codegangsta/martini-contrib/issues/40
|
||||||
|
// https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
|
||||||
|
func ensureNotPointer(obj interface{}) {
|
||||||
|
if reflect.TypeOf(obj).Kind() == reflect.Ptr {
|
||||||
|
panic("Pointers are not accepted as binding models")
|
||||||
|
}
|
||||||
|
}
|
24
vendor/github.com/gin-gonic/gin/binding/json.go
generated
vendored
Normal file
24
vendor/github.com/gin-gonic/gin/binding/json.go
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type jsonBinding struct{}
|
||||||
|
|
||||||
|
func (jsonBinding) Name() string {
|
||||||
|
return "json"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
decoder := json.NewDecoder(req.Body)
|
||||||
|
if err := decoder.Decode(obj); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return validate(obj)
|
||||||
|
}
|
28
vendor/github.com/gin-gonic/gin/binding/msgpack.go
generated
vendored
Normal file
28
vendor/github.com/gin-gonic/gin/binding/msgpack.go
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/ugorji/go/codec"
|
||||||
|
)
|
||||||
|
|
||||||
|
type msgpackBinding struct{}
|
||||||
|
|
||||||
|
func (msgpackBinding) Name() string {
|
||||||
|
return "msgpack"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msgpackBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
|
||||||
|
if err := codec.NewDecoder(req.Body, new(codec.MsgpackHandle)).Decode(&obj); err != nil {
|
||||||
|
//var decoder *codec.Decoder = codec.NewDecoder(req.Body, &codec.MsgpackHandle)
|
||||||
|
//if err := decoder.Decode(&obj); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return validate(obj)
|
||||||
|
|
||||||
|
}
|
35
vendor/github.com/gin-gonic/gin/binding/protobuf.go
generated
vendored
Normal file
35
vendor/github.com/gin-gonic/gin/binding/protobuf.go
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/golang/protobuf/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
type protobufBinding struct{}
|
||||||
|
|
||||||
|
func (protobufBinding) Name() string {
|
||||||
|
return "protobuf"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (protobufBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
|
||||||
|
buf, err := ioutil.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = proto.Unmarshal(buf, obj.(proto.Message)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Here it's same to return validate(obj), but util now we cann't add `binding:""` to the struct
|
||||||
|
//which automatically generate by gen-proto
|
||||||
|
return nil
|
||||||
|
//return validate(obj)
|
||||||
|
}
|
24
vendor/github.com/gin-gonic/gin/binding/xml.go
generated
vendored
Normal file
24
vendor/github.com/gin-gonic/gin/binding/xml.go
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type xmlBinding struct{}
|
||||||
|
|
||||||
|
func (xmlBinding) Name() string {
|
||||||
|
return "xml"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (xmlBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
decoder := xml.NewDecoder(req.Body)
|
||||||
|
if err := decoder.Decode(obj); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return validate(obj)
|
||||||
|
}
|
5
vendor/github.com/gin-gonic/gin/codecov.yml
generated
vendored
Normal file
5
vendor/github.com/gin-gonic/gin/codecov.yml
generated
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
coverage:
|
||||||
|
notify:
|
||||||
|
gitter:
|
||||||
|
default:
|
||||||
|
url: https://webhooks.gitter.im/e/d90dcdeeab2f1e357165
|
766
vendor/github.com/gin-gonic/gin/context.go
generated
vendored
Normal file
766
vendor/github.com/gin-gonic/gin/context.go
generated
vendored
Normal file
@ -0,0 +1,766 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"math"
|
||||||
|
"mime/multipart"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/sse"
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
|
"github.com/gin-gonic/gin/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Content-Type MIME of the most common data formats
|
||||||
|
const (
|
||||||
|
MIMEJSON = binding.MIMEJSON
|
||||||
|
MIMEHTML = binding.MIMEHTML
|
||||||
|
MIMEXML = binding.MIMEXML
|
||||||
|
MIMEXML2 = binding.MIMEXML2
|
||||||
|
MIMEPlain = binding.MIMEPlain
|
||||||
|
MIMEPOSTForm = binding.MIMEPOSTForm
|
||||||
|
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultMemory = 32 << 20 // 32 MB
|
||||||
|
abortIndex int8 = math.MaxInt8 / 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
||||||
|
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
||||||
|
type Context struct {
|
||||||
|
writermem responseWriter
|
||||||
|
Request *http.Request
|
||||||
|
Writer ResponseWriter
|
||||||
|
|
||||||
|
Params Params
|
||||||
|
handlers HandlersChain
|
||||||
|
index int8
|
||||||
|
|
||||||
|
engine *Engine
|
||||||
|
Keys map[string]interface{}
|
||||||
|
Errors errorMsgs
|
||||||
|
Accepted []string
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/********** CONTEXT CREATION ********/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
func (c *Context) reset() {
|
||||||
|
c.Writer = &c.writermem
|
||||||
|
c.Params = c.Params[0:0]
|
||||||
|
c.handlers = nil
|
||||||
|
c.index = -1
|
||||||
|
c.Keys = nil
|
||||||
|
c.Errors = c.Errors[0:0]
|
||||||
|
c.Accepted = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy returns a copy of the current context that can be safely used outside the request's scope.
|
||||||
|
// This has to be used when the context has to be passed to a goroutine.
|
||||||
|
func (c *Context) Copy() *Context {
|
||||||
|
var cp = *c
|
||||||
|
cp.writermem.ResponseWriter = nil
|
||||||
|
cp.Writer = &cp.writermem
|
||||||
|
cp.index = abortIndex
|
||||||
|
cp.handlers = nil
|
||||||
|
return &cp
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()", this
|
||||||
|
// function will return "main.handleGetUsers"
|
||||||
|
func (c *Context) HandlerName() string {
|
||||||
|
return nameOfFunction(c.handlers.Last())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler returns the main handler.
|
||||||
|
func (c *Context) Handler() HandlerFunc {
|
||||||
|
return c.handlers.Last()
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/*********** FLOW CONTROL ***********/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
// Next should be used only inside middleware.
|
||||||
|
// It executes the pending handlers in the chain inside the calling handler.
|
||||||
|
// See example in GitHub.
|
||||||
|
func (c *Context) Next() {
|
||||||
|
c.index++
|
||||||
|
s := int8(len(c.handlers))
|
||||||
|
for ; c.index < s; c.index++ {
|
||||||
|
c.handlers[c.index](c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsAborted returns true if the current context was aborted.
|
||||||
|
func (c *Context) IsAborted() bool {
|
||||||
|
return c.index >= abortIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
// Abort prevents pending handlers from being called. Note that this will not stop the current handler.
|
||||||
|
// Let's say you have an authorization middleware that validates that the current request is authorized. If the
|
||||||
|
// authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers
|
||||||
|
// for this request are not called.
|
||||||
|
func (c *Context) Abort() {
|
||||||
|
c.index = abortIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
// AbortWithStatus calls `Abort()` and writes the headers with the specified status code.
|
||||||
|
// For example, a failed attempt to authenticate a request could use: context.AbortWithStatus(401).
|
||||||
|
func (c *Context) AbortWithStatus(code int) {
|
||||||
|
c.Status(code)
|
||||||
|
c.Writer.WriteHeaderNow()
|
||||||
|
c.Abort()
|
||||||
|
}
|
||||||
|
|
||||||
|
// AbortWithStatusJSON calls `Abort()` and then `JSON` internally. This method stops the chain, writes the status code and return a JSON body
|
||||||
|
// It also sets the Content-Type as "application/json".
|
||||||
|
func (c *Context) AbortWithStatusJSON(code int, jsonObj interface{}) {
|
||||||
|
c.Abort()
|
||||||
|
c.JSON(code, jsonObj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AbortWithError calls `AbortWithStatus()` and `Error()` internally. This method stops the chain, writes the status code and
|
||||||
|
// pushes the specified error to `c.Errors`.
|
||||||
|
// See Context.Error() for more details.
|
||||||
|
func (c *Context) AbortWithError(code int, err error) *Error {
|
||||||
|
c.AbortWithStatus(code)
|
||||||
|
return c.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/********* ERROR MANAGEMENT *********/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
// Attaches an error to the current context. The error is pushed to a list of errors.
|
||||||
|
// It's a good idea to call Error for each error that occurred during the resolution of a request.
|
||||||
|
// A middleware can be used to collect all the errors
|
||||||
|
// and push them to a database together, print a log, or append it in the HTTP response.
|
||||||
|
func (c *Context) Error(err error) *Error {
|
||||||
|
var parsedError *Error
|
||||||
|
switch err.(type) {
|
||||||
|
case *Error:
|
||||||
|
parsedError = err.(*Error)
|
||||||
|
default:
|
||||||
|
parsedError = &Error{
|
||||||
|
Err: err,
|
||||||
|
Type: ErrorTypePrivate,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.Errors = append(c.Errors, parsedError)
|
||||||
|
return parsedError
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/******** METADATA MANAGEMENT********/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
// Set is used to store a new key/value pair exclusively for this context.
|
||||||
|
// It also lazy initializes c.Keys if it was not used previously.
|
||||||
|
func (c *Context) Set(key string, value interface{}) {
|
||||||
|
if c.Keys == nil {
|
||||||
|
c.Keys = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
c.Keys[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the value for the given key, ie: (value, true).
|
||||||
|
// If the value does not exists it returns (nil, false)
|
||||||
|
func (c *Context) Get(key string) (value interface{}, exists bool) {
|
||||||
|
value, exists = c.Keys[key]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustGet returns the value for the given key if it exists, otherwise it panics.
|
||||||
|
func (c *Context) MustGet(key string) interface{} {
|
||||||
|
if value, exists := c.Get(key); exists {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
panic("Key \"" + key + "\" does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetString returns the value associated with the key as a string.
|
||||||
|
func (c *Context) GetString(key string) (s string) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
s, _ = val.(string)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBool returns the value associated with the key as a boolean.
|
||||||
|
func (c *Context) GetBool(key string) (b bool) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
b, _ = val.(bool)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt returns the value associated with the key as an integer.
|
||||||
|
func (c *Context) GetInt(key string) (i int) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
i, _ = val.(int)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt64 returns the value associated with the key as an integer.
|
||||||
|
func (c *Context) GetInt64(key string) (i64 int64) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
i64, _ = val.(int64)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFloat64 returns the value associated with the key as a float64.
|
||||||
|
func (c *Context) GetFloat64(key string) (f64 float64) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
f64, _ = val.(float64)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTime returns the value associated with the key as time.
|
||||||
|
func (c *Context) GetTime(key string) (t time.Time) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
t, _ = val.(time.Time)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDuration returns the value associated with the key as a duration.
|
||||||
|
func (c *Context) GetDuration(key string) (d time.Duration) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
d, _ = val.(time.Duration)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringSlice returns the value associated with the key as a slice of strings.
|
||||||
|
func (c *Context) GetStringSlice(key string) (ss []string) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
ss, _ = val.([]string)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringMap returns the value associated with the key as a map of interfaces.
|
||||||
|
func (c *Context) GetStringMap(key string) (sm map[string]interface{}) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
sm, _ = val.(map[string]interface{})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringMapString returns the value associated with the key as a map of strings.
|
||||||
|
func (c *Context) GetStringMapString(key string) (sms map[string]string) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
sms, _ = val.(map[string]string)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
|
||||||
|
func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
smss, _ = val.(map[string][]string)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/************ INPUT DATA ************/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
// Param returns the value of the URL param.
|
||||||
|
// It is a shortcut for c.Params.ByName(key)
|
||||||
|
// router.GET("/user/:id", func(c *gin.Context) {
|
||||||
|
// // a GET request to /user/john
|
||||||
|
// id := c.Param("id") // id == "john"
|
||||||
|
// })
|
||||||
|
func (c *Context) Param(key string) string {
|
||||||
|
return c.Params.ByName(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query returns the keyed url query value if it exists,
|
||||||
|
// otherwise it returns an empty string `("")`.
|
||||||
|
// It is shortcut for `c.Request.URL.Query().Get(key)`
|
||||||
|
// GET /path?id=1234&name=Manu&value=
|
||||||
|
// c.Query("id") == "1234"
|
||||||
|
// c.Query("name") == "Manu"
|
||||||
|
// c.Query("value") == ""
|
||||||
|
// c.Query("wtf") == ""
|
||||||
|
func (c *Context) Query(key string) string {
|
||||||
|
value, _ := c.GetQuery(key)
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultQuery returns the keyed url query value if it exists,
|
||||||
|
// otherwise it returns the specified defaultValue string.
|
||||||
|
// See: Query() and GetQuery() for further information.
|
||||||
|
// GET /?name=Manu&lastname=
|
||||||
|
// c.DefaultQuery("name", "unknown") == "Manu"
|
||||||
|
// c.DefaultQuery("id", "none") == "none"
|
||||||
|
// c.DefaultQuery("lastname", "none") == ""
|
||||||
|
func (c *Context) DefaultQuery(key, defaultValue string) string {
|
||||||
|
if value, ok := c.GetQuery(key); ok {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQuery is like Query(), it returns the keyed url query value
|
||||||
|
// if it exists `(value, true)` (even when the value is an empty string),
|
||||||
|
// otherwise it returns `("", false)`.
|
||||||
|
// It is shortcut for `c.Request.URL.Query().Get(key)`
|
||||||
|
// GET /?name=Manu&lastname=
|
||||||
|
// ("Manu", true) == c.GetQuery("name")
|
||||||
|
// ("", false) == c.GetQuery("id")
|
||||||
|
// ("", true) == c.GetQuery("lastname")
|
||||||
|
func (c *Context) GetQuery(key string) (string, bool) {
|
||||||
|
if values, ok := c.GetQueryArray(key); ok {
|
||||||
|
return values[0], ok
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryArray returns a slice of strings for a given query key.
|
||||||
|
// The length of the slice depends on the number of params with the given key.
|
||||||
|
func (c *Context) QueryArray(key string) []string {
|
||||||
|
values, _ := c.GetQueryArray(key)
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQueryArray returns a slice of strings for a given query key, plus
|
||||||
|
// a boolean value whether at least one value exists for the given key.
|
||||||
|
func (c *Context) GetQueryArray(key string) ([]string, bool) {
|
||||||
|
req := c.Request
|
||||||
|
if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
|
||||||
|
return values, true
|
||||||
|
}
|
||||||
|
return []string{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostForm returns the specified key from a POST urlencoded form or multipart form
|
||||||
|
// when it exists, otherwise it returns an empty string `("")`.
|
||||||
|
func (c *Context) PostForm(key string) string {
|
||||||
|
value, _ := c.GetPostForm(key)
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultPostForm returns the specified key from a POST urlencoded form or multipart form
|
||||||
|
// when it exists, otherwise it returns the specified defaultValue string.
|
||||||
|
// See: PostForm() and GetPostForm() for further information.
|
||||||
|
func (c *Context) DefaultPostForm(key, defaultValue string) string {
|
||||||
|
if value, ok := c.GetPostForm(key); ok {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPostForm is like PostForm(key). It returns the specified key from a POST urlencoded
|
||||||
|
// form or multipart form when it exists `(value, true)` (even when the value is an empty string),
|
||||||
|
// otherwise it returns ("", false).
|
||||||
|
// For example, during a PATCH request to update the user's email:
|
||||||
|
// email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"
|
||||||
|
// email= --> ("", true) := GetPostForm("email") // set email to ""
|
||||||
|
// --> ("", false) := GetPostForm("email") // do nothing with email
|
||||||
|
func (c *Context) GetPostForm(key string) (string, bool) {
|
||||||
|
if values, ok := c.GetPostFormArray(key); ok {
|
||||||
|
return values[0], ok
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostFormArray returns a slice of strings for a given form key.
|
||||||
|
// The length of the slice depends on the number of params with the given key.
|
||||||
|
func (c *Context) PostFormArray(key string) []string {
|
||||||
|
values, _ := c.GetPostFormArray(key)
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPostFormArray returns a slice of strings for a given form key, plus
|
||||||
|
// a boolean value whether at least one value exists for the given key.
|
||||||
|
func (c *Context) GetPostFormArray(key string) ([]string, bool) {
|
||||||
|
req := c.Request
|
||||||
|
req.ParseForm()
|
||||||
|
req.ParseMultipartForm(defaultMemory)
|
||||||
|
if values := req.PostForm[key]; len(values) > 0 {
|
||||||
|
return values, true
|
||||||
|
}
|
||||||
|
if req.MultipartForm != nil && req.MultipartForm.File != nil {
|
||||||
|
if values := req.MultipartForm.Value[key]; len(values) > 0 {
|
||||||
|
return values, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []string{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormFile returns the first file for the provided form key.
|
||||||
|
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
|
||||||
|
_, fh, err := c.Request.FormFile(name)
|
||||||
|
return fh, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MultipartForm is the parsed multipart form, including file uploads.
|
||||||
|
func (c *Context) MultipartForm() (*multipart.Form, error) {
|
||||||
|
err := c.Request.ParseMultipartForm(defaultMemory)
|
||||||
|
return c.Request.MultipartForm, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind checks the Content-Type to select a binding engine automatically,
|
||||||
|
// Depending the "Content-Type" header different bindings are used:
|
||||||
|
// "application/json" --> JSON binding
|
||||||
|
// "application/xml" --> XML binding
|
||||||
|
// otherwise --> returns an error
|
||||||
|
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
|
||||||
|
// It decodes the json payload into the struct specified as a pointer.
|
||||||
|
// Like ParseBody() but this method also writes a 400 error if the json is not valid.
|
||||||
|
func (c *Context) Bind(obj interface{}) error {
|
||||||
|
b := binding.Default(c.Request.Method, c.ContentType())
|
||||||
|
return c.MustBindWith(obj, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON)
|
||||||
|
func (c *Context) BindJSON(obj interface{}) error {
|
||||||
|
return c.MustBindWith(obj, binding.JSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustBindWith binds the passed struct pointer using the specified binding
|
||||||
|
// engine. It will abort the request with HTTP 400 if any error ocurrs.
|
||||||
|
// See the binding package.
|
||||||
|
func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {
|
||||||
|
if err = c.ShouldBindWith(obj, b); err != nil {
|
||||||
|
c.AbortWithError(400, err).SetType(ErrorTypeBind)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ShouldBindWith binds the passed struct pointer using the specified binding
|
||||||
|
// engine.
|
||||||
|
// See the binding package.
|
||||||
|
func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error {
|
||||||
|
return b.Bind(c.Request, obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClientIP implements a best effort algorithm to return the real client IP, it parses
|
||||||
|
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
||||||
|
// Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
|
||||||
|
func (c *Context) ClientIP() string {
|
||||||
|
if c.engine.ForwardedByClientIP {
|
||||||
|
clientIP := c.requestHeader("X-Forwarded-For")
|
||||||
|
if index := strings.IndexByte(clientIP, ','); index >= 0 {
|
||||||
|
clientIP = clientIP[0:index]
|
||||||
|
}
|
||||||
|
clientIP = strings.TrimSpace(clientIP)
|
||||||
|
if len(clientIP) > 0 {
|
||||||
|
return clientIP
|
||||||
|
}
|
||||||
|
clientIP = strings.TrimSpace(c.requestHeader("X-Real-Ip"))
|
||||||
|
if len(clientIP) > 0 {
|
||||||
|
return clientIP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.engine.AppEngine {
|
||||||
|
if addr := c.Request.Header.Get("X-Appengine-Remote-Addr"); addr != "" {
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr)); err == nil {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContentType returns the Content-Type header of the request.
|
||||||
|
func (c *Context) ContentType() string {
|
||||||
|
return filterFlags(c.requestHeader("Content-Type"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsWebsocket returns true if the request headers indicate that a websocket
|
||||||
|
// handshake is being initiated by the client.
|
||||||
|
func (c *Context) IsWebsocket() bool {
|
||||||
|
if strings.Contains(strings.ToLower(c.requestHeader("Connection")), "upgrade") &&
|
||||||
|
strings.ToLower(c.requestHeader("Upgrade")) == "websocket" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) requestHeader(key string) string {
|
||||||
|
if values, _ := c.Request.Header[key]; len(values) > 0 {
|
||||||
|
return values[0]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/******** RESPONSE RENDERING ********/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
// bodyAllowedForStatus is a copy of http.bodyAllowedForStatus non-exported function
|
||||||
|
func bodyAllowedForStatus(status int) bool {
|
||||||
|
switch {
|
||||||
|
case status >= 100 && status <= 199:
|
||||||
|
return false
|
||||||
|
case status == 204:
|
||||||
|
return false
|
||||||
|
case status == 304:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Status(code int) {
|
||||||
|
c.writermem.WriteHeader(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header is a intelligent shortcut for c.Writer.Header().Set(key, value)
|
||||||
|
// It writes a header in the response.
|
||||||
|
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
|
||||||
|
func (c *Context) Header(key, value string) {
|
||||||
|
if len(value) == 0 {
|
||||||
|
c.Writer.Header().Del(key)
|
||||||
|
} else {
|
||||||
|
c.Writer.Header().Set(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHeader returns value from request headers
|
||||||
|
func (c *Context) GetHeader(key string) string {
|
||||||
|
return c.requestHeader(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRawData return stream data
|
||||||
|
func (c *Context) GetRawData() ([]byte, error) {
|
||||||
|
return ioutil.ReadAll(c.Request.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) SetCookie(
|
||||||
|
name string,
|
||||||
|
value string,
|
||||||
|
maxAge int,
|
||||||
|
path string,
|
||||||
|
domain string,
|
||||||
|
secure bool,
|
||||||
|
httpOnly bool,
|
||||||
|
) {
|
||||||
|
if path == "" {
|
||||||
|
path = "/"
|
||||||
|
}
|
||||||
|
http.SetCookie(c.Writer, &http.Cookie{
|
||||||
|
Name: name,
|
||||||
|
Value: url.QueryEscape(value),
|
||||||
|
MaxAge: maxAge,
|
||||||
|
Path: path,
|
||||||
|
Domain: domain,
|
||||||
|
Secure: secure,
|
||||||
|
HttpOnly: httpOnly,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Cookie(name string) (string, error) {
|
||||||
|
cookie, err := c.Request.Cookie(name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
val, _ := url.QueryUnescape(cookie.Value)
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Render(code int, r render.Render) {
|
||||||
|
c.Status(code)
|
||||||
|
|
||||||
|
if !bodyAllowedForStatus(code) {
|
||||||
|
r.WriteContentType(c.Writer)
|
||||||
|
c.Writer.WriteHeaderNow()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.Render(c.Writer); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTML renders the HTTP template specified by its file name.
|
||||||
|
// It also updates the HTTP code and sets the Content-Type as "text/html".
|
||||||
|
// See http://golang.org/doc/articles/wiki/
|
||||||
|
func (c *Context) HTML(code int, name string, obj interface{}) {
|
||||||
|
instance := c.engine.HTMLRender.Instance(name, obj)
|
||||||
|
c.Render(code, instance)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IndentedJSON serializes the given struct as pretty JSON (indented + endlines) into the response body.
|
||||||
|
// It also sets the Content-Type as "application/json".
|
||||||
|
// WARNING: we recommend to use this only for development purposes since printing pretty JSON is
|
||||||
|
// more CPU and bandwidth consuming. Use Context.JSON() instead.
|
||||||
|
func (c *Context) IndentedJSON(code int, obj interface{}) {
|
||||||
|
c.Render(code, render.IndentedJSON{Data: obj})
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSON serializes the given struct as JSON into the response body.
|
||||||
|
// It also sets the Content-Type as "application/json".
|
||||||
|
func (c *Context) JSON(code int, obj interface{}) {
|
||||||
|
c.Render(code, render.JSON{Data: obj})
|
||||||
|
}
|
||||||
|
|
||||||
|
// XML serializes the given struct as XML into the response body.
|
||||||
|
// It also sets the Content-Type as "application/xml".
|
||||||
|
func (c *Context) XML(code int, obj interface{}) {
|
||||||
|
c.Render(code, render.XML{Data: obj})
|
||||||
|
}
|
||||||
|
|
||||||
|
// YAML serializes the given struct as YAML into the response body.
|
||||||
|
func (c *Context) YAML(code int, obj interface{}) {
|
||||||
|
c.Render(code, render.YAML{Data: obj})
|
||||||
|
}
|
||||||
|
|
||||||
|
// String writes the given string into the response body.
|
||||||
|
func (c *Context) String(code int, format string, values ...interface{}) {
|
||||||
|
c.Render(code, render.String{Format: format, Data: values})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redirect returns a HTTP redirect to the specific location.
|
||||||
|
func (c *Context) Redirect(code int, location string) {
|
||||||
|
c.Render(-1, render.Redirect{
|
||||||
|
Code: code,
|
||||||
|
Location: location,
|
||||||
|
Request: c.Request,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data writes some data into the body stream and updates the HTTP code.
|
||||||
|
func (c *Context) Data(code int, contentType string, data []byte) {
|
||||||
|
c.Render(code, render.Data{
|
||||||
|
ContentType: contentType,
|
||||||
|
Data: data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// File writes the specified file into the body stream in a efficient way.
|
||||||
|
func (c *Context) File(filepath string) {
|
||||||
|
http.ServeFile(c.Writer, c.Request, filepath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSEvent writes a Server-Sent Event into the body stream.
|
||||||
|
func (c *Context) SSEvent(name string, message interface{}) {
|
||||||
|
c.Render(-1, sse.Event{
|
||||||
|
Event: name,
|
||||||
|
Data: message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Stream(step func(w io.Writer) bool) {
|
||||||
|
w := c.Writer
|
||||||
|
clientGone := w.CloseNotify()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-clientGone:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
keepOpen := step(w)
|
||||||
|
w.Flush()
|
||||||
|
if !keepOpen {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/******** CONTENT NEGOTIATION *******/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
type Negotiate struct {
|
||||||
|
Offered []string
|
||||||
|
HTMLName string
|
||||||
|
HTMLData interface{}
|
||||||
|
JSONData interface{}
|
||||||
|
XMLData interface{}
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Negotiate(code int, config Negotiate) {
|
||||||
|
switch c.NegotiateFormat(config.Offered...) {
|
||||||
|
case binding.MIMEJSON:
|
||||||
|
data := chooseData(config.JSONData, config.Data)
|
||||||
|
c.JSON(code, data)
|
||||||
|
|
||||||
|
case binding.MIMEHTML:
|
||||||
|
data := chooseData(config.HTMLData, config.Data)
|
||||||
|
c.HTML(code, config.HTMLName, data)
|
||||||
|
|
||||||
|
case binding.MIMEXML:
|
||||||
|
data := chooseData(config.XMLData, config.Data)
|
||||||
|
c.XML(code, data)
|
||||||
|
|
||||||
|
default:
|
||||||
|
c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) NegotiateFormat(offered ...string) string {
|
||||||
|
assert1(len(offered) > 0, "you must provide at least one offer")
|
||||||
|
|
||||||
|
if c.Accepted == nil {
|
||||||
|
c.Accepted = parseAccept(c.requestHeader("Accept"))
|
||||||
|
}
|
||||||
|
if len(c.Accepted) == 0 {
|
||||||
|
return offered[0]
|
||||||
|
}
|
||||||
|
for _, accepted := range c.Accepted {
|
||||||
|
for _, offert := range offered {
|
||||||
|
if accepted == offert {
|
||||||
|
return offert
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) SetAccepted(formats ...string) {
|
||||||
|
c.Accepted = formats
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/***** GOLANG.ORG/X/NET/CONTEXT *****/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
func (c *Context) Deadline() (deadline time.Time, ok bool) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Done() <-chan struct{} {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Err() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Value(key interface{}) interface{} {
|
||||||
|
if key == 0 {
|
||||||
|
return c.Request
|
||||||
|
}
|
||||||
|
if keyAsString, ok := key.(string); ok {
|
||||||
|
val, _ := c.Get(keyAsString)
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
11
vendor/github.com/gin-gonic/gin/context_appengine.go
generated
vendored
Normal file
11
vendor/github.com/gin-gonic/gin/context_appengine.go
generated
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// +build appengine
|
||||||
|
|
||||||
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
defaultAppEngine = true
|
||||||
|
}
|
71
vendor/github.com/gin-gonic/gin/debug.go
generated
vendored
Normal file
71
vendor/github.com/gin-gonic/gin/debug.go
generated
vendored
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetFlags(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsDebugging returns true if the framework is running in debug mode.
|
||||||
|
// Use SetMode(gin.Release) to switch to disable the debug mode.
|
||||||
|
func IsDebugging() bool {
|
||||||
|
return ginMode == debugCode
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||||
|
if IsDebugging() {
|
||||||
|
nuHandlers := len(handlers)
|
||||||
|
handlerName := nameOfFunction(handlers.Last())
|
||||||
|
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugPrintLoadTemplate(tmpl *template.Template) {
|
||||||
|
if IsDebugging() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
for _, tmpl := range tmpl.Templates() {
|
||||||
|
buf.WriteString("\t- ")
|
||||||
|
buf.WriteString(tmpl.Name())
|
||||||
|
buf.WriteString("\n")
|
||||||
|
}
|
||||||
|
debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugPrint(format string, values ...interface{}) {
|
||||||
|
if IsDebugging() {
|
||||||
|
log.Printf("[GIN-debug] "+format, values...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugPrintWARNINGNew() {
|
||||||
|
debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
|
||||||
|
- using env: export GIN_MODE=release
|
||||||
|
- using code: gin.SetMode(gin.ReleaseMode)
|
||||||
|
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugPrintWARNINGSetHTMLTemplate() {
|
||||||
|
debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
|
||||||
|
at initialization. ie. before any route is registered or the router is listening in a socket:
|
||||||
|
|
||||||
|
router := gin.Default()
|
||||||
|
router.SetHTMLTemplate(template) // << good place
|
||||||
|
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugPrintError(err error) {
|
||||||
|
if err != nil {
|
||||||
|
debugPrint("[ERROR] %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
25
vendor/github.com/gin-gonic/gin/deprecated.go
generated
vendored
Normal file
25
vendor/github.com/gin-gonic/gin/deprecated.go
generated
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Context) GetCookie(name string) (string, error) {
|
||||||
|
log.Println("GetCookie() method is deprecated. Use Cookie() instead.")
|
||||||
|
return c.Cookie(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BindWith binds the passed struct pointer using the specified binding engine.
|
||||||
|
// See the binding package.
|
||||||
|
func (c *Context) BindWith(obj interface{}, b binding.Binding) error {
|
||||||
|
log.Println(`BindWith(\"interface{}, binding.Binding\") error is going to
|
||||||
|
be deprecated, please check issue #662 and either use MustBindWith() if you
|
||||||
|
want HTTP 400 to be automatically returned if any error occur, of use
|
||||||
|
ShouldBindWith() if you need to manage the error.`)
|
||||||
|
return c.MustBindWith(obj, b)
|
||||||
|
}
|
159
vendor/github.com/gin-gonic/gin/errors.go
generated
vendored
Normal file
159
vendor/github.com/gin-gonic/gin/errors.go
generated
vendored
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ErrorType uint64
|
||||||
|
|
||||||
|
const (
|
||||||
|
ErrorTypeBind ErrorType = 1 << 63 // used when c.Bind() fails
|
||||||
|
ErrorTypeRender ErrorType = 1 << 62 // used when c.Render() fails
|
||||||
|
ErrorTypePrivate ErrorType = 1 << 0
|
||||||
|
ErrorTypePublic ErrorType = 1 << 1
|
||||||
|
|
||||||
|
ErrorTypeAny ErrorType = 1<<64 - 1
|
||||||
|
ErrorTypeNu = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Error struct {
|
||||||
|
Err error
|
||||||
|
Type ErrorType
|
||||||
|
Meta interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
errorMsgs []*Error
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ error = &Error{}
|
||||||
|
|
||||||
|
func (msg *Error) SetType(flags ErrorType) *Error {
|
||||||
|
msg.Type = flags
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *Error) SetMeta(data interface{}) *Error {
|
||||||
|
msg.Meta = data
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *Error) JSON() interface{} {
|
||||||
|
json := H{}
|
||||||
|
if msg.Meta != nil {
|
||||||
|
value := reflect.ValueOf(msg.Meta)
|
||||||
|
switch value.Kind() {
|
||||||
|
case reflect.Struct:
|
||||||
|
return msg.Meta
|
||||||
|
case reflect.Map:
|
||||||
|
for _, key := range value.MapKeys() {
|
||||||
|
json[key.String()] = value.MapIndex(key).Interface()
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
json["meta"] = msg.Meta
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, ok := json["error"]; !ok {
|
||||||
|
json["error"] = msg.Error()
|
||||||
|
}
|
||||||
|
return json
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements the json.Marshaller interface
|
||||||
|
func (msg *Error) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(msg.JSON())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements the error interface
|
||||||
|
func (msg Error) Error() string {
|
||||||
|
return msg.Err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *Error) IsType(flags ErrorType) bool {
|
||||||
|
return (msg.Type & flags) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a readonly copy filtered the byte.
|
||||||
|
// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic
|
||||||
|
func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
|
||||||
|
if len(a) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if typ == ErrorTypeAny {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
var result errorMsgs
|
||||||
|
for _, msg := range a {
|
||||||
|
if msg.IsType(typ) {
|
||||||
|
result = append(result, msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the last error in the slice. It returns nil if the array is empty.
|
||||||
|
// Shortcut for errors[len(errors)-1]
|
||||||
|
func (a errorMsgs) Last() *Error {
|
||||||
|
length := len(a)
|
||||||
|
if length > 0 {
|
||||||
|
return a[length-1]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns an array will all the error messages.
|
||||||
|
// Example:
|
||||||
|
// c.Error(errors.New("first"))
|
||||||
|
// c.Error(errors.New("second"))
|
||||||
|
// c.Error(errors.New("third"))
|
||||||
|
// c.Errors.Errors() // == []string{"first", "second", "third"}
|
||||||
|
func (a errorMsgs) Errors() []string {
|
||||||
|
if len(a) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
errorStrings := make([]string, len(a))
|
||||||
|
for i, err := range a {
|
||||||
|
errorStrings[i] = err.Error()
|
||||||
|
}
|
||||||
|
return errorStrings
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a errorMsgs) JSON() interface{} {
|
||||||
|
switch len(a) {
|
||||||
|
case 0:
|
||||||
|
return nil
|
||||||
|
case 1:
|
||||||
|
return a.Last().JSON()
|
||||||
|
default:
|
||||||
|
json := make([]interface{}, len(a))
|
||||||
|
for i, err := range a {
|
||||||
|
json[i] = err.JSON()
|
||||||
|
}
|
||||||
|
return json
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a errorMsgs) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(a.JSON())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a errorMsgs) String() string {
|
||||||
|
if len(a) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
for i, msg := range a {
|
||||||
|
fmt.Fprintf(&buffer, "Error #%02d: %s\n", (i + 1), msg.Err)
|
||||||
|
if msg.Meta != nil {
|
||||||
|
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buffer.String()
|
||||||
|
}
|
46
vendor/github.com/gin-gonic/gin/fs.go
generated
vendored
Normal file
46
vendor/github.com/gin-gonic/gin/fs.go
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
onlyfilesFS struct {
|
||||||
|
fs http.FileSystem
|
||||||
|
}
|
||||||
|
neuteredReaddirFile struct {
|
||||||
|
http.File
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Dir returns a http.Filesystem that can be used by http.FileServer(). It is used internally
|
||||||
|
// in router.Static().
|
||||||
|
// if listDirectory == true, then it works the same as http.Dir() otherwise it returns
|
||||||
|
// a filesystem that prevents http.FileServer() to list the directory files.
|
||||||
|
func Dir(root string, listDirectory bool) http.FileSystem {
|
||||||
|
fs := http.Dir(root)
|
||||||
|
if listDirectory {
|
||||||
|
return fs
|
||||||
|
}
|
||||||
|
return &onlyfilesFS{fs}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conforms to http.Filesystem
|
||||||
|
func (fs onlyfilesFS) Open(name string) (http.File, error) {
|
||||||
|
f, err := fs.fs.Open(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return neuteredReaddirFile{f}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overrides the http.File default implementation
|
||||||
|
func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
|
||||||
|
// this disables directory listing
|
||||||
|
return nil, nil
|
||||||
|
}
|
418
vendor/github.com/gin-gonic/gin/gin.go
generated
vendored
Normal file
418
vendor/github.com/gin-gonic/gin/gin.go
generated
vendored
Normal file
@ -0,0 +1,418 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Version is Framework's version
|
||||||
|
const Version = "v1.2"
|
||||||
|
|
||||||
|
var default404Body = []byte("404 page not found")
|
||||||
|
var default405Body = []byte("405 method not allowed")
|
||||||
|
var defaultAppEngine bool
|
||||||
|
|
||||||
|
type HandlerFunc func(*Context)
|
||||||
|
type HandlersChain []HandlerFunc
|
||||||
|
|
||||||
|
// Last returns the last handler in the chain. ie. the last handler is the main own.
|
||||||
|
func (c HandlersChain) Last() HandlerFunc {
|
||||||
|
length := len(c)
|
||||||
|
if length > 0 {
|
||||||
|
return c[length-1]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type (
|
||||||
|
RoutesInfo []RouteInfo
|
||||||
|
RouteInfo struct {
|
||||||
|
Method string
|
||||||
|
Path string
|
||||||
|
Handler string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
|
||||||
|
// Create an instance of Engine, by using New() or Default()
|
||||||
|
Engine struct {
|
||||||
|
RouterGroup
|
||||||
|
delims render.Delims
|
||||||
|
HTMLRender render.HTMLRender
|
||||||
|
FuncMap template.FuncMap
|
||||||
|
allNoRoute HandlersChain
|
||||||
|
allNoMethod HandlersChain
|
||||||
|
noRoute HandlersChain
|
||||||
|
noMethod HandlersChain
|
||||||
|
pool sync.Pool
|
||||||
|
trees methodTrees
|
||||||
|
|
||||||
|
// Enables automatic redirection if the current route can't be matched but a
|
||||||
|
// handler for the path with (without) the trailing slash exists.
|
||||||
|
// For example if /foo/ is requested but a route only exists for /foo, the
|
||||||
|
// client is redirected to /foo with http status code 301 for GET requests
|
||||||
|
// and 307 for all other request methods.
|
||||||
|
RedirectTrailingSlash bool
|
||||||
|
|
||||||
|
// If enabled, the router tries to fix the current request path, if no
|
||||||
|
// handle is registered for it.
|
||||||
|
// First superfluous path elements like ../ or // are removed.
|
||||||
|
// Afterwards the router does a case-insensitive lookup of the cleaned path.
|
||||||
|
// If a handle can be found for this route, the router makes a redirection
|
||||||
|
// to the corrected path with status code 301 for GET requests and 307 for
|
||||||
|
// all other request methods.
|
||||||
|
// For example /FOO and /..//Foo could be redirected to /foo.
|
||||||
|
// RedirectTrailingSlash is independent of this option.
|
||||||
|
RedirectFixedPath bool
|
||||||
|
|
||||||
|
// If enabled, the router checks if another method is allowed for the
|
||||||
|
// current route, if the current request can not be routed.
|
||||||
|
// If this is the case, the request is answered with 'Method Not Allowed'
|
||||||
|
// and HTTP status code 405.
|
||||||
|
// If no other Method is allowed, the request is delegated to the NotFound
|
||||||
|
// handler.
|
||||||
|
HandleMethodNotAllowed bool
|
||||||
|
ForwardedByClientIP bool
|
||||||
|
|
||||||
|
// #726 #755 If enabled, it will thrust some headers starting with
|
||||||
|
// 'X-AppEngine...' for better integration with that PaaS.
|
||||||
|
AppEngine bool
|
||||||
|
|
||||||
|
// If enabled, the url.RawPath will be used to find parameters.
|
||||||
|
UseRawPath bool
|
||||||
|
// If true, the path value will be unescaped.
|
||||||
|
// If UseRawPath is false (by default), the UnescapePathValues effectively is true,
|
||||||
|
// as url.Path gonna be used, which is already unescaped.
|
||||||
|
UnescapePathValues bool
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ IRouter = &Engine{}
|
||||||
|
|
||||||
|
// New returns a new blank Engine instance without any middleware attached.
|
||||||
|
// By default the configuration is:
|
||||||
|
// - RedirectTrailingSlash: true
|
||||||
|
// - RedirectFixedPath: false
|
||||||
|
// - HandleMethodNotAllowed: false
|
||||||
|
// - ForwardedByClientIP: true
|
||||||
|
// - UseRawPath: false
|
||||||
|
// - UnescapePathValues: true
|
||||||
|
func New() *Engine {
|
||||||
|
debugPrintWARNINGNew()
|
||||||
|
engine := &Engine{
|
||||||
|
RouterGroup: RouterGroup{
|
||||||
|
Handlers: nil,
|
||||||
|
basePath: "/",
|
||||||
|
root: true,
|
||||||
|
},
|
||||||
|
FuncMap: template.FuncMap{},
|
||||||
|
RedirectTrailingSlash: true,
|
||||||
|
RedirectFixedPath: false,
|
||||||
|
HandleMethodNotAllowed: false,
|
||||||
|
ForwardedByClientIP: true,
|
||||||
|
AppEngine: defaultAppEngine,
|
||||||
|
UseRawPath: false,
|
||||||
|
UnescapePathValues: true,
|
||||||
|
trees: make(methodTrees, 0, 9),
|
||||||
|
delims: render.Delims{"{{", "}}"},
|
||||||
|
}
|
||||||
|
engine.RouterGroup.engine = engine
|
||||||
|
engine.pool.New = func() interface{} {
|
||||||
|
return engine.allocateContext()
|
||||||
|
}
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default returns an Engine instance with the Logger and Recovery middleware already attached.
|
||||||
|
func Default() *Engine {
|
||||||
|
engine := New()
|
||||||
|
engine.Use(Logger(), Recovery())
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) allocateContext() *Context {
|
||||||
|
return &Context{engine: engine}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) Delims(left, right string) *Engine {
|
||||||
|
engine.delims = render.Delims{left, right}
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
||||||
|
if IsDebugging() {
|
||||||
|
debugPrintLoadTemplate(template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseGlob(pattern)))
|
||||||
|
engine.HTMLRender = render.HTMLDebug{Glob: pattern, FuncMap: engine.FuncMap, Delims: engine.delims}
|
||||||
|
} else {
|
||||||
|
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseGlob(pattern))
|
||||||
|
engine.SetHTMLTemplate(templ)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
||||||
|
if IsDebugging() {
|
||||||
|
engine.HTMLRender = render.HTMLDebug{Files: files, FuncMap: engine.FuncMap, Delims: engine.delims}
|
||||||
|
} else {
|
||||||
|
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseFiles(files...))
|
||||||
|
engine.SetHTMLTemplate(templ)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
|
||||||
|
if len(engine.trees) > 0 {
|
||||||
|
debugPrintWARNINGSetHTMLTemplate()
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.HTMLRender = render.HTMLProduction{Template: templ.Funcs(engine.FuncMap)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) SetFuncMap(funcMap template.FuncMap) {
|
||||||
|
engine.FuncMap = funcMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// NoRoute adds handlers for NoRoute. It return a 404 code by default.
|
||||||
|
func (engine *Engine) NoRoute(handlers ...HandlerFunc) {
|
||||||
|
engine.noRoute = handlers
|
||||||
|
engine.rebuild404Handlers()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NoMethod sets the handlers called when... TODO
|
||||||
|
func (engine *Engine) NoMethod(handlers ...HandlerFunc) {
|
||||||
|
engine.noMethod = handlers
|
||||||
|
engine.rebuild405Handlers()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use attachs a global middleware to the router. ie. the middleware attached though Use() will be
|
||||||
|
// included in the handlers chain for every single request. Even 404, 405, static files...
|
||||||
|
// For example, this is the right place for a logger or error management middleware.
|
||||||
|
func (engine *Engine) Use(middleware ...HandlerFunc) IRoutes {
|
||||||
|
engine.RouterGroup.Use(middleware...)
|
||||||
|
engine.rebuild404Handlers()
|
||||||
|
engine.rebuild405Handlers()
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) rebuild404Handlers() {
|
||||||
|
engine.allNoRoute = engine.combineHandlers(engine.noRoute)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) rebuild405Handlers() {
|
||||||
|
engine.allNoMethod = engine.combineHandlers(engine.noMethod)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
||||||
|
assert1(path[0] == '/', "path must begin with '/'")
|
||||||
|
assert1(len(method) > 0, "HTTP method can not be empty")
|
||||||
|
assert1(len(handlers) > 0, "there must be at least one handler")
|
||||||
|
|
||||||
|
debugPrintRoute(method, path, handlers)
|
||||||
|
root := engine.trees.get(method)
|
||||||
|
if root == nil {
|
||||||
|
root = new(node)
|
||||||
|
engine.trees = append(engine.trees, methodTree{method: method, root: root})
|
||||||
|
}
|
||||||
|
root.addRoute(path, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Routes returns a slice of registered routes, including some useful information, such as:
|
||||||
|
// the http method, path and the handler name.
|
||||||
|
func (engine *Engine) Routes() (routes RoutesInfo) {
|
||||||
|
for _, tree := range engine.trees {
|
||||||
|
routes = iterate("", tree.method, routes, tree.root)
|
||||||
|
}
|
||||||
|
return routes
|
||||||
|
}
|
||||||
|
|
||||||
|
func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo {
|
||||||
|
path += root.path
|
||||||
|
if len(root.handlers) > 0 {
|
||||||
|
routes = append(routes, RouteInfo{
|
||||||
|
Method: method,
|
||||||
|
Path: path,
|
||||||
|
Handler: nameOfFunction(root.handlers.Last()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for _, child := range root.children {
|
||||||
|
routes = iterate(path, method, routes, child)
|
||||||
|
}
|
||||||
|
return routes
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run attaches the router to a http.Server and starts listening and serving HTTP requests.
|
||||||
|
// It is a shortcut for http.ListenAndServe(addr, router)
|
||||||
|
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||||
|
func (engine *Engine) Run(addr ...string) (err error) {
|
||||||
|
defer func() { debugPrintError(err) }()
|
||||||
|
|
||||||
|
address := resolveAddress(addr)
|
||||||
|
debugPrint("Listening and serving HTTP on %s\n", address)
|
||||||
|
err = http.ListenAndServe(address, engine)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
|
||||||
|
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
||||||
|
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||||
|
func (engine *Engine) RunTLS(addr string, certFile string, keyFile string) (err error) {
|
||||||
|
debugPrint("Listening and serving HTTPS on %s\n", addr)
|
||||||
|
defer func() { debugPrintError(err) }()
|
||||||
|
|
||||||
|
err = http.ListenAndServeTLS(addr, certFile, keyFile, engine)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests
|
||||||
|
// through the specified unix socket (ie. a file).
|
||||||
|
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||||
|
func (engine *Engine) RunUnix(file string) (err error) {
|
||||||
|
debugPrint("Listening and serving HTTP on unix:/%s", file)
|
||||||
|
defer func() { debugPrintError(err) }()
|
||||||
|
|
||||||
|
os.Remove(file)
|
||||||
|
listener, err := net.Listen("unix", file)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
err = http.Serve(listener, engine)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conforms to the http.Handler interface.
|
||||||
|
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
c := engine.pool.Get().(*Context)
|
||||||
|
c.writermem.reset(w)
|
||||||
|
c.Request = req
|
||||||
|
c.reset()
|
||||||
|
|
||||||
|
engine.handleHTTPRequest(c)
|
||||||
|
|
||||||
|
engine.pool.Put(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-enter a context that has been rewritten.
|
||||||
|
// This can be done by setting c.Request.Path to your new target.
|
||||||
|
// Disclaimer: You can loop yourself to death with this, use wisely.
|
||||||
|
func (engine *Engine) HandleContext(c *Context) {
|
||||||
|
c.reset()
|
||||||
|
engine.handleHTTPRequest(c)
|
||||||
|
engine.pool.Put(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) handleHTTPRequest(context *Context) {
|
||||||
|
httpMethod := context.Request.Method
|
||||||
|
var path string
|
||||||
|
var unescape bool
|
||||||
|
if engine.UseRawPath && len(context.Request.URL.RawPath) > 0 {
|
||||||
|
path = context.Request.URL.RawPath
|
||||||
|
unescape = engine.UnescapePathValues
|
||||||
|
} else {
|
||||||
|
path = context.Request.URL.Path
|
||||||
|
unescape = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find root of the tree for the given HTTP method
|
||||||
|
t := engine.trees
|
||||||
|
for i, tl := 0, len(t); i < tl; i++ {
|
||||||
|
if t[i].method == httpMethod {
|
||||||
|
root := t[i].root
|
||||||
|
// Find route in tree
|
||||||
|
handlers, params, tsr := root.getValue(path, context.Params, unescape)
|
||||||
|
if handlers != nil {
|
||||||
|
context.handlers = handlers
|
||||||
|
context.Params = params
|
||||||
|
context.Next()
|
||||||
|
context.writermem.WriteHeaderNow()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if httpMethod != "CONNECT" && path != "/" {
|
||||||
|
if tsr && engine.RedirectTrailingSlash {
|
||||||
|
redirectTrailingSlash(context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if engine.RedirectFixedPath && redirectFixedPath(context, root, engine.RedirectFixedPath) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: unit test
|
||||||
|
if engine.HandleMethodNotAllowed {
|
||||||
|
for _, tree := range engine.trees {
|
||||||
|
if tree.method != httpMethod {
|
||||||
|
if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
|
||||||
|
context.handlers = engine.allNoMethod
|
||||||
|
serveError(context, 405, default405Body)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
context.handlers = engine.allNoRoute
|
||||||
|
serveError(context, 404, default404Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
var mimePlain = []string{MIMEPlain}
|
||||||
|
|
||||||
|
func serveError(c *Context, code int, defaultMessage []byte) {
|
||||||
|
c.writermem.status = code
|
||||||
|
c.Next()
|
||||||
|
if !c.writermem.Written() {
|
||||||
|
if c.writermem.Status() == code {
|
||||||
|
c.writermem.Header()["Content-Type"] = mimePlain
|
||||||
|
c.Writer.Write(defaultMessage)
|
||||||
|
} else {
|
||||||
|
c.writermem.WriteHeaderNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func redirectTrailingSlash(c *Context) {
|
||||||
|
req := c.Request
|
||||||
|
path := req.URL.Path
|
||||||
|
code := 301 // Permanent redirect, request with GET method
|
||||||
|
if req.Method != "GET" {
|
||||||
|
code = 307
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(path) > 1 && path[len(path)-1] == '/' {
|
||||||
|
req.URL.Path = path[:len(path)-1]
|
||||||
|
} else {
|
||||||
|
req.URL.Path = path + "/"
|
||||||
|
}
|
||||||
|
debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())
|
||||||
|
http.Redirect(c.Writer, req, req.URL.String(), code)
|
||||||
|
c.writermem.WriteHeaderNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool {
|
||||||
|
req := c.Request
|
||||||
|
path := req.URL.Path
|
||||||
|
|
||||||
|
fixedPath, found := root.findCaseInsensitivePath(
|
||||||
|
cleanPath(path),
|
||||||
|
trailingSlash,
|
||||||
|
)
|
||||||
|
if found {
|
||||||
|
code := 301 // Permanent redirect, request with GET method
|
||||||
|
if req.Method != "GET" {
|
||||||
|
code = 307
|
||||||
|
}
|
||||||
|
req.URL.Path = string(fixedPath)
|
||||||
|
debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())
|
||||||
|
http.Redirect(c.Writer, req, req.URL.String(), code)
|
||||||
|
c.writermem.WriteHeaderNow()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
142
vendor/github.com/gin-gonic/gin/logger.go
generated
vendored
Normal file
142
vendor/github.com/gin-gonic/gin/logger.go
generated
vendored
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
|
||||||
|
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
|
||||||
|
yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
|
||||||
|
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
|
||||||
|
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
|
||||||
|
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
|
||||||
|
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
||||||
|
reset = string([]byte{27, 91, 48, 109})
|
||||||
|
disableColor = false
|
||||||
|
)
|
||||||
|
|
||||||
|
func DisableConsoleColor() {
|
||||||
|
disableColor = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ErrorLogger() HandlerFunc {
|
||||||
|
return ErrorLoggerT(ErrorTypeAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ErrorLoggerT(typ ErrorType) HandlerFunc {
|
||||||
|
return func(c *Context) {
|
||||||
|
c.Next()
|
||||||
|
errors := c.Errors.ByType(typ)
|
||||||
|
if len(errors) > 0 {
|
||||||
|
c.JSON(-1, errors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logger instances a Logger middleware that will write the logs to gin.DefaultWriter
|
||||||
|
// By default gin.DefaultWriter = os.Stdout
|
||||||
|
func Logger() HandlerFunc {
|
||||||
|
return LoggerWithWriter(DefaultWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoggerWithWriter instance a Logger middleware with the specified writter buffer.
|
||||||
|
// Example: os.Stdout, a file opened in write mode, a socket...
|
||||||
|
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||||
|
isTerm := true
|
||||||
|
|
||||||
|
if w, ok := out.(*os.File); !ok ||
|
||||||
|
(os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()))) ||
|
||||||
|
disableColor {
|
||||||
|
isTerm = false
|
||||||
|
}
|
||||||
|
|
||||||
|
var skip map[string]struct{}
|
||||||
|
|
||||||
|
if length := len(notlogged); length > 0 {
|
||||||
|
skip = make(map[string]struct{}, length)
|
||||||
|
|
||||||
|
for _, path := range notlogged {
|
||||||
|
skip[path] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(c *Context) {
|
||||||
|
// Start timer
|
||||||
|
start := time.Now()
|
||||||
|
path := c.Request.URL.Path
|
||||||
|
|
||||||
|
// Process request
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
// Log only when path is not being skipped
|
||||||
|
if _, ok := skip[path]; !ok {
|
||||||
|
// Stop timer
|
||||||
|
end := time.Now()
|
||||||
|
latency := end.Sub(start)
|
||||||
|
|
||||||
|
clientIP := c.ClientIP()
|
||||||
|
method := c.Request.Method
|
||||||
|
statusCode := c.Writer.Status()
|
||||||
|
var statusColor, methodColor string
|
||||||
|
if isTerm {
|
||||||
|
statusColor = colorForStatus(statusCode)
|
||||||
|
methodColor = colorForMethod(method)
|
||||||
|
}
|
||||||
|
comment := c.Errors.ByType(ErrorTypePrivate).String()
|
||||||
|
|
||||||
|
fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %15s |%s %s %-7s %s\n%s",
|
||||||
|
end.Format("2006/01/02 - 15:04:05"),
|
||||||
|
statusColor, statusCode, reset,
|
||||||
|
latency,
|
||||||
|
clientIP,
|
||||||
|
methodColor, method, reset,
|
||||||
|
path,
|
||||||
|
comment,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func colorForStatus(code int) string {
|
||||||
|
switch {
|
||||||
|
case code >= 200 && code < 300:
|
||||||
|
return green
|
||||||
|
case code >= 300 && code < 400:
|
||||||
|
return white
|
||||||
|
case code >= 400 && code < 500:
|
||||||
|
return yellow
|
||||||
|
default:
|
||||||
|
return red
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func colorForMethod(method string) string {
|
||||||
|
switch method {
|
||||||
|
case "GET":
|
||||||
|
return blue
|
||||||
|
case "POST":
|
||||||
|
return cyan
|
||||||
|
case "PUT":
|
||||||
|
return yellow
|
||||||
|
case "DELETE":
|
||||||
|
return red
|
||||||
|
case "PATCH":
|
||||||
|
return green
|
||||||
|
case "HEAD":
|
||||||
|
return magenta
|
||||||
|
case "OPTIONS":
|
||||||
|
return white
|
||||||
|
default:
|
||||||
|
return reset
|
||||||
|
}
|
||||||
|
}
|
BIN
vendor/github.com/gin-gonic/gin/logo.jpg
generated
vendored
Normal file
BIN
vendor/github.com/gin-gonic/gin/logo.jpg
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
69
vendor/github.com/gin-gonic/gin/mode.go
generated
vendored
Normal file
69
vendor/github.com/gin-gonic/gin/mode.go
generated
vendored
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ENV_GIN_MODE = "GIN_MODE"
|
||||||
|
|
||||||
|
const (
|
||||||
|
DebugMode string = "debug"
|
||||||
|
ReleaseMode string = "release"
|
||||||
|
TestMode string = "test"
|
||||||
|
)
|
||||||
|
const (
|
||||||
|
debugCode = iota
|
||||||
|
releaseCode
|
||||||
|
testCode
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultWriter is the default io.Writer used the Gin for debug output and
|
||||||
|
// middleware output like Logger() or Recovery().
|
||||||
|
// Note that both Logger and Recovery provides custom ways to configure their
|
||||||
|
// output io.Writer.
|
||||||
|
// To support coloring in Windows use:
|
||||||
|
// import "github.com/mattn/go-colorable"
|
||||||
|
// gin.DefaultWriter = colorable.NewColorableStdout()
|
||||||
|
var DefaultWriter io.Writer = os.Stdout
|
||||||
|
var DefaultErrorWriter io.Writer = os.Stderr
|
||||||
|
|
||||||
|
var ginMode = debugCode
|
||||||
|
var modeName = DebugMode
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
mode := os.Getenv(ENV_GIN_MODE)
|
||||||
|
if len(mode) == 0 {
|
||||||
|
SetMode(DebugMode)
|
||||||
|
} else {
|
||||||
|
SetMode(mode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMode(value string) {
|
||||||
|
switch value {
|
||||||
|
case DebugMode:
|
||||||
|
ginMode = debugCode
|
||||||
|
case ReleaseMode:
|
||||||
|
ginMode = releaseCode
|
||||||
|
case TestMode:
|
||||||
|
ginMode = testCode
|
||||||
|
default:
|
||||||
|
panic("gin mode unknown: " + value)
|
||||||
|
}
|
||||||
|
modeName = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func DisableBindValidation() {
|
||||||
|
binding.Validator = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Mode() string {
|
||||||
|
return modeName
|
||||||
|
}
|
123
vendor/github.com/gin-gonic/gin/path.go
generated
vendored
Normal file
123
vendor/github.com/gin-gonic/gin/path.go
generated
vendored
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
// Copyright 2013 Julien Schmidt. All rights reserved.
|
||||||
|
// Based on the path package, Copyright 2009 The Go Authors.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// at https://github.com/julienschmidt/httprouter/blob/master/LICENSE.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
// CleanPath is the URL version of path.Clean, it returns a canonical URL path
|
||||||
|
// for p, eliminating . and .. elements.
|
||||||
|
//
|
||||||
|
// The following rules are applied iteratively until no further processing can
|
||||||
|
// be done:
|
||||||
|
// 1. Replace multiple slashes with a single slash.
|
||||||
|
// 2. Eliminate each . path name element (the current directory).
|
||||||
|
// 3. Eliminate each inner .. path name element (the parent directory)
|
||||||
|
// along with the non-.. element that precedes it.
|
||||||
|
// 4. Eliminate .. elements that begin a rooted path:
|
||||||
|
// that is, replace "/.." by "/" at the beginning of a path.
|
||||||
|
//
|
||||||
|
// If the result of this process is an empty string, "/" is returned
|
||||||
|
func cleanPath(p string) string {
|
||||||
|
// Turn empty string into "/"
|
||||||
|
if p == "" {
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
n := len(p)
|
||||||
|
var buf []byte
|
||||||
|
|
||||||
|
// Invariants:
|
||||||
|
// reading from path; r is index of next byte to process.
|
||||||
|
// writing to buf; w is index of next byte to write.
|
||||||
|
|
||||||
|
// path must start with '/'
|
||||||
|
r := 1
|
||||||
|
w := 1
|
||||||
|
|
||||||
|
if p[0] != '/' {
|
||||||
|
r = 0
|
||||||
|
buf = make([]byte, n+1)
|
||||||
|
buf[0] = '/'
|
||||||
|
}
|
||||||
|
|
||||||
|
trailing := n > 2 && p[n-1] == '/'
|
||||||
|
|
||||||
|
// A bit more clunky without a 'lazybuf' like the path package, but the loop
|
||||||
|
// gets completely inlined (bufApp). So in contrast to the path package this
|
||||||
|
// loop has no expensive function calls (except 1x make)
|
||||||
|
|
||||||
|
for r < n {
|
||||||
|
switch {
|
||||||
|
case p[r] == '/':
|
||||||
|
// empty path element, trailing slash is added after the end
|
||||||
|
r++
|
||||||
|
|
||||||
|
case p[r] == '.' && r+1 == n:
|
||||||
|
trailing = true
|
||||||
|
r++
|
||||||
|
|
||||||
|
case p[r] == '.' && p[r+1] == '/':
|
||||||
|
// . element
|
||||||
|
r++
|
||||||
|
|
||||||
|
case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
|
||||||
|
// .. element: remove to last /
|
||||||
|
r += 2
|
||||||
|
|
||||||
|
if w > 1 {
|
||||||
|
// can backtrack
|
||||||
|
w--
|
||||||
|
|
||||||
|
if buf == nil {
|
||||||
|
for w > 1 && p[w] != '/' {
|
||||||
|
w--
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for w > 1 && buf[w] != '/' {
|
||||||
|
w--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
// real path element.
|
||||||
|
// add slash if needed
|
||||||
|
if w > 1 {
|
||||||
|
bufApp(&buf, p, w, '/')
|
||||||
|
w++
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy element
|
||||||
|
for r < n && p[r] != '/' {
|
||||||
|
bufApp(&buf, p, w, p[r])
|
||||||
|
w++
|
||||||
|
r++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// re-append trailing slash
|
||||||
|
if trailing && w > 1 {
|
||||||
|
bufApp(&buf, p, w, '/')
|
||||||
|
w++
|
||||||
|
}
|
||||||
|
|
||||||
|
if buf == nil {
|
||||||
|
return p[:w]
|
||||||
|
}
|
||||||
|
return string(buf[:w])
|
||||||
|
}
|
||||||
|
|
||||||
|
// internal helper to lazily create a buffer if necessary
|
||||||
|
func bufApp(buf *[]byte, s string, w int, c byte) {
|
||||||
|
if *buf == nil {
|
||||||
|
if s[w] == c {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
*buf = make([]byte, len(s))
|
||||||
|
copy(*buf, s[:w])
|
||||||
|
}
|
||||||
|
(*buf)[w] = c
|
||||||
|
}
|
108
vendor/github.com/gin-gonic/gin/recovery.go
generated
vendored
Normal file
108
vendor/github.com/gin-gonic/gin/recovery.go
generated
vendored
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http/httputil"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
dunno = []byte("???")
|
||||||
|
centerDot = []byte("·")
|
||||||
|
dot = []byte(".")
|
||||||
|
slash = []byte("/")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
|
||||||
|
func Recovery() HandlerFunc {
|
||||||
|
return RecoveryWithWriter(DefaultErrorWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RecoveryWithWriter(out io.Writer) HandlerFunc {
|
||||||
|
var logger *log.Logger
|
||||||
|
if out != nil {
|
||||||
|
logger = log.New(out, "\n\n\x1b[31m", log.LstdFlags)
|
||||||
|
}
|
||||||
|
return func(c *Context) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
if logger != nil {
|
||||||
|
stack := stack(3)
|
||||||
|
httprequest, _ := httputil.DumpRequest(c.Request, false)
|
||||||
|
logger.Printf("[Recovery] panic recovered:\n%s\n%s\n%s%s", string(httprequest), err, stack, reset)
|
||||||
|
}
|
||||||
|
c.AbortWithStatus(500)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
23
vendor/github.com/gin-gonic/gin/render/data.go
generated
vendored
Normal file
23
vendor/github.com/gin-gonic/gin/render/data.go
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
ContentType string
|
||||||
|
Data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render (Data) writes data with custom ContentType
|
||||||
|
func (r Data) Render(w http.ResponseWriter) (err error) {
|
||||||
|
r.WriteContentType(w)
|
||||||
|
_, err = w.Write(r.Data)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Data) WriteContentType(w http.ResponseWriter) {
|
||||||
|
writeContentType(w, []string{r.ContentType})
|
||||||
|
}
|
82
vendor/github.com/gin-gonic/gin/render/html.go
generated
vendored
Normal file
82
vendor/github.com/gin-gonic/gin/render/html.go
generated
vendored
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Delims struct {
|
||||||
|
Left string
|
||||||
|
Right string
|
||||||
|
}
|
||||||
|
|
||||||
|
HTMLRender interface {
|
||||||
|
Instance(string, interface{}) Render
|
||||||
|
}
|
||||||
|
|
||||||
|
HTMLProduction struct {
|
||||||
|
Template *template.Template
|
||||||
|
Delims Delims
|
||||||
|
}
|
||||||
|
|
||||||
|
HTMLDebug struct {
|
||||||
|
Files []string
|
||||||
|
Glob string
|
||||||
|
Delims Delims
|
||||||
|
FuncMap template.FuncMap
|
||||||
|
}
|
||||||
|
|
||||||
|
HTML struct {
|
||||||
|
Template *template.Template
|
||||||
|
Name string
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var htmlContentType = []string{"text/html; charset=utf-8"}
|
||||||
|
|
||||||
|
func (r HTMLProduction) Instance(name string, data interface{}) Render {
|
||||||
|
return HTML{
|
||||||
|
Template: r.Template,
|
||||||
|
Name: name,
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r HTMLDebug) Instance(name string, data interface{}) Render {
|
||||||
|
return HTML{
|
||||||
|
Template: r.loadTemplate(),
|
||||||
|
Name: name,
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (r HTMLDebug) loadTemplate() *template.Template {
|
||||||
|
if r.FuncMap == nil {
|
||||||
|
r.FuncMap = template.FuncMap{}
|
||||||
|
}
|
||||||
|
if len(r.Files) > 0 {
|
||||||
|
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
|
||||||
|
}
|
||||||
|
if len(r.Glob) > 0 {
|
||||||
|
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
|
||||||
|
}
|
||||||
|
panic("the HTML debug render was created without files or glob pattern")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r HTML) Render(w http.ResponseWriter) error {
|
||||||
|
r.WriteContentType(w)
|
||||||
|
|
||||||
|
if len(r.Name) == 0 {
|
||||||
|
return r.Template.Execute(w, r.Data)
|
||||||
|
}
|
||||||
|
return r.Template.ExecuteTemplate(w, r.Name, r.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r HTML) WriteContentType(w http.ResponseWriter) {
|
||||||
|
writeContentType(w, htmlContentType)
|
||||||
|
}
|
57
vendor/github.com/gin-gonic/gin/render/json.go
generated
vendored
Normal file
57
vendor/github.com/gin-gonic/gin/render/json.go
generated
vendored
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
JSON struct {
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
IndentedJSON struct {
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var jsonContentType = []string{"application/json; charset=utf-8"}
|
||||||
|
|
||||||
|
func (r JSON) Render(w http.ResponseWriter) (err error) {
|
||||||
|
if err = WriteJSON(w, r.Data); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r JSON) WriteContentType(w http.ResponseWriter) {
|
||||||
|
writeContentType(w, jsonContentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
||||||
|
writeContentType(w, jsonContentType)
|
||||||
|
jsonBytes, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Write(jsonBytes)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
||||||
|
r.WriteContentType(w)
|
||||||
|
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Write(jsonBytes)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {
|
||||||
|
writeContentType(w, jsonContentType)
|
||||||
|
}
|
31
vendor/github.com/gin-gonic/gin/render/msgpack.go
generated
vendored
Normal file
31
vendor/github.com/gin-gonic/gin/render/msgpack.go
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/ugorji/go/codec"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MsgPack struct {
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
|
||||||
|
|
||||||
|
func (r MsgPack) WriteContentType(w http.ResponseWriter) {
|
||||||
|
writeContentType(w, msgpackContentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r MsgPack) Render(w http.ResponseWriter) error {
|
||||||
|
return WriteMsgPack(w, r.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteMsgPack(w http.ResponseWriter, obj interface{}) error {
|
||||||
|
writeContentType(w, msgpackContentType)
|
||||||
|
var h codec.Handle = new(codec.MsgpackHandle)
|
||||||
|
return codec.NewEncoder(w, h).Encode(obj)
|
||||||
|
}
|
26
vendor/github.com/gin-gonic/gin/render/redirect.go
generated
vendored
Normal file
26
vendor/github.com/gin-gonic/gin/render/redirect.go
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Redirect struct {
|
||||||
|
Code int
|
||||||
|
Request *http.Request
|
||||||
|
Location string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Redirect) Render(w http.ResponseWriter) error {
|
||||||
|
if (r.Code < 300 || r.Code > 308) && r.Code != 201 {
|
||||||
|
panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
|
||||||
|
}
|
||||||
|
http.Redirect(w, r.Request, r.Location, r.Code)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Redirect) WriteContentType(http.ResponseWriter) {}
|
34
vendor/github.com/gin-gonic/gin/render/render.go
generated
vendored
Normal file
34
vendor/github.com/gin-gonic/gin/render/render.go
generated
vendored
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type Render interface {
|
||||||
|
Render(http.ResponseWriter) error
|
||||||
|
WriteContentType(w http.ResponseWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ Render = JSON{}
|
||||||
|
_ Render = IndentedJSON{}
|
||||||
|
_ Render = XML{}
|
||||||
|
_ Render = String{}
|
||||||
|
_ Render = Redirect{}
|
||||||
|
_ Render = Data{}
|
||||||
|
_ Render = HTML{}
|
||||||
|
_ HTMLRender = HTMLDebug{}
|
||||||
|
_ HTMLRender = HTMLProduction{}
|
||||||
|
_ Render = YAML{}
|
||||||
|
_ Render = MsgPack{}
|
||||||
|
_ Render = MsgPack{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func writeContentType(w http.ResponseWriter, value []string) {
|
||||||
|
header := w.Header()
|
||||||
|
if val := header["Content-Type"]; len(val) == 0 {
|
||||||
|
header["Content-Type"] = value
|
||||||
|
}
|
||||||
|
}
|
36
vendor/github.com/gin-gonic/gin/render/text.go
generated
vendored
Normal file
36
vendor/github.com/gin-gonic/gin/render/text.go
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type String struct {
|
||||||
|
Format string
|
||||||
|
Data []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var plainContentType = []string{"text/plain; charset=utf-8"}
|
||||||
|
|
||||||
|
func (r String) Render(w http.ResponseWriter) error {
|
||||||
|
WriteString(w, r.Format, r.Data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r String) WriteContentType(w http.ResponseWriter) {
|
||||||
|
writeContentType(w, plainContentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteString(w http.ResponseWriter, format string, data []interface{}) {
|
||||||
|
writeContentType(w, plainContentType)
|
||||||
|
if len(data) > 0 {
|
||||||
|
fmt.Fprintf(w, format, data...)
|
||||||
|
} else {
|
||||||
|
io.WriteString(w, format)
|
||||||
|
}
|
||||||
|
}
|
25
vendor/github.com/gin-gonic/gin/render/xml.go
generated
vendored
Normal file
25
vendor/github.com/gin-gonic/gin/render/xml.go
generated
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type XML struct {
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var xmlContentType = []string{"application/xml; charset=utf-8"}
|
||||||
|
|
||||||
|
func (r XML) Render(w http.ResponseWriter) error {
|
||||||
|
r.WriteContentType(w)
|
||||||
|
return xml.NewEncoder(w).Encode(r.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r XML) WriteContentType(w http.ResponseWriter) {
|
||||||
|
writeContentType(w, xmlContentType)
|
||||||
|
}
|
33
vendor/github.com/gin-gonic/gin/render/yaml.go
generated
vendored
Normal file
33
vendor/github.com/gin-gonic/gin/render/yaml.go
generated
vendored
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type YAML struct {
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var yamlContentType = []string{"application/x-yaml; charset=utf-8"}
|
||||||
|
|
||||||
|
func (r YAML) Render(w http.ResponseWriter) error {
|
||||||
|
r.WriteContentType(w)
|
||||||
|
|
||||||
|
bytes, err := yaml.Marshal(r.Data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write(bytes)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r YAML) WriteContentType(w http.ResponseWriter) {
|
||||||
|
writeContentType(w, yamlContentType)
|
||||||
|
}
|
116
vendor/github.com/gin-gonic/gin/response_writer.go
generated
vendored
Normal file
116
vendor/github.com/gin-gonic/gin/response_writer.go
generated
vendored
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
noWritten = -1
|
||||||
|
defaultStatus = 200
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
ResponseWriter interface {
|
||||||
|
http.ResponseWriter
|
||||||
|
http.Hijacker
|
||||||
|
http.Flusher
|
||||||
|
http.CloseNotifier
|
||||||
|
|
||||||
|
// Returns the HTTP response status code of the current request.
|
||||||
|
Status() int
|
||||||
|
|
||||||
|
// Returns the number of bytes already written into the response http body.
|
||||||
|
// See Written()
|
||||||
|
Size() int
|
||||||
|
|
||||||
|
// Writes the string into the response body.
|
||||||
|
WriteString(string) (int, error)
|
||||||
|
|
||||||
|
// Returns true if the response body was already written.
|
||||||
|
Written() bool
|
||||||
|
|
||||||
|
// Forces to write the http header (status code + headers).
|
||||||
|
WriteHeaderNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
responseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
size int
|
||||||
|
status int
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ ResponseWriter = &responseWriter{}
|
||||||
|
|
||||||
|
func (w *responseWriter) reset(writer http.ResponseWriter) {
|
||||||
|
w.ResponseWriter = writer
|
||||||
|
w.size = noWritten
|
||||||
|
w.status = defaultStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriter) WriteHeader(code int) {
|
||||||
|
if code > 0 && w.status != code {
|
||||||
|
if w.Written() {
|
||||||
|
debugPrint("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.status, code)
|
||||||
|
}
|
||||||
|
w.status = code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriter) WriteHeaderNow() {
|
||||||
|
if !w.Written() {
|
||||||
|
w.size = 0
|
||||||
|
w.ResponseWriter.WriteHeader(w.status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriter) Write(data []byte) (n int, err error) {
|
||||||
|
w.WriteHeaderNow()
|
||||||
|
n, err = w.ResponseWriter.Write(data)
|
||||||
|
w.size += n
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriter) WriteString(s string) (n int, err error) {
|
||||||
|
w.WriteHeaderNow()
|
||||||
|
n, err = io.WriteString(w.ResponseWriter, s)
|
||||||
|
w.size += n
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriter) Status() int {
|
||||||
|
return w.status
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriter) Size() int {
|
||||||
|
return w.size
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriter) Written() bool {
|
||||||
|
return w.size != noWritten
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements the http.Hijacker interface
|
||||||
|
func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||||
|
if w.size < 0 {
|
||||||
|
w.size = 0
|
||||||
|
}
|
||||||
|
return w.ResponseWriter.(http.Hijacker).Hijack()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements the http.CloseNotify interface
|
||||||
|
func (w *responseWriter) CloseNotify() <-chan bool {
|
||||||
|
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements the http.Flush interface
|
||||||
|
func (w *responseWriter) Flush() {
|
||||||
|
w.ResponseWriter.(http.Flusher).Flush()
|
||||||
|
}
|
215
vendor/github.com/gin-gonic/gin/routergroup.go
generated
vendored
Normal file
215
vendor/github.com/gin-gonic/gin/routergroup.go
generated
vendored
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
IRouter interface {
|
||||||
|
IRoutes
|
||||||
|
Group(string, ...HandlerFunc) *RouterGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
IRoutes interface {
|
||||||
|
Use(...HandlerFunc) IRoutes
|
||||||
|
|
||||||
|
Handle(string, string, ...HandlerFunc) IRoutes
|
||||||
|
Any(string, ...HandlerFunc) IRoutes
|
||||||
|
GET(string, ...HandlerFunc) IRoutes
|
||||||
|
POST(string, ...HandlerFunc) IRoutes
|
||||||
|
DELETE(string, ...HandlerFunc) IRoutes
|
||||||
|
PATCH(string, ...HandlerFunc) IRoutes
|
||||||
|
PUT(string, ...HandlerFunc) IRoutes
|
||||||
|
OPTIONS(string, ...HandlerFunc) IRoutes
|
||||||
|
HEAD(string, ...HandlerFunc) IRoutes
|
||||||
|
|
||||||
|
StaticFile(string, string) IRoutes
|
||||||
|
Static(string, string) IRoutes
|
||||||
|
StaticFS(string, http.FileSystem) IRoutes
|
||||||
|
}
|
||||||
|
|
||||||
|
// RouterGroup is used internally to configure router, a RouterGroup is associated with a prefix
|
||||||
|
// and an array of handlers (middleware)
|
||||||
|
RouterGroup struct {
|
||||||
|
Handlers HandlersChain
|
||||||
|
basePath string
|
||||||
|
engine *Engine
|
||||||
|
root bool
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ IRouter = &RouterGroup{}
|
||||||
|
|
||||||
|
// Use adds middleware to the group, see example code in github.
|
||||||
|
func (group *RouterGroup) Use(middleware ...HandlerFunc) IRoutes {
|
||||||
|
group.Handlers = append(group.Handlers, middleware...)
|
||||||
|
return group.returnObj()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
||||||
|
// For example, all the routes that use a common middlware for authorization could be grouped.
|
||||||
|
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
|
||||||
|
return &RouterGroup{
|
||||||
|
Handlers: group.combineHandlers(handlers),
|
||||||
|
basePath: group.calculateAbsolutePath(relativePath),
|
||||||
|
engine: group.engine,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (group *RouterGroup) BasePath() string {
|
||||||
|
return group.basePath
|
||||||
|
}
|
||||||
|
|
||||||
|
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) IRoutes {
|
||||||
|
absolutePath := group.calculateAbsolutePath(relativePath)
|
||||||
|
handlers = group.combineHandlers(handlers)
|
||||||
|
group.engine.addRoute(httpMethod, absolutePath, handlers)
|
||||||
|
return group.returnObj()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle registers a new request handle and middleware with the given path and method.
|
||||||
|
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
|
||||||
|
// See the example code in github.
|
||||||
|
//
|
||||||
|
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
|
||||||
|
// functions can be used.
|
||||||
|
//
|
||||||
|
// This function is intended for bulk loading and to allow the usage of less
|
||||||
|
// frequently used, non-standardized or custom methods (e.g. for internal
|
||||||
|
// communication with a proxy).
|
||||||
|
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
if matches, err := regexp.MatchString("^[A-Z]+$", httpMethod); !matches || err != nil {
|
||||||
|
panic("http method " + httpMethod + " is not valid")
|
||||||
|
}
|
||||||
|
return group.handle(httpMethod, relativePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST is a shortcut for router.Handle("POST", path, handle)
|
||||||
|
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
return group.handle("POST", relativePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET is a shortcut for router.Handle("GET", path, handle)
|
||||||
|
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
return group.handle("GET", relativePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
|
||||||
|
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
return group.handle("DELETE", relativePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
|
||||||
|
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
return group.handle("PATCH", relativePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT is a shortcut for router.Handle("PUT", path, handle)
|
||||||
|
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
return group.handle("PUT", relativePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
|
||||||
|
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
return group.handle("OPTIONS", relativePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
|
||||||
|
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
return group.handle("HEAD", relativePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any registers a route that matches all the HTTP methods.
|
||||||
|
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
|
||||||
|
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {
|
||||||
|
group.handle("GET", relativePath, handlers)
|
||||||
|
group.handle("POST", relativePath, handlers)
|
||||||
|
group.handle("PUT", relativePath, handlers)
|
||||||
|
group.handle("PATCH", relativePath, handlers)
|
||||||
|
group.handle("HEAD", relativePath, handlers)
|
||||||
|
group.handle("OPTIONS", relativePath, handlers)
|
||||||
|
group.handle("DELETE", relativePath, handlers)
|
||||||
|
group.handle("CONNECT", relativePath, handlers)
|
||||||
|
group.handle("TRACE", relativePath, handlers)
|
||||||
|
return group.returnObj()
|
||||||
|
}
|
||||||
|
|
||||||
|
// StaticFile registers a single route in order to server a single file of the local filesystem.
|
||||||
|
// router.StaticFile("favicon.ico", "./resources/favicon.ico")
|
||||||
|
func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes {
|
||||||
|
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
|
||||||
|
panic("URL parameters can not be used when serving a static file")
|
||||||
|
}
|
||||||
|
handler := func(c *Context) {
|
||||||
|
c.File(filepath)
|
||||||
|
}
|
||||||
|
group.GET(relativePath, handler)
|
||||||
|
group.HEAD(relativePath, handler)
|
||||||
|
return group.returnObj()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Static serves files from the given file system root.
|
||||||
|
// Internally a http.FileServer is used, therefore http.NotFound is used instead
|
||||||
|
// of the Router's NotFound handler.
|
||||||
|
// To use the operating system's file system implementation,
|
||||||
|
// use :
|
||||||
|
// router.Static("/static", "/var/www")
|
||||||
|
func (group *RouterGroup) Static(relativePath, root string) IRoutes {
|
||||||
|
return group.StaticFS(relativePath, Dir(root, false))
|
||||||
|
}
|
||||||
|
|
||||||
|
// StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead.
|
||||||
|
// Gin by default user: gin.Dir()
|
||||||
|
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes {
|
||||||
|
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
|
||||||
|
panic("URL parameters can not be used when serving a static folder")
|
||||||
|
}
|
||||||
|
handler := group.createStaticHandler(relativePath, fs)
|
||||||
|
urlPattern := path.Join(relativePath, "/*filepath")
|
||||||
|
|
||||||
|
// Register GET and HEAD handlers
|
||||||
|
group.GET(urlPattern, handler)
|
||||||
|
group.HEAD(urlPattern, handler)
|
||||||
|
return group.returnObj()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
|
||||||
|
absolutePath := group.calculateAbsolutePath(relativePath)
|
||||||
|
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
|
||||||
|
_, nolisting := fs.(*onlyfilesFS)
|
||||||
|
return func(c *Context) {
|
||||||
|
if nolisting {
|
||||||
|
c.Writer.WriteHeader(404)
|
||||||
|
}
|
||||||
|
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
|
||||||
|
finalSize := len(group.Handlers) + len(handlers)
|
||||||
|
if finalSize >= int(abortIndex) {
|
||||||
|
panic("too many handlers")
|
||||||
|
}
|
||||||
|
mergedHandlers := make(HandlersChain, finalSize)
|
||||||
|
copy(mergedHandlers, group.Handlers)
|
||||||
|
copy(mergedHandlers[len(group.Handlers):], handlers)
|
||||||
|
return mergedHandlers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
|
||||||
|
return joinPaths(group.basePath, relativePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (group *RouterGroup) returnObj() IRoutes {
|
||||||
|
if group.root {
|
||||||
|
return group.engine
|
||||||
|
}
|
||||||
|
return group
|
||||||
|
}
|
17
vendor/github.com/gin-gonic/gin/test_helpers.go
generated
vendored
Normal file
17
vendor/github.com/gin-gonic/gin/test_helpers.go
generated
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine) {
|
||||||
|
r = New()
|
||||||
|
c = r.allocateContext()
|
||||||
|
c.reset()
|
||||||
|
c.writermem.reset(w)
|
||||||
|
return
|
||||||
|
}
|
622
vendor/github.com/gin-gonic/gin/tree.go
generated
vendored
Normal file
622
vendor/github.com/gin-gonic/gin/tree.go
generated
vendored
Normal file
@ -0,0 +1,622 @@
|
|||||||
|
// Copyright 2013 Julien Schmidt. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// at https://github.com/julienschmidt/httprouter/blob/master/LICENSE
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Param is a single URL parameter, consisting of a key and a value.
|
||||||
|
type Param struct {
|
||||||
|
Key string
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Params is a Param-slice, as returned by the router.
|
||||||
|
// The slice is ordered, the first URL parameter is also the first slice value.
|
||||||
|
// It is therefore safe to read values by the index.
|
||||||
|
type Params []Param
|
||||||
|
|
||||||
|
// Get returns the value of the first Param which key matches the given name.
|
||||||
|
// If no matching Param is found, an empty string is returned.
|
||||||
|
func (ps Params) Get(name string) (string, bool) {
|
||||||
|
for _, entry := range ps {
|
||||||
|
if entry.Key == name {
|
||||||
|
return entry.Value, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ByName returns the value of the first Param which key matches the given name.
|
||||||
|
// If no matching Param is found, an empty string is returned.
|
||||||
|
func (ps Params) ByName(name string) (va string) {
|
||||||
|
va, _ = ps.Get(name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type methodTree struct {
|
||||||
|
method string
|
||||||
|
root *node
|
||||||
|
}
|
||||||
|
|
||||||
|
type methodTrees []methodTree
|
||||||
|
|
||||||
|
func (trees methodTrees) get(method string) *node {
|
||||||
|
for _, tree := range trees {
|
||||||
|
if tree.method == method {
|
||||||
|
return tree.root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a <= b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func countParams(path string) uint8 {
|
||||||
|
var n uint
|
||||||
|
for i := 0; i < len(path); i++ {
|
||||||
|
if path[i] != ':' && path[i] != '*' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
if n >= 255 {
|
||||||
|
return 255
|
||||||
|
}
|
||||||
|
return uint8(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
type nodeType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
static nodeType = iota // default
|
||||||
|
root
|
||||||
|
param
|
||||||
|
catchAll
|
||||||
|
)
|
||||||
|
|
||||||
|
type node struct {
|
||||||
|
path string
|
||||||
|
wildChild bool
|
||||||
|
nType nodeType
|
||||||
|
maxParams uint8
|
||||||
|
indices string
|
||||||
|
children []*node
|
||||||
|
handlers HandlersChain
|
||||||
|
priority uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// increments priority of the given child and reorders if necessary
|
||||||
|
func (n *node) incrementChildPrio(pos int) int {
|
||||||
|
n.children[pos].priority++
|
||||||
|
prio := n.children[pos].priority
|
||||||
|
|
||||||
|
// adjust position (move to front)
|
||||||
|
newPos := pos
|
||||||
|
for newPos > 0 && n.children[newPos-1].priority < prio {
|
||||||
|
// swap node positions
|
||||||
|
tmpN := n.children[newPos-1]
|
||||||
|
n.children[newPos-1] = n.children[newPos]
|
||||||
|
n.children[newPos] = tmpN
|
||||||
|
|
||||||
|
newPos--
|
||||||
|
}
|
||||||
|
|
||||||
|
// build new index char string
|
||||||
|
if newPos != pos {
|
||||||
|
n.indices = n.indices[:newPos] + // unchanged prefix, might be empty
|
||||||
|
n.indices[pos:pos+1] + // the index char we move
|
||||||
|
n.indices[newPos:pos] + n.indices[pos+1:] // rest without char at 'pos'
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPos
|
||||||
|
}
|
||||||
|
|
||||||
|
// addRoute adds a node with the given handle to the path.
|
||||||
|
// Not concurrency-safe!
|
||||||
|
func (n *node) addRoute(path string, handlers HandlersChain) {
|
||||||
|
fullPath := path
|
||||||
|
n.priority++
|
||||||
|
numParams := countParams(path)
|
||||||
|
|
||||||
|
// non-empty tree
|
||||||
|
if len(n.path) > 0 || len(n.children) > 0 {
|
||||||
|
walk:
|
||||||
|
for {
|
||||||
|
// Update maxParams of the current node
|
||||||
|
if numParams > n.maxParams {
|
||||||
|
n.maxParams = numParams
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the longest common prefix.
|
||||||
|
// This also implies that the common prefix contains no ':' or '*'
|
||||||
|
// since the existing key can't contain those chars.
|
||||||
|
i := 0
|
||||||
|
max := min(len(path), len(n.path))
|
||||||
|
for i < max && path[i] == n.path[i] {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split edge
|
||||||
|
if i < len(n.path) {
|
||||||
|
child := node{
|
||||||
|
path: n.path[i:],
|
||||||
|
wildChild: n.wildChild,
|
||||||
|
indices: n.indices,
|
||||||
|
children: n.children,
|
||||||
|
handlers: n.handlers,
|
||||||
|
priority: n.priority - 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update maxParams (max of all children)
|
||||||
|
for i := range child.children {
|
||||||
|
if child.children[i].maxParams > child.maxParams {
|
||||||
|
child.maxParams = child.children[i].maxParams
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
n.children = []*node{&child}
|
||||||
|
// []byte for proper unicode char conversion, see #65
|
||||||
|
n.indices = string([]byte{n.path[i]})
|
||||||
|
n.path = path[:i]
|
||||||
|
n.handlers = nil
|
||||||
|
n.wildChild = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make new node a child of this node
|
||||||
|
if i < len(path) {
|
||||||
|
path = path[i:]
|
||||||
|
|
||||||
|
if n.wildChild {
|
||||||
|
n = n.children[0]
|
||||||
|
n.priority++
|
||||||
|
|
||||||
|
// Update maxParams of the child node
|
||||||
|
if numParams > n.maxParams {
|
||||||
|
n.maxParams = numParams
|
||||||
|
}
|
||||||
|
numParams--
|
||||||
|
|
||||||
|
// Check if the wildcard matches
|
||||||
|
if len(path) >= len(n.path) && n.path == path[:len(n.path)] {
|
||||||
|
// check for longer wildcard, e.g. :name and :names
|
||||||
|
if len(n.path) >= len(path) || path[len(n.path)] == '/' {
|
||||||
|
continue walk
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("path segment '" + path +
|
||||||
|
"' conflicts with existing wildcard '" + n.path +
|
||||||
|
"' in path '" + fullPath + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
c := path[0]
|
||||||
|
|
||||||
|
// slash after param
|
||||||
|
if n.nType == param && c == '/' && len(n.children) == 1 {
|
||||||
|
n = n.children[0]
|
||||||
|
n.priority++
|
||||||
|
continue walk
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a child with the next path byte exists
|
||||||
|
for i := 0; i < len(n.indices); i++ {
|
||||||
|
if c == n.indices[i] {
|
||||||
|
i = n.incrementChildPrio(i)
|
||||||
|
n = n.children[i]
|
||||||
|
continue walk
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise insert it
|
||||||
|
if c != ':' && c != '*' {
|
||||||
|
// []byte for proper unicode char conversion, see #65
|
||||||
|
n.indices += string([]byte{c})
|
||||||
|
child := &node{
|
||||||
|
maxParams: numParams,
|
||||||
|
}
|
||||||
|
n.children = append(n.children, child)
|
||||||
|
n.incrementChildPrio(len(n.indices) - 1)
|
||||||
|
n = child
|
||||||
|
}
|
||||||
|
n.insertChild(numParams, path, fullPath, handlers)
|
||||||
|
return
|
||||||
|
|
||||||
|
} else if i == len(path) { // Make node a (in-path) leaf
|
||||||
|
if n.handlers != nil {
|
||||||
|
panic("handlers are already registered for path ''" + fullPath + "'")
|
||||||
|
}
|
||||||
|
n.handlers = handlers
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else { // Empty tree
|
||||||
|
n.insertChild(numParams, path, fullPath, handlers)
|
||||||
|
n.nType = root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers HandlersChain) {
|
||||||
|
var offset int // already handled bytes of the path
|
||||||
|
|
||||||
|
// find prefix until first wildcard (beginning with ':'' or '*'')
|
||||||
|
for i, max := 0, len(path); numParams > 0; i++ {
|
||||||
|
c := path[i]
|
||||||
|
if c != ':' && c != '*' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// find wildcard end (either '/' or path end)
|
||||||
|
end := i + 1
|
||||||
|
for end < max && path[end] != '/' {
|
||||||
|
switch path[end] {
|
||||||
|
// the wildcard name must not contain ':' and '*'
|
||||||
|
case ':', '*':
|
||||||
|
panic("only one wildcard per path segment is allowed, has: '" +
|
||||||
|
path[i:] + "' in path '" + fullPath + "'")
|
||||||
|
default:
|
||||||
|
end++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if this Node existing children which would be
|
||||||
|
// unreachable if we insert the wildcard here
|
||||||
|
if len(n.children) > 0 {
|
||||||
|
panic("wildcard route '" + path[i:end] +
|
||||||
|
"' conflicts with existing children in path '" + fullPath + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the wildcard has a name
|
||||||
|
if end-i < 2 {
|
||||||
|
panic("wildcards must be named with a non-empty name in path '" + fullPath + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
if c == ':' { // param
|
||||||
|
// split path at the beginning of the wildcard
|
||||||
|
if i > 0 {
|
||||||
|
n.path = path[offset:i]
|
||||||
|
offset = i
|
||||||
|
}
|
||||||
|
|
||||||
|
child := &node{
|
||||||
|
nType: param,
|
||||||
|
maxParams: numParams,
|
||||||
|
}
|
||||||
|
n.children = []*node{child}
|
||||||
|
n.wildChild = true
|
||||||
|
n = child
|
||||||
|
n.priority++
|
||||||
|
numParams--
|
||||||
|
|
||||||
|
// if the path doesn't end with the wildcard, then there
|
||||||
|
// will be another non-wildcard subpath starting with '/'
|
||||||
|
if end < max {
|
||||||
|
n.path = path[offset:end]
|
||||||
|
offset = end
|
||||||
|
|
||||||
|
child := &node{
|
||||||
|
maxParams: numParams,
|
||||||
|
priority: 1,
|
||||||
|
}
|
||||||
|
n.children = []*node{child}
|
||||||
|
n = child
|
||||||
|
}
|
||||||
|
|
||||||
|
} else { // catchAll
|
||||||
|
if end != max || numParams > 1 {
|
||||||
|
panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
|
||||||
|
panic("catch-all conflicts with existing handle for the path segment root in path '" + fullPath + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
// currently fixed width 1 for '/'
|
||||||
|
i--
|
||||||
|
if path[i] != '/' {
|
||||||
|
panic("no / before catch-all in path '" + fullPath + "'")
|
||||||
|
}
|
||||||
|
|
||||||
|
n.path = path[offset:i]
|
||||||
|
|
||||||
|
// first node: catchAll node with empty path
|
||||||
|
child := &node{
|
||||||
|
wildChild: true,
|
||||||
|
nType: catchAll,
|
||||||
|
maxParams: 1,
|
||||||
|
}
|
||||||
|
n.children = []*node{child}
|
||||||
|
n.indices = string(path[i])
|
||||||
|
n = child
|
||||||
|
n.priority++
|
||||||
|
|
||||||
|
// second node: node holding the variable
|
||||||
|
child = &node{
|
||||||
|
path: path[i:],
|
||||||
|
nType: catchAll,
|
||||||
|
maxParams: 1,
|
||||||
|
handlers: handlers,
|
||||||
|
priority: 1,
|
||||||
|
}
|
||||||
|
n.children = []*node{child}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert remaining path part and handle to the leaf
|
||||||
|
n.path = path[offset:]
|
||||||
|
n.handlers = handlers
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the handle registered with the given path (key). The values of
|
||||||
|
// wildcards are saved to a map.
|
||||||
|
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
|
||||||
|
// made if a handle exists with an extra (without the) trailing slash for the
|
||||||
|
// given path.
|
||||||
|
func (n *node) getValue(path string, po Params, unescape bool) (handlers HandlersChain, p Params, tsr bool) {
|
||||||
|
p = po
|
||||||
|
walk: // Outer loop for walking the tree
|
||||||
|
for {
|
||||||
|
if len(path) > len(n.path) {
|
||||||
|
if path[:len(n.path)] == n.path {
|
||||||
|
path = path[len(n.path):]
|
||||||
|
// If this node does not have a wildcard (param or catchAll)
|
||||||
|
// child, we can just look up the next child node and continue
|
||||||
|
// to walk down the tree
|
||||||
|
if !n.wildChild {
|
||||||
|
c := path[0]
|
||||||
|
for i := 0; i < len(n.indices); i++ {
|
||||||
|
if c == n.indices[i] {
|
||||||
|
n = n.children[i]
|
||||||
|
continue walk
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing found.
|
||||||
|
// We can recommend to redirect to the same URL without a
|
||||||
|
// trailing slash if a leaf exists for that path.
|
||||||
|
tsr = (path == "/" && n.handlers != nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle wildcard child
|
||||||
|
n = n.children[0]
|
||||||
|
switch n.nType {
|
||||||
|
case param:
|
||||||
|
// find param end (either '/' or path end)
|
||||||
|
end := 0
|
||||||
|
for end < len(path) && path[end] != '/' {
|
||||||
|
end++
|
||||||
|
}
|
||||||
|
|
||||||
|
// save param value
|
||||||
|
if cap(p) < int(n.maxParams) {
|
||||||
|
p = make(Params, 0, n.maxParams)
|
||||||
|
}
|
||||||
|
i := len(p)
|
||||||
|
p = p[:i+1] // expand slice within preallocated capacity
|
||||||
|
p[i].Key = n.path[1:]
|
||||||
|
val := path[:end]
|
||||||
|
if unescape {
|
||||||
|
var err error
|
||||||
|
if p[i].Value, err = url.QueryUnescape(val); err != nil {
|
||||||
|
p[i].Value = val // fallback, in case of error
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
p[i].Value = val
|
||||||
|
}
|
||||||
|
|
||||||
|
// we need to go deeper!
|
||||||
|
if end < len(path) {
|
||||||
|
if len(n.children) > 0 {
|
||||||
|
path = path[end:]
|
||||||
|
n = n.children[0]
|
||||||
|
continue walk
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... but we can't
|
||||||
|
tsr = (len(path) == end+1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if handlers = n.handlers; handlers != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(n.children) == 1 {
|
||||||
|
// No handle found. Check if a handle for this path + a
|
||||||
|
// trailing slash exists for TSR recommendation
|
||||||
|
n = n.children[0]
|
||||||
|
tsr = (n.path == "/" && n.handlers != nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
case catchAll:
|
||||||
|
// save param value
|
||||||
|
if cap(p) < int(n.maxParams) {
|
||||||
|
p = make(Params, 0, n.maxParams)
|
||||||
|
}
|
||||||
|
i := len(p)
|
||||||
|
p = p[:i+1] // expand slice within preallocated capacity
|
||||||
|
p[i].Key = n.path[2:]
|
||||||
|
if unescape {
|
||||||
|
var err error
|
||||||
|
if p[i].Value, err = url.QueryUnescape(path); err != nil {
|
||||||
|
p[i].Value = path // fallback, in case of error
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
p[i].Value = path
|
||||||
|
}
|
||||||
|
|
||||||
|
handlers = n.handlers
|
||||||
|
return
|
||||||
|
|
||||||
|
default:
|
||||||
|
panic("invalid node type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if path == n.path {
|
||||||
|
// We should have reached the node containing the handle.
|
||||||
|
// Check if this node has a handle registered.
|
||||||
|
if handlers = n.handlers; handlers != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if path == "/" && n.wildChild && n.nType != root {
|
||||||
|
tsr = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// No handle found. Check if a handle for this path + a
|
||||||
|
// trailing slash exists for trailing slash recommendation
|
||||||
|
for i := 0; i < len(n.indices); i++ {
|
||||||
|
if n.indices[i] == '/' {
|
||||||
|
n = n.children[i]
|
||||||
|
tsr = (len(n.path) == 1 && n.handlers != nil) ||
|
||||||
|
(n.nType == catchAll && n.children[0].handlers != nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing found. We can recommend to redirect to the same URL with an
|
||||||
|
// extra trailing slash if a leaf exists for that path
|
||||||
|
tsr = (path == "/") ||
|
||||||
|
(len(n.path) == len(path)+1 && n.path[len(path)] == '/' &&
|
||||||
|
path == n.path[:len(n.path)-1] && n.handlers != nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Makes a case-insensitive lookup of the given path and tries to find a handler.
|
||||||
|
// It can optionally also fix trailing slashes.
|
||||||
|
// It returns the case-corrected path and a bool indicating whether the lookup
|
||||||
|
// was successful.
|
||||||
|
func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPath []byte, found bool) {
|
||||||
|
ciPath = make([]byte, 0, len(path)+1) // preallocate enough memory
|
||||||
|
|
||||||
|
// Outer loop for walking the tree
|
||||||
|
for len(path) >= len(n.path) && strings.ToLower(path[:len(n.path)]) == strings.ToLower(n.path) {
|
||||||
|
path = path[len(n.path):]
|
||||||
|
ciPath = append(ciPath, n.path...)
|
||||||
|
|
||||||
|
if len(path) > 0 {
|
||||||
|
// If this node does not have a wildcard (param or catchAll) child,
|
||||||
|
// we can just look up the next child node and continue to walk down
|
||||||
|
// the tree
|
||||||
|
if !n.wildChild {
|
||||||
|
r := unicode.ToLower(rune(path[0]))
|
||||||
|
for i, index := range n.indices {
|
||||||
|
// must use recursive approach since both index and
|
||||||
|
// ToLower(index) could exist. We must check both.
|
||||||
|
if r == unicode.ToLower(index) {
|
||||||
|
out, found := n.children[i].findCaseInsensitivePath(path, fixTrailingSlash)
|
||||||
|
if found {
|
||||||
|
return append(ciPath, out...), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing found. We can recommend to redirect to the same URL
|
||||||
|
// without a trailing slash if a leaf exists for that path
|
||||||
|
found = (fixTrailingSlash && path == "/" && n.handlers != nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
n = n.children[0]
|
||||||
|
switch n.nType {
|
||||||
|
case param:
|
||||||
|
// find param end (either '/' or path end)
|
||||||
|
k := 0
|
||||||
|
for k < len(path) && path[k] != '/' {
|
||||||
|
k++
|
||||||
|
}
|
||||||
|
|
||||||
|
// add param value to case insensitive path
|
||||||
|
ciPath = append(ciPath, path[:k]...)
|
||||||
|
|
||||||
|
// we need to go deeper!
|
||||||
|
if k < len(path) {
|
||||||
|
if len(n.children) > 0 {
|
||||||
|
path = path[k:]
|
||||||
|
n = n.children[0]
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... but we can't
|
||||||
|
if fixTrailingSlash && len(path) == k+1 {
|
||||||
|
return ciPath, true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if n.handlers != nil {
|
||||||
|
return ciPath, true
|
||||||
|
} else if fixTrailingSlash && len(n.children) == 1 {
|
||||||
|
// No handle found. Check if a handle for this path + a
|
||||||
|
// trailing slash exists
|
||||||
|
n = n.children[0]
|
||||||
|
if n.path == "/" && n.handlers != nil {
|
||||||
|
return append(ciPath, '/'), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
|
||||||
|
case catchAll:
|
||||||
|
return append(ciPath, path...), true
|
||||||
|
|
||||||
|
default:
|
||||||
|
panic("invalid node type")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// We should have reached the node containing the handle.
|
||||||
|
// Check if this node has a handle registered.
|
||||||
|
if n.handlers != nil {
|
||||||
|
return ciPath, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// No handle found.
|
||||||
|
// Try to fix the path by adding a trailing slash
|
||||||
|
if fixTrailingSlash {
|
||||||
|
for i := 0; i < len(n.indices); i++ {
|
||||||
|
if n.indices[i] == '/' {
|
||||||
|
n = n.children[i]
|
||||||
|
if (len(n.path) == 1 && n.handlers != nil) ||
|
||||||
|
(n.nType == catchAll && n.children[0].handlers != nil) {
|
||||||
|
return append(ciPath, '/'), true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing found.
|
||||||
|
// Try to fix the path by adding / removing a trailing slash
|
||||||
|
if fixTrailingSlash {
|
||||||
|
if path == "/" {
|
||||||
|
return ciPath, true
|
||||||
|
}
|
||||||
|
if len(path)+1 == len(n.path) && n.path[len(path)] == '/' &&
|
||||||
|
strings.ToLower(path) == strings.ToLower(n.path[:len(path)]) &&
|
||||||
|
n.handlers != nil {
|
||||||
|
return append(ciPath, n.path...), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
154
vendor/github.com/gin-gonic/gin/utils.go
generated
vendored
Normal file
154
vendor/github.com/gin-gonic/gin/utils.go
generated
vendored
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const BindKey = "_gin-gonic/gin/bindkey"
|
||||||
|
|
||||||
|
func Bind(val interface{}) HandlerFunc {
|
||||||
|
value := reflect.ValueOf(val)
|
||||||
|
if value.Kind() == reflect.Ptr {
|
||||||
|
panic(`Bind struct can not be a pointer. Example:
|
||||||
|
Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
typ := value.Type()
|
||||||
|
|
||||||
|
return func(c *Context) {
|
||||||
|
obj := reflect.New(typ).Interface()
|
||||||
|
if c.Bind(obj) == nil {
|
||||||
|
c.Set(BindKey, obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WrapF(f http.HandlerFunc) HandlerFunc {
|
||||||
|
return func(c *Context) {
|
||||||
|
f(c.Writer, c.Request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WrapH(h http.Handler) HandlerFunc {
|
||||||
|
return func(c *Context) {
|
||||||
|
h.ServeHTTP(c.Writer, c.Request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type H map[string]interface{}
|
||||||
|
|
||||||
|
// MarshalXML allows type H to be used with xml.Marshal
|
||||||
|
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||||
|
start.Name = xml.Name{
|
||||||
|
Space: "",
|
||||||
|
Local: "map",
|
||||||
|
}
|
||||||
|
if err := e.EncodeToken(start); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for key, value := range h {
|
||||||
|
elem := xml.StartElement{
|
||||||
|
Name: xml.Name{Space: "", Local: key},
|
||||||
|
Attr: []xml.Attr{},
|
||||||
|
}
|
||||||
|
if err := e.EncodeElement(value, elem); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func assert1(guard bool, text string) {
|
||||||
|
if !guard {
|
||||||
|
panic(text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterFlags(content string) string {
|
||||||
|
for i, char := range content {
|
||||||
|
if char == ' ' || char == ';' {
|
||||||
|
return content[:i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
func chooseData(custom, wildcard interface{}) interface{} {
|
||||||
|
if custom == nil {
|
||||||
|
if wildcard == nil {
|
||||||
|
panic("negotiation config is invalid")
|
||||||
|
}
|
||||||
|
return wildcard
|
||||||
|
}
|
||||||
|
return custom
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAccept(acceptHeader string) []string {
|
||||||
|
parts := strings.Split(acceptHeader, ",")
|
||||||
|
out := make([]string, 0, len(parts))
|
||||||
|
for _, part := range parts {
|
||||||
|
index := strings.IndexByte(part, ';')
|
||||||
|
if index >= 0 {
|
||||||
|
part = part[0:index]
|
||||||
|
}
|
||||||
|
part = strings.TrimSpace(part)
|
||||||
|
if len(part) > 0 {
|
||||||
|
out = append(out, part)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func lastChar(str string) uint8 {
|
||||||
|
size := len(str)
|
||||||
|
if size == 0 {
|
||||||
|
panic("The length of the string can't be 0")
|
||||||
|
}
|
||||||
|
return str[size-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func nameOfFunction(f interface{}) string {
|
||||||
|
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
func joinPaths(absolutePath, relativePath string) string {
|
||||||
|
if len(relativePath) == 0 {
|
||||||
|
return absolutePath
|
||||||
|
}
|
||||||
|
|
||||||
|
finalPath := path.Join(absolutePath, relativePath)
|
||||||
|
appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
|
||||||
|
if appendSlash {
|
||||||
|
return finalPath + "/"
|
||||||
|
}
|
||||||
|
return finalPath
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveAddress(addr []string) string {
|
||||||
|
switch len(addr) {
|
||||||
|
case 0:
|
||||||
|
if port := os.Getenv("PORT"); len(port) > 0 {
|
||||||
|
debugPrint("Environment variable PORT=\"%s\"", port)
|
||||||
|
return ":" + port
|
||||||
|
}
|
||||||
|
debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
|
||||||
|
return ":8080"
|
||||||
|
case 1:
|
||||||
|
return addr[0]
|
||||||
|
default:
|
||||||
|
panic("too much parameters")
|
||||||
|
}
|
||||||
|
}
|
1
vendor/github.com/gin-gonic/gin/wercker.yml
generated
vendored
Normal file
1
vendor/github.com/gin-gonic/gin/wercker.yml
generated
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
box: wercker/default
|
27
vendor/github.com/go-xorm/builder/LICENSE
generated
vendored
Normal file
27
vendor/github.com/go-xorm/builder/LICENSE
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
Copyright (c) 2016 The Xorm 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 the {organization} 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 HOLDER 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.
|
175
vendor/github.com/go-xorm/builder/README.md
generated
vendored
Normal file
175
vendor/github.com/go-xorm/builder/README.md
generated
vendored
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
# SQL builder
|
||||||
|
|
||||||
|
[![CircleCI](https://circleci.com/gh/go-xorm/builder/tree/master.svg?style=svg)](https://circleci.com/gh/go-xorm/builder/tree/master)
|
||||||
|
|
||||||
|
Package builder is a lightweight and fast SQL builder for Go and XORM.
|
||||||
|
|
||||||
|
Make sure you have installed Go 1.1+ and then:
|
||||||
|
|
||||||
|
go get github.com/go-xorm/builder
|
||||||
|
|
||||||
|
# Insert
|
||||||
|
|
||||||
|
```Go
|
||||||
|
sql, args, err := Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()
|
||||||
|
```
|
||||||
|
|
||||||
|
# Select
|
||||||
|
|
||||||
|
```Go
|
||||||
|
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
|
||||||
|
|
||||||
|
sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
|
||||||
|
RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
|
||||||
|
```
|
||||||
|
|
||||||
|
# Update
|
||||||
|
|
||||||
|
```Go
|
||||||
|
sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
|
||||||
|
```
|
||||||
|
|
||||||
|
# Delete
|
||||||
|
|
||||||
|
```Go
|
||||||
|
sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()
|
||||||
|
```
|
||||||
|
|
||||||
|
# Conditions
|
||||||
|
|
||||||
|
* `Eq` is a redefine of a map, you can give one or more conditions to `Eq`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Eq{"a":1})
|
||||||
|
// a=? [1]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
|
||||||
|
// b=? AND c=? ["c", 0]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
|
||||||
|
// b=? AND c=? ["c", 0]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
|
||||||
|
// b=? OR b=? ["c", "d"]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
|
||||||
|
// b IN (?,?) ["c", "d"]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
|
||||||
|
// b=? AND c IN (?,?) [1, 2, 3]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Neq` is the same to `Eq`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Neq{"a":1})
|
||||||
|
// a<>? [1]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
|
||||||
|
// b<>? AND c<>? ["c", 0]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
|
||||||
|
// b<>? AND c<>? ["c", 0]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
|
||||||
|
// b<>? OR b<>? ["c", "d"]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
|
||||||
|
// b NOT IN (?,?) ["c", "d"]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
|
||||||
|
// b<>? AND c NOT IN (?,?) [1, 2, 3]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Gt`, `Gte`, `Lt`, `Lte`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
|
||||||
|
// a>? AND b>=? [1, 2]
|
||||||
|
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
|
||||||
|
// a<? OR b<=? [1, 2]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Like`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Like{"a", "c"})
|
||||||
|
// a LIKE ? [%c%]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Expr` you can customerize your sql with `Expr`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Expr("a = ? ", 1))
|
||||||
|
// a = ? [1]
|
||||||
|
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
|
||||||
|
// a=(select id from table where c = ?) [1]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `In` and `NotIn`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(In("a", 1, 2, 3))
|
||||||
|
// a IN (?,?,?) [1,2,3]
|
||||||
|
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
|
||||||
|
// a IN (?,?,?) [1,2,3]
|
||||||
|
sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
|
||||||
|
// a IN (select id from b where c = ?) [1]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `IsNull` and `NotNull`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(IsNull{"a"})
|
||||||
|
// a IS NULL []
|
||||||
|
sql, args, _ := ToSQL(NotNull{"b"})
|
||||||
|
// b IS NOT NULL []
|
||||||
|
```
|
||||||
|
|
||||||
|
* `And(conds ...Cond)`, And can connect one or more condtions via And
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
|
||||||
|
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Or(conds ...Cond)`, Or can connect one or more conditions via Or
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
|
||||||
|
// a=? OR b LIKE ? OR d<>? [1, %c%, 2]
|
||||||
|
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
|
||||||
|
// a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Between`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Between{"a", 1, 2})
|
||||||
|
// a BETWEEN 1 AND 2
|
||||||
|
```
|
||||||
|
|
||||||
|
* Define yourself conditions
|
||||||
|
|
||||||
|
Since `Cond` is an interface.
|
||||||
|
|
||||||
|
```Go
|
||||||
|
type Cond interface {
|
||||||
|
WriteTo(Writer) error
|
||||||
|
And(...Cond) Cond
|
||||||
|
Or(...Cond) Cond
|
||||||
|
IsValid() bool
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You can define yourself conditions and compose with other `Cond`.
|
190
vendor/github.com/go-xorm/builder/builder.go
generated
vendored
Normal file
190
vendor/github.com/go-xorm/builder/builder.go
generated
vendored
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
type optype byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
condType optype = iota // only conditions
|
||||||
|
selectType // select
|
||||||
|
insertType // insert
|
||||||
|
updateType // update
|
||||||
|
deleteType // delete
|
||||||
|
)
|
||||||
|
|
||||||
|
type join struct {
|
||||||
|
joinType string
|
||||||
|
joinTable string
|
||||||
|
joinCond Cond
|
||||||
|
}
|
||||||
|
|
||||||
|
// Builder describes a SQL statement
|
||||||
|
type Builder struct {
|
||||||
|
optype
|
||||||
|
tableName string
|
||||||
|
cond Cond
|
||||||
|
selects []string
|
||||||
|
joins []join
|
||||||
|
inserts Eq
|
||||||
|
updates []Eq
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select creates a select Builder
|
||||||
|
func Select(cols ...string) *Builder {
|
||||||
|
builder := &Builder{cond: NewCond()}
|
||||||
|
return builder.Select(cols...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert creates an insert Builder
|
||||||
|
func Insert(eq Eq) *Builder {
|
||||||
|
builder := &Builder{cond: NewCond()}
|
||||||
|
return builder.Insert(eq)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update creates an update Builder
|
||||||
|
func Update(updates ...Eq) *Builder {
|
||||||
|
builder := &Builder{cond: NewCond()}
|
||||||
|
return builder.Update(updates...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete creates a delete Builder
|
||||||
|
func Delete(conds ...Cond) *Builder {
|
||||||
|
builder := &Builder{cond: NewCond()}
|
||||||
|
return builder.Delete(conds...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where sets where SQL
|
||||||
|
func (b *Builder) Where(cond Cond) *Builder {
|
||||||
|
b.cond = b.cond.And(cond)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// From sets the table name
|
||||||
|
func (b *Builder) From(tableName string) *Builder {
|
||||||
|
b.tableName = tableName
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Into sets insert table name
|
||||||
|
func (b *Builder) Into(tableName string) *Builder {
|
||||||
|
b.tableName = tableName
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join sets join table and contions
|
||||||
|
func (b *Builder) Join(joinType, joinTable string, joinCond interface{}) *Builder {
|
||||||
|
switch joinCond.(type) {
|
||||||
|
case Cond:
|
||||||
|
b.joins = append(b.joins, join{joinType, joinTable, joinCond.(Cond)})
|
||||||
|
case string:
|
||||||
|
b.joins = append(b.joins, join{joinType, joinTable, Expr(joinCond.(string))})
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// InnerJoin sets inner join
|
||||||
|
func (b *Builder) InnerJoin(joinTable string, joinCond interface{}) *Builder {
|
||||||
|
return b.Join("INNER", joinTable, joinCond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LeftJoin sets left join SQL
|
||||||
|
func (b *Builder) LeftJoin(joinTable string, joinCond interface{}) *Builder {
|
||||||
|
return b.Join("LEFT", joinTable, joinCond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RightJoin sets right join SQL
|
||||||
|
func (b *Builder) RightJoin(joinTable string, joinCond interface{}) *Builder {
|
||||||
|
return b.Join("RIGHT", joinTable, joinCond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CrossJoin sets cross join SQL
|
||||||
|
func (b *Builder) CrossJoin(joinTable string, joinCond interface{}) *Builder {
|
||||||
|
return b.Join("CROSS", joinTable, joinCond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FullJoin sets full join SQL
|
||||||
|
func (b *Builder) FullJoin(joinTable string, joinCond interface{}) *Builder {
|
||||||
|
return b.Join("FULL", joinTable, joinCond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select sets select SQL
|
||||||
|
func (b *Builder) Select(cols ...string) *Builder {
|
||||||
|
b.selects = cols
|
||||||
|
b.optype = selectType
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// And sets AND condition
|
||||||
|
func (b *Builder) And(cond Cond) *Builder {
|
||||||
|
b.cond = And(b.cond, cond)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or sets OR condition
|
||||||
|
func (b *Builder) Or(cond Cond) *Builder {
|
||||||
|
b.cond = Or(b.cond, cond)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert sets insert SQL
|
||||||
|
func (b *Builder) Insert(eq Eq) *Builder {
|
||||||
|
b.inserts = eq
|
||||||
|
b.optype = insertType
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update sets update SQL
|
||||||
|
func (b *Builder) Update(updates ...Eq) *Builder {
|
||||||
|
b.updates = updates
|
||||||
|
b.optype = updateType
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete sets delete SQL
|
||||||
|
func (b *Builder) Delete(conds ...Cond) *Builder {
|
||||||
|
b.cond = b.cond.And(conds...)
|
||||||
|
b.optype = deleteType
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo implements Writer interface
|
||||||
|
func (b *Builder) WriteTo(w Writer) error {
|
||||||
|
switch b.optype {
|
||||||
|
case condType:
|
||||||
|
return b.cond.WriteTo(w)
|
||||||
|
case selectType:
|
||||||
|
return b.selectWriteTo(w)
|
||||||
|
case insertType:
|
||||||
|
return b.insertWriteTo(w)
|
||||||
|
case updateType:
|
||||||
|
return b.updateWriteTo(w)
|
||||||
|
case deleteType:
|
||||||
|
return b.deleteWriteTo(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ErrNotSupportType
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToSQL convert a builder to SQL and args
|
||||||
|
func (b *Builder) ToSQL() (string, []interface{}, error) {
|
||||||
|
w := NewWriter()
|
||||||
|
if err := b.WriteTo(w); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.writer.String(), w.args, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToSQL convert a builder or condtions to SQL and args
|
||||||
|
func ToSQL(cond interface{}) (string, []interface{}, error) {
|
||||||
|
switch cond.(type) {
|
||||||
|
case Cond:
|
||||||
|
return condToSQL(cond.(Cond))
|
||||||
|
case *Builder:
|
||||||
|
return cond.(*Builder).ToSQL()
|
||||||
|
}
|
||||||
|
return "", nil, ErrNotSupportType
|
||||||
|
}
|
22
vendor/github.com/go-xorm/builder/builder_delete.go
generated
vendored
Normal file
22
vendor/github.com/go-xorm/builder/builder_delete.go
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Builder) deleteWriteTo(w Writer) error {
|
||||||
|
if len(b.tableName) <= 0 {
|
||||||
|
return errors.New("no table indicated")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, "DELETE FROM %s WHERE ", b.tableName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.cond.WriteTo(w)
|
||||||
|
}
|
64
vendor/github.com/go-xorm/builder/builder_insert.go
generated
vendored
Normal file
64
vendor/github.com/go-xorm/builder/builder_insert.go
generated
vendored
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Builder) insertWriteTo(w Writer) error {
|
||||||
|
if len(b.tableName) <= 0 {
|
||||||
|
return errors.New("no table indicated")
|
||||||
|
}
|
||||||
|
if len(b.inserts) <= 0 {
|
||||||
|
return errors.New("no column to be update")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, "INSERT INTO %s (", b.tableName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var args = make([]interface{}, 0)
|
||||||
|
var bs []byte
|
||||||
|
var valBuffer = bytes.NewBuffer(bs)
|
||||||
|
var i = 0
|
||||||
|
for col, value := range b.inserts {
|
||||||
|
fmt.Fprint(w, col)
|
||||||
|
if e, ok := value.(expr); ok {
|
||||||
|
fmt.Fprint(valBuffer, e.sql)
|
||||||
|
args = append(args, e.args...)
|
||||||
|
} else {
|
||||||
|
fmt.Fprint(valBuffer, "?")
|
||||||
|
args = append(args, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if i != len(b.inserts)-1 {
|
||||||
|
if _, err := fmt.Fprint(w, ","); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprint(valBuffer, ","); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprint(w, ") Values ("); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(valBuffer.Bytes()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprint(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Append(args...)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
57
vendor/github.com/go-xorm/builder/builder_select.go
generated
vendored
Normal file
57
vendor/github.com/go-xorm/builder/builder_select.go
generated
vendored
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Builder) selectWriteTo(w Writer) error {
|
||||||
|
if len(b.tableName) <= 0 {
|
||||||
|
return errors.New("no table indicated")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprint(w, "SELECT "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(b.selects) > 0 {
|
||||||
|
for i, s := range b.selects {
|
||||||
|
if _, err := fmt.Fprint(w, s); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if i != len(b.selects)-1 {
|
||||||
|
if _, err := fmt.Fprint(w, ","); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if _, err := fmt.Fprint(w, "*"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, " FROM %s", b.tableName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range b.joins {
|
||||||
|
fmt.Fprintf(w, " %s JOIN %s ON ", v.joinType, v.joinTable)
|
||||||
|
if err := v.joinCond.WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !b.cond.IsValid() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.cond.WriteTo(w)
|
||||||
|
}
|
41
vendor/github.com/go-xorm/builder/builder_update.go
generated
vendored
Normal file
41
vendor/github.com/go-xorm/builder/builder_update.go
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Builder) updateWriteTo(w Writer) error {
|
||||||
|
if len(b.tableName) <= 0 {
|
||||||
|
return errors.New("no table indicated")
|
||||||
|
}
|
||||||
|
if len(b.updates) <= 0 {
|
||||||
|
return errors.New("no column to be update")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, "UPDATE %s SET ", b.tableName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, s := range b.updates {
|
||||||
|
if err := s.opWriteTo(",", w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if i != len(b.updates)-1 {
|
||||||
|
if _, err := fmt.Fprint(w, ","); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.cond.WriteTo(w)
|
||||||
|
}
|
12
vendor/github.com/go-xorm/builder/circle.yml
generated
vendored
Normal file
12
vendor/github.com/go-xorm/builder/circle.yml
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
dependencies:
|
||||||
|
override:
|
||||||
|
# './...' is a relative pattern which means all subdirectories
|
||||||
|
- go get -t -d -v ./...
|
||||||
|
- go build -v
|
||||||
|
- go get -u github.com/golang/lint/golint
|
||||||
|
|
||||||
|
test:
|
||||||
|
override:
|
||||||
|
# './...' is a relative pattern which means all subdirectories
|
||||||
|
- golint ./...
|
||||||
|
- go test -v -race
|
87
vendor/github.com/go-xorm/builder/cond.go
generated
vendored
Normal file
87
vendor/github.com/go-xorm/builder/cond.go
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Writer defines the interface
|
||||||
|
type Writer interface {
|
||||||
|
io.Writer
|
||||||
|
Append(...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Writer = NewWriter()
|
||||||
|
|
||||||
|
// BytesWriter implments Writer and save SQL in bytes.Buffer
|
||||||
|
type BytesWriter struct {
|
||||||
|
writer *bytes.Buffer
|
||||||
|
buffer []byte
|
||||||
|
args []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWriter creates a new string writer
|
||||||
|
func NewWriter() *BytesWriter {
|
||||||
|
w := &BytesWriter{}
|
||||||
|
w.writer = bytes.NewBuffer(w.buffer)
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write writes data to Writer
|
||||||
|
func (s *BytesWriter) Write(buf []byte) (int, error) {
|
||||||
|
return s.writer.Write(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append appends args to Writer
|
||||||
|
func (s *BytesWriter) Append(args ...interface{}) {
|
||||||
|
s.args = append(s.args, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cond defines an interface
|
||||||
|
type Cond interface {
|
||||||
|
WriteTo(Writer) error
|
||||||
|
And(...Cond) Cond
|
||||||
|
Or(...Cond) Cond
|
||||||
|
IsValid() bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type condEmpty struct{}
|
||||||
|
|
||||||
|
var _ Cond = condEmpty{}
|
||||||
|
|
||||||
|
// NewCond creates an empty condition
|
||||||
|
func NewCond() Cond {
|
||||||
|
return condEmpty{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condEmpty) WriteTo(w Writer) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condEmpty) And(conds ...Cond) Cond {
|
||||||
|
return And(conds...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condEmpty) Or(conds ...Cond) Cond {
|
||||||
|
return Or(conds...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condEmpty) IsValid() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func condToSQL(cond Cond) (string, []interface{}, error) {
|
||||||
|
if cond == nil || !cond.IsValid() {
|
||||||
|
return "", nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
w := NewWriter()
|
||||||
|
if err := cond.WriteTo(w); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
return w.writer.String(), w.args, nil
|
||||||
|
}
|
61
vendor/github.com/go-xorm/builder/cond_and.go
generated
vendored
Normal file
61
vendor/github.com/go-xorm/builder/cond_and.go
generated
vendored
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type condAnd []Cond
|
||||||
|
|
||||||
|
var _ Cond = condAnd{}
|
||||||
|
|
||||||
|
// And generates AND conditions
|
||||||
|
func And(conds ...Cond) Cond {
|
||||||
|
var result = make(condAnd, 0, len(conds))
|
||||||
|
for _, cond := range conds {
|
||||||
|
if cond == nil || !cond.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, cond)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (and condAnd) WriteTo(w Writer) error {
|
||||||
|
for i, cond := range and {
|
||||||
|
_, isOr := cond.(condOr)
|
||||||
|
_, isExpr := cond.(expr)
|
||||||
|
wrap := isOr || isExpr
|
||||||
|
if wrap {
|
||||||
|
fmt.Fprint(w, "(")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cond.WriteTo(w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if wrap {
|
||||||
|
fmt.Fprint(w, ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
if i != len(and)-1 {
|
||||||
|
fmt.Fprint(w, " AND ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (and condAnd) And(conds ...Cond) Cond {
|
||||||
|
return And(and, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (and condAnd) Or(conds ...Cond) Cond {
|
||||||
|
return Or(and, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (and condAnd) IsValid() bool {
|
||||||
|
return len(and) > 0
|
||||||
|
}
|
40
vendor/github.com/go-xorm/builder/cond_between.go
generated
vendored
Normal file
40
vendor/github.com/go-xorm/builder/cond_between.go
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Between implmentes between condition
|
||||||
|
type Between struct {
|
||||||
|
Col string
|
||||||
|
LessVal interface{}
|
||||||
|
MoreVal interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Cond = Between{}
|
||||||
|
|
||||||
|
// WriteTo write data to Writer
|
||||||
|
func (between Between) WriteTo(w Writer) error {
|
||||||
|
if _, err := fmt.Fprintf(w, "%s BETWEEN ? AND ?", between.Col); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(between.LessVal, between.MoreVal)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implments And with other conditions
|
||||||
|
func (between Between) And(conds ...Cond) Cond {
|
||||||
|
return And(between, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implments Or with other conditions
|
||||||
|
func (between Between) Or(conds ...Cond) Cond {
|
||||||
|
return Or(between, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if the condition is valid
|
||||||
|
func (between Between) IsValid() bool {
|
||||||
|
return len(between.Col) > 0
|
||||||
|
}
|
154
vendor/github.com/go-xorm/builder/cond_compare.go
generated
vendored
Normal file
154
vendor/github.com/go-xorm/builder/cond_compare.go
generated
vendored
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.
|
||||||
|
func WriteMap(w Writer, data map[string]interface{}, op string) error {
|
||||||
|
var args = make([]interface{}, 0, len(data))
|
||||||
|
var i = 0
|
||||||
|
for k, v := range data {
|
||||||
|
switch v.(type) {
|
||||||
|
case expr:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := v.(expr).WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *Builder:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := v.(*Builder).WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s%s?", k, op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
args = append(args, v)
|
||||||
|
}
|
||||||
|
if i != len(data)-1 {
|
||||||
|
if _, err := fmt.Fprint(w, " AND "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
w.Append(args...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lt defines < condition
|
||||||
|
type Lt map[string]interface{}
|
||||||
|
|
||||||
|
var _ Cond = Lt{}
|
||||||
|
|
||||||
|
// WriteTo write SQL to Writer
|
||||||
|
func (lt Lt) WriteTo(w Writer) error {
|
||||||
|
return WriteMap(w, lt, "<")
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (lt Lt) And(conds ...Cond) Cond {
|
||||||
|
return condAnd{lt, And(conds...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (lt Lt) Or(conds ...Cond) Cond {
|
||||||
|
return condOr{lt, Or(conds...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this Eq is valid
|
||||||
|
func (lt Lt) IsValid() bool {
|
||||||
|
return len(lt) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lte defines <= condition
|
||||||
|
type Lte map[string]interface{}
|
||||||
|
|
||||||
|
var _ Cond = Lte{}
|
||||||
|
|
||||||
|
// WriteTo write SQL to Writer
|
||||||
|
func (lte Lte) WriteTo(w Writer) error {
|
||||||
|
return WriteMap(w, lte, "<=")
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (lte Lte) And(conds ...Cond) Cond {
|
||||||
|
return And(lte, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (lte Lte) Or(conds ...Cond) Cond {
|
||||||
|
return Or(lte, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this Eq is valid
|
||||||
|
func (lte Lte) IsValid() bool {
|
||||||
|
return len(lte) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gt defines > condition
|
||||||
|
type Gt map[string]interface{}
|
||||||
|
|
||||||
|
var _ Cond = Gt{}
|
||||||
|
|
||||||
|
// WriteTo write SQL to Writer
|
||||||
|
func (gt Gt) WriteTo(w Writer) error {
|
||||||
|
return WriteMap(w, gt, ">")
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (gt Gt) And(conds ...Cond) Cond {
|
||||||
|
return And(gt, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (gt Gt) Or(conds ...Cond) Cond {
|
||||||
|
return Or(gt, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this Eq is valid
|
||||||
|
func (gt Gt) IsValid() bool {
|
||||||
|
return len(gt) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gte defines >= condition
|
||||||
|
type Gte map[string]interface{}
|
||||||
|
|
||||||
|
var _ Cond = Gte{}
|
||||||
|
|
||||||
|
// WriteTo write SQL to Writer
|
||||||
|
func (gte Gte) WriteTo(w Writer) error {
|
||||||
|
return WriteMap(w, gte, ">=")
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (gte Gte) And(conds ...Cond) Cond {
|
||||||
|
return And(gte, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (gte Gte) Or(conds ...Cond) Cond {
|
||||||
|
return Or(gte, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this Eq is valid
|
||||||
|
func (gte Gte) IsValid() bool {
|
||||||
|
return len(gte) > 0
|
||||||
|
}
|
96
vendor/github.com/go-xorm/builder/cond_eq.go
generated
vendored
Normal file
96
vendor/github.com/go-xorm/builder/cond_eq.go
generated
vendored
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Incr implements a type used by Eq
|
||||||
|
type Incr int
|
||||||
|
|
||||||
|
// Decr implements a type used by Eq
|
||||||
|
type Decr int
|
||||||
|
|
||||||
|
// Eq defines equals conditions
|
||||||
|
type Eq map[string]interface{}
|
||||||
|
|
||||||
|
var _ Cond = Eq{}
|
||||||
|
|
||||||
|
func (eq Eq) opWriteTo(op string, w Writer) error {
|
||||||
|
var i = 0
|
||||||
|
for k, v := range eq {
|
||||||
|
switch v.(type) {
|
||||||
|
case []int, []int64, []string, []int32, []int16, []int8, []uint, []uint64, []uint32, []uint16, []interface{}:
|
||||||
|
if err := In(k, v).WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case expr:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s=(", k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := v.(expr).WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *Builder:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s=(", k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := v.(*Builder).WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case Incr:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s=%s+?", k, k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(int(v.(Incr)))
|
||||||
|
case Decr:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s=%s-?", k, k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(int(v.(Decr)))
|
||||||
|
default:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s=?", k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(v)
|
||||||
|
}
|
||||||
|
if i != len(eq)-1 {
|
||||||
|
if _, err := fmt.Fprint(w, op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo writes SQL to Writer
|
||||||
|
func (eq Eq) WriteTo(w Writer) error {
|
||||||
|
return eq.opWriteTo(" AND ", w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (eq Eq) And(conds ...Cond) Cond {
|
||||||
|
return And(eq, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (eq Eq) Or(conds ...Cond) Cond {
|
||||||
|
return Or(eq, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this Eq is valid
|
||||||
|
func (eq Eq) IsValid() bool {
|
||||||
|
return len(eq) > 0
|
||||||
|
}
|
39
vendor/github.com/go-xorm/builder/cond_expr.go
generated
vendored
Normal file
39
vendor/github.com/go-xorm/builder/cond_expr.go
generated
vendored
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type expr struct {
|
||||||
|
sql string
|
||||||
|
args []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Cond = expr{}
|
||||||
|
|
||||||
|
// Expr generate customerize SQL
|
||||||
|
func Expr(sql string, args ...interface{}) Cond {
|
||||||
|
return expr{sql, args}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (expr expr) WriteTo(w Writer) error {
|
||||||
|
if _, err := fmt.Fprint(w, expr.sql); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(expr.args...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (expr expr) And(conds ...Cond) Cond {
|
||||||
|
return And(expr, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (expr expr) Or(conds ...Cond) Cond {
|
||||||
|
return Or(expr, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (expr expr) IsValid() bool {
|
||||||
|
return len(expr.sql) > 0
|
||||||
|
}
|
237
vendor/github.com/go-xorm/builder/cond_in.go
generated
vendored
Normal file
237
vendor/github.com/go-xorm/builder/cond_in.go
generated
vendored
Normal file
@ -0,0 +1,237 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type condIn struct {
|
||||||
|
col string
|
||||||
|
vals []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Cond = condIn{}
|
||||||
|
|
||||||
|
// In generates IN condition
|
||||||
|
func In(col string, values ...interface{}) Cond {
|
||||||
|
return condIn{col, values}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condIn condIn) handleBlank(w Writer) error {
|
||||||
|
_, err := fmt.Fprint(w, "0=1")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condIn condIn) WriteTo(w Writer) error {
|
||||||
|
if len(condIn.vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch condIn.vals[0].(type) {
|
||||||
|
case []int8:
|
||||||
|
vals := condIn.vals[0].([]int8)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []int16:
|
||||||
|
vals := condIn.vals[0].([]int16)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []int:
|
||||||
|
vals := condIn.vals[0].([]int)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []int32:
|
||||||
|
vals := condIn.vals[0].([]int32)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []int64:
|
||||||
|
vals := condIn.vals[0].([]int64)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint8:
|
||||||
|
vals := condIn.vals[0].([]uint8)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint16:
|
||||||
|
vals := condIn.vals[0].([]uint16)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint:
|
||||||
|
vals := condIn.vals[0].([]uint)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint32:
|
||||||
|
vals := condIn.vals[0].([]uint32)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint64:
|
||||||
|
vals := condIn.vals[0].([]uint64)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []string:
|
||||||
|
vals := condIn.vals[0].([]string)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
vals := condIn.vals[0].([]interface{})
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(vals...)
|
||||||
|
case expr:
|
||||||
|
val := condIn.vals[0].(expr)
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (", condIn.col); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := val.WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *Builder:
|
||||||
|
bd := condIn.vals[0].(*Builder)
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (", condIn.col); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := bd.WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
v := reflect.ValueOf(condIn.vals[0])
|
||||||
|
if v.Kind() == reflect.Slice {
|
||||||
|
l := v.Len()
|
||||||
|
if l == 0 {
|
||||||
|
return condIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
questionMark := strings.Repeat("?,", l)
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < l; i++ {
|
||||||
|
w.Append(v.Index(i).Interface())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
questionMark := strings.Repeat("?,", len(condIn.vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(condIn.vals...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condIn condIn) And(conds ...Cond) Cond {
|
||||||
|
return And(condIn, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condIn condIn) Or(conds ...Cond) Cond {
|
||||||
|
return Or(condIn, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condIn condIn) IsValid() bool {
|
||||||
|
return len(condIn.col) > 0 && len(condIn.vals) > 0
|
||||||
|
}
|
41
vendor/github.com/go-xorm/builder/cond_like.go
generated
vendored
Normal file
41
vendor/github.com/go-xorm/builder/cond_like.go
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Like defines like condition
|
||||||
|
type Like [2]string
|
||||||
|
|
||||||
|
var _ Cond = Like{"", ""}
|
||||||
|
|
||||||
|
// WriteTo write SQL to Writer
|
||||||
|
func (like Like) WriteTo(w Writer) error {
|
||||||
|
if _, err := fmt.Fprintf(w, "%s LIKE ?", like[0]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// FIXME: if use other regular express, this will be failed. but for compitable, keep this
|
||||||
|
if like[1][0] == '%' || like[1][len(like[1])-1] == '%' {
|
||||||
|
w.Append(like[1])
|
||||||
|
} else {
|
||||||
|
w.Append("%" + like[1] + "%")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (like Like) And(conds ...Cond) Cond {
|
||||||
|
return And(like, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (like Like) Or(conds ...Cond) Cond {
|
||||||
|
return Or(like, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this condition is valid
|
||||||
|
func (like Like) IsValid() bool {
|
||||||
|
return len(like[0]) > 0 && len(like[1]) > 0
|
||||||
|
}
|
78
vendor/github.com/go-xorm/builder/cond_neq.go
generated
vendored
Normal file
78
vendor/github.com/go-xorm/builder/cond_neq.go
generated
vendored
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Neq defines not equal conditions
|
||||||
|
type Neq map[string]interface{}
|
||||||
|
|
||||||
|
var _ Cond = Neq{}
|
||||||
|
|
||||||
|
// WriteTo writes SQL to Writer
|
||||||
|
func (neq Neq) WriteTo(w Writer) error {
|
||||||
|
var args = make([]interface{}, 0, len(neq))
|
||||||
|
var i = 0
|
||||||
|
for k, v := range neq {
|
||||||
|
switch v.(type) {
|
||||||
|
case []int, []int64, []string, []int32, []int16, []int8:
|
||||||
|
if err := NotIn(k, v).WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case expr:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := v.(expr).WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *Builder:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := v.(*Builder).WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if _, err := fmt.Fprintf(w, "%s<>?", k); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
args = append(args, v)
|
||||||
|
}
|
||||||
|
if i != len(neq)-1 {
|
||||||
|
if _, err := fmt.Fprint(w, " AND "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
w.Append(args...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (neq Neq) And(conds ...Cond) Cond {
|
||||||
|
return And(neq, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (neq Neq) Or(conds ...Cond) Cond {
|
||||||
|
return Or(neq, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this condition is valid
|
||||||
|
func (neq Neq) IsValid() bool {
|
||||||
|
return len(neq) > 0
|
||||||
|
}
|
53
vendor/github.com/go-xorm/builder/cond_not.go
generated
vendored
Normal file
53
vendor/github.com/go-xorm/builder/cond_not.go
generated
vendored
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Not defines NOT condition
|
||||||
|
type Not [1]Cond
|
||||||
|
|
||||||
|
var _ Cond = Not{}
|
||||||
|
|
||||||
|
// WriteTo writes SQL to Writer
|
||||||
|
func (not Not) WriteTo(w Writer) error {
|
||||||
|
if _, err := fmt.Fprint(w, "NOT "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
switch not[0].(type) {
|
||||||
|
case condAnd, condOr:
|
||||||
|
if _, err := fmt.Fprint(w, "("); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := not[0].WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch not[0].(type) {
|
||||||
|
case condAnd, condOr:
|
||||||
|
if _, err := fmt.Fprint(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (not Not) And(conds ...Cond) Cond {
|
||||||
|
return And(not, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (not Not) Or(conds ...Cond) Cond {
|
||||||
|
return Or(not, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this condition is valid
|
||||||
|
func (not Not) IsValid() bool {
|
||||||
|
return not[0] != nil && not[0].IsValid()
|
||||||
|
}
|
234
vendor/github.com/go-xorm/builder/cond_notin.go
generated
vendored
Normal file
234
vendor/github.com/go-xorm/builder/cond_notin.go
generated
vendored
Normal file
@ -0,0 +1,234 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type condNotIn condIn
|
||||||
|
|
||||||
|
var _ Cond = condNotIn{}
|
||||||
|
|
||||||
|
// NotIn generate NOT IN condition
|
||||||
|
func NotIn(col string, values ...interface{}) Cond {
|
||||||
|
return condNotIn{col, values}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condNotIn condNotIn) handleBlank(w Writer) error {
|
||||||
|
_, err := fmt.Fprint(w, "0=0")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condNotIn condNotIn) WriteTo(w Writer) error {
|
||||||
|
if len(condNotIn.vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch condNotIn.vals[0].(type) {
|
||||||
|
case []int8:
|
||||||
|
vals := condNotIn.vals[0].([]int8)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []int16:
|
||||||
|
vals := condNotIn.vals[0].([]int16)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []int:
|
||||||
|
vals := condNotIn.vals[0].([]int)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []int32:
|
||||||
|
vals := condNotIn.vals[0].([]int32)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []int64:
|
||||||
|
vals := condNotIn.vals[0].([]int64)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint8:
|
||||||
|
vals := condNotIn.vals[0].([]uint8)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint16:
|
||||||
|
vals := condNotIn.vals[0].([]uint16)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint:
|
||||||
|
vals := condNotIn.vals[0].([]uint)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint32:
|
||||||
|
vals := condNotIn.vals[0].([]uint32)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []uint64:
|
||||||
|
vals := condNotIn.vals[0].([]uint64)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []string:
|
||||||
|
vals := condNotIn.vals[0].([]string)
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, val := range vals {
|
||||||
|
w.Append(val)
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
vals := condNotIn.vals[0].([]interface{})
|
||||||
|
if len(vals) <= 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
questionMark := strings.Repeat("?,", len(vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(vals...)
|
||||||
|
case expr:
|
||||||
|
val := condNotIn.vals[0].(expr)
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := val.WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *Builder:
|
||||||
|
val := condNotIn.vals[0].(*Builder)
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := val.WriteTo(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
v := reflect.ValueOf(condNotIn.vals[0])
|
||||||
|
if v.Kind() == reflect.Slice {
|
||||||
|
l := v.Len()
|
||||||
|
if l == 0 {
|
||||||
|
return condNotIn.handleBlank(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
questionMark := strings.Repeat("?,", l)
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < l; i++ {
|
||||||
|
w.Append(v.Index(i).Interface())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
questionMark := strings.Repeat("?,", len(condNotIn.vals))
|
||||||
|
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.Append(condNotIn.vals...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condNotIn condNotIn) And(conds ...Cond) Cond {
|
||||||
|
return And(condNotIn, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condNotIn condNotIn) Or(conds ...Cond) Cond {
|
||||||
|
return Or(condNotIn, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (condNotIn condNotIn) IsValid() bool {
|
||||||
|
return len(condNotIn.col) > 0 && len(condNotIn.vals) > 0
|
||||||
|
}
|
59
vendor/github.com/go-xorm/builder/cond_null.go
generated
vendored
Normal file
59
vendor/github.com/go-xorm/builder/cond_null.go
generated
vendored
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// IsNull defines IS NULL condition
|
||||||
|
type IsNull [1]string
|
||||||
|
|
||||||
|
var _ Cond = IsNull{""}
|
||||||
|
|
||||||
|
// WriteTo write SQL to Writer
|
||||||
|
func (isNull IsNull) WriteTo(w Writer) error {
|
||||||
|
_, err := fmt.Fprintf(w, "%s IS NULL", isNull[0])
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (isNull IsNull) And(conds ...Cond) Cond {
|
||||||
|
return And(isNull, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (isNull IsNull) Or(conds ...Cond) Cond {
|
||||||
|
return Or(isNull, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this condition is valid
|
||||||
|
func (isNull IsNull) IsValid() bool {
|
||||||
|
return len(isNull[0]) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotNull defines NOT NULL condition
|
||||||
|
type NotNull [1]string
|
||||||
|
|
||||||
|
var _ Cond = NotNull{""}
|
||||||
|
|
||||||
|
// WriteTo write SQL to Writer
|
||||||
|
func (notNull NotNull) WriteTo(w Writer) error {
|
||||||
|
_, err := fmt.Fprintf(w, "%s IS NOT NULL", notNull[0])
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// And implements And with other conditions
|
||||||
|
func (notNull NotNull) And(conds ...Cond) Cond {
|
||||||
|
return And(notNull, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or implements Or with other conditions
|
||||||
|
func (notNull NotNull) Or(conds ...Cond) Cond {
|
||||||
|
return Or(notNull, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid tests if this condition is valid
|
||||||
|
func (notNull NotNull) IsValid() bool {
|
||||||
|
return len(notNull[0]) > 0
|
||||||
|
}
|
67
vendor/github.com/go-xorm/builder/cond_or.go
generated
vendored
Normal file
67
vendor/github.com/go-xorm/builder/cond_or.go
generated
vendored
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type condOr []Cond
|
||||||
|
|
||||||
|
var _ Cond = condOr{}
|
||||||
|
|
||||||
|
// Or sets OR conditions
|
||||||
|
func Or(conds ...Cond) Cond {
|
||||||
|
var result = make(condOr, 0, len(conds))
|
||||||
|
for _, cond := range conds {
|
||||||
|
if cond == nil || !cond.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, cond)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo implments Cond
|
||||||
|
func (o condOr) WriteTo(w Writer) error {
|
||||||
|
for i, cond := range o {
|
||||||
|
var needQuote bool
|
||||||
|
switch cond.(type) {
|
||||||
|
case condAnd:
|
||||||
|
needQuote = true
|
||||||
|
case Eq:
|
||||||
|
needQuote = (len(cond.(Eq)) > 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if needQuote {
|
||||||
|
fmt.Fprint(w, "(")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cond.WriteTo(w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if needQuote {
|
||||||
|
fmt.Fprint(w, ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
if i != len(o)-1 {
|
||||||
|
fmt.Fprint(w, " OR ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o condOr) And(conds ...Cond) Cond {
|
||||||
|
return And(o, And(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o condOr) Or(conds ...Cond) Cond {
|
||||||
|
return Or(o, Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o condOr) IsValid() bool {
|
||||||
|
return len(o) > 0
|
||||||
|
}
|
120
vendor/github.com/go-xorm/builder/doc.go
generated
vendored
Normal file
120
vendor/github.com/go-xorm/builder/doc.go
generated
vendored
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
// Copyright 2016 The XORM Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
Package builder is a simple and powerful sql builder for Go.
|
||||||
|
|
||||||
|
Make sure you have installed Go 1.1+ and then:
|
||||||
|
|
||||||
|
go get github.com/go-xorm/builder
|
||||||
|
|
||||||
|
WARNNING: Currently, only query conditions are supported. Below is the supported conditions.
|
||||||
|
|
||||||
|
1. Eq is a redefine of a map, you can give one or more conditions to Eq
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Eq{"a":1})
|
||||||
|
// a=? [1]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
|
||||||
|
// b=? AND c=? ["c", 0]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
|
||||||
|
// b=? AND c=? ["c", 0]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
|
||||||
|
// b=? OR b=? ["c", "d"]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
|
||||||
|
// b IN (?,?) ["c", "d"]
|
||||||
|
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
|
||||||
|
// b=? AND c IN (?,?) [1, 2, 3]
|
||||||
|
|
||||||
|
2. Neq is the same to Eq
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Neq{"a":1})
|
||||||
|
// a<>? [1]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
|
||||||
|
// b<>? AND c<>? ["c", 0]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
|
||||||
|
// b<>? AND c<>? ["c", 0]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
|
||||||
|
// b<>? OR b<>? ["c", "d"]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
|
||||||
|
// b NOT IN (?,?) ["c", "d"]
|
||||||
|
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
|
||||||
|
// b<>? AND c NOT IN (?,?) [1, 2, 3]
|
||||||
|
|
||||||
|
3. Gt, Gte, Lt, Lte
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
|
||||||
|
// a>? AND b>=? [1, 2]
|
||||||
|
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
|
||||||
|
// a<? OR b<=? [1, 2]
|
||||||
|
|
||||||
|
4. Like
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Like{"a", "c"})
|
||||||
|
// a LIKE ? [%c%]
|
||||||
|
|
||||||
|
5. Expr you can customerize your sql with Expr
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Expr("a = ? ", 1))
|
||||||
|
// a = ? [1]
|
||||||
|
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
|
||||||
|
// a=(select id from table where c = ?) [1]
|
||||||
|
|
||||||
|
6. In and NotIn
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(In("a", 1, 2, 3))
|
||||||
|
// a IN (?,?,?) [1,2,3]
|
||||||
|
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
|
||||||
|
// a IN (?,?,?) [1,2,3]
|
||||||
|
sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
|
||||||
|
// a IN (select id from b where c = ?) [1]
|
||||||
|
|
||||||
|
7. IsNull and NotNull
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(IsNull{"a"})
|
||||||
|
// a IS NULL []
|
||||||
|
sql, args, _ := ToSQL(NotNull{"b"})
|
||||||
|
// b IS NOT NULL []
|
||||||
|
|
||||||
|
8. And(conds ...Cond), And can connect one or more condtions via AND
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
|
||||||
|
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]
|
||||||
|
|
||||||
|
9. Or(conds ...Cond), Or can connect one or more conditions via Or
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
|
||||||
|
// a=? OR b LIKE ? OR d<>? [1, %c%, 2]
|
||||||
|
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
|
||||||
|
// a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
|
||||||
|
|
||||||
|
10. Between
|
||||||
|
|
||||||
|
import . "github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
sql, args, _ := ToSQL(Between("a", 1, 2))
|
||||||
|
// a BETWEEN 1 AND 2
|
||||||
|
|
||||||
|
11. define yourself conditions
|
||||||
|
Since Cond is a interface, you can define yourself conditions and compare with them
|
||||||
|
*/
|
||||||
|
package builder
|
16
vendor/github.com/go-xorm/builder/error.go
generated
vendored
Normal file
16
vendor/github.com/go-xorm/builder/error.go
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// Copyright 2016 The Xorm 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 builder
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrNotSupportType not supported SQL type error
|
||||||
|
ErrNotSupportType = errors.New("not supported SQL type")
|
||||||
|
// ErrNoNotInConditions no NOT IN params error
|
||||||
|
ErrNoNotInConditions = errors.New("No NOT IN conditions")
|
||||||
|
// ErrNoInConditions no IN params error
|
||||||
|
ErrNoInConditions = errors.New("No IN conditions")
|
||||||
|
)
|
1
vendor/github.com/go-xorm/builder/go.mod
generated
vendored
Normal file
1
vendor/github.com/go-xorm/builder/go.mod
generated
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
module "github.com/go-xorm/builder"
|
27
vendor/github.com/go-xorm/core/LICENSE
generated
vendored
Normal file
27
vendor/github.com/go-xorm/core/LICENSE
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
Copyright (c) 2013 - 2015 Lunny Xiao <xiaolunwen@gmail.com>
|
||||||
|
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 the {organization} 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 HOLDER 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.
|
116
vendor/github.com/go-xorm/core/README.md
generated
vendored
Normal file
116
vendor/github.com/go-xorm/core/README.md
generated
vendored
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
Core is a lightweight wrapper of sql.DB.
|
||||||
|
|
||||||
|
[![CircleCI](https://circleci.com/gh/go-xorm/core/tree/master.svg?style=svg)](https://circleci.com/gh/go-xorm/core/tree/master)
|
||||||
|
|
||||||
|
# Open
|
||||||
|
```Go
|
||||||
|
db, _ := core.Open(db, connstr)
|
||||||
|
```
|
||||||
|
|
||||||
|
# SetMapper
|
||||||
|
```Go
|
||||||
|
db.SetMapper(SameMapper())
|
||||||
|
```
|
||||||
|
|
||||||
|
## Scan usage
|
||||||
|
|
||||||
|
### Scan
|
||||||
|
```Go
|
||||||
|
rows, _ := db.Query()
|
||||||
|
for rows.Next() {
|
||||||
|
rows.Scan()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ScanMap
|
||||||
|
```Go
|
||||||
|
rows, _ := db.Query()
|
||||||
|
for rows.Next() {
|
||||||
|
rows.ScanMap()
|
||||||
|
```
|
||||||
|
|
||||||
|
### ScanSlice
|
||||||
|
|
||||||
|
You can use `[]string`, `[][]byte`, `[]interface{}`, `[]*string`, `[]sql.NullString` to ScanSclice. Notice, slice's length should be equal or less than select columns.
|
||||||
|
|
||||||
|
```Go
|
||||||
|
rows, _ := db.Query()
|
||||||
|
cols, _ := rows.Columns()
|
||||||
|
for rows.Next() {
|
||||||
|
var s = make([]string, len(cols))
|
||||||
|
rows.ScanSlice(&s)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```Go
|
||||||
|
rows, _ := db.Query()
|
||||||
|
cols, _ := rows.Columns()
|
||||||
|
for rows.Next() {
|
||||||
|
var s = make([]*string, len(cols))
|
||||||
|
rows.ScanSlice(&s)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ScanStruct
|
||||||
|
```Go
|
||||||
|
rows, _ := db.Query()
|
||||||
|
for rows.Next() {
|
||||||
|
rows.ScanStructByName()
|
||||||
|
rows.ScanStructByIndex()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Query usage
|
||||||
|
```Go
|
||||||
|
rows, err := db.Query("select * from table where name = ?", name)
|
||||||
|
|
||||||
|
user = User{
|
||||||
|
Name:"lunny",
|
||||||
|
}
|
||||||
|
rows, err := db.QueryStruct("select * from table where name = ?Name",
|
||||||
|
&user)
|
||||||
|
|
||||||
|
var user = map[string]interface{}{
|
||||||
|
"name": "lunny",
|
||||||
|
}
|
||||||
|
rows, err = db.QueryMap("select * from table where name = ?name",
|
||||||
|
&user)
|
||||||
|
```
|
||||||
|
|
||||||
|
## QueryRow usage
|
||||||
|
```Go
|
||||||
|
row := db.QueryRow("select * from table where name = ?", name)
|
||||||
|
|
||||||
|
user = User{
|
||||||
|
Name:"lunny",
|
||||||
|
}
|
||||||
|
row := db.QueryRowStruct("select * from table where name = ?Name",
|
||||||
|
&user)
|
||||||
|
|
||||||
|
var user = map[string]interface{}{
|
||||||
|
"name": "lunny",
|
||||||
|
}
|
||||||
|
row = db.QueryRowMap("select * from table where name = ?name",
|
||||||
|
&user)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Exec usage
|
||||||
|
```Go
|
||||||
|
db.Exec("insert into user (`name`, title, age, alias, nick_name,created) values (?,?,?,?,?,?)", name, title, age, alias...)
|
||||||
|
|
||||||
|
user = User{
|
||||||
|
Name:"lunny",
|
||||||
|
Title:"test",
|
||||||
|
Age: 18,
|
||||||
|
}
|
||||||
|
result, err = db.ExecStruct("insert into user (`name`, title, age, alias, nick_name,created) values (?Name,?Title,?Age,?Alias,?NickName,?Created)",
|
||||||
|
&user)
|
||||||
|
|
||||||
|
var user = map[string]interface{}{
|
||||||
|
"Name": "lunny",
|
||||||
|
"Title": "test",
|
||||||
|
"Age": 18,
|
||||||
|
}
|
||||||
|
result, err = db.ExecMap("insert into user (`name`, title, age, alias, nick_name,created) values (?Name,?Title,?Age,?Alias,?NickName,?Created)",
|
||||||
|
&user)
|
||||||
|
```
|
1
vendor/github.com/go-xorm/core/benchmark.sh
generated
vendored
Executable file
1
vendor/github.com/go-xorm/core/benchmark.sh
generated
vendored
Executable file
@ -0,0 +1 @@
|
|||||||
|
go test -v -bench=. -run=XXX
|
87
vendor/github.com/go-xorm/core/cache.go
generated
vendored
Normal file
87
vendor/github.com/go-xorm/core/cache.go
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// default cache expired time
|
||||||
|
CacheExpired = 60 * time.Minute
|
||||||
|
// not use now
|
||||||
|
CacheMaxMemory = 256
|
||||||
|
// evey ten minutes to clear all expired nodes
|
||||||
|
CacheGcInterval = 10 * time.Minute
|
||||||
|
// each time when gc to removed max nodes
|
||||||
|
CacheGcMaxRemoved = 20
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrCacheMiss = errors.New("xorm/cache: key not found.")
|
||||||
|
ErrNotStored = errors.New("xorm/cache: not stored.")
|
||||||
|
)
|
||||||
|
|
||||||
|
// CacheStore is a interface to store cache
|
||||||
|
type CacheStore interface {
|
||||||
|
// key is primary key or composite primary key
|
||||||
|
// value is struct's pointer
|
||||||
|
// key format : <tablename>-p-<pk1>-<pk2>...
|
||||||
|
Put(key string, value interface{}) error
|
||||||
|
Get(key string) (interface{}, error)
|
||||||
|
Del(key string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cacher is an interface to provide cache
|
||||||
|
// id format : u-<pk1>-<pk2>...
|
||||||
|
type Cacher interface {
|
||||||
|
GetIds(tableName, sql string) interface{}
|
||||||
|
GetBean(tableName string, id string) interface{}
|
||||||
|
PutIds(tableName, sql string, ids interface{})
|
||||||
|
PutBean(tableName string, id string, obj interface{})
|
||||||
|
DelIds(tableName, sql string)
|
||||||
|
DelBean(tableName string, id string)
|
||||||
|
ClearIds(tableName string)
|
||||||
|
ClearBeans(tableName string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeIds(ids []PK) (string, error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
enc := gob.NewEncoder(buf)
|
||||||
|
err := enc.Encode(ids)
|
||||||
|
|
||||||
|
return buf.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeIds(s string) ([]PK, error) {
|
||||||
|
pks := make([]PK, 0)
|
||||||
|
|
||||||
|
dec := gob.NewDecoder(strings.NewReader(s))
|
||||||
|
err := dec.Decode(&pks)
|
||||||
|
|
||||||
|
return pks, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]PK, error) {
|
||||||
|
bytes := m.GetIds(tableName, GenSqlKey(sql, args))
|
||||||
|
if bytes == nil {
|
||||||
|
return nil, errors.New("Not Exist")
|
||||||
|
}
|
||||||
|
return decodeIds(bytes.(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
func PutCacheSql(m Cacher, ids []PK, tableName, sql string, args interface{}) error {
|
||||||
|
bytes, err := encodeIds(ids)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m.PutIds(tableName, GenSqlKey(sql, args), bytes)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenSqlKey(sql string, args interface{}) string {
|
||||||
|
return fmt.Sprintf("%v-%v", sql, args)
|
||||||
|
}
|
15
vendor/github.com/go-xorm/core/circle.yml
generated
vendored
Normal file
15
vendor/github.com/go-xorm/core/circle.yml
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
dependencies:
|
||||||
|
override:
|
||||||
|
# './...' is a relative pattern which means all subdirectories
|
||||||
|
- go get -t -d -v ./...
|
||||||
|
- go build -v
|
||||||
|
|
||||||
|
database:
|
||||||
|
override:
|
||||||
|
- mysql -u root -e "CREATE DATABASE core_test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"
|
||||||
|
|
||||||
|
test:
|
||||||
|
override:
|
||||||
|
# './...' is a relative pattern which means all subdirectories
|
||||||
|
- go test -v -race
|
||||||
|
- go test -v -race --dbtype=sqlite3
|
159
vendor/github.com/go-xorm/core/column.go
generated
vendored
Normal file
159
vendor/github.com/go-xorm/core/column.go
generated
vendored
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TWOSIDES = iota + 1
|
||||||
|
ONLYTODB
|
||||||
|
ONLYFROMDB
|
||||||
|
)
|
||||||
|
|
||||||
|
// Column defines database column
|
||||||
|
type Column struct {
|
||||||
|
Name string
|
||||||
|
TableName string
|
||||||
|
FieldName string
|
||||||
|
SQLType SQLType
|
||||||
|
IsJSON bool
|
||||||
|
Length int
|
||||||
|
Length2 int
|
||||||
|
Nullable bool
|
||||||
|
Default string
|
||||||
|
Indexes map[string]int
|
||||||
|
IsPrimaryKey bool
|
||||||
|
IsAutoIncrement bool
|
||||||
|
MapType int
|
||||||
|
IsCreated bool
|
||||||
|
IsUpdated bool
|
||||||
|
IsDeleted bool
|
||||||
|
IsCascade bool
|
||||||
|
IsVersion bool
|
||||||
|
DefaultIsEmpty bool
|
||||||
|
EnumOptions map[string]int
|
||||||
|
SetOptions map[string]int
|
||||||
|
DisableTimeZone bool
|
||||||
|
TimeZone *time.Location // column specified time zone
|
||||||
|
Comment string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
|
||||||
|
return &Column{
|
||||||
|
Name: name,
|
||||||
|
TableName: "",
|
||||||
|
FieldName: fieldName,
|
||||||
|
SQLType: sqlType,
|
||||||
|
Length: len1,
|
||||||
|
Length2: len2,
|
||||||
|
Nullable: nullable,
|
||||||
|
Default: "",
|
||||||
|
Indexes: make(map[string]int),
|
||||||
|
IsPrimaryKey: false,
|
||||||
|
IsAutoIncrement: false,
|
||||||
|
MapType: TWOSIDES,
|
||||||
|
IsCreated: false,
|
||||||
|
IsUpdated: false,
|
||||||
|
IsDeleted: false,
|
||||||
|
IsCascade: false,
|
||||||
|
IsVersion: false,
|
||||||
|
DefaultIsEmpty: false,
|
||||||
|
EnumOptions: make(map[string]int),
|
||||||
|
Comment: "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate column description string according dialect
|
||||||
|
func (col *Column) String(d Dialect) string {
|
||||||
|
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
|
||||||
|
|
||||||
|
sql += d.SqlType(col) + " "
|
||||||
|
|
||||||
|
if col.IsPrimaryKey {
|
||||||
|
sql += "PRIMARY KEY "
|
||||||
|
if col.IsAutoIncrement {
|
||||||
|
sql += d.AutoIncrStr() + " "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if col.Default != "" {
|
||||||
|
sql += "DEFAULT " + col.Default + " "
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.ShowCreateNull() {
|
||||||
|
if col.Nullable {
|
||||||
|
sql += "NULL "
|
||||||
|
} else {
|
||||||
|
sql += "NOT NULL "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sql
|
||||||
|
}
|
||||||
|
|
||||||
|
func (col *Column) StringNoPk(d Dialect) string {
|
||||||
|
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
|
||||||
|
|
||||||
|
sql += d.SqlType(col) + " "
|
||||||
|
|
||||||
|
if col.Default != "" {
|
||||||
|
sql += "DEFAULT " + col.Default + " "
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.ShowCreateNull() {
|
||||||
|
if col.Nullable {
|
||||||
|
sql += "NULL "
|
||||||
|
} else {
|
||||||
|
sql += "NOT NULL "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sql
|
||||||
|
}
|
||||||
|
|
||||||
|
// return col's filed of struct's value
|
||||||
|
func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
|
||||||
|
dataStruct := reflect.Indirect(reflect.ValueOf(bean))
|
||||||
|
return col.ValueOfV(&dataStruct)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
|
||||||
|
var fieldValue reflect.Value
|
||||||
|
fieldPath := strings.Split(col.FieldName, ".")
|
||||||
|
|
||||||
|
if dataStruct.Type().Kind() == reflect.Map {
|
||||||
|
keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
|
||||||
|
fieldValue = dataStruct.MapIndex(keyValue)
|
||||||
|
return &fieldValue, nil
|
||||||
|
} else if dataStruct.Type().Kind() == reflect.Interface {
|
||||||
|
structValue := reflect.ValueOf(dataStruct.Interface())
|
||||||
|
dataStruct = &structValue
|
||||||
|
}
|
||||||
|
|
||||||
|
level := len(fieldPath)
|
||||||
|
fieldValue = dataStruct.FieldByName(fieldPath[0])
|
||||||
|
for i := 0; i < level-1; i++ {
|
||||||
|
if !fieldValue.IsValid() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if fieldValue.Kind() == reflect.Struct {
|
||||||
|
fieldValue = fieldValue.FieldByName(fieldPath[i+1])
|
||||||
|
} else if fieldValue.Kind() == reflect.Ptr {
|
||||||
|
if fieldValue.IsNil() {
|
||||||
|
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
|
||||||
|
}
|
||||||
|
fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !fieldValue.IsValid() {
|
||||||
|
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &fieldValue, nil
|
||||||
|
}
|
8
vendor/github.com/go-xorm/core/converstion.go
generated
vendored
Normal file
8
vendor/github.com/go-xorm/core/converstion.go
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
// Conversion is an interface. A type implements Conversion will according
|
||||||
|
// the custom method to fill into database and retrieve from database.
|
||||||
|
type Conversion interface {
|
||||||
|
FromDB([]byte) error
|
||||||
|
ToDB() ([]byte, error)
|
||||||
|
}
|
401
vendor/github.com/go-xorm/core/db.go
generated
vendored
Normal file
401
vendor/github.com/go-xorm/core/db.go
generated
vendored
Normal file
@ -0,0 +1,401 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"regexp"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultCacheSize = 200
|
||||||
|
)
|
||||||
|
|
||||||
|
func MapToSlice(query string, mp interface{}) (string, []interface{}, error) {
|
||||||
|
vv := reflect.ValueOf(mp)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
||||||
|
return "", []interface{}{}, ErrNoMapPointer
|
||||||
|
}
|
||||||
|
|
||||||
|
args := make([]interface{}, 0, len(vv.Elem().MapKeys()))
|
||||||
|
var err error
|
||||||
|
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
||||||
|
v := vv.Elem().MapIndex(reflect.ValueOf(src[1:]))
|
||||||
|
if !v.IsValid() {
|
||||||
|
err = fmt.Errorf("map key %s is missing", src[1:])
|
||||||
|
} else {
|
||||||
|
args = append(args, v.Interface())
|
||||||
|
}
|
||||||
|
return "?"
|
||||||
|
})
|
||||||
|
|
||||||
|
return query, args, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func StructToSlice(query string, st interface{}) (string, []interface{}, error) {
|
||||||
|
vv := reflect.ValueOf(st)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
||||||
|
return "", []interface{}{}, ErrNoStructPointer
|
||||||
|
}
|
||||||
|
|
||||||
|
args := make([]interface{}, 0)
|
||||||
|
var err error
|
||||||
|
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
||||||
|
fv := vv.Elem().FieldByName(src[1:]).Interface()
|
||||||
|
if v, ok := fv.(driver.Valuer); ok {
|
||||||
|
var value driver.Value
|
||||||
|
value, err = v.Value()
|
||||||
|
if err != nil {
|
||||||
|
return "?"
|
||||||
|
}
|
||||||
|
args = append(args, value)
|
||||||
|
} else {
|
||||||
|
args = append(args, fv)
|
||||||
|
}
|
||||||
|
return "?"
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", []interface{}{}, err
|
||||||
|
}
|
||||||
|
return query, args, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type cacheStruct struct {
|
||||||
|
value reflect.Value
|
||||||
|
idx int
|
||||||
|
}
|
||||||
|
|
||||||
|
type DB struct {
|
||||||
|
*sql.DB
|
||||||
|
Mapper IMapper
|
||||||
|
reflectCache map[reflect.Type]*cacheStruct
|
||||||
|
reflectCacheMutex sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func Open(driverName, dataSourceName string) (*DB, error) {
|
||||||
|
db, err := sql.Open(driverName, dataSourceName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &DB{
|
||||||
|
DB: db,
|
||||||
|
Mapper: NewCacheMapper(&SnakeMapper{}),
|
||||||
|
reflectCache: make(map[reflect.Type]*cacheStruct),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func FromDB(db *sql.DB) *DB {
|
||||||
|
return &DB{
|
||||||
|
DB: db,
|
||||||
|
Mapper: NewCacheMapper(&SnakeMapper{}),
|
||||||
|
reflectCache: make(map[reflect.Type]*cacheStruct),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) reflectNew(typ reflect.Type) reflect.Value {
|
||||||
|
db.reflectCacheMutex.Lock()
|
||||||
|
defer db.reflectCacheMutex.Unlock()
|
||||||
|
cs, ok := db.reflectCache[typ]
|
||||||
|
if !ok || cs.idx+1 > DefaultCacheSize-1 {
|
||||||
|
cs = &cacheStruct{reflect.MakeSlice(reflect.SliceOf(typ), DefaultCacheSize, DefaultCacheSize), 0}
|
||||||
|
db.reflectCache[typ] = cs
|
||||||
|
} else {
|
||||||
|
cs.idx = cs.idx + 1
|
||||||
|
}
|
||||||
|
return cs.value.Index(cs.idx).Addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
|
||||||
|
rows, err := db.DB.Query(query, args...)
|
||||||
|
if err != nil {
|
||||||
|
if rows != nil {
|
||||||
|
rows.Close()
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{rows, db}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) QueryMap(query string, mp interface{}) (*Rows, error) {
|
||||||
|
query, args, err := MapToSlice(query, mp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db.Query(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) QueryStruct(query string, st interface{}) (*Rows, error) {
|
||||||
|
query, args, err := StructToSlice(query, st)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db.Query(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
||||||
|
rows, err := db.Query(query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return &Row{nil, err}
|
||||||
|
}
|
||||||
|
return &Row{rows, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) QueryRowMap(query string, mp interface{}) *Row {
|
||||||
|
query, args, err := MapToSlice(query, mp)
|
||||||
|
if err != nil {
|
||||||
|
return &Row{nil, err}
|
||||||
|
}
|
||||||
|
return db.QueryRow(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) QueryRowStruct(query string, st interface{}) *Row {
|
||||||
|
query, args, err := StructToSlice(query, st)
|
||||||
|
if err != nil {
|
||||||
|
return &Row{nil, err}
|
||||||
|
}
|
||||||
|
return db.QueryRow(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Stmt struct {
|
||||||
|
*sql.Stmt
|
||||||
|
db *DB
|
||||||
|
names map[string]int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) Prepare(query string) (*Stmt, error) {
|
||||||
|
names := make(map[string]int)
|
||||||
|
var i int
|
||||||
|
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
||||||
|
names[src[1:]] = i
|
||||||
|
i += 1
|
||||||
|
return "?"
|
||||||
|
})
|
||||||
|
|
||||||
|
stmt, err := db.DB.Prepare(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Stmt{stmt, db, names}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stmt) ExecMap(mp interface{}) (sql.Result, error) {
|
||||||
|
vv := reflect.ValueOf(mp)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
||||||
|
return nil, errors.New("mp should be a map's pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
args := make([]interface{}, len(s.names))
|
||||||
|
for k, i := range s.names {
|
||||||
|
args[i] = vv.Elem().MapIndex(reflect.ValueOf(k)).Interface()
|
||||||
|
}
|
||||||
|
return s.Stmt.Exec(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stmt) ExecStruct(st interface{}) (sql.Result, error) {
|
||||||
|
vv := reflect.ValueOf(st)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
||||||
|
return nil, errors.New("mp should be a map's pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
args := make([]interface{}, len(s.names))
|
||||||
|
for k, i := range s.names {
|
||||||
|
args[i] = vv.Elem().FieldByName(k).Interface()
|
||||||
|
}
|
||||||
|
return s.Stmt.Exec(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
|
||||||
|
rows, err := s.Stmt.Query(args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{rows, s.db}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stmt) QueryMap(mp interface{}) (*Rows, error) {
|
||||||
|
vv := reflect.ValueOf(mp)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
||||||
|
return nil, errors.New("mp should be a map's pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
args := make([]interface{}, len(s.names))
|
||||||
|
for k, i := range s.names {
|
||||||
|
args[i] = vv.Elem().MapIndex(reflect.ValueOf(k)).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.Query(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stmt) QueryStruct(st interface{}) (*Rows, error) {
|
||||||
|
vv := reflect.ValueOf(st)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
||||||
|
return nil, errors.New("mp should be a map's pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
args := make([]interface{}, len(s.names))
|
||||||
|
for k, i := range s.names {
|
||||||
|
args[i] = vv.Elem().FieldByName(k).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.Query(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
||||||
|
rows, err := s.Query(args...)
|
||||||
|
return &Row{rows, err}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stmt) QueryRowMap(mp interface{}) *Row {
|
||||||
|
vv := reflect.ValueOf(mp)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
||||||
|
return &Row{nil, errors.New("mp should be a map's pointer")}
|
||||||
|
}
|
||||||
|
|
||||||
|
args := make([]interface{}, len(s.names))
|
||||||
|
for k, i := range s.names {
|
||||||
|
args[i] = vv.Elem().MapIndex(reflect.ValueOf(k)).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.QueryRow(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stmt) QueryRowStruct(st interface{}) *Row {
|
||||||
|
vv := reflect.ValueOf(st)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
||||||
|
return &Row{nil, errors.New("st should be a struct's pointer")}
|
||||||
|
}
|
||||||
|
|
||||||
|
args := make([]interface{}, len(s.names))
|
||||||
|
for k, i := range s.names {
|
||||||
|
args[i] = vv.Elem().FieldByName(k).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.QueryRow(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
re = regexp.MustCompile(`[?](\w+)`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// insert into (name) values (?)
|
||||||
|
// insert into (name) values (?name)
|
||||||
|
func (db *DB) ExecMap(query string, mp interface{}) (sql.Result, error) {
|
||||||
|
query, args, err := MapToSlice(query, mp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db.DB.Exec(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ExecStruct(query string, st interface{}) (sql.Result, error) {
|
||||||
|
query, args, err := StructToSlice(query, st)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db.DB.Exec(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
type EmptyScanner struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (EmptyScanner) Scan(src interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tx struct {
|
||||||
|
*sql.Tx
|
||||||
|
db *DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) Begin() (*Tx, error) {
|
||||||
|
tx, err := db.DB.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Tx{tx, db}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) Prepare(query string) (*Stmt, error) {
|
||||||
|
names := make(map[string]int)
|
||||||
|
var i int
|
||||||
|
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
||||||
|
names[src[1:]] = i
|
||||||
|
i += 1
|
||||||
|
return "?"
|
||||||
|
})
|
||||||
|
|
||||||
|
stmt, err := tx.Tx.Prepare(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Stmt{stmt, tx.db, names}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
|
||||||
|
// TODO:
|
||||||
|
return stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error) {
|
||||||
|
query, args, err := MapToSlice(query, mp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tx.Tx.Exec(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error) {
|
||||||
|
query, args, err := StructToSlice(query, st)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tx.Tx.Exec(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
|
||||||
|
rows, err := tx.Tx.Query(query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{rows, tx.db}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error) {
|
||||||
|
query, args, err := MapToSlice(query, mp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tx.Query(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error) {
|
||||||
|
query, args, err := StructToSlice(query, st)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tx.Query(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
||||||
|
rows, err := tx.Query(query, args...)
|
||||||
|
return &Row{rows, err}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row {
|
||||||
|
query, args, err := MapToSlice(query, mp)
|
||||||
|
if err != nil {
|
||||||
|
return &Row{nil, err}
|
||||||
|
}
|
||||||
|
return tx.QueryRow(query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row {
|
||||||
|
query, args, err := StructToSlice(query, st)
|
||||||
|
if err != nil {
|
||||||
|
return &Row{nil, err}
|
||||||
|
}
|
||||||
|
return tx.QueryRow(query, args...)
|
||||||
|
}
|
315
vendor/github.com/go-xorm/core/dialect.go
generated
vendored
Normal file
315
vendor/github.com/go-xorm/core/dialect.go
generated
vendored
Normal file
@ -0,0 +1,315 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DbType string
|
||||||
|
|
||||||
|
type Uri struct {
|
||||||
|
DbType DbType
|
||||||
|
Proto string
|
||||||
|
Host string
|
||||||
|
Port string
|
||||||
|
DbName string
|
||||||
|
User string
|
||||||
|
Passwd string
|
||||||
|
Charset string
|
||||||
|
Laddr string
|
||||||
|
Raddr string
|
||||||
|
Timeout time.Duration
|
||||||
|
Schema string
|
||||||
|
}
|
||||||
|
|
||||||
|
// a dialect is a driver's wrapper
|
||||||
|
type Dialect interface {
|
||||||
|
SetLogger(logger ILogger)
|
||||||
|
Init(*DB, *Uri, string, string) error
|
||||||
|
URI() *Uri
|
||||||
|
DB() *DB
|
||||||
|
DBType() DbType
|
||||||
|
SqlType(*Column) string
|
||||||
|
FormatBytes(b []byte) string
|
||||||
|
|
||||||
|
DriverName() string
|
||||||
|
DataSourceName() string
|
||||||
|
|
||||||
|
QuoteStr() string
|
||||||
|
IsReserved(string) bool
|
||||||
|
Quote(string) string
|
||||||
|
AndStr() string
|
||||||
|
OrStr() string
|
||||||
|
EqStr() string
|
||||||
|
RollBackStr() string
|
||||||
|
AutoIncrStr() string
|
||||||
|
|
||||||
|
SupportInsertMany() bool
|
||||||
|
SupportEngine() bool
|
||||||
|
SupportCharset() bool
|
||||||
|
SupportDropIfExists() bool
|
||||||
|
IndexOnTable() bool
|
||||||
|
ShowCreateNull() bool
|
||||||
|
|
||||||
|
IndexCheckSql(tableName, idxName string) (string, []interface{})
|
||||||
|
TableCheckSql(tableName string) (string, []interface{})
|
||||||
|
|
||||||
|
IsColumnExist(tableName string, colName string) (bool, error)
|
||||||
|
|
||||||
|
CreateTableSql(table *Table, tableName, storeEngine, charset string) string
|
||||||
|
DropTableSql(tableName string) string
|
||||||
|
CreateIndexSql(tableName string, index *Index) string
|
||||||
|
DropIndexSql(tableName string, index *Index) string
|
||||||
|
|
||||||
|
ModifyColumnSql(tableName string, col *Column) string
|
||||||
|
|
||||||
|
ForUpdateSql(query string) string
|
||||||
|
|
||||||
|
//CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error
|
||||||
|
//MustDropTable(tableName string) error
|
||||||
|
|
||||||
|
GetColumns(tableName string) ([]string, map[string]*Column, error)
|
||||||
|
GetTables() ([]*Table, error)
|
||||||
|
GetIndexes(tableName string) (map[string]*Index, error)
|
||||||
|
|
||||||
|
Filters() []Filter
|
||||||
|
SetParams(params map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpenDialect(dialect Dialect) (*DB, error) {
|
||||||
|
return Open(dialect.DriverName(), dialect.DataSourceName())
|
||||||
|
}
|
||||||
|
|
||||||
|
type Base struct {
|
||||||
|
db *DB
|
||||||
|
dialect Dialect
|
||||||
|
driverName string
|
||||||
|
dataSourceName string
|
||||||
|
logger ILogger
|
||||||
|
*Uri
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) DB() *DB {
|
||||||
|
return b.db
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) SetLogger(logger ILogger) {
|
||||||
|
b.logger = logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) Init(db *DB, dialect Dialect, uri *Uri, drivername, dataSourceName string) error {
|
||||||
|
b.db, b.dialect, b.Uri = db, dialect, uri
|
||||||
|
b.driverName, b.dataSourceName = drivername, dataSourceName
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) URI() *Uri {
|
||||||
|
return b.Uri
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) DBType() DbType {
|
||||||
|
return b.Uri.DbType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) FormatBytes(bs []byte) string {
|
||||||
|
return fmt.Sprintf("0x%x", bs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) DriverName() string {
|
||||||
|
return b.driverName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) ShowCreateNull() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) DataSourceName() string {
|
||||||
|
return b.dataSourceName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) AndStr() string {
|
||||||
|
return "AND"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) OrStr() string {
|
||||||
|
return "OR"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) EqStr() string {
|
||||||
|
return "="
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Base) RollBackStr() string {
|
||||||
|
return "ROLL BACK"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Base) SupportDropIfExists() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Base) DropTableSql(tableName string) string {
|
||||||
|
quote := db.dialect.Quote
|
||||||
|
return fmt.Sprintf("DROP TABLE IF EXISTS %s", quote(tableName))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Base) HasRecords(query string, args ...interface{}) (bool, error) {
|
||||||
|
db.LogSQL(query, args)
|
||||||
|
rows, err := db.DB().Query(query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
if rows.Next() {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Base) IsColumnExist(tableName, colName string) (bool, error) {
|
||||||
|
query := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
||||||
|
query = strings.Replace(query, "`", db.dialect.QuoteStr(), -1)
|
||||||
|
return db.HasRecords(query, db.DbName, tableName, colName)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
func (db *Base) CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error {
|
||||||
|
sql, args := db.dialect.TableCheckSql(tableName)
|
||||||
|
rows, err := db.DB().Query(sql, args...)
|
||||||
|
if db.Logger != nil {
|
||||||
|
db.Logger.Info("[sql]", sql, args)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
if rows.Next() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
sql = db.dialect.CreateTableSql(table, tableName, storeEngine, charset)
|
||||||
|
_, err = db.DB().Exec(sql)
|
||||||
|
if db.Logger != nil {
|
||||||
|
db.Logger.Info("[sql]", sql)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}*/
|
||||||
|
|
||||||
|
func (db *Base) CreateIndexSql(tableName string, index *Index) string {
|
||||||
|
quote := db.dialect.Quote
|
||||||
|
var unique string
|
||||||
|
var idxName string
|
||||||
|
if index.Type == UniqueType {
|
||||||
|
unique = " UNIQUE"
|
||||||
|
}
|
||||||
|
idxName = index.XName(tableName)
|
||||||
|
return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v)", unique,
|
||||||
|
quote(idxName), quote(tableName),
|
||||||
|
quote(strings.Join(index.Cols, quote(","))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Base) DropIndexSql(tableName string, index *Index) string {
|
||||||
|
quote := db.dialect.Quote
|
||||||
|
var name string
|
||||||
|
if index.IsRegular {
|
||||||
|
name = index.XName(tableName)
|
||||||
|
} else {
|
||||||
|
name = index.Name
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("DROP INDEX %v ON %s", quote(name), quote(tableName))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Base) ModifyColumnSql(tableName string, col *Column) string {
|
||||||
|
return fmt.Sprintf("alter table %s MODIFY COLUMN %s", tableName, col.StringNoPk(db.dialect))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string {
|
||||||
|
var sql string
|
||||||
|
sql = "CREATE TABLE IF NOT EXISTS "
|
||||||
|
if tableName == "" {
|
||||||
|
tableName = table.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
sql += b.dialect.Quote(tableName)
|
||||||
|
sql += " ("
|
||||||
|
|
||||||
|
if len(table.ColumnsSeq()) > 0 {
|
||||||
|
pkList := table.PrimaryKeys
|
||||||
|
|
||||||
|
for _, colName := range table.ColumnsSeq() {
|
||||||
|
col := table.GetColumn(colName)
|
||||||
|
if col.IsPrimaryKey && len(pkList) == 1 {
|
||||||
|
sql += col.String(b.dialect)
|
||||||
|
} else {
|
||||||
|
sql += col.StringNoPk(b.dialect)
|
||||||
|
}
|
||||||
|
sql = strings.TrimSpace(sql)
|
||||||
|
if b.DriverName() == MYSQL && len(col.Comment) > 0 {
|
||||||
|
sql += " COMMENT '" + col.Comment + "'"
|
||||||
|
}
|
||||||
|
sql += ", "
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(pkList) > 1 {
|
||||||
|
sql += "PRIMARY KEY ( "
|
||||||
|
sql += b.dialect.Quote(strings.Join(pkList, b.dialect.Quote(",")))
|
||||||
|
sql += " ), "
|
||||||
|
}
|
||||||
|
|
||||||
|
sql = sql[:len(sql)-2]
|
||||||
|
}
|
||||||
|
sql += ")"
|
||||||
|
|
||||||
|
if b.dialect.SupportEngine() && storeEngine != "" {
|
||||||
|
sql += " ENGINE=" + storeEngine
|
||||||
|
}
|
||||||
|
if b.dialect.SupportCharset() {
|
||||||
|
if len(charset) == 0 {
|
||||||
|
charset = b.dialect.URI().Charset
|
||||||
|
}
|
||||||
|
if len(charset) > 0 {
|
||||||
|
sql += " DEFAULT CHARSET " + charset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sql
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) ForUpdateSql(query string) string {
|
||||||
|
return query + " FOR UPDATE"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) LogSQL(sql string, args []interface{}) {
|
||||||
|
if b.logger != nil && b.logger.IsShowSQL() {
|
||||||
|
if len(args) > 0 {
|
||||||
|
b.logger.Infof("[SQL] %v %v", sql, args)
|
||||||
|
} else {
|
||||||
|
b.logger.Infof("[SQL] %v", sql)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Base) SetParams(params map[string]string) {
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
dialects = map[string]func() Dialect{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterDialect register database dialect
|
||||||
|
func RegisterDialect(dbName DbType, dialectFunc func() Dialect) {
|
||||||
|
if dialectFunc == nil {
|
||||||
|
panic("core: Register dialect is nil")
|
||||||
|
}
|
||||||
|
dialects[strings.ToLower(string(dbName))] = dialectFunc // !nashtsai! allow override dialect
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryDialect query if registed database dialect
|
||||||
|
func QueryDialect(dbName DbType) Dialect {
|
||||||
|
if d, ok := dialects[strings.ToLower(string(dbName))]; ok {
|
||||||
|
return d()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
27
vendor/github.com/go-xorm/core/driver.go
generated
vendored
Normal file
27
vendor/github.com/go-xorm/core/driver.go
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
type Driver interface {
|
||||||
|
Parse(string, string) (*Uri, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
drivers = map[string]Driver{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterDriver(driverName string, driver Driver) {
|
||||||
|
if driver == nil {
|
||||||
|
panic("core: Register driver is nil")
|
||||||
|
}
|
||||||
|
if _, dup := drivers[driverName]; dup {
|
||||||
|
panic("core: Register called twice for driver " + driverName)
|
||||||
|
}
|
||||||
|
drivers[driverName] = driver
|
||||||
|
}
|
||||||
|
|
||||||
|
func QueryDriver(driverName string) Driver {
|
||||||
|
return drivers[driverName]
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisteredDriverSize() int {
|
||||||
|
return len(drivers)
|
||||||
|
}
|
8
vendor/github.com/go-xorm/core/error.go
generated
vendored
Normal file
8
vendor/github.com/go-xorm/core/error.go
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNoMapPointer = errors.New("mp should be a map's pointer")
|
||||||
|
ErrNoStructPointer = errors.New("mp should be a struct's pointer")
|
||||||
|
)
|
64
vendor/github.com/go-xorm/core/filter.go
generated
vendored
Normal file
64
vendor/github.com/go-xorm/core/filter.go
generated
vendored
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Filter is an interface to filter SQL
|
||||||
|
type Filter interface {
|
||||||
|
Do(sql string, dialect Dialect, table *Table) string
|
||||||
|
}
|
||||||
|
|
||||||
|
// QuoteFilter filter SQL replace ` to database's own quote character
|
||||||
|
type QuoteFilter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *QuoteFilter) Do(sql string, dialect Dialect, table *Table) string {
|
||||||
|
return strings.Replace(sql, "`", dialect.QuoteStr(), -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IdFilter filter SQL replace (id) to primary key column name
|
||||||
|
type IdFilter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type Quoter struct {
|
||||||
|
dialect Dialect
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewQuoter(dialect Dialect) *Quoter {
|
||||||
|
return &Quoter{dialect}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Quoter) Quote(content string) string {
|
||||||
|
return q.dialect.QuoteStr() + content + q.dialect.QuoteStr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IdFilter) Do(sql string, dialect Dialect, table *Table) string {
|
||||||
|
quoter := NewQuoter(dialect)
|
||||||
|
if table != nil && len(table.PrimaryKeys) == 1 {
|
||||||
|
sql = strings.Replace(sql, " `(id)` ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
|
||||||
|
sql = strings.Replace(sql, " "+quoter.Quote("(id)")+" ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
|
||||||
|
return strings.Replace(sql, " (id) ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
|
||||||
|
}
|
||||||
|
return sql
|
||||||
|
}
|
||||||
|
|
||||||
|
// SeqFilter filter SQL replace ?, ? ... to $1, $2 ...
|
||||||
|
type SeqFilter struct {
|
||||||
|
Prefix string
|
||||||
|
Start int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeqFilter) Do(sql string, dialect Dialect, table *Table) string {
|
||||||
|
segs := strings.Split(sql, "?")
|
||||||
|
size := len(segs)
|
||||||
|
res := ""
|
||||||
|
for i, c := range segs {
|
||||||
|
if i < size-1 {
|
||||||
|
res += c + fmt.Sprintf("%s%v", s.Prefix, i+s.Start)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += segs[size-1]
|
||||||
|
return res
|
||||||
|
}
|
1
vendor/github.com/go-xorm/core/go.mod
generated
vendored
Normal file
1
vendor/github.com/go-xorm/core/go.mod
generated
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
module "github.com/go-xorm/core"
|
31
vendor/github.com/go-xorm/core/ilogger.go
generated
vendored
Normal file
31
vendor/github.com/go-xorm/core/ilogger.go
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
type LogLevel int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// !nashtsai! following level also match syslog.Priority value
|
||||||
|
LOG_DEBUG LogLevel = iota
|
||||||
|
LOG_INFO
|
||||||
|
LOG_WARNING
|
||||||
|
LOG_ERR
|
||||||
|
LOG_OFF
|
||||||
|
LOG_UNKNOWN
|
||||||
|
)
|
||||||
|
|
||||||
|
// logger interface
|
||||||
|
type ILogger interface {
|
||||||
|
Debug(v ...interface{})
|
||||||
|
Debugf(format string, v ...interface{})
|
||||||
|
Error(v ...interface{})
|
||||||
|
Errorf(format string, v ...interface{})
|
||||||
|
Info(v ...interface{})
|
||||||
|
Infof(format string, v ...interface{})
|
||||||
|
Warn(v ...interface{})
|
||||||
|
Warnf(format string, v ...interface{})
|
||||||
|
|
||||||
|
Level() LogLevel
|
||||||
|
SetLevel(l LogLevel)
|
||||||
|
|
||||||
|
ShowSQL(show ...bool)
|
||||||
|
IsShowSQL() bool
|
||||||
|
}
|
63
vendor/github.com/go-xorm/core/index.go
generated
vendored
Normal file
63
vendor/github.com/go-xorm/core/index.go
generated
vendored
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
IndexType = iota + 1
|
||||||
|
UniqueType
|
||||||
|
)
|
||||||
|
|
||||||
|
// database index
|
||||||
|
type Index struct {
|
||||||
|
IsRegular bool
|
||||||
|
Name string
|
||||||
|
Type int
|
||||||
|
Cols []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (index *Index) XName(tableName string) string {
|
||||||
|
if !strings.HasPrefix(index.Name, "UQE_") &&
|
||||||
|
!strings.HasPrefix(index.Name, "IDX_") {
|
||||||
|
tableName = strings.Replace(tableName, `"`, "", -1)
|
||||||
|
tableName = strings.Replace(tableName, `.`, "_", -1)
|
||||||
|
if index.Type == UniqueType {
|
||||||
|
return fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
|
||||||
|
}
|
||||||
|
return index.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// add columns which will be composite index
|
||||||
|
func (index *Index) AddColumn(cols ...string) {
|
||||||
|
for _, col := range cols {
|
||||||
|
index.Cols = append(index.Cols, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (index *Index) Equal(dst *Index) bool {
|
||||||
|
if index.Type != dst.Type {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(index.Cols) != len(dst.Cols) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
sort.StringSlice(index.Cols).Sort()
|
||||||
|
sort.StringSlice(dst.Cols).Sort()
|
||||||
|
|
||||||
|
for i := 0; i < len(index.Cols); i++ {
|
||||||
|
if index.Cols[i] != dst.Cols[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// new an index
|
||||||
|
func NewIndex(name string, indexType int) *Index {
|
||||||
|
return &Index{true, name, indexType, make([]string, 0)}
|
||||||
|
}
|
254
vendor/github.com/go-xorm/core/mapper.go
generated
vendored
Normal file
254
vendor/github.com/go-xorm/core/mapper.go
generated
vendored
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// name translation between struct, fields names and table, column names
|
||||||
|
type IMapper interface {
|
||||||
|
Obj2Table(string) string
|
||||||
|
Table2Obj(string) string
|
||||||
|
}
|
||||||
|
|
||||||
|
type CacheMapper struct {
|
||||||
|
oriMapper IMapper
|
||||||
|
obj2tableCache map[string]string
|
||||||
|
obj2tableMutex sync.RWMutex
|
||||||
|
table2objCache map[string]string
|
||||||
|
table2objMutex sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCacheMapper(mapper IMapper) *CacheMapper {
|
||||||
|
return &CacheMapper{oriMapper: mapper, obj2tableCache: make(map[string]string),
|
||||||
|
table2objCache: make(map[string]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *CacheMapper) Obj2Table(o string) string {
|
||||||
|
m.obj2tableMutex.RLock()
|
||||||
|
t, ok := m.obj2tableCache[o]
|
||||||
|
m.obj2tableMutex.RUnlock()
|
||||||
|
if ok {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
t = m.oriMapper.Obj2Table(o)
|
||||||
|
m.obj2tableMutex.Lock()
|
||||||
|
m.obj2tableCache[o] = t
|
||||||
|
m.obj2tableMutex.Unlock()
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *CacheMapper) Table2Obj(t string) string {
|
||||||
|
m.table2objMutex.RLock()
|
||||||
|
o, ok := m.table2objCache[t]
|
||||||
|
m.table2objMutex.RUnlock()
|
||||||
|
if ok {
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
o = m.oriMapper.Table2Obj(t)
|
||||||
|
m.table2objMutex.Lock()
|
||||||
|
m.table2objCache[t] = o
|
||||||
|
m.table2objMutex.Unlock()
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SameMapper implements IMapper and provides same name between struct and
|
||||||
|
// database table
|
||||||
|
type SameMapper struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m SameMapper) Obj2Table(o string) string {
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m SameMapper) Table2Obj(t string) string {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
// SnakeMapper implements IMapper and provides name transaltion between
|
||||||
|
// struct and database table
|
||||||
|
type SnakeMapper struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func snakeCasedName(name string) string {
|
||||||
|
newstr := make([]rune, 0)
|
||||||
|
for idx, chr := range name {
|
||||||
|
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
|
||||||
|
if idx > 0 {
|
||||||
|
newstr = append(newstr, '_')
|
||||||
|
}
|
||||||
|
chr -= ('A' - 'a')
|
||||||
|
}
|
||||||
|
newstr = append(newstr, chr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(newstr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mapper SnakeMapper) Obj2Table(name string) string {
|
||||||
|
return snakeCasedName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func titleCasedName(name string) string {
|
||||||
|
newstr := make([]rune, 0)
|
||||||
|
upNextChar := true
|
||||||
|
|
||||||
|
name = strings.ToLower(name)
|
||||||
|
|
||||||
|
for _, chr := range name {
|
||||||
|
switch {
|
||||||
|
case upNextChar:
|
||||||
|
upNextChar = false
|
||||||
|
if 'a' <= chr && chr <= 'z' {
|
||||||
|
chr -= ('a' - 'A')
|
||||||
|
}
|
||||||
|
case chr == '_':
|
||||||
|
upNextChar = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
newstr = append(newstr, chr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(newstr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mapper SnakeMapper) Table2Obj(name string) string {
|
||||||
|
return titleCasedName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GonicMapper implements IMapper. It will consider initialisms when mapping names.
|
||||||
|
// E.g. id -> ID, user -> User and to table names: UserID -> user_id, MyUID -> my_uid
|
||||||
|
type GonicMapper map[string]bool
|
||||||
|
|
||||||
|
func isASCIIUpper(r rune) bool {
|
||||||
|
return 'A' <= r && r <= 'Z'
|
||||||
|
}
|
||||||
|
|
||||||
|
func toASCIIUpper(r rune) rune {
|
||||||
|
if 'a' <= r && r <= 'z' {
|
||||||
|
r -= ('a' - 'A')
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func gonicCasedName(name string) string {
|
||||||
|
newstr := make([]rune, 0, len(name)+3)
|
||||||
|
for idx, chr := range name {
|
||||||
|
if isASCIIUpper(chr) && idx > 0 {
|
||||||
|
if !isASCIIUpper(newstr[len(newstr)-1]) {
|
||||||
|
newstr = append(newstr, '_')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isASCIIUpper(chr) && idx > 1 {
|
||||||
|
l := len(newstr)
|
||||||
|
if isASCIIUpper(newstr[l-1]) && isASCIIUpper(newstr[l-2]) {
|
||||||
|
newstr = append(newstr, newstr[l-1])
|
||||||
|
newstr[l-1] = '_'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newstr = append(newstr, chr)
|
||||||
|
}
|
||||||
|
return strings.ToLower(string(newstr))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mapper GonicMapper) Obj2Table(name string) string {
|
||||||
|
return gonicCasedName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mapper GonicMapper) Table2Obj(name string) string {
|
||||||
|
newstr := make([]rune, 0)
|
||||||
|
|
||||||
|
name = strings.ToLower(name)
|
||||||
|
parts := strings.Split(name, "_")
|
||||||
|
|
||||||
|
for _, p := range parts {
|
||||||
|
_, isInitialism := mapper[strings.ToUpper(p)]
|
||||||
|
for i, r := range p {
|
||||||
|
if i == 0 || isInitialism {
|
||||||
|
r = toASCIIUpper(r)
|
||||||
|
}
|
||||||
|
newstr = append(newstr, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(newstr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A GonicMapper that contains a list of common initialisms taken from golang/lint
|
||||||
|
var LintGonicMapper = GonicMapper{
|
||||||
|
"API": true,
|
||||||
|
"ASCII": true,
|
||||||
|
"CPU": true,
|
||||||
|
"CSS": true,
|
||||||
|
"DNS": true,
|
||||||
|
"EOF": true,
|
||||||
|
"GUID": true,
|
||||||
|
"HTML": true,
|
||||||
|
"HTTP": true,
|
||||||
|
"HTTPS": true,
|
||||||
|
"ID": true,
|
||||||
|
"IP": true,
|
||||||
|
"JSON": true,
|
||||||
|
"LHS": true,
|
||||||
|
"QPS": true,
|
||||||
|
"RAM": true,
|
||||||
|
"RHS": true,
|
||||||
|
"RPC": true,
|
||||||
|
"SLA": true,
|
||||||
|
"SMTP": true,
|
||||||
|
"SSH": true,
|
||||||
|
"TLS": true,
|
||||||
|
"TTL": true,
|
||||||
|
"UI": true,
|
||||||
|
"UID": true,
|
||||||
|
"UUID": true,
|
||||||
|
"URI": true,
|
||||||
|
"URL": true,
|
||||||
|
"UTF8": true,
|
||||||
|
"VM": true,
|
||||||
|
"XML": true,
|
||||||
|
"XSRF": true,
|
||||||
|
"XSS": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// provide prefix table name support
|
||||||
|
type PrefixMapper struct {
|
||||||
|
Mapper IMapper
|
||||||
|
Prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mapper PrefixMapper) Obj2Table(name string) string {
|
||||||
|
return mapper.Prefix + mapper.Mapper.Obj2Table(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mapper PrefixMapper) Table2Obj(name string) string {
|
||||||
|
return mapper.Mapper.Table2Obj(name[len(mapper.Prefix):])
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPrefixMapper(mapper IMapper, prefix string) PrefixMapper {
|
||||||
|
return PrefixMapper{mapper, prefix}
|
||||||
|
}
|
||||||
|
|
||||||
|
// provide suffix table name support
|
||||||
|
type SuffixMapper struct {
|
||||||
|
Mapper IMapper
|
||||||
|
Suffix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mapper SuffixMapper) Obj2Table(name string) string {
|
||||||
|
return mapper.Mapper.Obj2Table(name) + mapper.Suffix
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mapper SuffixMapper) Table2Obj(name string) string {
|
||||||
|
return mapper.Mapper.Table2Obj(name[:len(name)-len(mapper.Suffix)])
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSuffixMapper(mapper IMapper, suffix string) SuffixMapper {
|
||||||
|
return SuffixMapper{mapper, suffix}
|
||||||
|
}
|
26
vendor/github.com/go-xorm/core/pk.go
generated
vendored
Normal file
26
vendor/github.com/go-xorm/core/pk.go
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PK []interface{}
|
||||||
|
|
||||||
|
func NewPK(pks ...interface{}) *PK {
|
||||||
|
p := PK(pks)
|
||||||
|
return &p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PK) ToString() (string, error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
enc := gob.NewEncoder(buf)
|
||||||
|
err := enc.Encode(*p)
|
||||||
|
return buf.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PK) FromString(content string) error {
|
||||||
|
dec := gob.NewDecoder(bytes.NewBufferString(content))
|
||||||
|
err := dec.Decode(p)
|
||||||
|
return err
|
||||||
|
}
|
334
vendor/github.com/go-xorm/core/rows.go
generated
vendored
Normal file
334
vendor/github.com/go-xorm/core/rows.go
generated
vendored
Normal file
@ -0,0 +1,334 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Rows struct {
|
||||||
|
*sql.Rows
|
||||||
|
db *DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rs *Rows) ToMapString() ([]map[string]string, error) {
|
||||||
|
cols, err := rs.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var results = make([]map[string]string, 0, 10)
|
||||||
|
for rs.Next() {
|
||||||
|
var record = make(map[string]string, len(cols))
|
||||||
|
err = rs.ScanMap(&record)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
results = append(results, record)
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan data to a struct's pointer according field index
|
||||||
|
func (rs *Rows) ScanStructByIndex(dest ...interface{}) error {
|
||||||
|
if len(dest) == 0 {
|
||||||
|
return errors.New("at least one struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
vvvs := make([]reflect.Value, len(dest))
|
||||||
|
for i, s := range dest {
|
||||||
|
vv := reflect.ValueOf(s)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
||||||
|
return errors.New("dest should be a struct's pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
vvvs[i] = vv.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
cols, err := rs.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
newDest := make([]interface{}, len(cols))
|
||||||
|
|
||||||
|
var i = 0
|
||||||
|
for _, vvv := range vvvs {
|
||||||
|
for j := 0; j < vvv.NumField(); j++ {
|
||||||
|
newDest[i] = vvv.Field(j).Addr().Interface()
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rs.Rows.Scan(newDest...)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
fieldCache = make(map[reflect.Type]map[string]int)
|
||||||
|
fieldCacheMutex sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func fieldByName(v reflect.Value, name string) reflect.Value {
|
||||||
|
t := v.Type()
|
||||||
|
fieldCacheMutex.RLock()
|
||||||
|
cache, ok := fieldCache[t]
|
||||||
|
fieldCacheMutex.RUnlock()
|
||||||
|
if !ok {
|
||||||
|
cache = make(map[string]int)
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
cache[t.Field(i).Name] = i
|
||||||
|
}
|
||||||
|
fieldCacheMutex.Lock()
|
||||||
|
fieldCache[t] = cache
|
||||||
|
fieldCacheMutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
if i, ok := cache[name]; ok {
|
||||||
|
return v.Field(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return reflect.Zero(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan data to a struct's pointer according field name
|
||||||
|
func (rs *Rows) ScanStructByName(dest interface{}) error {
|
||||||
|
vv := reflect.ValueOf(dest)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
||||||
|
return errors.New("dest should be a struct's pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
cols, err := rs.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newDest := make([]interface{}, len(cols))
|
||||||
|
var v EmptyScanner
|
||||||
|
for j, name := range cols {
|
||||||
|
f := fieldByName(vv.Elem(), rs.db.Mapper.Table2Obj(name))
|
||||||
|
if f.IsValid() {
|
||||||
|
newDest[j] = f.Addr().Interface()
|
||||||
|
} else {
|
||||||
|
newDest[j] = &v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rs.Rows.Scan(newDest...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan data to a slice's pointer, slice's length should equal to columns' number
|
||||||
|
func (rs *Rows) ScanSlice(dest interface{}) error {
|
||||||
|
vv := reflect.ValueOf(dest)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Slice {
|
||||||
|
return errors.New("dest should be a slice's pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
vvv := vv.Elem()
|
||||||
|
cols, err := rs.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newDest := make([]interface{}, len(cols))
|
||||||
|
|
||||||
|
for j := 0; j < len(cols); j++ {
|
||||||
|
if j >= vvv.Len() {
|
||||||
|
newDest[j] = reflect.New(vvv.Type().Elem()).Interface()
|
||||||
|
} else {
|
||||||
|
newDest[j] = vvv.Index(j).Addr().Interface()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rs.Rows.Scan(newDest...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
srcLen := vvv.Len()
|
||||||
|
for i := srcLen; i < len(cols); i++ {
|
||||||
|
vvv = reflect.Append(vvv, reflect.ValueOf(newDest[i]).Elem())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan data to a map's pointer
|
||||||
|
func (rs *Rows) ScanMap(dest interface{}) error {
|
||||||
|
vv := reflect.ValueOf(dest)
|
||||||
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
||||||
|
return errors.New("dest should be a map's pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
cols, err := rs.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newDest := make([]interface{}, len(cols))
|
||||||
|
vvv := vv.Elem()
|
||||||
|
|
||||||
|
for i, _ := range cols {
|
||||||
|
newDest[i] = rs.db.reflectNew(vvv.Type().Elem()).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rs.Rows.Scan(newDest...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, name := range cols {
|
||||||
|
vname := reflect.ValueOf(name)
|
||||||
|
vvv.SetMapIndex(vname, reflect.ValueOf(newDest[i]).Elem())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Row struct {
|
||||||
|
rows *Rows
|
||||||
|
// One of these two will be non-nil:
|
||||||
|
err error // deferred error for easy chaining
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorRow return an error row
|
||||||
|
func ErrorRow(err error) *Row {
|
||||||
|
return &Row{
|
||||||
|
err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRow from rows
|
||||||
|
func NewRow(rows *Rows, err error) *Row {
|
||||||
|
return &Row{rows, err}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (row *Row) Columns() ([]string, error) {
|
||||||
|
if row.err != nil {
|
||||||
|
return nil, row.err
|
||||||
|
}
|
||||||
|
return row.rows.Columns()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (row *Row) Scan(dest ...interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
defer row.rows.Close()
|
||||||
|
|
||||||
|
for _, dp := range dest {
|
||||||
|
if _, ok := dp.(*sql.RawBytes); ok {
|
||||||
|
return errors.New("sql: RawBytes isn't allowed on Row.Scan")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !row.rows.Next() {
|
||||||
|
if err := row.rows.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sql.ErrNoRows
|
||||||
|
}
|
||||||
|
err := row.rows.Scan(dest...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Make sure the query can be processed to completion with no errors.
|
||||||
|
return row.rows.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (row *Row) ScanStructByName(dest interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
defer row.rows.Close()
|
||||||
|
|
||||||
|
if !row.rows.Next() {
|
||||||
|
if err := row.rows.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sql.ErrNoRows
|
||||||
|
}
|
||||||
|
err := row.rows.ScanStructByName(dest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Make sure the query can be processed to completion with no errors.
|
||||||
|
return row.rows.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (row *Row) ScanStructByIndex(dest interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
defer row.rows.Close()
|
||||||
|
|
||||||
|
if !row.rows.Next() {
|
||||||
|
if err := row.rows.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sql.ErrNoRows
|
||||||
|
}
|
||||||
|
err := row.rows.ScanStructByIndex(dest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Make sure the query can be processed to completion with no errors.
|
||||||
|
return row.rows.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan data to a slice's pointer, slice's length should equal to columns' number
|
||||||
|
func (row *Row) ScanSlice(dest interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
defer row.rows.Close()
|
||||||
|
|
||||||
|
if !row.rows.Next() {
|
||||||
|
if err := row.rows.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sql.ErrNoRows
|
||||||
|
}
|
||||||
|
err := row.rows.ScanSlice(dest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the query can be processed to completion with no errors.
|
||||||
|
return row.rows.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan data to a map's pointer
|
||||||
|
func (row *Row) ScanMap(dest interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
defer row.rows.Close()
|
||||||
|
|
||||||
|
if !row.rows.Next() {
|
||||||
|
if err := row.rows.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sql.ErrNoRows
|
||||||
|
}
|
||||||
|
err := row.rows.ScanMap(dest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the query can be processed to completion with no errors.
|
||||||
|
return row.rows.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (row *Row) ToMapString() (map[string]string, error) {
|
||||||
|
cols, err := row.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var record = make(map[string]string, len(cols))
|
||||||
|
err = row.ScanMap(&record)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return record, nil
|
||||||
|
}
|
55
vendor/github.com/go-xorm/core/scan.go
generated
vendored
Normal file
55
vendor/github.com/go-xorm/core/scan.go
generated
vendored
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NullTime time.Time
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ driver.Valuer = NullTime{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (ns *NullTime) Scan(value interface{}) error {
|
||||||
|
if value == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return convertTime(ns, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value implements the driver Valuer interface.
|
||||||
|
func (ns NullTime) Value() (driver.Value, error) {
|
||||||
|
if (time.Time)(ns).IsZero() {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return (time.Time)(ns).Format("2006-01-02 15:04:05"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertTime(dest *NullTime, src interface{}) error {
|
||||||
|
// Common cases, without reflect.
|
||||||
|
switch s := src.(type) {
|
||||||
|
case string:
|
||||||
|
t, err := time.Parse("2006-01-02 15:04:05", s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*dest = NullTime(t)
|
||||||
|
return nil
|
||||||
|
case []uint8:
|
||||||
|
t, err := time.Parse("2006-01-02 15:04:05", string(s))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*dest = NullTime(t)
|
||||||
|
return nil
|
||||||
|
case time.Time:
|
||||||
|
*dest = NullTime(s)
|
||||||
|
return nil
|
||||||
|
case nil:
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported driver -> Scan pair: %T -> %T", src, dest)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
152
vendor/github.com/go-xorm/core/table.go
generated
vendored
Normal file
152
vendor/github.com/go-xorm/core/table.go
generated
vendored
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// database table
|
||||||
|
type Table struct {
|
||||||
|
Name string
|
||||||
|
Type reflect.Type
|
||||||
|
columnsSeq []string
|
||||||
|
columnsMap map[string][]*Column
|
||||||
|
columns []*Column
|
||||||
|
Indexes map[string]*Index
|
||||||
|
PrimaryKeys []string
|
||||||
|
AutoIncrement string
|
||||||
|
Created map[string]bool
|
||||||
|
Updated string
|
||||||
|
Deleted string
|
||||||
|
Version string
|
||||||
|
Cacher Cacher
|
||||||
|
StoreEngine string
|
||||||
|
Charset string
|
||||||
|
Comment string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) Columns() []*Column {
|
||||||
|
return table.columns
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) ColumnsSeq() []string {
|
||||||
|
return table.columnsSeq
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEmptyTable() *Table {
|
||||||
|
return NewTable("", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTable(name string, t reflect.Type) *Table {
|
||||||
|
return &Table{Name: name, Type: t,
|
||||||
|
columnsSeq: make([]string, 0),
|
||||||
|
columns: make([]*Column, 0),
|
||||||
|
columnsMap: make(map[string][]*Column),
|
||||||
|
Indexes: make(map[string]*Index),
|
||||||
|
Created: make(map[string]bool),
|
||||||
|
PrimaryKeys: make([]string, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) columnsByName(name string) []*Column {
|
||||||
|
|
||||||
|
n := len(name)
|
||||||
|
|
||||||
|
for k := range table.columnsMap {
|
||||||
|
if len(k) != n {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.EqualFold(k, name) {
|
||||||
|
return table.columnsMap[k]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) GetColumn(name string) *Column {
|
||||||
|
|
||||||
|
cols := table.columnsByName(name)
|
||||||
|
|
||||||
|
if cols != nil {
|
||||||
|
return cols[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) GetColumnIdx(name string, idx int) *Column {
|
||||||
|
|
||||||
|
cols := table.columnsByName(name)
|
||||||
|
|
||||||
|
if cols != nil && idx < len(cols) {
|
||||||
|
return cols[idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// if has primary key, return column
|
||||||
|
func (table *Table) PKColumns() []*Column {
|
||||||
|
columns := make([]*Column, len(table.PrimaryKeys))
|
||||||
|
for i, name := range table.PrimaryKeys {
|
||||||
|
columns[i] = table.GetColumn(name)
|
||||||
|
}
|
||||||
|
return columns
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) ColumnType(name string) reflect.Type {
|
||||||
|
t, _ := table.Type.FieldByName(name)
|
||||||
|
return t.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) AutoIncrColumn() *Column {
|
||||||
|
return table.GetColumn(table.AutoIncrement)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) VersionColumn() *Column {
|
||||||
|
return table.GetColumn(table.Version)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) UpdatedColumn() *Column {
|
||||||
|
return table.GetColumn(table.Updated)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) DeletedColumn() *Column {
|
||||||
|
return table.GetColumn(table.Deleted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add a column to table
|
||||||
|
func (table *Table) AddColumn(col *Column) {
|
||||||
|
table.columnsSeq = append(table.columnsSeq, col.Name)
|
||||||
|
table.columns = append(table.columns, col)
|
||||||
|
colName := strings.ToLower(col.Name)
|
||||||
|
if c, ok := table.columnsMap[colName]; ok {
|
||||||
|
table.columnsMap[colName] = append(c, col)
|
||||||
|
} else {
|
||||||
|
table.columnsMap[colName] = []*Column{col}
|
||||||
|
}
|
||||||
|
|
||||||
|
if col.IsPrimaryKey {
|
||||||
|
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
|
||||||
|
}
|
||||||
|
if col.IsAutoIncrement {
|
||||||
|
table.AutoIncrement = col.Name
|
||||||
|
}
|
||||||
|
if col.IsCreated {
|
||||||
|
table.Created[col.Name] = true
|
||||||
|
}
|
||||||
|
if col.IsUpdated {
|
||||||
|
table.Updated = col.Name
|
||||||
|
}
|
||||||
|
if col.IsDeleted {
|
||||||
|
table.Deleted = col.Name
|
||||||
|
}
|
||||||
|
if col.IsVersion {
|
||||||
|
table.Version = col.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add an index or an unique to table
|
||||||
|
func (table *Table) AddIndex(index *Index) {
|
||||||
|
table.Indexes[index.Name] = index
|
||||||
|
}
|
309
vendor/github.com/go-xorm/core/type.go
generated
vendored
Normal file
309
vendor/github.com/go-xorm/core/type.go
generated
vendored
Normal file
@ -0,0 +1,309 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
POSTGRES = "postgres"
|
||||||
|
SQLITE = "sqlite3"
|
||||||
|
MYSQL = "mysql"
|
||||||
|
MSSQL = "mssql"
|
||||||
|
ORACLE = "oracle"
|
||||||
|
)
|
||||||
|
|
||||||
|
// xorm SQL types
|
||||||
|
type SQLType struct {
|
||||||
|
Name string
|
||||||
|
DefaultLength int
|
||||||
|
DefaultLength2 int
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
UNKNOW_TYPE = iota
|
||||||
|
TEXT_TYPE
|
||||||
|
BLOB_TYPE
|
||||||
|
TIME_TYPE
|
||||||
|
NUMERIC_TYPE
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *SQLType) IsType(st int) bool {
|
||||||
|
if t, ok := SqlTypes[s.Name]; ok && t == st {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SQLType) IsText() bool {
|
||||||
|
return s.IsType(TEXT_TYPE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SQLType) IsBlob() bool {
|
||||||
|
return s.IsType(BLOB_TYPE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SQLType) IsTime() bool {
|
||||||
|
return s.IsType(TIME_TYPE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SQLType) IsNumeric() bool {
|
||||||
|
return s.IsType(NUMERIC_TYPE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SQLType) IsJson() bool {
|
||||||
|
return s.Name == Json || s.Name == Jsonb
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
Bit = "BIT"
|
||||||
|
TinyInt = "TINYINT"
|
||||||
|
SmallInt = "SMALLINT"
|
||||||
|
MediumInt = "MEDIUMINT"
|
||||||
|
Int = "INT"
|
||||||
|
Integer = "INTEGER"
|
||||||
|
BigInt = "BIGINT"
|
||||||
|
|
||||||
|
Enum = "ENUM"
|
||||||
|
Set = "SET"
|
||||||
|
|
||||||
|
Char = "CHAR"
|
||||||
|
Varchar = "VARCHAR"
|
||||||
|
NVarchar = "NVARCHAR"
|
||||||
|
TinyText = "TINYTEXT"
|
||||||
|
Text = "TEXT"
|
||||||
|
Clob = "CLOB"
|
||||||
|
MediumText = "MEDIUMTEXT"
|
||||||
|
LongText = "LONGTEXT"
|
||||||
|
Uuid = "UUID"
|
||||||
|
UniqueIdentifier = "UNIQUEIDENTIFIER"
|
||||||
|
SysName = "SYSNAME"
|
||||||
|
|
||||||
|
Date = "DATE"
|
||||||
|
DateTime = "DATETIME"
|
||||||
|
Time = "TIME"
|
||||||
|
TimeStamp = "TIMESTAMP"
|
||||||
|
TimeStampz = "TIMESTAMPZ"
|
||||||
|
|
||||||
|
Decimal = "DECIMAL"
|
||||||
|
Numeric = "NUMERIC"
|
||||||
|
|
||||||
|
Real = "REAL"
|
||||||
|
Float = "FLOAT"
|
||||||
|
Double = "DOUBLE"
|
||||||
|
|
||||||
|
Binary = "BINARY"
|
||||||
|
VarBinary = "VARBINARY"
|
||||||
|
TinyBlob = "TINYBLOB"
|
||||||
|
Blob = "BLOB"
|
||||||
|
MediumBlob = "MEDIUMBLOB"
|
||||||
|
LongBlob = "LONGBLOB"
|
||||||
|
Bytea = "BYTEA"
|
||||||
|
|
||||||
|
Bool = "BOOL"
|
||||||
|
Boolean = "BOOLEAN"
|
||||||
|
|
||||||
|
Serial = "SERIAL"
|
||||||
|
BigSerial = "BIGSERIAL"
|
||||||
|
|
||||||
|
Json = "JSON"
|
||||||
|
Jsonb = "JSONB"
|
||||||
|
|
||||||
|
SqlTypes = map[string]int{
|
||||||
|
Bit: NUMERIC_TYPE,
|
||||||
|
TinyInt: NUMERIC_TYPE,
|
||||||
|
SmallInt: NUMERIC_TYPE,
|
||||||
|
MediumInt: NUMERIC_TYPE,
|
||||||
|
Int: NUMERIC_TYPE,
|
||||||
|
Integer: NUMERIC_TYPE,
|
||||||
|
BigInt: NUMERIC_TYPE,
|
||||||
|
|
||||||
|
Enum: TEXT_TYPE,
|
||||||
|
Set: TEXT_TYPE,
|
||||||
|
Json: TEXT_TYPE,
|
||||||
|
Jsonb: TEXT_TYPE,
|
||||||
|
|
||||||
|
Char: TEXT_TYPE,
|
||||||
|
Varchar: TEXT_TYPE,
|
||||||
|
NVarchar: TEXT_TYPE,
|
||||||
|
TinyText: TEXT_TYPE,
|
||||||
|
Text: TEXT_TYPE,
|
||||||
|
MediumText: TEXT_TYPE,
|
||||||
|
LongText: TEXT_TYPE,
|
||||||
|
Uuid: TEXT_TYPE,
|
||||||
|
Clob: TEXT_TYPE,
|
||||||
|
SysName: TEXT_TYPE,
|
||||||
|
|
||||||
|
Date: TIME_TYPE,
|
||||||
|
DateTime: TIME_TYPE,
|
||||||
|
Time: TIME_TYPE,
|
||||||
|
TimeStamp: TIME_TYPE,
|
||||||
|
TimeStampz: TIME_TYPE,
|
||||||
|
|
||||||
|
Decimal: NUMERIC_TYPE,
|
||||||
|
Numeric: NUMERIC_TYPE,
|
||||||
|
Real: NUMERIC_TYPE,
|
||||||
|
Float: NUMERIC_TYPE,
|
||||||
|
Double: NUMERIC_TYPE,
|
||||||
|
|
||||||
|
Binary: BLOB_TYPE,
|
||||||
|
VarBinary: BLOB_TYPE,
|
||||||
|
|
||||||
|
TinyBlob: BLOB_TYPE,
|
||||||
|
Blob: BLOB_TYPE,
|
||||||
|
MediumBlob: BLOB_TYPE,
|
||||||
|
LongBlob: BLOB_TYPE,
|
||||||
|
Bytea: BLOB_TYPE,
|
||||||
|
UniqueIdentifier: BLOB_TYPE,
|
||||||
|
|
||||||
|
Bool: NUMERIC_TYPE,
|
||||||
|
|
||||||
|
Serial: NUMERIC_TYPE,
|
||||||
|
BigSerial: NUMERIC_TYPE,
|
||||||
|
}
|
||||||
|
|
||||||
|
intTypes = sort.StringSlice{"*int", "*int16", "*int32", "*int8"}
|
||||||
|
uintTypes = sort.StringSlice{"*uint", "*uint16", "*uint32", "*uint8"}
|
||||||
|
)
|
||||||
|
|
||||||
|
// !nashtsai! treat following var as interal const values, these are used for reflect.TypeOf comparison
|
||||||
|
var (
|
||||||
|
c_EMPTY_STRING string
|
||||||
|
c_BOOL_DEFAULT bool
|
||||||
|
c_BYTE_DEFAULT byte
|
||||||
|
c_COMPLEX64_DEFAULT complex64
|
||||||
|
c_COMPLEX128_DEFAULT complex128
|
||||||
|
c_FLOAT32_DEFAULT float32
|
||||||
|
c_FLOAT64_DEFAULT float64
|
||||||
|
c_INT64_DEFAULT int64
|
||||||
|
c_UINT64_DEFAULT uint64
|
||||||
|
c_INT32_DEFAULT int32
|
||||||
|
c_UINT32_DEFAULT uint32
|
||||||
|
c_INT16_DEFAULT int16
|
||||||
|
c_UINT16_DEFAULT uint16
|
||||||
|
c_INT8_DEFAULT int8
|
||||||
|
c_UINT8_DEFAULT uint8
|
||||||
|
c_INT_DEFAULT int
|
||||||
|
c_UINT_DEFAULT uint
|
||||||
|
c_TIME_DEFAULT time.Time
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
IntType = reflect.TypeOf(c_INT_DEFAULT)
|
||||||
|
Int8Type = reflect.TypeOf(c_INT8_DEFAULT)
|
||||||
|
Int16Type = reflect.TypeOf(c_INT16_DEFAULT)
|
||||||
|
Int32Type = reflect.TypeOf(c_INT32_DEFAULT)
|
||||||
|
Int64Type = reflect.TypeOf(c_INT64_DEFAULT)
|
||||||
|
|
||||||
|
UintType = reflect.TypeOf(c_UINT_DEFAULT)
|
||||||
|
Uint8Type = reflect.TypeOf(c_UINT8_DEFAULT)
|
||||||
|
Uint16Type = reflect.TypeOf(c_UINT16_DEFAULT)
|
||||||
|
Uint32Type = reflect.TypeOf(c_UINT32_DEFAULT)
|
||||||
|
Uint64Type = reflect.TypeOf(c_UINT64_DEFAULT)
|
||||||
|
|
||||||
|
Float32Type = reflect.TypeOf(c_FLOAT32_DEFAULT)
|
||||||
|
Float64Type = reflect.TypeOf(c_FLOAT64_DEFAULT)
|
||||||
|
|
||||||
|
Complex64Type = reflect.TypeOf(c_COMPLEX64_DEFAULT)
|
||||||
|
Complex128Type = reflect.TypeOf(c_COMPLEX128_DEFAULT)
|
||||||
|
|
||||||
|
StringType = reflect.TypeOf(c_EMPTY_STRING)
|
||||||
|
BoolType = reflect.TypeOf(c_BOOL_DEFAULT)
|
||||||
|
ByteType = reflect.TypeOf(c_BYTE_DEFAULT)
|
||||||
|
BytesType = reflect.SliceOf(ByteType)
|
||||||
|
|
||||||
|
TimeType = reflect.TypeOf(c_TIME_DEFAULT)
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
PtrIntType = reflect.PtrTo(IntType)
|
||||||
|
PtrInt8Type = reflect.PtrTo(Int8Type)
|
||||||
|
PtrInt16Type = reflect.PtrTo(Int16Type)
|
||||||
|
PtrInt32Type = reflect.PtrTo(Int32Type)
|
||||||
|
PtrInt64Type = reflect.PtrTo(Int64Type)
|
||||||
|
|
||||||
|
PtrUintType = reflect.PtrTo(UintType)
|
||||||
|
PtrUint8Type = reflect.PtrTo(Uint8Type)
|
||||||
|
PtrUint16Type = reflect.PtrTo(Uint16Type)
|
||||||
|
PtrUint32Type = reflect.PtrTo(Uint32Type)
|
||||||
|
PtrUint64Type = reflect.PtrTo(Uint64Type)
|
||||||
|
|
||||||
|
PtrFloat32Type = reflect.PtrTo(Float32Type)
|
||||||
|
PtrFloat64Type = reflect.PtrTo(Float64Type)
|
||||||
|
|
||||||
|
PtrComplex64Type = reflect.PtrTo(Complex64Type)
|
||||||
|
PtrComplex128Type = reflect.PtrTo(Complex128Type)
|
||||||
|
|
||||||
|
PtrStringType = reflect.PtrTo(StringType)
|
||||||
|
PtrBoolType = reflect.PtrTo(BoolType)
|
||||||
|
PtrByteType = reflect.PtrTo(ByteType)
|
||||||
|
|
||||||
|
PtrTimeType = reflect.PtrTo(TimeType)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Type2SQLType generate SQLType acorrding Go's type
|
||||||
|
func Type2SQLType(t reflect.Type) (st SQLType) {
|
||||||
|
switch k := t.Kind(); k {
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||||
|
st = SQLType{Int, 0, 0}
|
||||||
|
case reflect.Int64, reflect.Uint64:
|
||||||
|
st = SQLType{BigInt, 0, 0}
|
||||||
|
case reflect.Float32:
|
||||||
|
st = SQLType{Float, 0, 0}
|
||||||
|
case reflect.Float64:
|
||||||
|
st = SQLType{Double, 0, 0}
|
||||||
|
case reflect.Complex64, reflect.Complex128:
|
||||||
|
st = SQLType{Varchar, 64, 0}
|
||||||
|
case reflect.Array, reflect.Slice, reflect.Map:
|
||||||
|
if t.Elem() == reflect.TypeOf(c_BYTE_DEFAULT) {
|
||||||
|
st = SQLType{Blob, 0, 0}
|
||||||
|
} else {
|
||||||
|
st = SQLType{Text, 0, 0}
|
||||||
|
}
|
||||||
|
case reflect.Bool:
|
||||||
|
st = SQLType{Bool, 0, 0}
|
||||||
|
case reflect.String:
|
||||||
|
st = SQLType{Varchar, 255, 0}
|
||||||
|
case reflect.Struct:
|
||||||
|
if t.ConvertibleTo(TimeType) {
|
||||||
|
st = SQLType{DateTime, 0, 0}
|
||||||
|
} else {
|
||||||
|
// TODO need to handle association struct
|
||||||
|
st = SQLType{Text, 0, 0}
|
||||||
|
}
|
||||||
|
case reflect.Ptr:
|
||||||
|
st = Type2SQLType(t.Elem())
|
||||||
|
default:
|
||||||
|
st = SQLType{Text, 0, 0}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// default sql type change to go types
|
||||||
|
func SQLType2Type(st SQLType) reflect.Type {
|
||||||
|
name := strings.ToUpper(st.Name)
|
||||||
|
switch name {
|
||||||
|
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, Serial:
|
||||||
|
return reflect.TypeOf(1)
|
||||||
|
case BigInt, BigSerial:
|
||||||
|
return reflect.TypeOf(int64(1))
|
||||||
|
case Float, Real:
|
||||||
|
return reflect.TypeOf(float32(1))
|
||||||
|
case Double:
|
||||||
|
return reflect.TypeOf(float64(1))
|
||||||
|
case Char, Varchar, NVarchar, TinyText, Text, MediumText, LongText, Enum, Set, Uuid, Clob, SysName:
|
||||||
|
return reflect.TypeOf("")
|
||||||
|
case TinyBlob, Blob, LongBlob, Bytea, Binary, MediumBlob, VarBinary, UniqueIdentifier:
|
||||||
|
return reflect.TypeOf([]byte{})
|
||||||
|
case Bool:
|
||||||
|
return reflect.TypeOf(true)
|
||||||
|
case DateTime, Date, Time, TimeStamp, TimeStampz:
|
||||||
|
return reflect.TypeOf(c_TIME_DEFAULT)
|
||||||
|
case Decimal, Numeric:
|
||||||
|
return reflect.TypeOf("")
|
||||||
|
default:
|
||||||
|
return reflect.TypeOf("")
|
||||||
|
}
|
||||||
|
}
|
46
vendor/github.com/go-xorm/xorm/CONTRIBUTING.md
generated
vendored
Normal file
46
vendor/github.com/go-xorm/xorm/CONTRIBUTING.md
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
## Contributing to xorm
|
||||||
|
|
||||||
|
`xorm` has a backlog of [pull requests](https://help.github.com/articles/using-pull-requests), but contributions are still very
|
||||||
|
much welcome. You can help with patch review, submitting bug reports,
|
||||||
|
or adding new functionality. There is no formal style guide, but
|
||||||
|
please conform to the style of existing code and general Go formatting
|
||||||
|
conventions when submitting patches.
|
||||||
|
|
||||||
|
* [fork a repo](https://help.github.com/articles/fork-a-repo)
|
||||||
|
* [creating a pull request ](https://help.github.com/articles/creating-a-pull-request)
|
||||||
|
|
||||||
|
### Language
|
||||||
|
|
||||||
|
Since `xorm` is a world-wide open source project, please describe your issues or code changes in English as soon as possible.
|
||||||
|
|
||||||
|
### Sign your codes with comments
|
||||||
|
```
|
||||||
|
// !<you github id>! your comments
|
||||||
|
|
||||||
|
e.g.,
|
||||||
|
|
||||||
|
// !lunny! this is comments made by lunny
|
||||||
|
```
|
||||||
|
|
||||||
|
### Patch review
|
||||||
|
|
||||||
|
Help review existing open [pull requests](https://help.github.com/articles/using-pull-requests) by commenting on the code or
|
||||||
|
proposed functionality.
|
||||||
|
|
||||||
|
### Bug reports
|
||||||
|
|
||||||
|
We appreciate any bug reports, but especially ones with self-contained
|
||||||
|
(doesn't depend on code outside of xorm), minimal (can't be simplified
|
||||||
|
further) test cases. It's especially helpful if you can submit a pull
|
||||||
|
request with just the failing test case(you can find some example test file like [session_get_test.go](https://github.com/go-xorm/xorm/blob/master/session_get_test.go)).
|
||||||
|
|
||||||
|
If you implements a new database interface, you maybe need to add a test_<databasename>.sh file.
|
||||||
|
For example, [mysql_test.go](https://github.com/go-xorm/xorm/blob/master/test_mysql.sh)
|
||||||
|
|
||||||
|
### New functionality
|
||||||
|
|
||||||
|
There are a number of pending patches for new functionality, so
|
||||||
|
additional feature patches will take a while to merge. Still, patches
|
||||||
|
are generally reviewed based on usefulness and complexity in addition
|
||||||
|
to time-in-queue, so if you have a knockout idea, take a shot. Feel
|
||||||
|
free to open an issue discussion your proposed patch beforehand.
|
27
vendor/github.com/go-xorm/xorm/LICENSE
generated
vendored
Normal file
27
vendor/github.com/go-xorm/xorm/LICENSE
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
Copyright (c) 2013 - 2015 The Xorm 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 the {organization} 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 HOLDER 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.
|
437
vendor/github.com/go-xorm/xorm/README.md
generated
vendored
Normal file
437
vendor/github.com/go-xorm/xorm/README.md
generated
vendored
Normal file
@ -0,0 +1,437 @@
|
|||||||
|
[中文](https://github.com/go-xorm/xorm/blob/master/README_CN.md)
|
||||||
|
|
||||||
|
Xorm is a simple and powerful ORM for Go.
|
||||||
|
|
||||||
|
[![CircleCI](https://circleci.com/gh/go-xorm/xorm.svg?style=shield)](https://circleci.com/gh/go-xorm/xorm) [![codecov](https://codecov.io/gh/go-xorm/xorm/branch/master/graph/badge.svg)](https://codecov.io/gh/go-xorm/xorm)
|
||||||
|
[![](https://goreportcard.com/badge/github.com/go-xorm/xorm)](https://goreportcard.com/report/github.com/go-xorm/xorm)
|
||||||
|
[![Join the chat at https://img.shields.io/discord/323460943201959939.svg](https://img.shields.io/discord/323460943201959939.svg)](https://discord.gg/HuR2CF3)
|
||||||
|
|
||||||
|
# Features
|
||||||
|
|
||||||
|
* Struct <-> Table Mapping Support
|
||||||
|
|
||||||
|
* Chainable APIs
|
||||||
|
|
||||||
|
* Transaction Support
|
||||||
|
|
||||||
|
* Both ORM and raw SQL operation Support
|
||||||
|
|
||||||
|
* Sync database schema Support
|
||||||
|
|
||||||
|
* Query Cache speed up
|
||||||
|
|
||||||
|
* Database Reverse support, See [Xorm Tool README](https://github.com/go-xorm/cmd/blob/master/README.md)
|
||||||
|
|
||||||
|
* Simple cascade loading support
|
||||||
|
|
||||||
|
* Optimistic Locking support
|
||||||
|
|
||||||
|
* SQL Builder support via [github.com/go-xorm/builder](https://github.com/go-xorm/builder)
|
||||||
|
|
||||||
|
* Automatical Read/Write seperatelly
|
||||||
|
|
||||||
|
* Postgres schema support
|
||||||
|
|
||||||
|
# Drivers Support
|
||||||
|
|
||||||
|
Drivers for Go's sql package which currently support database/sql includes:
|
||||||
|
|
||||||
|
* Mysql: [github.com/go-sql-driver/mysql](https://github.com/go-sql-driver/mysql)
|
||||||
|
|
||||||
|
* MyMysql: [github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/tree/master/godrv)
|
||||||
|
|
||||||
|
* Postgres: [github.com/lib/pq](https://github.com/lib/pq)
|
||||||
|
|
||||||
|
* Tidb: [github.com/pingcap/tidb](https://github.com/pingcap/tidb)
|
||||||
|
|
||||||
|
* SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
|
||||||
|
|
||||||
|
* MsSql: [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
|
||||||
|
|
||||||
|
* Oracle: [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (experiment)
|
||||||
|
|
||||||
|
# Changelog
|
||||||
|
|
||||||
|
* **v0.6.6**
|
||||||
|
* Some bugs fixed
|
||||||
|
|
||||||
|
* **v0.6.5**
|
||||||
|
* Postgres schema support
|
||||||
|
* vgo support
|
||||||
|
* Add FindAndCount
|
||||||
|
* Database special params support via NewEngineWithParams
|
||||||
|
* Some bugs fixed
|
||||||
|
|
||||||
|
* **v0.6.4**
|
||||||
|
* Automatical Read/Write seperatelly
|
||||||
|
* Query/QueryString/QueryInterface and action with Where/And
|
||||||
|
* Get support non-struct variables
|
||||||
|
* BufferSize on Iterate
|
||||||
|
* fix some other bugs.
|
||||||
|
|
||||||
|
* **v0.6.3**
|
||||||
|
* merge tests to main project
|
||||||
|
* add `Exist` function
|
||||||
|
* add `SumInt` function
|
||||||
|
* Mysql now support read and create column comment.
|
||||||
|
* fix time related bugs.
|
||||||
|
* fix some other bugs.
|
||||||
|
|
||||||
|
[More changes ...](https://github.com/go-xorm/manual-en-US/tree/master/chapter-16)
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
|
||||||
|
go get github.com/go-xorm/xorm
|
||||||
|
|
||||||
|
# Documents
|
||||||
|
|
||||||
|
* [Manual](http://xorm.io/docs)
|
||||||
|
|
||||||
|
* [GoDoc](http://godoc.org/github.com/go-xorm/xorm)
|
||||||
|
|
||||||
|
* [GoWalker](http://gowalker.org/github.com/go-xorm/xorm)
|
||||||
|
|
||||||
|
# Quick Start
|
||||||
|
|
||||||
|
* Create Engine
|
||||||
|
|
||||||
|
```Go
|
||||||
|
engine, err := xorm.NewEngine(driverName, dataSourceName)
|
||||||
|
```
|
||||||
|
|
||||||
|
* Define a struct and Sync2 table struct to database
|
||||||
|
|
||||||
|
```Go
|
||||||
|
type User struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
Salt string
|
||||||
|
Age int
|
||||||
|
Passwd string `xorm:"varchar(200)"`
|
||||||
|
Created time.Time `xorm:"created"`
|
||||||
|
Updated time.Time `xorm:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := engine.Sync2(new(User))
|
||||||
|
```
|
||||||
|
|
||||||
|
* Create Engine Group
|
||||||
|
|
||||||
|
```Go
|
||||||
|
dataSourceNameSlice := []string{masterDataSourceName, slave1DataSourceName, slave2DataSourceName}
|
||||||
|
engineGroup, err := xorm.NewEngineGroup(driverName, dataSourceNameSlice)
|
||||||
|
```
|
||||||
|
|
||||||
|
```Go
|
||||||
|
masterEngine, err := xorm.NewEngine(driverName, masterDataSourceName)
|
||||||
|
slave1Engine, err := xorm.NewEngine(driverName, slave1DataSourceName)
|
||||||
|
slave2Engine, err := xorm.NewEngine(driverName, slave2DataSourceName)
|
||||||
|
engineGroup, err := xorm.NewEngineGroup(masterEngine, []*Engine{slave1Engine, slave2Engine})
|
||||||
|
```
|
||||||
|
|
||||||
|
Then all place where `engine` you can just use `engineGroup`.
|
||||||
|
|
||||||
|
* `Query` runs a SQL string, the returned results is `[]map[string][]byte`, `QueryString` returns `[]map[string]string`, `QueryInterface` returns `[]map[string]interface{}`.
|
||||||
|
|
||||||
|
```Go
|
||||||
|
results, err := engine.Query("select * from user")
|
||||||
|
results, err := engine.Where("a = 1").Query()
|
||||||
|
|
||||||
|
results, err := engine.QueryString("select * from user")
|
||||||
|
results, err := engine.Where("a = 1").QueryString()
|
||||||
|
|
||||||
|
results, err := engine.QueryInterface("select * from user")
|
||||||
|
results, err := engine.Where("a = 1").QueryInterface()
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Exec` runs a SQL string, it returns `affected` and `error`
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Exec("update user set age = ? where name = ?", age, name)
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Insert` one or multiple records to database
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Insert(&user)
|
||||||
|
// INSERT INTO struct () values ()
|
||||||
|
|
||||||
|
affected, err := engine.Insert(&user1, &user2)
|
||||||
|
// INSERT INTO struct1 () values ()
|
||||||
|
// INSERT INTO struct2 () values ()
|
||||||
|
|
||||||
|
affected, err := engine.Insert(&users)
|
||||||
|
// INSERT INTO struct () values (),(),()
|
||||||
|
|
||||||
|
affected, err := engine.Insert(&user1, &users)
|
||||||
|
// INSERT INTO struct1 () values ()
|
||||||
|
// INSERT INTO struct2 () values (),(),()
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Get` query one record from database
|
||||||
|
|
||||||
|
```Go
|
||||||
|
has, err := engine.Get(&user)
|
||||||
|
// SELECT * FROM user LIMIT 1
|
||||||
|
|
||||||
|
has, err := engine.Where("name = ?", name).Desc("id").Get(&user)
|
||||||
|
// SELECT * FROM user WHERE name = ? ORDER BY id DESC LIMIT 1
|
||||||
|
|
||||||
|
var name string
|
||||||
|
has, err := engine.Where("id = ?", id).Cols("name").Get(&name)
|
||||||
|
// SELECT name FROM user WHERE id = ?
|
||||||
|
|
||||||
|
var id int64
|
||||||
|
has, err := engine.Where("name = ?", name).Cols("id").Get(&id)
|
||||||
|
has, err := engine.SQL("select id from user").Get(&id)
|
||||||
|
// SELECT id FROM user WHERE name = ?
|
||||||
|
|
||||||
|
var valuesMap = make(map[string]string)
|
||||||
|
has, err := engine.Where("id = ?", id).Get(&valuesMap)
|
||||||
|
// SELECT * FROM user WHERE id = ?
|
||||||
|
|
||||||
|
var valuesSlice = make([]interface{}, len(cols))
|
||||||
|
has, err := engine.Where("id = ?", id).Cols(cols...).Get(&valuesSlice)
|
||||||
|
// SELECT col1, col2, col3 FROM user WHERE id = ?
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Exist` check if one record exist on table
|
||||||
|
|
||||||
|
```Go
|
||||||
|
has, err := testEngine.Exist(new(RecordExist))
|
||||||
|
// SELECT * FROM record_exist LIMIT 1
|
||||||
|
|
||||||
|
has, err = testEngine.Exist(&RecordExist{
|
||||||
|
Name: "test1",
|
||||||
|
})
|
||||||
|
// SELECT * FROM record_exist WHERE name = ? LIMIT 1
|
||||||
|
|
||||||
|
has, err = testEngine.Where("name = ?", "test1").Exist(&RecordExist{})
|
||||||
|
// SELECT * FROM record_exist WHERE name = ? LIMIT 1
|
||||||
|
|
||||||
|
has, err = testEngine.SQL("select * from record_exist where name = ?", "test1").Exist()
|
||||||
|
// select * from record_exist where name = ?
|
||||||
|
|
||||||
|
has, err = testEngine.Table("record_exist").Exist()
|
||||||
|
// SELECT * FROM record_exist LIMIT 1
|
||||||
|
|
||||||
|
has, err = testEngine.Table("record_exist").Where("name = ?", "test1").Exist()
|
||||||
|
// SELECT * FROM record_exist WHERE name = ? LIMIT 1
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Find` query multiple records from database, also you can use join and extends
|
||||||
|
|
||||||
|
```Go
|
||||||
|
var users []User
|
||||||
|
err := engine.Where("name = ?", name).And("age > 10").Limit(10, 0).Find(&users)
|
||||||
|
// SELECT * FROM user WHERE name = ? AND age > 10 limit 10 offset 0
|
||||||
|
|
||||||
|
type Detail struct {
|
||||||
|
Id int64
|
||||||
|
UserId int64 `xorm:"index"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDetail struct {
|
||||||
|
User `xorm:"extends"`
|
||||||
|
Detail `xorm:"extends"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var users []UserDetail
|
||||||
|
err := engine.Table("user").Select("user.*, detail.*").
|
||||||
|
Join("INNER", "detail", "detail.user_id = user.id").
|
||||||
|
Where("user.name = ?", name).Limit(10, 0).
|
||||||
|
Find(&users)
|
||||||
|
// SELECT user.*, detail.* FROM user INNER JOIN detail WHERE user.name = ? limit 10 offset 0
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Iterate` and `Rows` query multiple records and record by record handle, there are two methods Iterate and Rows
|
||||||
|
|
||||||
|
```Go
|
||||||
|
err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
|
||||||
|
user := bean.(*User)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
// SELECT * FROM user
|
||||||
|
|
||||||
|
err := engine.BufferSize(100).Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
|
||||||
|
user := bean.(*User)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
// SELECT * FROM user Limit 0, 100
|
||||||
|
// SELECT * FROM user Limit 101, 100
|
||||||
|
|
||||||
|
rows, err := engine.Rows(&User{Name:name})
|
||||||
|
// SELECT * FROM user
|
||||||
|
defer rows.Close()
|
||||||
|
bean := new(Struct)
|
||||||
|
for rows.Next() {
|
||||||
|
err = rows.Scan(bean)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Update` update one or more records, default will update non-empty and non-zero fields except when you use Cols, AllCols and so on.
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.ID(1).Update(&user)
|
||||||
|
// UPDATE user SET ... Where id = ?
|
||||||
|
|
||||||
|
affected, err := engine.Update(&user, &User{Name:name})
|
||||||
|
// UPDATE user SET ... Where name = ?
|
||||||
|
|
||||||
|
var ids = []int64{1, 2, 3}
|
||||||
|
affected, err := engine.In("id", ids).Update(&user)
|
||||||
|
// UPDATE user SET ... Where id IN (?, ?, ?)
|
||||||
|
|
||||||
|
// force update indicated columns by Cols
|
||||||
|
affected, err := engine.ID(1).Cols("age").Update(&User{Name:name, Age: 12})
|
||||||
|
// UPDATE user SET age = ?, updated=? Where id = ?
|
||||||
|
|
||||||
|
// force NOT update indicated columns by Omit
|
||||||
|
affected, err := engine.ID(1).Omit("name").Update(&User{Name:name, Age: 12})
|
||||||
|
// UPDATE user SET age = ?, updated=? Where id = ?
|
||||||
|
|
||||||
|
affected, err := engine.ID(1).AllCols().Update(&user)
|
||||||
|
// UPDATE user SET name=?,age=?,salt=?,passwd=?,updated=? Where id = ?
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Delete` delete one or more records, Delete MUST have condition
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Where(...).Delete(&user)
|
||||||
|
// DELETE FROM user Where ...
|
||||||
|
|
||||||
|
affected, err := engine.ID(2).Delete(&user)
|
||||||
|
// DELETE FROM user Where id = ?
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Count` count records
|
||||||
|
|
||||||
|
```Go
|
||||||
|
counts, err := engine.Count(&user)
|
||||||
|
// SELECT count(*) AS total FROM user
|
||||||
|
```
|
||||||
|
|
||||||
|
* `Sum` sum functions
|
||||||
|
|
||||||
|
```Go
|
||||||
|
agesFloat64, err := engine.Sum(&user, "age")
|
||||||
|
// SELECT sum(age) AS total FROM user
|
||||||
|
|
||||||
|
agesInt64, err := engine.SumInt(&user, "age")
|
||||||
|
// SELECT sum(age) AS total FROM user
|
||||||
|
|
||||||
|
sumFloat64Slice, err := engine.Sums(&user, "age", "score")
|
||||||
|
// SELECT sum(age), sum(score) FROM user
|
||||||
|
|
||||||
|
sumInt64Slice, err := engine.SumsInt(&user, "age", "score")
|
||||||
|
// SELECT sum(age), sum(score) FROM user
|
||||||
|
```
|
||||||
|
|
||||||
|
* Query conditions builder
|
||||||
|
|
||||||
|
```Go
|
||||||
|
err := engine.Where(builder.NotIn("a", 1, 2).And(builder.In("b", "c", "d", "e"))).Find(&users)
|
||||||
|
// SELECT id, name ... FROM user WHERE a NOT IN (?, ?) AND b IN (?, ?, ?)
|
||||||
|
```
|
||||||
|
|
||||||
|
* Multiple operations in one go routine, no transation here but resue session memory
|
||||||
|
|
||||||
|
```Go
|
||||||
|
session := engine.NewSession()
|
||||||
|
defer session.Close()
|
||||||
|
|
||||||
|
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
|
||||||
|
if _, err := session.Insert(&user1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
user2 := Userinfo{Username: "yyy"}
|
||||||
|
if _, err := session.Where("id = ?", 2).Update(&user2); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := session.Exec("delete from userinfo where username = ?", user2.Username); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
```
|
||||||
|
|
||||||
|
* Transation should on one go routine. There is transaction and resue session memory
|
||||||
|
|
||||||
|
```Go
|
||||||
|
session := engine.NewSession()
|
||||||
|
defer session.Close()
|
||||||
|
|
||||||
|
// add Begin() before any action
|
||||||
|
if err := session.Begin(); err != nil {
|
||||||
|
// if returned then will rollback automatically
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
|
||||||
|
if _, err := session.Insert(&user1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
user2 := Userinfo{Username: "yyy"}
|
||||||
|
if _, err := session.Where("id = ?", 2).Update(&user2); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := session.Exec("delete from userinfo where username = ?", user2.Username); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// add Commit() after all actions
|
||||||
|
return session.Commit()
|
||||||
|
```
|
||||||
|
|
||||||
|
# Cases
|
||||||
|
|
||||||
|
* [studygolang](http://studygolang.com/) - [github.com/studygolang/studygolang](https://github.com/studygolang/studygolang)
|
||||||
|
|
||||||
|
* [Gitea](http://gitea.io) - [github.com/go-gitea/gitea](http://github.com/go-gitea/gitea)
|
||||||
|
|
||||||
|
* [Gogs](http://try.gogits.org) - [github.com/gogits/gogs](http://github.com/gogits/gogs)
|
||||||
|
|
||||||
|
* [grafana](https://grafana.com/) - [github.com/grafana/grafana](http://github.com/grafana/grafana)
|
||||||
|
|
||||||
|
* [github.com/m3ng9i/qreader](https://github.com/m3ng9i/qreader)
|
||||||
|
|
||||||
|
* [Wego](http://github.com/go-tango/wego)
|
||||||
|
|
||||||
|
* [Docker.cn](https://docker.cn/)
|
||||||
|
|
||||||
|
* [Xorm Adapter](https://github.com/casbin/xorm-adapter) for [Casbin](https://github.com/casbin/casbin) - [github.com/casbin/xorm-adapter](https://github.com/casbin/xorm-adapter)
|
||||||
|
|
||||||
|
* [Gorevel](http://gorevel.cn/) - [github.com/goofcc/gorevel](http://github.com/goofcc/gorevel)
|
||||||
|
|
||||||
|
* [Gowalker](http://gowalker.org) - [github.com/Unknwon/gowalker](http://github.com/Unknwon/gowalker)
|
||||||
|
|
||||||
|
* [Gobuild.io](http://gobuild.io) - [github.com/shxsun/gobuild](http://github.com/shxsun/gobuild)
|
||||||
|
|
||||||
|
* [Sudo China](http://sudochina.com) - [github.com/insionng/toropress](http://github.com/insionng/toropress)
|
||||||
|
|
||||||
|
* [Godaily](http://godaily.org) - [github.com/govc/godaily](http://github.com/govc/godaily)
|
||||||
|
|
||||||
|
* [YouGam](http://www.yougam.com/)
|
||||||
|
|
||||||
|
* [GoCMS - github.com/zzboy/GoCMS](https://github.com/zzdboy/GoCMS)
|
||||||
|
|
||||||
|
* [GoBBS - gobbs.domolo.com](http://gobbs.domolo.com/)
|
||||||
|
|
||||||
|
* [go-blog](http://wangcheng.me) - [github.com/easykoo/go-blog](https://github.com/easykoo/go-blog)
|
||||||
|
|
||||||
|
# Discuss
|
||||||
|
|
||||||
|
Please visit [Xorm on Google Groups](https://groups.google.com/forum/#!forum/xorm)
|
||||||
|
|
||||||
|
# Contributing
|
||||||
|
|
||||||
|
If you want to pull request, please see [CONTRIBUTING](https://github.com/go-xorm/xorm/blob/master/CONTRIBUTING.md)
|
||||||
|
|
||||||
|
# LICENSE
|
||||||
|
|
||||||
|
BSD License
|
||||||
|
[http://creativecommons.org/licenses/BSD/](http://creativecommons.org/licenses/BSD/)
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user