Skip to content

Commit

Permalink
refactor: apply @Jorropo suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Aug 11, 2023
1 parent 4407684 commit 0e88cb9
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,39 @@ 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) {
if p.Mutable() {
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 {
Expand All @@ -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, "/")
Expand All @@ -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
}

Expand Down

0 comments on commit 0e88cb9

Please sign in to comment.