From 0e88cb998cd466c8162b654e3dd0545c659f1bf9 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 11 Aug 2023 07:52:36 +0200 Subject: [PATCH] refactor: apply @Jorropo suggestions --- path/path.go | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/path/path.go b/path/path.go index b0ea5edb3d..b211090d7d 100644 --- a/path/path.go +++ b/path/path.go @@ -68,9 +68,11 @@ type ResolvedPath interface { Remainder() string } -// ImmutablePath is a [Path] which is guaranteed to return "false" to [Mutable]. +var _ Path = ImmutablePath{} + +// ImmutablePath is a [Path] which is guaranteed to return "false" to [Path.Mutable]. type ImmutablePath struct { - Path + path Path } func NewImmutablePath(p Path) (ImmutablePath, error) { @@ -78,7 +80,27 @@ func NewImmutablePath(p Path) (ImmutablePath, error) { return ImmutablePath{}, fmt.Errorf("path was expected to be immutable: %s", p.String()) } - return ImmutablePath{p}, nil + return ImmutablePath{path: p}, nil +} + +func (ip ImmutablePath) String() string { + return ip.path.String() +} + +func (ip ImmutablePath) Namespace() Namespace { + return ip.path.Namespace() +} + +func (ip ImmutablePath) Mutable() bool { + return false +} + +func (ip ImmutablePath) Root() cid.Cid { + return ip.path.Root() +} + +func (ip ImmutablePath) Segments() []string { + return ip.path.Segments() } type path struct { @@ -87,23 +109,23 @@ type path struct { namespace Namespace } -func (p *path) String() string { +func (p path) String() string { return p.str } -func (p *path) Namespace() Namespace { +func (p path) Namespace() Namespace { return p.namespace } -func (p *path) Mutable() bool { +func (p path) Mutable() bool { return p.namespace == IPNSNamespace } -func (p *path) Root() cid.Cid { +func (p path) Root() cid.Cid { return p.root } -func (p *path) Segments() []string { +func (p path) Segments() []string { // Trim slashes from beginning and end, such that we do not return empty segments. str := strings.TrimSuffix(p.str, "/") str = strings.TrimPrefix(str, "/") @@ -117,11 +139,11 @@ type resolvedPath struct { remainder string } -func (p *resolvedPath) Cid() cid.Cid { +func (p resolvedPath) Cid() cid.Cid { return p.cid } -func (p *resolvedPath) Remainder() string { +func (p resolvedPath) Remainder() string { return p.remainder }