package httprule
import (
"github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
)
const (
opcodeVersion = 1
)
type Template struct {
Version int
OpCodes []int
Pool []string
Verb string
Fields []string
Template string
}
type Compiler interface {
Compile () Template
}
type op struct {
code utilities .OpCode
str string
num int
}
func (w wildcard ) compile () []op {
return []op {
{code : utilities .OpPush },
}
}
func (w deepWildcard ) compile () []op {
return []op {
{code : utilities .OpPushM },
}
}
func (l literal ) compile () []op {
return []op {
{
code : utilities .OpLitPush ,
str : string (l ),
},
}
}
func (v variable ) compile () []op {
var ops []op
for _ , s := range v .segments {
ops = append (ops , s .compile ()...)
}
ops = append (ops , op {
code : utilities .OpConcatN ,
num : len (v .segments ),
}, op {
code : utilities .OpCapture ,
str : v .path ,
})
return ops
}
func (t template ) Compile () Template {
var rawOps []op
for _ , s := range t .segments {
rawOps = append (rawOps , s .compile ()...)
}
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 .num )
} else {
if op .str == eof {
op .str = ""
}
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 == utilities .OpCapture {
fields = append (fields , op .str )
}
}
return Template {
Version : opcodeVersion ,
OpCodes : ops ,
Pool : pool ,
Verb : t .verb ,
Fields : fields ,
Template : t .template ,
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .