really ugly impl of 'ipfs dht query' command

This commit is contained in:
Jeromy 2015-01-22 07:59:57 +00:00
parent b20f767eb4
commit bb4ad4494b
1 changed files with 30 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package peer
import (
"encoding/hex"
"encoding/json"
"fmt"
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
@ -131,6 +132,35 @@ type PeerInfo struct {
Addrs []ma.Multiaddr
}
func (pi *PeerInfo) MarshalJSON() ([]byte, error) {
out := make(map[string]interface{})
out["ID"] = IDB58Encode(pi.ID)
var addrs []string
for _, a := range pi.Addrs {
addrs = append(addrs, a.String())
}
out["Addrs"] = addrs
return json.Marshal(out)
}
func (pi *PeerInfo) UnmarshalJSON(b []byte) error {
var data map[string]interface{}
err := json.Unmarshal(b, &data)
if err != nil {
return err
}
pid, err := IDB58Decode(data["ID"].(string))
if err != nil {
return err
}
pi.ID = pid
addrs := data["Addrs"].([]interface{})
for _, a := range addrs {
pi.Addrs = append(pi.Addrs, ma.StringCast(a.(string)))
}
return nil
}
// IDSlice for sorting peers
type IDSlice []ID