Skip to content

Commit

Permalink
feat: wip upload command
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Dec 8, 2023
1 parent e340d25 commit 4c51c6a
Show file tree
Hide file tree
Showing 17 changed files with 511 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ proof, _ := delegation.Extract(b)
// space to list uploads from
space, _ := did.Parse("did:key:z6MkwDuRThQcyWjqNsK54yKAmzfsiH6BTkASyiucThMtHt1y")

rcpt, _ := client.List(
rcpt, _ := client.UploadList(
signer,
space,
&uploadlist.Caveat{},
Expand Down
52 changes: 52 additions & 0 deletions capability/storeadd/capability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package storeadd

import (
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/web3-storage/go-ucanto/did"
"github.com/web3-storage/go-ucanto/ucan"
)

const Ability = "store/add"

type Caveat struct {
Link ipld.Link
Size uint64
Origin *ipld.Link
}

var _ ucan.MapBuilder = (*Caveat)(nil)

func (c *Caveat) Build() (map[string]datamodel.Node, error) {
data := map[string]datamodel.Node{}

b := basicnode.Prototype.Link.NewBuilder()
err := b.AssignLink(c.Link)
if err != nil {
return nil, err
}
data["link"] = b.Build()

b = basicnode.Prototype.Int.NewBuilder()
err = b.AssignInt(int64(c.Size))
if err != nil {
return nil, err
}
data["size"] = b.Build()

if c.Origin != nil {
b = basicnode.Prototype.Link.NewBuilder()
err = b.AssignLink(c.Link)
if err != nil {
return nil, err
}
data["origin"] = b.Build()
}

return data, nil
}

func NewCapability(space did.DID, nb *Caveat) ucan.Capability[ucan.MapBuilder] {
return ucan.NewCapability(Ability, space.String(), ucan.MapBuilder(nb))
}
7 changes: 7 additions & 0 deletions capability/storeadd/receipt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package storeadd

import "github.com/web3-storage/go-ucanto/core/receipt"

func NewReceiptReader() (receipt.ReceiptReader[*Success, *Failure], error) {
return receipt.NewReceiptReader[*Success, *Failure](ResultSchema)
}
29 changes: 29 additions & 0 deletions capability/storeadd/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package storeadd

import (
_ "embed"

"github.com/ipld/go-ipld-prime"
)

//go:embed result.ipldsch
var ResultSchema []byte

type Success struct {
Status string
With string
Link ipld.Link
Url *string
Headers *Headers
}

type Headers struct {
Keys []string
Values map[string]string
}

type Failure struct {
Name *string
Message string
Stack *string
}
40 changes: 40 additions & 0 deletions capability/storeadd/result.ipldsch
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
type Result union {
| Success "ok"
| Failure "error"
} representation keyed

type Success struct {
status String
with DID
link Link
url optional URL
headers optional {String: String}
}

# type Success union {
# | SuccessUpload "upload"
# | SuccessDone "done"
# } representation inline {
# discriminantKey "status"
# }

# type SuccessUpload struct {
# with DID
# link Link
# url URL
# headers {String: String}
# }

# type SuccessDone struct {
# with DID
# link Link
# }

type DID string
type URL string

type Failure struct {
name optional String
message String
stack optional String
}
50 changes: 50 additions & 0 deletions capability/uploadadd/capability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package uploadadd

import (
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/web3-storage/go-ucanto/did"
"github.com/web3-storage/go-ucanto/ucan"
)

const Ability = "upload/add"

type Caveat struct {
Root ipld.Link
Shards []ipld.Link
}

var _ ucan.MapBuilder = (*Caveat)(nil)

func (c *Caveat) Build() (map[string]datamodel.Node, error) {
data := map[string]datamodel.Node{}

b := basicnode.Prototype.Link.NewBuilder()
err := b.AssignLink(c.Root)
if err != nil {
return nil, err
}
data["root"] = b.Build()

if c.Shards != nil {
b := basicnode.Prototype.Any.NewBuilder()
la, err := b.BeginList(int64(len(c.Shards)))
if err != nil {
return nil, err
}
for _, s := range c.Shards {
err := la.AssembleValue().AssignLink(s)
if err != nil {
return nil, err
}
}
la.Finish()
data["shards"] = b.Build()
}
return data, nil
}

func NewCapability(space did.DID, nb *Caveat) ucan.Capability[ucan.MapBuilder] {
return ucan.NewCapability(Ability, space.String(), ucan.MapBuilder(nb))
}
7 changes: 7 additions & 0 deletions capability/uploadadd/receipt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uploadadd

import "github.com/web3-storage/go-ucanto/core/receipt"

func NewReceiptReader() (receipt.ReceiptReader[*Success, *Failure], error) {
return receipt.NewReceiptReader[*Success, *Failure](ResultSchema)
}
21 changes: 21 additions & 0 deletions capability/uploadadd/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uploadadd

import (
_ "embed"

"github.com/ipld/go-ipld-prime"
)

//go:embed result.ipldsch
var ResultSchema []byte

type Success struct {
Root ipld.Link
Shards []ipld.Link
}

type Failure struct {
Name *string
Message string
Stack *string
}
15 changes: 15 additions & 0 deletions capability/uploadadd/result.ipldsch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Result union {
| Success "ok"
| Failure "error"
} representation keyed

type Success struct {
root Link
shards optional [Link]
}

type Failure struct {
name optional String
message String
stack optional String
}
15 changes: 12 additions & 3 deletions capability/uploadlist/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ func (c *Caveat) Build() (map[string]datamodel.Node, error) {
data := map[string]datamodel.Node{}
if c.Cursor != "" {
b := basicnode.Prototype.String.NewBuilder()
b.AssignString(c.Cursor)
err := b.AssignString(c.Cursor)
if err != nil {
return nil, err
}
data["cursor"] = b.Build()
}
if c.Size != 0 {
b := basicnode.Prototype.Int.NewBuilder()
b.AssignInt(c.Size)
err := b.AssignInt(c.Size)
if err != nil {
return nil, err
}
data["size"] = b.Build()
}
if c.Pre {
b := basicnode.Prototype.Bool.NewBuilder()
b.AssignBool(c.Pre)
err := b.AssignBool(c.Pre)
if err != nil {
return nil, err
}
data["pre"] = b.Build()
}
return data, nil
Expand Down
54 changes: 0 additions & 54 deletions capability/uploadlist/model.go

This file was deleted.

4 changes: 2 additions & 2 deletions capability/uploadlist/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package uploadlist

import "github.com/web3-storage/go-ucanto/core/receipt"

func NewReceiptReader() (receipt.ReceiptReader[*UploadListSuccess, *UploadListFailure], error) {
return receipt.NewReceiptReader[*UploadListSuccess, *UploadListFailure](UploadSchema)
func NewReceiptReader() (receipt.ReceiptReader[*Success, *Failure], error) {
return receipt.NewReceiptReader[*Success, *Failure](ResultSchema)
}
31 changes: 31 additions & 0 deletions capability/uploadlist/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uploadlist

import (
_ "embed"

"github.com/ipld/go-ipld-prime"
)

//go:embed result.ipldsch
var ResultSchema []byte

type Success struct {
Results []Item
Before *string
After *string
Cursor *string
Size uint64
}

type Item struct {
Root ipld.Link
Shards []ipld.Link
InsertedAt string
UpdatedAt string
}

type Failure struct {
Name *string
Message string
Stack *string
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
type Result union {
| UploadListSuccess "ok"
| UploadListFailure "error"
| Success "ok"
| Failure "error"
} representation keyed

type UploadListSuccess struct {
results [UploadListItem]
type Success struct {
results [Item]
cursor optional String
before optional String
after optional String
size Int
}

type UploadListItem struct {
type Item struct {
root Link
shards optional [Link]
insertedAt String
updatedAt String
}

type UploadListFailure struct {
type Failure struct {
name optional String
message String
stack optional String
Expand Down
Loading

0 comments on commit 4c51c6a

Please sign in to comment.