2020-08-07 15:30:29 +03:00
|
|
|
package router
|
2020-04-15 17:50:51 +03:00
|
|
|
|
|
|
|
// download from https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-grpc-gateway/httprule/compile.go
|
|
|
|
|
|
|
|
const (
|
|
|
|
opcodeVersion = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// Template is a compiled representation of path templates.
|
|
|
|
type Template struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Verb is a VERB part in the template
|
|
|
|
Verb string
|
|
|
|
// Original template (example: /v1/a_bit_of_everything)
|
|
|
|
Template string
|
|
|
|
// OpCodes is a sequence of operations
|
2020-04-15 17:50:51 +03:00
|
|
|
OpCodes []int
|
|
|
|
// Pool is a constant pool
|
|
|
|
Pool []string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Fields is a list of field paths bound in this template
|
2020-04-15 17:50:51 +03:00
|
|
|
Fields []string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Version is the version number of the format
|
|
|
|
Version int
|
2020-04-15 17:50:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compiler compiles utilities representation of path templates into marshallable operations.
|
|
|
|
// They can be unmarshalled by runtime.NewPattern.
|
|
|
|
type Compiler interface {
|
|
|
|
Compile() Template
|
|
|
|
}
|
|
|
|
|
|
|
|
type op struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
str string
|
|
|
|
code OpCode
|
2020-04-15 17:50:51 +03:00
|
|
|
operand int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w wildcard) compile() []op {
|
|
|
|
return []op{
|
|
|
|
{code: OpPush},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w deepWildcard) compile() []op {
|
|
|
|
return []op{
|
|
|
|
{code: OpPushM},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l literal) compile() []op {
|
|
|
|
return []op{
|
|
|
|
{
|
|
|
|
code: OpLitPush,
|
|
|
|
str: string(l),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v variable) compile() []op {
|
2021-02-13 01:46:16 +03:00
|
|
|
ops := make([]op, 0, len(v.segments))
|
2020-04-15 17:50:51 +03:00
|
|
|
for _, s := range v.segments {
|
|
|
|
ops = append(ops, s.compile()...)
|
|
|
|
}
|
|
|
|
ops = append(ops, op{
|
|
|
|
code: OpConcatN,
|
|
|
|
operand: len(v.segments),
|
|
|
|
}, op{
|
|
|
|
code: OpCapture,
|
|
|
|
str: v.path,
|
|
|
|
})
|
|
|
|
|
|
|
|
return ops
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t template) Compile() Template {
|
2021-02-13 01:46:16 +03:00
|
|
|
rawOps := make([]op, 0, len(t.segments))
|
2020-04-15 17:50:51 +03:00
|
|
|
for _, s := range t.segments {
|
|
|
|
rawOps = append(rawOps, s.compile()...)
|
|
|
|
}
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// ops := make([]int, 0, len(rawOps))
|
2020-04-15 17:50:51 +03:00
|
|
|
var (
|
|
|
|
ops []int
|
|
|
|
pool []string
|
|
|
|
fields []string
|
|
|
|
)
|
|
|
|
consts := make(map[string]int)
|
|
|
|
for _, op := range rawOps {
|
|
|
|
ops = append(ops, int(op.code))
|
|
|
|
if op.str == "" {
|
|
|
|
ops = append(ops, op.operand)
|
|
|
|
} else {
|
|
|
|
if _, ok := consts[op.str]; !ok {
|
|
|
|
consts[op.str] = len(pool)
|
|
|
|
pool = append(pool, op.str)
|
|
|
|
}
|
|
|
|
ops = append(ops, consts[op.str])
|
|
|
|
}
|
|
|
|
if op.code == OpCapture {
|
|
|
|
fields = append(fields, op.str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Template{
|
|
|
|
Version: opcodeVersion,
|
|
|
|
OpCodes: ops,
|
|
|
|
Pool: pool,
|
|
|
|
Verb: t.verb,
|
|
|
|
Fields: fields,
|
|
|
|
Template: t.template,
|
|
|
|
}
|
|
|
|
}
|