Skip to content

Commit

Permalink
feat(container): read-write versioned dag-cbor
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobozzo committed Nov 13, 2024
1 parent 284a9d6 commit c5191a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
28 changes: 24 additions & 4 deletions pkg/container/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,32 @@ func FromCbor(r io.Reader) (Reader, error) {
if err != nil {
return nil, err
}
if n.Kind() != datamodel.Kind_List {
return nil, fmt.Errorf("not a list")
if n.Kind() != datamodel.Kind_Map {
return nil, fmt.Errorf("invalid container format: expected map")
}

ctn := make(Reader, n.Length())
vNode, err := n.LookupByString("version")
if err != nil {
return nil, fmt.Errorf("invalid container format: missing version")
}
version, err := vNode.AsString()
if err != nil {
return nil, fmt.Errorf("invalid container format: version must be string")
}
if version != defaultContainerVersion {
return nil, fmt.Errorf("unsupported container version: %s", version)
}

it := n.ListIterator()
tokensNode, err := n.LookupByString("tokens")
if err != nil {
return nil, fmt.Errorf("invalid container format: missing tokens")
}
if tokensNode.Kind() != datamodel.Kind_List {
return nil, fmt.Errorf("invalid container format: tokens must be a list")
}

ctn := make(Reader, tokensNode.Length())
it := tokensNode.ListIterator()
for !it.Done() {
_, val, err := it.Next()
if err != nil {
Expand All @@ -119,6 +138,7 @@ func FromCbor(r io.Reader) (Reader, error) {
return nil, err
}
}

return ctn, nil
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/container/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

// TODO: should we have a multibase to wrap the cbor? but there is no reader/write in go-multibase :-(

const defaultContainerVersion = "ctn-v1"

// Writer is a token container writer. It provides a convenient way to aggregate and serialize tokens together.
type Writer map[cid.Cid][]byte

Expand Down Expand Up @@ -44,10 +46,14 @@ func (ctn Writer) ToCarBase64(w io.Writer) error {
}

func (ctn Writer) ToCbor(w io.Writer) error {
node, err := qp.BuildList(basicnode.Prototype.Any, int64(len(ctn)), func(la datamodel.ListAssembler) {
for _, bytes := range ctn {
qp.ListEntry(la, qp.Bytes(bytes))
}
// Create a map with version and tokens
node, err := qp.BuildMap(basicnode.Prototype.Any, 2, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "version", qp.String(defaultContainerVersion))
qp.MapEntry(ma, "tokens", qp.List(int64(len(ctn)), func(la datamodel.ListAssembler) {
for _, bytes := range ctn {
qp.ListEntry(la, qp.Bytes(bytes))
}
}))
})
if err != nil {
return err
Expand Down

0 comments on commit c5191a9

Please sign in to comment.