File: types.go

package info (click to toggle)
golang-github-pingcap-fn 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156 kB
  • sloc: makefile: 2
file content (116 lines) | stat: -rw-r--r-- 3,293 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fn

import (
	"context"
	"io"
	"mime/multipart"
	"net/http"
	"net/url"
	"reflect"
)

type valuer func(r *http.Request) (reflect.Value, error)

var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()

// BenchmarkIsBuiltinType-8   	100000000	        23.1 ns/op	       0 B/op	       0 allocs/op
var supportTypes = map[reflect.Type]valuer{
	reflect.TypeOf((*io.ReadCloser)(nil)).Elem(): bodyValuer,        // request.Body
	reflect.TypeOf((http.Header)(nil)):           headerValuer,      // request.Header
	reflect.TypeOf(Form{}):                       formValuer,        // request.Form
	reflect.TypeOf(PostForm{}):                   postFromValuer,    // request.PostFrom
	reflect.TypeOf((*Form)(nil)):                 formPtrValuer,     // request.Form
	reflect.TypeOf((*PostForm)(nil)):             postFromPtrValuer, // request.PostFrom
	reflect.TypeOf((*url.URL)(nil)):              urlValuer,         // request.URL
	reflect.TypeOf((*multipart.Form)(nil)):       multipartValuer,   // request.MultipartForm
	reflect.TypeOf((*http.Request)(nil)):         requestValuer,     // raw request
}

var maxMemory = int64(2 * 1024 * 1024)

type uniform struct {
	url.Values
}

type Form struct {
	uniform
}

type PostForm struct {
	uniform
}

func bodyValuer(r *http.Request) (reflect.Value, error) {
	return reflect.ValueOf(r.Body), nil
}

func urlValuer(r *http.Request) (reflect.Value, error) {
	return reflect.ValueOf(r.URL), nil
}

func headerValuer(r *http.Request) (reflect.Value, error) {
	return reflect.ValueOf(r.Header), nil
}

func multipartValuer(r *http.Request) (reflect.Value, error) {
	err := r.ParseMultipartForm(maxMemory)
	if err != nil {
		return reflect.Value{}, err
	}
	return reflect.ValueOf(r.MultipartForm), nil
}

func formValuer(r *http.Request) (reflect.Value, error) {
	err := r.ParseForm()
	if err != nil {
		return reflect.Value{}, nil
	}
	return reflect.ValueOf(Form{uniform{r.Form}}), nil
}

func postFromValuer(r *http.Request) (reflect.Value, error) {
	err := r.ParseForm()
	if err != nil {
		return reflect.Value{}, nil
	}
	return reflect.ValueOf(PostForm{uniform{r.PostForm}}), nil
}

func formPtrValuer(r *http.Request) (reflect.Value, error) {
	err := r.ParseForm()
	if err != nil {
		return reflect.Value{}, nil
	}
	return reflect.ValueOf(&Form{uniform{r.Form}}), nil
}

func postFromPtrValuer(r *http.Request) (reflect.Value, error) {
	err := r.ParseForm()
	if err != nil {
		return reflect.Value{}, nil
	}
	return reflect.ValueOf(&PostForm{uniform{r.PostForm}}), nil
}

func requestValuer(r *http.Request) (reflect.Value, error) {
	return reflect.ValueOf(r), nil
}

func isBuiltinType(t reflect.Type) bool {
	_, ok := supportTypes[t]
	return ok
}