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.
Add CBOR & JSON implementation for the NodeReader interface
# Conflicts: # reader/reader.go
- Loading branch information
Showing
9 changed files
with
488 additions
and
17 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 |
---|---|---|
@@ -1,10 +1,22 @@ | ||
bin/multicodec: | ||
mkdir -p bin | ||
go get -d github.com/jbenet/go-multicodec/multicodec | ||
go build -o "$@" github.com/jbenet/go-multicodec/multicodec | ||
|
||
pb/bin/multicodec: | ||
$(MAKE) -C pb bin/multicodec | ||
bin/json2cbor: | ||
mkdir -p bin | ||
go get -d github.com/whyrusleeping/cbor/go/json2cbor | ||
go build -o "$@" github.com/whyrusleeping/cbor/go/json2cbor | ||
|
||
json.testfile: pb/bin/multicodec Makefile | ||
json.testfile: bin/multicodec Makefile test1.json | ||
: >$@ | ||
pb/bin/multicodec header /multicodec >>$@ | ||
pb/bin/multicodec header /json >>$@ | ||
echo '{"@codec":"/json","abc":{"mlink":"QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V"}}' >>$@ | ||
bin/multicodec header /multicodec >>$@ | ||
bin/multicodec header /json >>$@ | ||
cat test1.json >>$@ | ||
|
||
cbor.testfile: bin/multicodec bin/json2cbor Makefile test1.json | ||
: >$@ | ||
bin/multicodec header /multicodec >>$@ | ||
bin/multicodec header /cbor >>$@ | ||
bin/json2cbor -i test1.json -o - >>$@ | ||
|
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 | ||
/cbor | ||
�cabc�emlinkx.QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39Vf@codece/json |
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,161 @@ | ||
package ipfsld | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"math/big" | ||
|
||
reader "github.com/ipfs/go-ipld/reader" | ||
cbor "github.com/whyrusleeping/cbor/go" | ||
) | ||
|
||
type CBORDecoder struct { | ||
r io.Reader | ||
} | ||
|
||
type cborParser struct { | ||
reader.BaseReader | ||
decoder *cbor.Decoder | ||
} | ||
|
||
func (d *CBORDecoder) Read(cb reader.ReadFun) error { | ||
dec := cbor.NewDecoder(d.r) | ||
return dec.DecodeAny(&cborParser{reader.CreateBaseReader(cb), dec}) | ||
} | ||
|
||
func (p *cborParser) Prepare() error { | ||
log.Printf("Prepare") | ||
return nil | ||
} | ||
|
||
func (p *cborParser) SetBytes(buf []byte) error { | ||
log.Printf("SetBytes") | ||
err := p.ExecCallback(reader.TokenValue, buf) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) SetUint(i uint64) error { | ||
log.Printf("SetUint") | ||
err := p.ExecCallback(reader.TokenValue, i) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) SetInt(i int64) error { | ||
log.Printf("setint") | ||
err := p.ExecCallback(reader.TokenValue, i) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) SetFloat32(f float32) error { | ||
log.Printf("setfloat32") | ||
err := p.ExecCallback(reader.TokenValue, f) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) SetFloat64(f float64) error { | ||
log.Printf("setfloat64") | ||
err := p.ExecCallback(reader.TokenValue, f) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) SetBignum(i *big.Int) error { | ||
log.Printf("setbignum") | ||
err := p.ExecCallback(reader.TokenValue, i) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) SetNil() error { | ||
log.Printf("nil") | ||
err := p.ExecCallback(reader.TokenValue, nil) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) SetBool(b bool) error { | ||
log.Printf("setbool") | ||
err := p.ExecCallback(reader.TokenValue, b) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) SetString(s string) error { | ||
log.Printf("setstring") | ||
err := p.ExecCallback(reader.TokenValue, s) | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) CreateMap() (cbor.DecodeValueMap, error) { | ||
log.Printf("createmap") | ||
return p, p.ExecCallback(reader.TokenNode, nil) | ||
} | ||
|
||
func (p *cborParser) CreateMapKey() (cbor.DecodeValue, error) { | ||
log.Printf("createmapkey") | ||
return cbor.NewMemoryValue(""), nil | ||
} | ||
|
||
func (p *cborParser) CreateMapValue(key cbor.DecodeValue) (cbor.DecodeValue, error) { | ||
log.Printf("createmapvalue") | ||
err := p.ExecCallback(reader.TokenKey, key.(*cbor.MemoryValue).Value) | ||
p.Descope() | ||
p.PushPath(key.(*cbor.MemoryValue).Value) | ||
return p, err | ||
} | ||
|
||
func (p *cborParser) SetMap(key, val cbor.DecodeValue) error { | ||
log.Printf("setmap") | ||
p.PopPath() | ||
return nil | ||
} | ||
|
||
func (p *cborParser) EndMap() error { | ||
log.Printf("endmap") | ||
err := p.ExecCallback(reader.TokenEndNode, nil) | ||
p.Descope() | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) CreateArray(len int) (cbor.DecodeValueArray, error) { | ||
log.Printf("createarray") | ||
return p, p.ExecCallback(reader.TokenArray, nil) | ||
} | ||
|
||
func (p *cborParser) GetArrayValue(index uint64) (cbor.DecodeValue, error) { | ||
log.Printf("getarrvalue") | ||
err := p.ExecCallback(reader.TokenIndex, index) | ||
p.Descope() | ||
p.PushPath(index) | ||
return p, err | ||
} | ||
|
||
func (p *cborParser) AppendArray(val cbor.DecodeValue) error { | ||
log.Printf("appendarray") | ||
p.PopPath() | ||
return nil | ||
} | ||
|
||
func (p *cborParser) EndArray() error { | ||
log.Printf("endarray") | ||
err := p.ExecCallback(reader.TokenEndArray, nil) | ||
p.Descope() | ||
p.Descope() | ||
return err | ||
} | ||
|
||
func (p *cborParser) CreateTag(tag uint64, decoder cbor.TagDecoder) (cbor.DecodeValue, interface{}, error) { | ||
log.Printf("createtag") | ||
return p, nil, nil | ||
} | ||
|
||
func (p *cborParser) SetTag(tag uint64, decval cbor.DecodeValue, decoder cbor.TagDecoder, val interface{}) error { | ||
log.Printf("settag") | ||
return nil | ||
} |
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
Oops, something went wrong.