Skip to content

Commit

Permalink
refactor!: make namespace a string
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 5, 2023
1 parent 41ccdb3 commit 0d4a993
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 49 deletions.
4 changes: 2 additions & 2 deletions coreiface/tests/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (tp *TestSuite) TestMutablePath(t *testing.T) {

blk, err := api.Block().Put(ctx, strings.NewReader(`foo`))
require.NoError(t, err)
require.False(t, blk.Path().Namespace().Mutable())
require.False(t, blk.Path().Mutable())
require.NotNil(t, api.Key())

keys, err := api.Key().List(ctx)
require.NoError(t, err)
require.True(t, keys[0].Path().Namespace().Mutable())
require.True(t, keys[0].Path().Mutable())
}

func (tp *TestSuite) TestPathRemainder(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (i *handler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) {

defer func() {
if success {
i.getMetric.WithLabelValues(contentPath.Namespace().String()).Observe(time.Since(begin).Seconds())
i.getMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
}
}()

Expand All @@ -255,7 +255,7 @@ func (i *handler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) {
return
}
trace.SpanFromContext(r.Context()).SetAttributes(attribute.String("ResponseFormat", responseFormat))
i.requestTypeMetric.WithLabelValues(contentPath.Namespace().String(), responseFormat).Inc()
i.requestTypeMetric.WithLabelValues(contentPath.Namespace(), responseFormat).Inc()

addCustomHeaders(w, i.config.Headers) // ok, _now_ write user's headers.
w.Header().Set("X-Ipfs-Path", contentPath.String())
Expand Down Expand Up @@ -284,7 +284,7 @@ func (i *handler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) {
return
}

if contentPath.Namespace().Mutable() {
if contentPath.Mutable() {
rq.immutablePath, err = i.backend.ResolveMutable(r.Context(), contentPath)
if err != nil {
err = fmt.Errorf("failed to resolve %s: %w", debugStr(contentPath.String()), err)
Expand Down Expand Up @@ -423,7 +423,7 @@ func addCacheControlHeaders(w http.ResponseWriter, r *http.Request, contentPath
}

// Set Cache-Control and Last-Modified based on contentPath properties
if contentPath.Namespace().Mutable() {
if contentPath.Mutable() {
// mutable namespaces such as /ipns/ can't be cached forever

// For now we set Last-Modified to Now() to leverage caching heuristics built into modern browsers:
Expand Down
2 changes: 1 addition & 1 deletion gateway/handler_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (i *handler) serveRawBlock(ctx context.Context, w http.ResponseWriter, r *h

if dataSent {
// Update metrics
i.rawBlockGetMetric.WithLabelValues(rq.contentPath.Namespace().String()).Observe(time.Since(rq.begin).Seconds())
i.rawBlockGetMetric.WithLabelValues(rq.contentPath.Namespace()).Observe(time.Since(rq.begin).Seconds())
}

return dataSent
Expand Down
4 changes: 2 additions & 2 deletions gateway/handler_car.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (i *handler) serveCAR(ctx context.Context, w http.ResponseWriter, r *http.R
streamErr := multierr.Combine(carErr, copyErr)
if streamErr != nil {
// Update fail metric
i.carStreamFailMetric.WithLabelValues(rq.contentPath.Namespace().String()).Observe(time.Since(rq.begin).Seconds())
i.carStreamFailMetric.WithLabelValues(rq.contentPath.Namespace()).Observe(time.Since(rq.begin).Seconds())

// We return error as a trailer, however it is not something browsers can access
// (https://github.com/mdn/browser-compat-data/issues/14703)
Expand All @@ -101,7 +101,7 @@ func (i *handler) serveCAR(ctx context.Context, w http.ResponseWriter, r *http.R
}

// Update metrics
i.carStreamGetMetric.WithLabelValues(rq.contentPath.Namespace().String()).Observe(time.Since(rq.begin).Seconds())
i.carStreamGetMetric.WithLabelValues(rq.contentPath.Namespace()).Observe(time.Since(rq.begin).Seconds())
return true
}

Expand Down
4 changes: 2 additions & 2 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (i *handler) serveCodecRaw(ctx context.Context, w http.ResponseWriter, r *h

if dataSent {
// Update metrics
i.jsoncborDocumentGetMetric.WithLabelValues(contentPath.Namespace().String()).Observe(time.Since(begin).Seconds())
i.jsoncborDocumentGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
}

return dataSent
Expand Down Expand Up @@ -292,7 +292,7 @@ func (i *handler) serveCodecConverted(ctx context.Context, w http.ResponseWriter
_, err = w.Write(buf.Bytes())
if err == nil {
// Update metrics
i.jsoncborDocumentGetMetric.WithLabelValues(contentPath.Namespace().String()).Observe(time.Since(begin).Seconds())
i.jsoncborDocumentGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
return true
}

Expand Down
2 changes: 1 addition & 1 deletion gateway/handler_ipns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (i *handler) serveIpnsRecord(ctx context.Context, w http.ResponseWriter, r
_, err = w.Write(rawRecord)
if err == nil {
// Update metrics
i.ipnsRecordGetMetric.WithLabelValues(rq.contentPath.Namespace().String()).Observe(time.Since(rq.begin).Seconds())
i.ipnsRecordGetMetric.WithLabelValues(rq.contentPath.Namespace()).Observe(time.Since(rq.begin).Seconds())
return true
}

Expand Down
4 changes: 2 additions & 2 deletions gateway/handler_tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (i *handler) serveTAR(ctx context.Context, w http.ResponseWriter, r *http.R
// The TAR has a top-level directory (or file) named by the CID.
if err := tarw.WriteFile(file, rootCid.String()); err != nil {
// Update fail metric
i.tarStreamFailMetric.WithLabelValues(rq.contentPath.Namespace().String()).Observe(time.Since(rq.begin).Seconds())
i.tarStreamFailMetric.WithLabelValues(rq.contentPath.Namespace()).Observe(time.Since(rq.begin).Seconds())

w.Header().Set("X-Stream-Error", err.Error())
// Trailer headers do not work in web browsers
Expand All @@ -77,6 +77,6 @@ func (i *handler) serveTAR(ctx context.Context, w http.ResponseWriter, r *http.R
}

// Update metrics
i.tarStreamGetMetric.WithLabelValues(rq.contentPath.Namespace().String()).Observe(time.Since(rq.begin).Seconds())
i.tarStreamGetMetric.WithLabelValues(rq.contentPath.Namespace()).Observe(time.Since(rq.begin).Seconds())
return true
}
2 changes: 1 addition & 1 deletion gateway/handler_unixfs__redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (i *handler) getRedirectRules(r *http.Request, redirectsPath path.Immutable
// Returns the root CID Path for the given path
func getRootPath(p path.Path) (path.Path, error) {
parts := strings.Split(p.String(), "/")
return path.NewPath(gopath.Join("/", p.Namespace().String(), parts[2]))
return path.NewPath(gopath.Join("/", p.Namespace(), parts[2]))
}

func (i *handler) serve4xx(w http.ResponseWriter, r *http.Request, content4xxPathImPath path.ImmutablePath, content4xxPath path.Path, status int, logger *zap.SugaredLogger) error {
Expand Down
4 changes: 2 additions & 2 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
// write to request
success := i.serveFile(ctx, w, r, resolvedPath, idxPath, idxFileSize, idxFileBytes, false, returnRangeStartsAtZero, "text/html", begin)
if success {
i.unixfsDirIndexGetMetric.WithLabelValues(contentPath.Namespace().String()).Observe(time.Since(begin).Seconds())
i.unixfsDirIndexGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
}
return success
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
}

// Update metrics
i.unixfsGenDirListingGetMetric.WithLabelValues(contentPath.Namespace().String()).Observe(time.Since(begin).Seconds())
i.unixfsGenDirListingGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
return true
}

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 @@ -89,7 +89,7 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.
// Was response successful?
if dataSent {
// Update metrics
i.unixfsFileGetMetric.WithLabelValues(contentPath.Namespace().String()).Observe(time.Since(begin).Seconds())
i.unixfsFileGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
}

return dataSent
Expand Down
2 changes: 1 addition & 1 deletion gateway/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (mb *mockBackend) ResolvePath(ctx context.Context, immutablePath path.Immut
func (mb *mockBackend) resolvePathNoRootsReturned(ctx context.Context, ip path.Path) (path.ImmutablePath, error) {
var imPath path.ImmutablePath
var err error
if ip.Namespace().Mutable() {
if ip.Mutable() {
imPath, err = mb.ResolveMutable(ctx, ip)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion ipns/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (n Name) Equal(other Name) bool {

// AsPath returns the IPNS Name as a [path.Path] prefixed by [path.IPNSNamespace].
func (n Name) AsPath() path.Path {
p, err := path.NewPathFromSegments(path.IPNSNamespace.String(), n.String())
p, err := path.NewPathFromSegments(path.IPNSNamespace, n.String())
if err != nil {
panic(fmt.Errorf("path.NewPathFromSegments was called with invalid parameters: %w", err))
}
Expand Down
49 changes: 22 additions & 27 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,10 @@ import (
"github.com/ipfs/go-cid"
)

type Namespace string

func (namespace Namespace) String() string {
return string(namespace)
}

// Mutable returns false if the data under this namespace is guaranteed to not change.
func (namespace Namespace) Mutable() bool {
return namespace != IPFSNamespace && namespace != IPLDNamespace
}

const (
IPFSNamespace Namespace = "ipfs"
IPNSNamespace Namespace = "ipns"
IPLDNamespace Namespace = "ipld"
IPFSNamespace = "ipfs"
IPNSNamespace = "ipns"
IPLDNamespace = "ipld"
)

// Path is a generic, valid, and well-formed path. A valid path is shaped as follows:
Expand All @@ -43,7 +32,10 @@ type Path interface {

// Namespace returns the first component of the path. For example, the namespace
// of "/ipfs/bafy" is "ipfs".
Namespace() Namespace
Namespace() string

// Mutable returns false if the data under this path's namespace is guaranteed to not change.
Mutable() bool

// Segments returns the different elements of a path delimited by a forward
// slash ("/"). The returned array must not contain any empty segments, and
Expand All @@ -61,17 +53,21 @@ var _ Path = path{}

type path struct {
str string
namespace Namespace
namespace string
}

func (p path) String() string {
return p.str
}

func (p path) Namespace() Namespace {
func (p path) Namespace() string {
return p.namespace
}

func (p path) Mutable() bool {
return p.Namespace() != IPFSNamespace && p.Namespace() != IPLDNamespace
}

func (p path) Segments() []string {
return StringToSegments(p.str)
}
Expand All @@ -93,7 +89,7 @@ type immutablePath struct {
}

func NewImmutablePath(p Path) (ImmutablePath, error) {
if p.Namespace().Mutable() {
if p.Mutable() {
return nil, &ErrInvalidPath{err: ErrExpectedImmutable, path: p.String()}
}

Expand All @@ -110,10 +106,14 @@ func (ip immutablePath) String() string {
return ip.path.String()
}

func (ip immutablePath) Namespace() Namespace {
func (ip immutablePath) Namespace() string {
return ip.path.Namespace()
}

func (ip immutablePath) Mutable() bool {
return false
}

func (ip immutablePath) Segments() []string {
return ip.path.Segments()
}
Expand Down Expand Up @@ -155,28 +155,23 @@ func NewPath(str string) (Path, error) {
}

switch segments[0] {
case "ipfs", "ipld":
case IPFSNamespace, IPLDNamespace:
cid, err := cid.Decode(segments[1])
if err != nil {
return nil, &ErrInvalidPath{err: err, path: str}
}

ns := IPFSNamespace
if segments[0] == "ipld" {
ns = IPLDNamespace
}

return immutablePath{
path: path{
str: cleaned,
namespace: ns,
namespace: segments[0],
},
rootCid: cid,
}, nil
case "ipns":
return path{
str: cleaned,
namespace: IPNSNamespace,
namespace: segments[0],
}, nil
default:
return nil, &ErrInvalidPath{err: fmt.Errorf("%w: %q", ErrUnknownNamespace, segments[0]), path: str}
Expand Down
4 changes: 2 additions & 2 deletions path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestNewPath(t *testing.T) {
testCases := []struct {
src string
canonical string
namespace Namespace
namespace string
mutable bool
}{
// IPFS CIDv0
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestNewPath(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, testCase.canonical, p.String())
assert.Equal(t, testCase.namespace, p.Namespace())
assert.Equal(t, testCase.mutable, p.Namespace().Mutable())
assert.Equal(t, testCase.mutable, p.Mutable())
}
})

Expand Down

0 comments on commit 0d4a993

Please sign in to comment.