forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
58 lines (50 loc) · 1.16 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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"
"reflect"
"runtime"
)
type H map[string]interface{}
var _ xml.Marshaler = H(nil)
// MarshalXML implements xml.Marshaler
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
if err = e.EncodeToken(start); err != nil {
return
}
for key, value := range h {
if key == "" {
continue
}
elem := xml.StartElement{
Name: xml.Name{Local: key},
}
if err = e.EncodeElement(value, elem); err != nil {
return
}
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
func nameOfFunction(fn interface{}) string {
if fn == nil {
return "unknown"
}
pc := reflect.ValueOf(fn).Pointer()
if pc == 0 {
return "unknown"
}
return runtime.FuncForPC(pc).Name()
}
func WrapHandlerFunc(fn http.HandlerFunc) HandlerFunc {
return func(ctx *Context) {
fn(ctx.ResponseWriter, ctx.Request)
}
}
func WrapHandler(h http.Handler) HandlerFunc {
return func(ctx *Context) {
h.ServeHTTP(ctx.ResponseWriter, ctx.Request)
}
}