Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
pin verify: use temporary struct
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Feb 19, 2019
1 parent dbee4e2 commit e400fa3
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,20 @@ func (api *PinAPI) Update(ctx context.Context, from iface.Path, to iface.Path, o
}

type pinVerifyRes struct {
Cid string
JOk bool `json:"Ok"`
JBadNodes []*badNode `json:"BadNodes,omitempty"`
ok bool
badNodes []iface.BadPinNode
}

func (r *pinVerifyRes) Ok() bool {
return r.JOk
return r.ok
}

func (r *pinVerifyRes) BadNodes() []iface.BadPinNode {
out := make([]iface.BadPinNode, len(r.JBadNodes))
for i, n := range r.JBadNodes {
out[i] = n
}
return out
return r.badNodes
}

type badNode struct {
Cid string
JErr string `json:"Err"`

err error
cid cid.Cid
}

Expand All @@ -119,10 +112,7 @@ func (n *badNode) Path() iface.ResolvedPath {
}

func (n *badNode) Err() error {
if n.JErr != "" {
return errors.New(n.JErr)
}
return nil
return n.err
}

func (api *PinAPI) Verify(ctx context.Context) (<-chan iface.PinStatus, error) {
Expand All @@ -140,20 +130,45 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan iface.PinStatus, error) {
defer close(res)
dec := json.NewDecoder(resp.Output)
for {
var out pinVerifyRes
var out struct {
Cid string
Ok bool

BadNodes []struct{
Cid string
Err string
}
}
if err := dec.Decode(&out); err != nil {
return // todo: handle non io.EOF somehow
}

for i, n := range out.JBadNodes {
out.JBadNodes[i].cid, err = cid.Decode(n.Cid)
badNodes := make([]iface.BadPinNode, len(out.BadNodes))
for i, n := range out.BadNodes {
c, err := cid.Decode(n.Cid)
if err != nil {
return
badNodes[i] = &badNode{
cid: c,
err: err,
}
continue
}

if n.Err != "" {
err = errors.New(n.Err)
}
badNodes[i] = &badNode{
cid: c,
err: err,
}
}

select {
case res <- &out:
case res <- &pinVerifyRes{
ok: out.Ok,

badNodes: badNodes,
}:
case <-ctx.Done():
return
}
Expand Down

0 comments on commit e400fa3

Please sign in to comment.