github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/blocks/key/key.go (about) 1 package key 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 b58 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58" 8 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" 9 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" 10 ) 11 12 // Key is a string representation of multihash for use with maps. 13 type Key string 14 15 // String is utililty function for printing out keys as strings (Pretty). 16 func (k Key) String() string { 17 return k.Pretty() 18 } 19 20 // Pretty returns Key in a b58 encoded string 21 // TODO: deprecate Pretty. bad name. 22 func (k Key) Pretty() string { 23 return k.B58String() 24 } 25 26 func (k Key) ToMultihash() mh.Multihash { 27 return mh.Multihash(k) 28 } 29 30 // B58String returns Key in a b58 encoded string 31 func (k Key) B58String() string { 32 return B58KeyEncode(k) 33 } 34 35 // B58KeyDecode returns Key from a b58 encoded string 36 func B58KeyDecode(s string) Key { 37 return Key(string(b58.Decode(s))) 38 } 39 40 // B58KeyEncode returns Key in a b58 encoded string 41 func B58KeyEncode(k Key) string { 42 return b58.Encode([]byte(k)) 43 } 44 45 // DsKey returns a Datastore key 46 func (k Key) DsKey() ds.Key { 47 return ds.NewKey(string(k)) 48 } 49 50 // UnmarshalJSON returns a JSON-encoded Key (string) 51 func (k *Key) UnmarshalJSON(mk []byte) error { 52 var s string 53 err := json.Unmarshal(mk, &s) 54 if err != nil { 55 return err 56 } 57 58 *k = Key(string(b58.Decode(s))) 59 if len(*k) == 0 && len(s) > 2 { // if b58.Decode fails, k == "" 60 return fmt.Errorf("Key.UnmarshalJSON: invalid b58 string: %v", mk) 61 } 62 return nil 63 } 64 65 // MarshalJSON returns a JSON-encoded Key (string) 66 func (k *Key) MarshalJSON() ([]byte, error) { 67 return json.Marshal(b58.Encode([]byte(*k))) 68 } 69 70 func (k *Key) Loggable() map[string]interface{} { 71 return map[string]interface{}{ 72 "key": k.String(), 73 } 74 } 75 76 // KeyFromDsKey returns a Datastore key 77 func KeyFromDsKey(dsk ds.Key) Key { 78 return Key(dsk.String()[1:]) 79 } 80 81 // B58KeyConverter -- for KeyTransform datastores 82 // (static as only one obj needed) 83 var B58KeyConverter = b58KeyConverter{} 84 85 type b58KeyConverter struct{} 86 87 // ConvertKey returns a B58 encoded Datastore key 88 // TODO: this is hacky because it encodes every path component. some 89 // path components may be proper strings already... 90 func (b58KeyConverter) ConvertKey(dsk ds.Key) ds.Key { 91 k := ds.NewKey("/") 92 for _, n := range dsk.Namespaces() { 93 k = k.ChildString(b58.Encode([]byte(n))) 94 } 95 return k 96 } 97 98 // InvertKey returns a b58 decoded Datastore key 99 // TODO: this is hacky because it encodes every path component. some 100 // path components may be proper strings already... 101 func (b58KeyConverter) InvertKey(dsk ds.Key) ds.Key { 102 k := ds.NewKey("/") 103 for _, n := range dsk.Namespaces() { 104 k = k.ChildString(string(b58.Decode(n))) 105 } 106 return k 107 } 108 109 // KeySlice is used for sorting Keys 110 type KeySlice []Key 111 112 func (es KeySlice) Len() int { return len(es) } 113 func (es KeySlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] } 114 func (es KeySlice) Less(i, j int) bool { return es[i] < es[j] }