1
// Copyright 2014 Manu Martinez-Almeida.  All rights reserved.
2
// Use of this source code is governed by a MIT style
3
// license that can be found in the LICENSE file.
4

5
package gin
6

7
import (
8
	"encoding/xml"
9
	"net/http"
10
	"os"
11
	"path"
12
	"reflect"
13
	"runtime"
14
	"strings"
15
)
16

17
// BindKey indicates a default bind key.
18
const BindKey = "_gin-gonic/gin/bindkey"
19

20
// Bind is a helper function for given interface object and returns a Gin middleware.
21
func Bind(val interface{}) HandlerFunc {
22 14
	value := reflect.ValueOf(val)
23 14
	if value.Kind() == reflect.Ptr {
24 14
		panic(`Bind struct can not be a pointer. Example:
25 14
	Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
26 14
`)
27
	}
28 14
	typ := value.Type()
29

30 14
	return func(c *Context) {
31 14
		obj := reflect.New(typ).Interface()
32 14
		if c.Bind(obj) == nil {
33 14
			c.Set(BindKey, obj)
34
		}
35
	}
36
}
37

38
// WrapF is a helper function for wrapping http.HandlerFunc and returns a Gin middleware.
39
func WrapF(f http.HandlerFunc) HandlerFunc {
40 14
	return func(c *Context) {
41 14
		f(c.Writer, c.Request)
42
	}
43
}
44

45
// WrapH is a helper function for wrapping http.Handler and returns a Gin middleware.
46
func WrapH(h http.Handler) HandlerFunc {
47 14
	return func(c *Context) {
48 14
		h.ServeHTTP(c.Writer, c.Request)
49
	}
50
}
51

52
// H is a shortcut for map[string]interface{}
53
type H map[string]interface{}
54

55
// MarshalXML allows type H to be used with xml.Marshal.
56
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
57 14
	start.Name = xml.Name{
58 14
		Space: "",
59 14
		Local: "map",
60
	}
61 14
	if err := e.EncodeToken(start); err != nil {
62 0
		return err
63
	}
64
	for key, value := range h {
65 14
		elem := xml.StartElement{
66 14
			Name: xml.Name{Space: "", Local: key},
67 14
			Attr: []xml.Attr{},
68
		}
69 14
		if err := e.EncodeElement(value, elem); err != nil {
70 14
			return err
71
		}
72
	}
73

74 14
	return e.EncodeToken(xml.EndElement{Name: start.Name})
75
}
76

77
func assert1(guard bool, text string) {
78 14
	if !guard {
79 14
		panic(text)
80
	}
81
}
82

83
func filterFlags(content string) string {
84
	for i, char := range content {
85 14
		if char == ' ' || char == ';' {
86 14
			return content[:i]
87
		}
88
	}
89 14
	return content
90
}
91

92
func chooseData(custom, wildcard interface{}) interface{} {
93 14
	if custom != nil {
94 14
		return custom
95
	}
96 14
	if wildcard != nil {
97 14
		return wildcard
98
	}
99 14
	panic("negotiation config is invalid")
100
}
101

102
func parseAccept(acceptHeader string) []string {
103 14
	parts := strings.Split(acceptHeader, ",")
104 14
	out := make([]string, 0, len(parts))
105
	for _, part := range parts {
106 14
		if i := strings.IndexByte(part, ';'); i > 0 {
107 14
			part = part[:i]
108
		}
109 14
		if part = strings.TrimSpace(part); part != "" {
110 14
			out = append(out, part)
111
		}
112
	}
113 14
	return out
114
}
115

116
func lastChar(str string) uint8 {
117 14
	if str == "" {
118 14
		panic("The length of the string can't be 0")
119
	}
120 14
	return str[len(str)-1]
121
}
122

123
func nameOfFunction(f interface{}) string {
124 14
	return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
125
}
126

127
func joinPaths(absolutePath, relativePath string) string {
128 14
	if relativePath == "" {
129 14
		return absolutePath
130
	}
131

132 14
	finalPath := path.Join(absolutePath, relativePath)
133 14
	if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
134 14
		return finalPath + "/"
135
	}
136 14
	return finalPath
137
}
138

139
func resolveAddress(addr []string) string {
140 14
	switch len(addr) {
141 14
	case 0:
142 14
		if port := os.Getenv("PORT"); port != "" {
143 14
			debugPrint("Environment variable PORT=\"%s\"", port)
144 14
			return ":" + port
145
		}
146 14
		debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
147 14
		return ":8080"
148 14
	case 1:
149 14
		return addr[0]
150 14
	default:
151 14
		panic("too many parameters")
152
	}
153
}

Read our documentation on viewing source code .

Loading