Skip to content

Commit edbed55

Browse files
committed
feat: path consolidation
1 parent 434c17b commit edbed55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+627
-427
lines changed

assets/assets.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/ipfs/kubo/core/coreapi"
1010

1111
options "github.com/ipfs/boxo/coreiface/options"
12-
"github.com/ipfs/boxo/coreiface/path"
1312
"github.com/ipfs/boxo/files"
13+
"github.com/ipfs/boxo/path"
1414
cid "github.com/ipfs/go-cid"
1515
)
1616

@@ -44,7 +44,7 @@ func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
4444
return cid.Cid{}, err
4545
}
4646

47-
basePath := path.IpfsPath(dirb.Cid())
47+
basePath := path.NewIPFSPath(dirb.Cid())
4848

4949
for _, p := range l {
5050
d, err := Asset.ReadFile(p)

client/rpc/api_test.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"time"
1313

1414
iface "github.com/ipfs/boxo/coreiface"
15-
"github.com/ipfs/boxo/coreiface/path"
1615
"github.com/ipfs/boxo/coreiface/tests"
16+
"github.com/ipfs/boxo/path"
1717
"github.com/ipfs/kubo/test/cli/harness"
1818
ma "github.com/multiformats/go-multiaddr"
1919
"go.uber.org/multierr"
@@ -70,7 +70,11 @@ func (np NodeProvider) MakeAPISwarm(t *testing.T, ctx context.Context, fullIdent
7070
apis[i] = api
7171

7272
// empty node is pinned even with --empty-repo, we don't want that
73-
emptyNode := path.New("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
73+
emptyNode, err := path.NewPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
74+
if err != nil {
75+
return err
76+
}
77+
7478
if err := api.Pin().Rm(ctx, emptyNode); err != nil {
7579
return err
7680
}
@@ -126,7 +130,11 @@ func Test_NewURLApiWithClient_With_Headers(t *testing.T) {
126130
t.Fatal(err)
127131
}
128132
api.Headers.Set(headerToTest, expectedHeaderValue)
129-
if err := api.Pin().Rm(context.Background(), path.New("/ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")); err != nil {
133+
p, err := path.NewPath("/ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")
134+
if err != nil {
135+
t.Fatal(err)
136+
}
137+
if err := api.Pin().Rm(context.Background(), p); err != nil {
130138
t.Fatal(err)
131139
}
132140
}

client/rpc/apifile.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import (
66
"fmt"
77
"io"
88

9-
"github.com/ipfs/boxo/coreiface/path"
109
"github.com/ipfs/boxo/files"
1110
unixfs "github.com/ipfs/boxo/ipld/unixfs"
11+
"github.com/ipfs/boxo/path"
1212
"github.com/ipfs/go-cid"
1313
)
1414

1515
const forwardSeekLimit = 1 << 14 // 16k
1616

1717
func (api *UnixfsAPI) Get(ctx context.Context, p path.Path) (files.Node, error) {
18-
if p.Mutable() { // use resolved path in case we are dealing with IPNS / MFS
18+
if p.Namespace().Mutable() { // use resolved path in case we are dealing with IPNS / MFS
1919
var err error
2020
p, err = api.core().ResolvePath(ctx, p)
2121
if err != nil {
@@ -195,13 +195,13 @@ func (it *apiIter) Next() bool {
195195

196196
switch it.cur.Type {
197197
case unixfs.THAMTShard, unixfs.TMetadata, unixfs.TDirectory:
198-
it.curFile, err = it.core.getDir(it.ctx, path.IpfsPath(c), int64(it.cur.Size))
198+
it.curFile, err = it.core.getDir(it.ctx, path.NewIPFSPath(c), int64(it.cur.Size))
199199
if err != nil {
200200
it.err = err
201201
return false
202202
}
203203
case unixfs.TFile:
204-
it.curFile, err = it.core.getFile(it.ctx, path.IpfsPath(c), int64(it.cur.Size))
204+
it.curFile, err = it.core.getFile(it.ctx, path.NewIPFSPath(c), int64(it.cur.Size))
205205
if err != nil {
206206
it.err = err
207207
return false

client/rpc/block.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
iface "github.com/ipfs/boxo/coreiface"
1010
caopts "github.com/ipfs/boxo/coreiface/options"
11-
"github.com/ipfs/boxo/coreiface/path"
11+
"github.com/ipfs/boxo/path"
1212
"github.com/ipfs/go-cid"
1313
mc "github.com/multiformats/go-multicodec"
1414
mh "github.com/multiformats/go-multihash"
@@ -27,8 +27,8 @@ func (s *blockStat) Size() int {
2727
return s.BSize
2828
}
2929

30-
func (s *blockStat) Path() path.Resolved {
31-
return path.IpldPath(s.cid)
30+
func (s *blockStat) Path() path.ImmutablePath {
31+
return path.NewIPLDPath(s.cid)
3232
}
3333

3434
func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.BlockPutOption) (iface.BlockStat, error) {

client/rpc/dag.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"io"
88

99
"github.com/ipfs/boxo/coreiface/options"
10-
"github.com/ipfs/boxo/coreiface/path"
11-
"github.com/ipfs/go-block-format"
10+
"github.com/ipfs/boxo/path"
11+
blocks "github.com/ipfs/go-block-format"
1212
"github.com/ipfs/go-cid"
1313
format "github.com/ipfs/go-ipld-format"
1414
multicodec "github.com/multiformats/go-multicodec"
@@ -21,7 +21,7 @@ type (
2121
)
2222

2323
func (api *HttpDagServ) Get(ctx context.Context, c cid.Cid) (format.Node, error) {
24-
r, err := api.core().Block().Get(ctx, path.IpldPath(c))
24+
r, err := api.core().Block().Get(ctx, path.NewIPLDPath(c))
2525
if err != nil {
2626
return nil, err
2727
}
@@ -116,7 +116,7 @@ func (api *HttpDagServ) Pinning() format.NodeAdder {
116116
}
117117

118118
func (api *HttpDagServ) Remove(ctx context.Context, c cid.Cid) error {
119-
return api.core().Block().Rm(ctx, path.IpldPath(c)) // TODO: should we force rm?
119+
return api.core().Block().Rm(ctx, path.NewIPLDPath(c)) // TODO: should we force rm?
120120
}
121121

122122
func (api *HttpDagServ) RemoveMany(ctx context.Context, cids []cid.Cid) error {

client/rpc/dht.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66

77
caopts "github.com/ipfs/boxo/coreiface/options"
8-
"github.com/ipfs/boxo/coreiface/path"
8+
"github.com/ipfs/boxo/path"
99
"github.com/libp2p/go-libp2p/core/peer"
1010
"github.com/libp2p/go-libp2p/core/routing"
1111
)

client/rpc/key.go

+47-27
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,50 @@ import (
66

77
iface "github.com/ipfs/boxo/coreiface"
88
caopts "github.com/ipfs/boxo/coreiface/options"
9-
"github.com/ipfs/boxo/coreiface/path"
9+
"github.com/ipfs/boxo/ipns"
10+
"github.com/ipfs/boxo/path"
1011
"github.com/libp2p/go-libp2p/core/peer"
1112
)
1213

1314
type KeyAPI HttpApi
1415

15-
type keyOutput struct {
16-
JName string `json:"Name"`
17-
Id string
16+
type key struct {
17+
name string
18+
pid peer.ID
19+
path path.Path
20+
}
21+
22+
func newKey(name, pidStr string) (*key, error) {
23+
pid, err := peer.Decode(pidStr)
24+
if err != nil {
25+
return nil, err
26+
}
1827

19-
pid peer.ID
28+
path, err := path.NewPath("/ipns/" + ipns.NameFromPeer(pid).String())
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
return &key{name: name, pid: pid, path: path}, nil
2034
}
2135

22-
func (k *keyOutput) Name() string {
23-
return k.JName
36+
func (k *key) Name() string {
37+
return k.name
2438
}
2539

26-
func (k *keyOutput) Path() path.Path {
27-
return path.New("/ipns/" + k.Id)
40+
func (k *key) Path() path.Path {
41+
return k.path
2842
}
2943

30-
func (k *keyOutput) ID() peer.ID {
44+
func (k *key) ID() peer.ID {
3145
return k.pid
3246
}
3347

48+
type keyOutput struct {
49+
Name string
50+
Id string
51+
}
52+
3453
func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (iface.Key, error) {
3554
options, err := caopts.KeyGenerateOptions(opts...)
3655
if err != nil {
@@ -45,8 +64,8 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
4564
if err != nil {
4665
return nil, err
4766
}
48-
out.pid, err = peer.Decode(out.Id)
49-
return &out, err
67+
68+
return newKey(out.Name, out.Id)
5069
}
5170

5271
func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (iface.Key, bool, error) {
@@ -68,25 +87,29 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
6887
return nil, false, err
6988
}
7089

71-
id := &keyOutput{JName: out.Now, Id: out.Id}
72-
id.pid, err = peer.Decode(id.Id)
73-
return id, out.Overwrite, err
90+
key, err := newKey(out.Now, out.Id)
91+
if err != nil {
92+
return nil, false, err
93+
}
94+
95+
return key, out.Overwrite, err
7496
}
7597

7698
func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
77-
var out struct{ Keys []*keyOutput }
99+
var out struct {
100+
Keys []keyOutput
101+
}
78102
if err := api.core().Request("key/list").Exec(ctx, &out); err != nil {
79103
return nil, err
80104
}
81105

82106
res := make([]iface.Key, len(out.Keys))
83107
for i, k := range out.Keys {
84-
var err error
85-
k.pid, err = peer.Decode(k.Id)
108+
key, err := newKey(k.Name, k.Id)
86109
if err != nil {
87110
return nil, err
88111
}
89-
res[i] = k
112+
res[i] = key
90113
}
91114

92115
return res, nil
@@ -98,24 +121,21 @@ func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
98121
return nil, err
99122
}
100123

101-
var err error
102-
out := keyOutput{JName: "self", Id: id.ID}
103-
out.pid, err = peer.Decode(out.Id)
104-
return &out, err
124+
return newKey("self", id.ID)
105125
}
106126

107127
func (api *KeyAPI) Remove(ctx context.Context, name string) (iface.Key, error) {
108-
var out struct{ Keys []keyOutput }
128+
var out struct {
129+
Keys []keyOutput
130+
}
109131
if err := api.core().Request("key/rm", name).Exec(ctx, &out); err != nil {
110132
return nil, err
111133
}
112134
if len(out.Keys) != 1 {
113135
return nil, errors.New("got unexpected number of keys back")
114136
}
115137

116-
var err error
117-
out.Keys[0].pid, err = peer.Decode(out.Keys[0].Id)
118-
return &out.Keys[0], err
138+
return newKey(out.Keys[0].Name, out.Keys[0].Id)
119139
}
120140

121141
func (api *KeyAPI) core() *HttpApi {

client/rpc/name.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
iface "github.com/ipfs/boxo/coreiface"
1010
caopts "github.com/ipfs/boxo/coreiface/options"
1111
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
12-
"github.com/ipfs/boxo/coreiface/path"
1312
"github.com/ipfs/boxo/ipns"
13+
"github.com/ipfs/boxo/path"
1414
)
1515

1616
type NameAPI HttpApi
@@ -84,7 +84,11 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
8484
}
8585
var ires iface.IpnsResult
8686
if err == nil {
87-
ires.Path = path.New(out.Path)
87+
p, err := path.NewPath(out.Path)
88+
if err != nil {
89+
return
90+
}
91+
ires.Path = p
8892
}
8993

9094
select {
@@ -122,7 +126,7 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
122126
return nil, err
123127
}
124128

125-
return path.New(out.Path), nil
129+
return path.NewPath(out.Path)
126130
}
127131

128132
func (api *NameAPI) core() *HttpApi {

0 commit comments

Comments
 (0)