mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-06 00:09:37 +07:00
feat: support overwrite node name using tag
This commit is contained in:
@ -8,6 +8,7 @@ package dialer
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FromLinkCreator func(gOption *GlobalOption, iOption InstanceOption, link string) (dialer *Dialer, err error)
|
type FromLinkCreator func(gOption *GlobalOption, iOption InstanceOption, link string) (dialer *Dialer, err error)
|
||||||
@ -19,12 +20,35 @@ func FromLinkRegister(name string, creator FromLinkCreator) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewFromLink(gOption *GlobalOption, iOption InstanceOption, link string) (dialer *Dialer, err error) {
|
func NewFromLink(gOption *GlobalOption, iOption InstanceOption, link string) (dialer *Dialer, err error) {
|
||||||
|
/// Get overwritten name.
|
||||||
|
var overwrittenName string
|
||||||
|
iColon := strings.Index(link, ":")
|
||||||
|
if iColon == -1 {
|
||||||
|
goto parseUrl
|
||||||
|
}
|
||||||
|
// If first colon is like "://" in "scheme://linkbody", no tag is present.
|
||||||
|
if strings.HasPrefix(link[iColon:], "://") {
|
||||||
|
goto parseUrl
|
||||||
|
}
|
||||||
|
// Else tag is the part before colon.
|
||||||
|
overwrittenName = link[:iColon]
|
||||||
|
link = link[iColon+1:]
|
||||||
|
|
||||||
|
parseUrl:
|
||||||
u, err := url.Parse(link)
|
u, err := url.Parse(link)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if creator, ok := fromLinkCreators[u.Scheme]; ok {
|
if creator, ok := fromLinkCreators[u.Scheme]; ok {
|
||||||
return creator(gOption, iOption, link)
|
node, err := creator(gOption, iOption, link)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Overwrite node name using user given tag.
|
||||||
|
if overwrittenName != "" {
|
||||||
|
node.name = overwrittenName
|
||||||
|
}
|
||||||
|
return node, err
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("unexpected link type: %v", u.Scheme)
|
return nil, fmt.Errorf("unexpected link type: %v", u.Scheme)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user