-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath.go
More file actions
113 lines (100 loc) · 2.82 KB
/
path.go
File metadata and controls
113 lines (100 loc) · 2.82 KB
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
package shiftapi
import (
"fmt"
"net/http"
"reflect"
"strings"
)
// hasPathTag returns true if the struct field has a `path` tag.
func hasPathTag(f reflect.StructField) bool {
return f.Tag.Get("path") != ""
}
// pathFieldName returns the path parameter name for a struct field.
func pathFieldName(f reflect.StructField) string {
name, _, _ := strings.Cut(f.Tag.Get("path"), ",")
if name == "" {
return f.Name
}
return name
}
// resetPathFields zeros out any path-tagged fields on a struct value.
// This is called after body decode so that path-tagged fields are only
// populated by parsePathInto, not by JSON keys that happen to match.
func resetPathFields(rv reflect.Value) {
for rv.Kind() == reflect.Pointer {
rv = rv.Elem()
}
if rv.Kind() != reflect.Struct {
return
}
rt := rv.Type()
for i := range rt.NumField() {
f := rt.Field(i)
if f.IsExported() && hasPathTag(f) {
rv.Field(i).SetZero()
}
}
}
// parsePathInto populates path-tagged fields on an existing struct value
// from URL path parameters via r.PathValue. Only scalar types are supported;
// pointers and slices are rejected at registration time.
func parsePathInto(rv reflect.Value, r *http.Request) error {
for rv.Kind() == reflect.Pointer {
if rv.IsNil() {
rv.Set(reflect.New(rv.Type().Elem()))
}
rv = rv.Elem()
}
rt := rv.Type()
if rt.Kind() != reflect.Struct {
return fmt.Errorf("path type must be a struct, got %s", rt.Kind())
}
for i := range rt.NumField() {
field := rt.Field(i)
if !field.IsExported() || !hasPathTag(field) {
continue
}
name := pathFieldName(field)
raw := r.PathValue(name)
if raw == "" {
continue
}
if err := setScalarValue(rv.Field(i), raw); err != nil {
return &pathParseError{Field: name, Err: err}
}
}
return nil
}
// validatePathFields checks that path-tagged fields are scalar types (no
// pointers or slices) and that every path-tagged field name appears in the
// route pattern. Called at registration time.
func validatePathFields(t reflect.Type, routeParams map[string]bool) {
for t.Kind() == reflect.Pointer {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return
}
for f := range t.Fields() {
if !f.IsExported() || !hasPathTag(f) {
continue
}
ft := f.Type
if ft.Kind() == reflect.Pointer || ft.Kind() == reflect.Slice {
panic(fmt.Sprintf("shiftapi: path-tagged field %q must be a scalar type, got %s", f.Name, ft.Kind()))
}
name := pathFieldName(f)
if !routeParams[name] {
panic(fmt.Sprintf("shiftapi: path-tagged field %q not found in route pattern", name))
}
}
}
// pathParseError is returned when a path parameter cannot be parsed.
type pathParseError struct {
Field string
Err error
}
func (e *pathParseError) Error() string {
return fmt.Sprintf("invalid path parameter %q: %v", e.Field, e.Err)
}
func (e *pathParseError) Unwrap() error { return e.Err }