Skip to content

Commit

Permalink
refactor!: remove ResolvedPath and use ImmutablePath instead
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Aug 14, 2023
1 parent 2744ea9 commit 0a71844
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 148 deletions.
2 changes: 1 addition & 1 deletion coreiface/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type BlockStat interface {
Size() int

// Path returns path to the block
Path() path.ResolvedPath
Path() path.ImmutablePath
}

// BlockAPI specifies the interface to the block layer
Expand Down
2 changes: 1 addition & 1 deletion coreiface/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type CoreAPI interface {
Routing() RoutingAPI

// ResolvePath resolves the path using Unixfs resolver
ResolvePath(context.Context, path.Path) (path.ResolvedPath, error)
ResolvePath(context.Context, path.Path) (path.ImmutablePath, error)

// ResolveNode resolves the path (if not resolved already) using Unixfs
// resolver, gets and returns the resolved Node
Expand Down
14 changes: 7 additions & 7 deletions coreiface/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ type ObjectChange struct {

// Before holds the link path before the change. Note that when a link is
// added, this will be nil.
Before path.ResolvedPath
Before path.ImmutablePath

// After holds the link path after the change. Note that when a link is
// removed, this will be nil.
After path.ResolvedPath
After path.ImmutablePath
}

// ObjectAPI specifies the interface to MerkleDAG and contains useful utilities
Expand All @@ -73,7 +73,7 @@ type ObjectAPI interface {
New(context.Context, ...options.ObjectNewOption) (ipld.Node, error)

// Put imports the data into merkledag
Put(context.Context, io.Reader, ...options.ObjectPutOption) (path.ResolvedPath, error)
Put(context.Context, io.Reader, ...options.ObjectPutOption) (path.ImmutablePath, error)

// Get returns the node for the path
Get(context.Context, path.Path) (ipld.Node, error)
Expand All @@ -90,16 +90,16 @@ type ObjectAPI interface {
// AddLink adds a link under the specified path. child path can point to a
// subdirectory within the patent which must be present (can be overridden
// with WithCreate option).
AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...options.ObjectAddLinkOption) (path.ResolvedPath, error)
AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...options.ObjectAddLinkOption) (path.ImmutablePath, error)

// RmLink removes a link from the node
RmLink(ctx context.Context, base path.Path, link string) (path.ResolvedPath, error)
RmLink(ctx context.Context, base path.Path, link string) (path.ImmutablePath, error)

// AppendData appends data to the node
AppendData(context.Context, path.Path, io.Reader) (path.ResolvedPath, error)
AppendData(context.Context, path.Path, io.Reader) (path.ImmutablePath, error)

// SetData sets the data contained in the node
SetData(context.Context, path.Path, io.Reader) (path.ResolvedPath, error)
SetData(context.Context, path.Path, io.Reader) (path.ImmutablePath, error)

// Diff returns a set of changes needed to transform the first object into the
// second.
Expand Down
4 changes: 2 additions & 2 deletions coreiface/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Pin holds information about pinned resource
type Pin interface {
// Path to the pinned object
Path() path.ResolvedPath
Path() path.ImmutablePath

// Type of the pin
Type() string
Expand All @@ -35,7 +35,7 @@ type PinStatus interface {
// BadPinNode is a node that has been marked as bad by Pin.Verify
type BadPinNode interface {
// Path is the path of the node
Path() path.ResolvedPath
Path() path.ImmutablePath

// Err is the reason why the node has been marked as bad
Err() error
Expand Down
2 changes: 1 addition & 1 deletion coreiface/tests/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (tp *TestSuite) TestPathRoot(t *testing.T) {
t.Fatal(err)
}

if rp.Root().String() != nd.Cid().String() {
if rp.Cid().String() != nd.Cid().String() {

Check warning on line 196 in coreiface/tests/path.go

View check run for this annotation

Codecov / codecov/patch

coreiface/tests/path.go#L196

Added line #L196 was not covered by tests
t.Error("unexpected path root")
}

Expand Down
2 changes: 1 addition & 1 deletion coreiface/tests/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (tp *TestSuite) TestAdd(t *testing.T) {
t.Fatal(err)
}

p := func(h string) path.ResolvedPath {
p := func(h string) path.ImmutablePath {

Check warning on line 106 in coreiface/tests/unixfs.go

View check run for this annotation

Codecov / codecov/patch

coreiface/tests/unixfs.go#L106

Added line #L106 was not covered by tests
c, err := cid.Parse(h)
if err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions coreiface/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

type AddEvent struct {
Name string
Path path.ResolvedPath `json:",omitempty"`
Bytes int64 `json:",omitempty"`
Size string `json:",omitempty"`
Path path.ImmutablePath `json:",omitempty"`
Bytes int64 `json:",omitempty"`
Size string `json:",omitempty"`
}

// FileType is an enum of possible UnixFS file types.
Expand Down Expand Up @@ -65,7 +65,7 @@ type UnixfsAPI interface {
// Add imports the data from the reader into merkledag file
//
// TODO: a long useful comment on how to use this for many different scenarios
Add(context.Context, files.Node, ...options.UnixfsAddOption) (path.ResolvedPath, error)
Add(context.Context, files.Node, ...options.UnixfsAddOption) (path.ImmutablePath, error)

// Get returns a read-only handle to a file tree referenced by a path
//
Expand Down
22 changes: 11 additions & 11 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
gopath "path"
"strings"

"github.com/ipfs/boxo/blockservice"
Expand Down Expand Up @@ -471,7 +470,7 @@ func (bb *BlocksBackend) getNode(ctx context.Context, path path.ImmutablePath) (
return md, nd, err
}

func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.ImmutablePath) ([]cid.Cid, path.ResolvedPath, error) {
func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.ImmutablePath) ([]cid.Cid, path.ImmutablePath, error) {
/*
These are logical roots where each CID represent one path segment
and resolves to either a directory or the root block of a file.
Expand All @@ -495,7 +494,7 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
contentPathStr := contentPath.String()
pathSegments := strings.Split(contentPathStr[6:], "/")
sp.WriteString(contentPathStr[:5]) // /ipfs or /ipns
var lastPath path.ResolvedPath
var lastPath path.ImmutablePath
for _, root := range pathSegments {
if root == "" {
continue
Expand Down Expand Up @@ -528,13 +527,13 @@ func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.
case path.IPNSNamespace:
p, err := resolve.ResolveIPNS(ctx, bb.namesys, p)
if err != nil {
return path.ImmutablePath{}, err
return nil, err
}
return path.NewImmutablePath(p)
case path.IPFSNamespace:
return path.NewImmutablePath(p)

Check warning on line 534 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L533-L534

Added lines #L533 - L534 were not covered by tests
default:
return path.ImmutablePath{}, NewErrorStatusCode(fmt.Errorf("unsupported path namespace: %s", p.Namespace()), http.StatusNotImplemented)
return nil, NewErrorStatusCode(fmt.Errorf("unsupported path namespace: %s", p.Namespace()), http.StatusNotImplemented)

Check warning on line 536 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L536

Added line #L536 was not covered by tests
}
}

Expand Down Expand Up @@ -592,11 +591,7 @@ func (bb *BlocksBackend) ResolvePath(ctx context.Context, path path.ImmutablePat
return md, nil
}

func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ResolvedPath, error) {
if _, ok := p.(path.ResolvedPath); ok {
return p.(path.ResolvedPath), nil
}

func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
var err error
if p.Namespace() == path.IPNSNamespace {
p, err = resolve.ResolveIPNS(ctx, bb.namesys, p)

Check warning on line 597 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L597

Added line #L597 was not covered by tests
Expand All @@ -614,7 +609,12 @@ func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.Res
return nil, err
}

return path.NewResolvedPath(p, node, gopath.Join(rest...)), nil
p, err = path.Join(path.NewIPFSPath(node), rest...)
if err != nil {
return nil, err
}

return path.NewImmutablePath(p)
}

type nodeGetterToCarExporer struct {
Expand Down
2 changes: 1 addition & 1 deletion gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (d DuplicateBlocksPolicy) String() string {

type ContentPathMetadata struct {
PathSegmentRoots []cid.Cid
LastSegment path.ResolvedPath
LastSegment path.ImmutablePath
ContentType string // Only used for UnixFS requests
}

Expand Down
2 changes: 1 addition & 1 deletion gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ func (mb *errorMockBackend) GetCAR(ctx context.Context, path path.ImmutablePath,
}

func (mb *errorMockBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
return path.ImmutablePath{}, mb.err
return nil, mb.err
}

func (mb *errorMockBackend) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, error) {
Expand Down
6 changes: 3 additions & 3 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func (i *handler) handleWebRequestErrors(w http.ResponseWriter, r *http.Request,
if errors.Is(err, ErrServiceUnavailable) {
err = fmt.Errorf("failed to resolve %s: %w", debugStr(contentPath.String()), err)
i.webError(w, r, err, http.StatusServiceUnavailable)
return path.ImmutablePath{}, false
return nil, false

Check warning on line 776 in gateway/handler.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler.go#L776

Added line #L776 was not covered by tests
}

// If we have origin isolation (subdomain gw, DNSLink website),
Expand All @@ -795,12 +795,12 @@ func (i *handler) handleWebRequestErrors(w http.ResponseWriter, r *http.Request,
// follow https://docs.ipfs.tech/how-to/websites-on-ipfs/redirects-and-custom-404s/ instead.
if i.serveLegacy404IfPresent(w, r, immutableContentPath, logger) {
logger.Debugw("served legacy 404")
return path.ImmutablePath{}, false
return nil, false
}

err = fmt.Errorf("failed to resolve %s: %w", debugStr(contentPath.String()), err)
i.webError(w, r, err, http.StatusInternalServerError)
return path.ImmutablePath{}, false
return nil, false
}

// Detect 'Cache-Control: only-if-cached' in request and return data if it is already in the local datastore.
Expand Down
4 changes: 2 additions & 2 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (i *handler) renderCodec(ctx context.Context, w http.ResponseWriter, r *htt
return i.serveCodecConverted(ctx, w, r, blockCid, blockData, rq.contentPath, toCodec, modtime, rq.begin)
}

func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *http.Request, blockCid cid.Cid, blockData io.ReadSeekCloser, resolvedPath path.ResolvedPath, contentPath path.Path) bool {
func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *http.Request, blockCid cid.Cid, blockData io.ReadSeekCloser, resolvedPath path.ImmutablePath, contentPath path.Path) bool {
// WithHostname may have constructed an IPFS (or IPNS) path using the Host header.
// In this case, we need the original path for constructing the redirect.
requestURI, err := url.ParseRequestURI(r.RequestURI)
Expand Down Expand Up @@ -288,7 +288,7 @@ func (i *handler) serveCodecConverted(ctx context.Context, w http.ResponseWriter
return false
}

func setCodecContentDisposition(w http.ResponseWriter, r *http.Request, resolvedPath path.ResolvedPath, contentType string) string {
func setCodecContentDisposition(w http.ResponseWriter, r *http.Request, resolvedPath path.ImmutablePath, contentType string) string {
var dispType, name string

ext, ok := contentTypeToExtension[contentType]
Expand Down
16 changes: 8 additions & 8 deletions gateway/handler_unixfs__redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,40 @@ func (i *handler) serveRedirectsIfPresent(w http.ResponseWriter, r *http.Request
if err != nil {
err = fmt.Errorf("trouble processing _redirects path %q: %w", immutableContentPath.String(), err)
i.webError(w, r, err, http.StatusInternalServerError)
return path.ImmutablePath{}, false, true
return nil, false, true
}

Check warning on line 49 in gateway/handler_unixfs__redirects.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs__redirects.go#L46-L49

Added lines #L46 - L49 were not covered by tests

redirectsPath, err := path.Join(rootPath, "_redirects")
if err != nil {
err = fmt.Errorf("trouble processing _redirects path %q: %w", rootPath.String(), err)
i.webError(w, r, err, http.StatusInternalServerError)
return path.ImmutablePath{}, false, true
return nil, false, true
}

Check warning on line 56 in gateway/handler_unixfs__redirects.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs__redirects.go#L53-L56

Added lines #L53 - L56 were not covered by tests

imRedirectsPath, err := path.NewImmutablePath(redirectsPath)
if err != nil {
err = fmt.Errorf("trouble processing _redirects path %q: %w", redirectsPath, err)
i.webError(w, r, err, http.StatusInternalServerError)
return path.ImmutablePath{}, false, true
return nil, false, true

Check warning on line 62 in gateway/handler_unixfs__redirects.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs__redirects.go#L62

Added line #L62 was not covered by tests
}

foundRedirect, redirectRules, err := i.getRedirectRules(r, imRedirectsPath)
if err != nil {
err = fmt.Errorf("trouble processing _redirects file at %q: %w", redirectsPath, err)
i.webError(w, r, err, http.StatusInternalServerError)
return path.ImmutablePath{}, false, true
return nil, false, true

Check warning on line 69 in gateway/handler_unixfs__redirects.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs__redirects.go#L69

Added line #L69 was not covered by tests
}

if foundRedirect {
redirected, newPath, err := i.handleRedirectsFileRules(w, r, immutableContentPath, contentPath, redirectRules, logger)
if err != nil {
err = fmt.Errorf("trouble processing _redirects file at %q: %w", redirectsPath, err)
i.webError(w, r, err, http.StatusInternalServerError)
return path.ImmutablePath{}, false, true
return nil, false, true

Check warning on line 77 in gateway/handler_unixfs__redirects.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs__redirects.go#L77

Added line #L77 was not covered by tests
}

if redirected {
return path.ImmutablePath{}, false, true
return nil, false, true

Check warning on line 81 in gateway/handler_unixfs__redirects.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs__redirects.go#L81

Added line #L81 was not covered by tests
}

// 200 is treated as a rewrite, so update the path and continue
Expand All @@ -88,13 +88,13 @@ func (i *handler) serveRedirectsIfPresent(w http.ResponseWriter, r *http.Request
if err != nil {
err = fmt.Errorf("could not use _redirects file to %q: %w", p, err)
i.webError(w, r, err, http.StatusInternalServerError)
return path.ImmutablePath{}, false, true
return nil, false, true
}

Check warning on line 92 in gateway/handler_unixfs__redirects.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs__redirects.go#L91-L92

Added lines #L91 - L92 were not covered by tests
imPath, err := path.NewImmutablePath(p)
if err != nil {
err = fmt.Errorf("could not use _redirects file to %q: %w", p, err)
i.webError(w, r, err, http.StatusInternalServerError)
return path.ImmutablePath{}, false, true
return nil, false, true

Check warning on line 97 in gateway/handler_unixfs__redirects.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs__redirects.go#L95-L97

Added lines #L95 - L97 were not covered by tests
}
return imPath, true, true
}
Expand Down
2 changes: 1 addition & 1 deletion gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// serveDirectory returns the best representation of UnixFS directory
//
// It will return index.html if present, or generate directory listing otherwise.
func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath path.ResolvedPath, contentPath path.Path, isHeadRequest bool, directoryMetadata *directoryMetadata, ranges []ByteRange, begin time.Time, logger *zap.SugaredLogger) bool {
func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath path.ImmutablePath, contentPath path.Path, isHeadRequest bool, directoryMetadata *directoryMetadata, ranges []ByteRange, begin time.Time, logger *zap.SugaredLogger) bool {
ctx, span := spanTrace(ctx, "Handler.ServeDirectory", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
defer span.End()

Expand Down
2 changes: 1 addition & 1 deletion gateway/handler_unixfs_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// serveFile returns data behind a file along with HTTP headers based on
// the file itself, its CID and the contentPath used for accessing it.
func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath path.ResolvedPath, contentPath path.Path, file files.File, fileContentType string, begin time.Time) bool {
func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath path.ImmutablePath, contentPath path.Path, file files.File, fileContentType string, begin time.Time) bool {
_, span := spanTrace(ctx, "Handler.ServeFile", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
defer span.End()

Expand Down
2 changes: 1 addition & 1 deletion gateway/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (mb *mockBackend) ResolvePath(ctx context.Context, immutablePath path.Immut
return mb.gw.ResolvePath(ctx, immutablePath)
}

func (mb *mockBackend) resolvePathNoRootsReturned(ctx context.Context, ip path.Path) (path.ResolvedPath, error) {
func (mb *mockBackend) resolvePathNoRootsReturned(ctx context.Context, ip path.Path) (path.ImmutablePath, error) {
var imPath path.ImmutablePath
var err error
if ip.Namespace().Mutable() {
Expand Down
Loading

0 comments on commit 0a71844

Please sign in to comment.