tree

package
v0.0.85-test Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2023 License: AGPL-3.0, BSD-3-Clause Imports: 6 Imported by: 0

README

This is a tree code from https://github.com/julienschmidt/httprouter with an important fixes/improvements made inside Gin web framework.

See:

See also https://github.com/julienschmidt/httprouter/issues/235 – that's the reason why we can't use a custom branch patched with fixes.

Original LICENSE and copyright left unchanged here.

How channel pattern matching works

As stated in httprouter readme about route matching behavior:

Only explicit matches: With other routers, like http.ServeMux, a requested URL path could match multiple patterns. Therefore they have some awkward pattern priority rules, like longest match or first registered, first matched. By design of this router, a request can only match exactly one or no route. As a result, there are also no unintended matches

This copy of the httprouter follows this semantics. But it also contains several improvements where original httprouter was too restrictive.

One possible way is to start with tree_test.go file for routing examples.

What's possible:

  • Possible to define explicit patterns without any parameters: stream/metrics/cpu, stream/metrics/mem, stream/metrics/cpu/spikes
  • Possible to have named parameter to match multiple channels: stream/metrics/:metric, stream/metrics/:metric/spikes
  • Possible to have stream/metrics/:metric and stream/metrics/cpu defined together so a version without named parameter is preferred when matching
  • Possible to use catch-all parameters like stream/metrics/*rest which will match multiple segments

What's impossible:

  • Having 2 patterns like this: stream/:scope/cpu and stream/metrics/:metric since it will cause conflict in the underlying tree
  • Having pattern like stream/metrics/:metric while you already have stream/*rest – again this is a conflict. So use catch-all parameters only in cases where you know for sure there won't be any sub-patterns in the future for which you need to override the behavior.

Documentation

Overview

Copyright 2013 Julien Schmidt. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

View Source
var ParamsKey = paramsKey{}

ParamsKey is the request context key under which URL Params are stored.

Functions

func BytesToString

func BytesToString(b []byte) string

BytesToString converts byte slice to string without a memory allocation.

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes converts string to byte slice without a memory allocation.

Types

type Handler

type Handler interface{}

type Node

type Node struct {
	// contains filtered or unexported fields
}

func New

func New() *Node

func (*Node) AddRoute

func (n *Node) AddRoute(path string, handlers Handler)

func (*Node) GetValue

func (n *Node) GetValue(path string, unescape bool) (value NodeValue)

type NodeValue

type NodeValue struct {
	Handler  Handler
	Params   *Params
	Tsr      bool
	FullPath string
}

NodeValue holds return values of (*Node).getValue method

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func ParamsFromContext

func ParamsFromContext(ctx context.Context) Params

ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present.

func (Params) Get

func (ps Params) Get(name string) (string, bool)

Get returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL