Compare commits
8 Commits
gx/v0.7.7
...
gx/v0.7.11
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a22bf1e2bf | ||
|
|
7bcaf9264a | ||
|
|
c1db98f9ae | ||
|
|
40c5ff1bfe | ||
|
|
ebda3c73ae | ||
|
|
03f8d08574 | ||
|
|
4ff5ee2b99 | ||
|
|
2b1b4a8339 |
@@ -1 +1 @@
|
|||||||
0.7.7: QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD
|
0.7.11: QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk
|
||||||
|
|||||||
21
cid.go
21
cid.go
@@ -3,6 +3,7 @@ package cid
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -209,7 +210,19 @@ func (c *Cid) UnmarshalJSON(b []byte) error {
|
|||||||
if len(b) < 2 {
|
if len(b) < 2 {
|
||||||
return fmt.Errorf("invalid cid json blob")
|
return fmt.Errorf("invalid cid json blob")
|
||||||
}
|
}
|
||||||
out, err := Decode(string(b[1 : len(b)-1]))
|
obj := struct {
|
||||||
|
CidTarget string `json:"/"`
|
||||||
|
}{}
|
||||||
|
err := json.Unmarshal(b, &obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if obj.CidTarget == "" {
|
||||||
|
return fmt.Errorf("cid was incorrectly formatted")
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := Decode(obj.CidTarget)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -221,7 +234,7 @@ func (c *Cid) UnmarshalJSON(b []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cid) MarshalJSON() ([]byte, error) {
|
func (c *Cid) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(fmt.Sprintf("\"%s\"", c.String())), nil
|
return []byte(fmt.Sprintf("{\"/\":\"%s\"}", c.String())), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cid) KeyString() string {
|
func (c *Cid) KeyString() string {
|
||||||
@@ -248,7 +261,7 @@ func (c *Cid) Prefix() Prefix {
|
|||||||
type Prefix struct {
|
type Prefix struct {
|
||||||
Version uint64
|
Version uint64
|
||||||
Codec uint64
|
Codec uint64
|
||||||
MhType int
|
MhType uint64
|
||||||
MhLength int
|
MhLength int
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,7 +315,7 @@ func PrefixFromBytes(buf []byte) (Prefix, error) {
|
|||||||
return Prefix{
|
return Prefix{
|
||||||
Version: vers,
|
Version: vers,
|
||||||
Codec: codec,
|
Codec: codec,
|
||||||
MhType: int(mhtype),
|
MhType: mhtype,
|
||||||
MhLength: int(mhlen),
|
MhLength: int(mhlen),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
27
cid_test.go
27
cid_test.go
@@ -2,6 +2,7 @@ package cid
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -188,3 +189,29 @@ func TestParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHexDecode(t *testing.T) {
|
||||||
|
hexcid := "f015512209d8453505bdc6f269678e16b3e56c2a2948a41f2c792617cc9611ed363c95b63"
|
||||||
|
c, err := Decode(hexcid)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.String() != "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG" {
|
||||||
|
t.Fatal("hash value failed to round trip decoding from hex")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFromJson(t *testing.T) {
|
||||||
|
cval := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG"
|
||||||
|
jsoncid := []byte(`{"/":"` + cval + `"}`)
|
||||||
|
var c Cid
|
||||||
|
err := json.Unmarshal(jsoncid, &c)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.String() != cval {
|
||||||
|
t.Fatal("json parsing failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
10
package.json
10
package.json
@@ -9,15 +9,15 @@
|
|||||||
"gxDependencies": [
|
"gxDependencies": [
|
||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
"hash": "QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J",
|
"hash": "QmbZ6Cee2uHjG7hf19qLHppgKDRtaG4CVtMzdmK9VCVqLu",
|
||||||
"name": "go-multihash",
|
"name": "go-multihash",
|
||||||
"version": "1.0.0"
|
"version": "1.0.2"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
"hash": "QmUq3H9YpcPphbRj6ct6rBgBE377A8wANP8zPMRqe1WYbf",
|
"hash": "QmcxkxTVuURV2Ptse8TvkqH5BQDwV62X1x19JqqvbBzwUM",
|
||||||
"name": "go-multibase",
|
"name": "go-multibase",
|
||||||
"version": "0.2.1"
|
"version": "0.2.3"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"gxVersion": "0.8.0",
|
"gxVersion": "0.8.0",
|
||||||
@@ -25,6 +25,6 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"name": "go-cid",
|
"name": "go-cid",
|
||||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||||
"version": "0.7.7"
|
"version": "0.7.11"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user