Skip to content

Commit

Permalink
fix: warnings and notices (#66)
Browse files Browse the repository at this point in the history
Fixed code to be compliant with the latest version of V.
  • Loading branch information
islonely authored Mar 22, 2023
1 parent caada31 commit 4dce806
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ctx/cookie.v
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn (c Cookie) header_str() string {

// parse_cookies parses the Cookie header content and returns the
// content. Returns an error if the header is not present.
pub fn (req &Req) parse_cookies() ?map[string]Cookie {
pub fn (req &Req) parse_cookies() !map[string]Cookie {
if 'Cookie' !in req.headers {
return error('Cookies not found.')
}
Expand Down
6 changes: 3 additions & 3 deletions ctx/ctx.v
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn (req &Req) parse_query() ?map[string]string {
// and `application/json` content types. Returns an error if the body is blank,
// the "Content-Type" header is not present, or the content type header
// is not supported.
pub fn (req &Req) parse_form() ?map[string]string {
pub fn (req &Req) parse_form() !map[string]string {
if req.body.len == 0 {
return error('Form body is empty.')
} else if 'Content-Type' !in req.headers {
Expand All @@ -83,7 +83,7 @@ pub fn (req &Req) parse_form() ?map[string]string {
return form_data_map
}
'multipart/form-data' {
multipart_form_data := req.parse_files() ?
multipart_form_data := req.parse_files() !
mut form_data_map := map[string]string{}
for key, datum in multipart_form_data {
for i, data in datum {
Expand All @@ -103,7 +103,7 @@ pub fn (req &Req) parse_form() ?map[string]string {
}

// parse_files parses the `multipart/form-data` content-type
pub fn (req &Req) parse_files() ?map[string][]FormData {
pub fn (req &Req) parse_files() !map[string][]FormData {
if req.headers['Content-Type'][0] != 'multipart/form-data' {
return error('Content type must be `multipart/form-data`.')
}
Expand Down
28 changes: 17 additions & 11 deletions router/router.v
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn identify_kind(route_name string) Kind {

// extract_route_path returns the name, the parameter name (if present),
// and the remaining children route paths
pub fn extract_route_path(path string) ?(string, string, string) {
pub fn extract_route_path(path string) !(string, string, string) {
if !path.starts_with('/') {
return error('Route path must start with a slash (/)')
}
Expand Down Expand Up @@ -211,9 +211,9 @@ pub fn extract_route_path(path string) ?(string, string, string) {

// add creates a new route based on the given method, path, and the handlers.
// See `router.Method` for the list of available methods.
fn (mut routes map[string]&Route) add(method Method, path string, handlers ...ctx.HandlerFunc) ? {
name, param_name, children := extract_route_path(path) ?
if '*' in routes || (':' in routes && (routes[':'].param_name != param_name || method.str() in routes[':'].methods)) {
fn (mut routes map[string]&Route) add(method Method, path string, handlers ...ctx.HandlerFunc) ! {
name, param_name, children := extract_route_path(path) !
if '*' in routes || (':' in routes && (unsafe { routes[':'].param_name } != param_name || method.str() in unsafe { routes[':'].methods })) {
return error('Only one wildcard OR param route in a route list is allowed.')
}
if name !in routes {
Expand All @@ -225,7 +225,9 @@ fn (mut routes map[string]&Route) add(method Method, path string, handlers ...ct
}
}
if children.len > 0 {
routes[name].children.add(method, children, ...handlers) ?
unsafe {
routes[name].children.add(method, children, ...handlers) !
}
return
} else if handlers.len == 0 {
return error('Provided route handlers are empty.')
Expand All @@ -235,14 +237,16 @@ fn (mut routes map[string]&Route) add(method Method, path string, handlers ...ct
}

// find searches the matching route and returns the injected params data and the route handlers.
pub fn (routes map[string]&Route) find(method string, path string) ?(map[string]string, []ctx.MiddlewareFunc, []ctx.HandlerFunc) {
mut r_name, mut param_name, children := extract_route_path(path) ?
pub fn (routes map[string]&Route) find(method string, path string) !(map[string]string, []ctx.MiddlewareFunc, []ctx.HandlerFunc) {
mut r_name, mut param_name, children := extract_route_path(path) !
mut params := map[string]string{}
param_value := r_name
if r_name !in routes {
if ':' in routes || '*' in routes {
r_name = if '*' in routes { '*' } else { ':' }
param_name = routes[r_name].param_name
unsafe {
param_name = routes[r_name].param_name
}
} else {
return error('Route not foun.')
}
Expand All @@ -252,10 +256,10 @@ pub fn (routes map[string]&Route) find(method string, path string) ?(map[string]
'*' { params[param_name] = param_value + children }
else {}
}
route := routes[r_name]
route := unsafe { routes[r_name] }
if r_name != '*' && children.len > 0 {
child_params, child_route_middlewares, handlers := route.children.find(method,
children) ?
children) !
for name, value in child_params {
params[name] = value
}
Expand Down Expand Up @@ -307,6 +311,8 @@ pub fn (mut routes map[string]&Route) use(middlewares ...ctx.MiddlewareFunc) {
panic(utils.red_log('Endpoint/route middlewares can only be added after creating a route.'))
}
for name, _ in routes {
routes[name].middlewares << middlewares
unsafe {
routes[name].middlewares << middlewares
}
}
}

0 comments on commit 4dce806

Please sign in to comment.