2016-02-23 19:28:23 +08:00
|
|
|
package metainfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/bencode"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Node string
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ bencode.Unmarshaler = new(Node)
|
|
|
|
)
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (n *Node) UnmarshalBencode(b []byte) (err error) {
|
2016-02-23 19:28:23 +08:00
|
|
|
var iface interface{}
|
|
|
|
err = bencode.Unmarshal(b, &iface)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch v := iface.(type) {
|
|
|
|
case string:
|
2016-04-19 12:11:11 +08:00
|
|
|
*n = Node(v)
|
2016-02-23 19:28:23 +08:00
|
|
|
case []interface{}:
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r != nil {
|
|
|
|
err = r.(error)
|
|
|
|
}
|
|
|
|
}()
|
2016-04-19 12:11:11 +08:00
|
|
|
*n = Node(net.JoinHostPort(v[0].(string), strconv.FormatInt(v[1].(int64), 10)))
|
2016-02-23 19:28:23 +08:00
|
|
|
}()
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported type: %T", iface)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|