This repository has been archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test JSON decoding and fix implentation
Add a test that decodes a JSON object, and detect links in it. Then encode it again and it must match the original file. The test failed (because the link was a map[string]interface{} instead of a Node). The fix post-process the result of the JSON decoder to convert map[string]interface{} to Node.
- Loading branch information
Showing
5 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
pb/bin/multicodec: | ||
$(MAKE) -C pb bin/multicodec | ||
|
||
json.testfile: pb/bin/multicodec Makefile | ||
: >$@ | ||
pb/bin/multicodec header /multicodec >>$@ | ||
pb/bin/multicodec header /json >>$@ | ||
echo '{"@codec":"/json","abc":{"mlink":"QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V"}}' >>$@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package ipfsld | ||
|
||
import ( | ||
"io" | ||
|
||
mc "github.com/jbenet/go-multicodec" | ||
mcjson "github.com/jbenet/go-multicodec/json" | ||
ipld "github.com/ipfs/go-ipld" | ||
) | ||
|
||
type codec struct { | ||
mc.Multicodec | ||
} | ||
|
||
type decoder struct { | ||
mc.Decoder | ||
} | ||
|
||
func jsonMulticodec() mc.Multicodec { | ||
return &codec{mcjson.Multicodec(false)} | ||
} | ||
|
||
func (c *codec) Decoder(r io.Reader) mc.Decoder { | ||
return &decoder{ c.Multicodec.Decoder(r) } | ||
} | ||
|
||
func (c *decoder) Decode(v interface{}) error { | ||
err := c.Decoder.Decode(v) | ||
if err == nil { | ||
convert(v) | ||
} | ||
return err | ||
} | ||
|
||
func convert(val interface{}) interface{} { | ||
switch val.(type) { | ||
case *map[string]interface{}: | ||
vmi := val.(*map[string]interface{}) | ||
n := ipld.Node{} | ||
for k, v := range *vmi { | ||
n[k] = convert(v) | ||
(*vmi)[k] = convert(v) | ||
} | ||
return &n | ||
case map[string]interface{}: | ||
vmi := val.(map[string]interface{}) | ||
n := ipld.Node{} | ||
for k, v := range vmi { | ||
n[k] = convert(v) | ||
vmi[k] = convert(v) | ||
} | ||
return n | ||
case *[]interface{}: | ||
convert(*val.(*[]interface{})) | ||
case []interface{}: | ||
slice := val.([]interface{}) | ||
for k, v := range slice { | ||
slice[k] = convert(v) | ||
} | ||
case *ipld.Node: | ||
convert(*val.(*ipld.Node)) | ||
case ipld.Node: | ||
n := val.(ipld.Node) | ||
for k, v := range n { | ||
n[k] = convert(v) | ||
} | ||
default: | ||
} | ||
return val | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/multicodec | ||
/json | ||
{"@codec":"/json","abc":{"mlink":"QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V"}} |