Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for embedded CBOR in EDN decoder #116

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions edn.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@
case '\'':
s.convertBytes()

case '<':
s.convertEmbeddedCBOR()

// text string
case '"':
s.convertString()
Expand Down Expand Up @@ -779,6 +782,67 @@
s.writeByte(0xff)
}

func (s *ednDecState) convertEmbeddedCBOR() {
if !bytes.HasPrefix(s.data[s.off:], []byte("<<")) {
s.err = newSemanticError("cbor: invalid embedded CBOR")
return
}

Check warning on line 789 in edn.go

View check run for this annotation

Codecov / codecov/patch

edn.go#L787-L789

Added lines #L787 - L789 were not covered by tests

// skip "<<"
s.off += len("<<")
t := &ednDecState{data: s.data, off: s.off}
first := true
for {
t.skipWhitespace()
if t.err != nil {
s.err = t.err
return
}

Check warning on line 800 in edn.go

View check run for this annotation

Codecov / codecov/patch

edn.go#L798-L800

Added lines #L798 - L800 were not covered by tests
ch, err := t.peekByte()
if err != nil {
s.err = err
return
}

Check warning on line 805 in edn.go

View check run for this annotation

Codecov / codecov/patch

edn.go#L803-L805

Added lines #L803 - L805 were not covered by tests
if ch == '>' {
// end of array
t.off++
break
}
if !first {
if ch == ',' {
t.off++
} else {
s.err = newSemanticError("cbor: expected comma")
return
}

Check warning on line 817 in edn.go

View check run for this annotation

Codecov / codecov/patch

edn.go#L815-L817

Added lines #L815 - L817 were not covered by tests
}
first = false

// next element
t.decode()
if t.err != nil {
s.err = t.err
return
}

Check warning on line 826 in edn.go

View check run for this annotation

Codecov / codecov/patch

edn.go#L824-L826

Added lines #L824 - L826 were not covered by tests
}
s.off = t.off
ch, err := s.peekByte()
if err != nil {
s.err = err
return
}

Check warning on line 833 in edn.go

View check run for this annotation

Codecov / codecov/patch

edn.go#L831-L833

Added lines #L831 - L833 were not covered by tests
if ch != '>' {
s.err = newSemanticError("cbor: expected '>'")
return
}

Check warning on line 837 in edn.go

View check run for this annotation

Codecov / codecov/patch

edn.go#L835-L837

Added lines #L835 - L837 were not covered by tests
s.off++

ind := s.decodeEncodingIndicator()
s.writeUint(majorTypeBytes, ind, uint64(t.buf.Len()))
t.buf.WriteTo(&s.buf)

}

func (s *ednDecState) convertArray() {
ch, err := s.peekByte()
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions edn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ func TestDecodeEDN(t *testing.T) {
out: RawMessage{0x5f, 0xff},
},

// from RFC 8610 Appendix G.3.
{
in: "<<1>>",
out: RawMessage{0x41, 0x01},
},
{
in: "<<1, 2>>",
out: RawMessage{0x42, 0x01, 0x02},
},
{
in: `<<"foo", null>>`,
out: RawMessage{0x45, 0x63, 0x66, 0x6f, 0x6f, 0xf6},
},
{
in: `<<>>`,
out: RawMessage{0x40},
},

// from RFC 8610 Appendix G.1. and G.6.
{
in: "h'48656c6c6f20776f726c64'",
Expand Down