Skip to content

Commit

Permalink
refactor!: namesys package
Browse files Browse the repository at this point in the history
- move namesys options out of coreiface/options/namesys
  into namesys directly.
- rename options to make them consistent and make sense.
- move code around to be in self-explanatory file names.
  • Loading branch information
hacdias committed Sep 5, 2023
1 parent 574df96 commit 429a1eb
Show file tree
Hide file tree
Showing 20 changed files with 783 additions and 918 deletions.
6 changes: 3 additions & 3 deletions coreiface/options/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package options
import (
"time"

ropts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/namesys"
)

const (
Expand All @@ -21,7 +21,7 @@ type NamePublishSettings struct {
type NameResolveSettings struct {
Cache bool

ResolveOpts []ropts.ResolveOpt
ResolveOpts []namesys.ResolveOption
}

type (
Expand Down Expand Up @@ -123,7 +123,7 @@ func (nameOpts) Cache(cache bool) NameResolveOption {
}
}

func (nameOpts) ResolveOption(opt ropts.ResolveOpt) NameResolveOption {
func (nameOpts) ResolveOption(opt namesys.ResolveOption) NameResolveOption {
return func(settings *NameResolveSettings) error {
settings.ResolveOpts = append(settings.ResolveOpts, opt)
return nil
Expand Down
131 changes: 0 additions & 131 deletions coreiface/options/namesys/opts.go

This file was deleted.

3 changes: 1 addition & 2 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
ifacepath "github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/fetcher"
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
Expand Down Expand Up @@ -608,7 +607,7 @@ func (bb *BlocksBackend) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte,

func (bb *BlocksBackend) GetDNSLinkRecord(ctx context.Context, hostname string) (ifacepath.Path, error) {
if bb.namesys != nil {
p, err := bb.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
p, _, err := bb.namesys.Resolve(ctx, "/ipns/"+hostname, namesys.ResolveWithDepth(1))
if err == namesys.ErrResolveRecursion {
err = nil
}
Expand Down
26 changes: 13 additions & 13 deletions gateway/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"regexp"
"strings"
"testing"
"time"

"github.com/ipfs/boxo/blockservice"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
ipath "github.com/ipfs/boxo/coreiface/path"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/files"
Expand Down Expand Up @@ -54,41 +54,41 @@ func mustDo(t *testing.T, req *http.Request) *http.Response {

type mockNamesys map[string]path.Path

func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.ResolveOpt) (value path.Path, err error) {
cfg := nsopts.DefaultResolveOpts()
func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...namesys.ResolveOption) (value path.Path, ttl time.Duration, err error) {
cfg := namesys.DefaultResolveOptions()
for _, o := range opts {
o(&cfg)
}
depth := cfg.Depth
if depth == nsopts.UnlimitedDepth {
if depth == namesys.UnlimitedDepth {
// max uint
depth = ^uint(0)
}
for strings.HasPrefix(name, "/ipns/") {
if depth == 0 {
return value, namesys.ErrResolveRecursion
return value, 0, namesys.ErrResolveRecursion
}
depth--

var ok bool
value, ok = m[name]
if !ok {
return "", namesys.ErrResolveFailed
return "", 0, namesys.ErrResolveFailed
}
name = value.String()
}
return value, nil
return value, 0, nil
}

func (m mockNamesys) ResolveAsync(ctx context.Context, name string, opts ...nsopts.ResolveOpt) <-chan namesys.Result {
out := make(chan namesys.Result, 1)
v, err := m.Resolve(ctx, name, opts...)
out <- namesys.Result{Path: v, Err: err}
func (m mockNamesys) ResolveAsync(ctx context.Context, name string, opts ...namesys.ResolveOption) <-chan namesys.ResolveResult {
out := make(chan namesys.ResolveResult, 1)
v, ttl, err := m.Resolve(ctx, name, opts...)
out <- namesys.ResolveResult{Path: v, TTL: ttl, Err: err}
close(out)
return out
}

func (m mockNamesys) Publish(ctx context.Context, name crypto.PrivKey, value path.Path, opts ...nsopts.PublishOption) error {
func (m mockNamesys) Publish(ctx context.Context, name crypto.PrivKey, value path.Path, opts ...namesys.PublishOption) error {
return errors.New("not implemented for mockNamesys")
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func (mb *mockBackend) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, er

func (mb *mockBackend) GetDNSLinkRecord(ctx context.Context, hostname string) (ipath.Path, error) {
if mb.namesys != nil {
p, err := mb.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
p, _, err := mb.namesys.Resolve(ctx, "/ipns/"+hostname, namesys.ResolveWithDepth(1))
if err == namesys.ErrResolveRecursion {
err = nil
}
Expand Down
Loading

0 comments on commit 429a1eb

Please sign in to comment.