Skip to content

Commit

Permalink
cbor: encode tags prefixed to values when present.
Browse files Browse the repository at this point in the history
We should now be able to round-trip cbor tags losslessly in a cbor<->tokenstream<->cbor decode/encoder.  This is good; but also fairly useless, because nothing but cbor supports these tags; no other serializers, nor have we yet implemented any way to alter the behavior of obj unmarshal in response to them, nor yet obj marshal to emit them.

Partially fixes #7.

Signed-off-by: Eric Myhre <hash@exultant.us>
  • Loading branch information
warpfork committed Oct 15, 2017
1 parent a3a8b32 commit 9443ddd
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cbor/cborEncoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
d.current -= 1
fallthrough
case phase_anyExpectValue, phase_arrDefExpectValueOrEnd, phase_arrIndefExpectValueOrEnd:
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
if tokenSlot.Length >= 0 {
d.pushPhase(phase_mapDefExpectKeyOrEnd)
d.emitMajorPlusLen(cborMajorMap, uint64(tokenSlot.Length))
Expand Down Expand Up @@ -110,6 +113,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
d.current -= 1
fallthrough
case phase_anyExpectValue, phase_arrDefExpectValueOrEnd, phase_arrIndefExpectValueOrEnd:
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
if tokenSlot.Length >= 0 {
d.pushPhase(phase_arrDefExpectValueOrEnd)
d.emitMajorPlusLen(cborMajorArray, uint64(tokenSlot.Length))
Expand Down Expand Up @@ -143,6 +149,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
d.current -= 1
fallthrough
case phase_anyExpectValue, phase_arrDefExpectValueOrEnd, phase_arrIndefExpectValueOrEnd:
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
d.w.writen1(cborSigilBreak)
return d.popPhase(), d.w.checkErr()
case phase_mapDefExpectKeyOrEnd, phase_mapIndefExpectKeyOrEnd:
Expand All @@ -165,6 +174,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
}
emitStr:
{
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
d.encodeString(tokenSlot.Str)
return phase == phase_anyExpectValue, d.w.checkErr()
}
Expand All @@ -174,6 +186,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
d.current -= 1
fallthrough
case phase_anyExpectValue, phase_arrDefExpectValueOrEnd, phase_arrIndefExpectValueOrEnd:
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
d.encodeBytes(tokenSlot.Bytes)
return phase == phase_anyExpectValue, d.w.checkErr()
case phase_mapDefExpectKeyOrEnd, phase_mapIndefExpectKeyOrEnd:
Expand All @@ -187,6 +202,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
d.current -= 1
fallthrough
case phase_anyExpectValue, phase_arrDefExpectValueOrEnd, phase_arrIndefExpectValueOrEnd:
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
d.encodeBool(tokenSlot.Bool)
return phase == phase_anyExpectValue, d.w.checkErr()
case phase_mapDefExpectKeyOrEnd, phase_mapIndefExpectKeyOrEnd:
Expand All @@ -209,6 +227,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
}
emitInt:
{
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
d.encodeInt64(tokenSlot.Int)
return phase == phase_anyExpectValue, d.w.checkErr()
}
Expand All @@ -227,6 +248,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
}
emitUint:
{
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
d.encodeUint64(tokenSlot.Uint)
return phase == phase_anyExpectValue, d.w.checkErr()
}
Expand All @@ -236,6 +260,9 @@ func (d *Encoder) Step(tokenSlot *Token) (done bool, err error) {
d.current -= 1
fallthrough
case phase_anyExpectValue, phase_arrDefExpectValueOrEnd, phase_arrIndefExpectValueOrEnd:
if tokenSlot.Tagged {
d.emitMajorPlusLen(cborMajorTag, uint64(tokenSlot.Tag))
}
d.encodeFloat64(tokenSlot.Float64)
return phase == phase_anyExpectValue, d.w.checkErr()
case phase_mapDefExpectKeyOrEnd, phase_mapIndefExpectKeyOrEnd:
Expand Down

0 comments on commit 9443ddd

Please sign in to comment.