package mafmt
import (
"strings"
ma "github.com/multiformats/go-multiaddr"
)
var DNS4 = Base (ma .P_DNS4 )
var DNS6 = Base (ma .P_DNS6 )
var DNS = Or (
Base (ma .P_DNS ),
Base (ma .P_DNSADDR ),
DNS4 ,
DNS6 ,
)
var IP = Or (Base (ma .P_IP4 ), Base (ma .P_IP6 ))
var TCP = Or (
And (DNS , Base (ma .P_TCP )),
And (IP , Base (ma .P_TCP )),
)
var UDP = Or (
And (DNS , Base (ma .P_UDP )),
And (IP , Base (ma .P_UDP )),
)
var UTP = And (UDP , Base (ma .P_UTP ))
var QUIC = And (UDP , Base (ma .P_QUIC ))
var Unreliable = Or (UDP )
var Reliable = Or (TCP , UTP , QUIC )
var P2P = And (Reliable , Base (ma .P_P2P ))
var IPFS = P2P
var HTTP = Or (
And (TCP , Base (ma .P_HTTP )),
And (IP , Base (ma .P_HTTP )),
And (DNS , Base (ma .P_HTTP )),
)
var HTTPS = Or (
And (TCP , Base (ma .P_HTTPS )),
And (IP , Base (ma .P_HTTPS )),
And (DNS , Base (ma .P_HTTPS )),
)
var WebRTCDirect = Or (
And (HTTP , Base (ma .P_P2P_WEBRTC_DIRECT )),
And (HTTPS , Base (ma .P_P2P_WEBRTC_DIRECT )))
const (
or = iota
and = iota
)
func And (ps ...Pattern ) Pattern {
return &pattern {
Op : and ,
Args : ps ,
}
}
func Or (ps ...Pattern ) Pattern {
return &pattern {
Op : or ,
Args : ps ,
}
}
type Pattern interface {
Matches (ma .Multiaddr ) bool
partialMatch([]ma .Protocol ) (bool , []ma .Protocol )
String () string
}
type pattern struct {
Args []Pattern
Op int
}
func (ptrn *pattern ) Matches (a ma .Multiaddr ) bool {
ok , rem := ptrn .partialMatch (a .Protocols ())
return ok && len (rem ) == 0
}
func (ptrn *pattern ) partialMatch (pcs []ma .Protocol ) (bool , []ma .Protocol ) {
switch ptrn .Op {
case or :
for _ , a := range ptrn .Args {
ok , rem := a .partialMatch (pcs )
if ok {
return true , rem
}
}
return false , nil
case and :
if len (pcs ) < len (ptrn .Args ) {
return false , nil
}
for i := 0 ; i < len (ptrn .Args ); i ++ {
ok , rem := ptrn .Args [i ].partialMatch (pcs )
if !ok {
return false , nil
}
pcs = rem
}
return true , pcs
default :
panic ("unrecognized pattern operand" )
}
}
func (ptrn *pattern ) String () string {
var sub []string
for _ , a := range ptrn .Args {
sub = append (sub , a .String ())
}
switch ptrn .Op {
case and :
return strings .Join (sub , "/" )
case or :
return "{" + strings .Join (sub , "|" ) + "}"
default :
panic ("unrecognized pattern op!" )
}
}
type Base int
func (p Base ) Matches (a ma .Multiaddr ) bool {
pcs := a .Protocols ()
return pcs [0 ].Code == int (p ) && len (pcs ) == 1
}
func (p Base ) partialMatch (pcs []ma .Protocol ) (bool , []ma .Protocol ) {
if len (pcs ) == 0 {
return false , nil
}
if pcs [0 ].Code == int (p ) {
return true , pcs [1 :]
}
return false , nil
}
func (p Base ) String () string {
return ma .ProtocolWithCode (int (p )).Name
}
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 .