Skip to content

Commit

Permalink
Make Options private
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 1, 2021
1 parent 5b6a589 commit 3ff3c49
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
17 changes: 6 additions & 11 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import "math"

// Options holds the configured options after applying a number of
// Option funcs.
//
// This type should not be used directly by end users; it's only exposed as a
// side effect of Option.
type Options struct {
type options struct {
TraverseLinksOnlyOnce bool
MaxTraversalLinks uint64
}

// Option describes an option which affects behavior when
// interacting with the interface.
type Option func(*Options)
type Option func(*options)

// TraverseLinksOnlyOnce prevents the traversal engine from repeatedly visiting
// the same links more than once.
Expand All @@ -25,7 +22,7 @@ type Option func(*Options)
// links for different reasons during selector execution can be valid and
// necessary to perform full traversal.
func TraverseLinksOnlyOnce() Option {
return func(sco *Options) {
return func(sco *options) {
sco.TraverseLinksOnlyOnce = true
}
}
Expand All @@ -36,16 +33,14 @@ func TraverseLinksOnlyOnce() Option {
// Note that setting this option may cause an error to be returned from selector
// execution when building a SelectiveCar.
func MaxTraversalLinks(MaxTraversalLinks uint64) Option {
return func(sco *Options) {
return func(sco *options) {
sco.MaxTraversalLinks = MaxTraversalLinks
}
}

// ApplyOptions applies given opts and returns the resulting Options.
// This function should not be used directly by end users; it's only exposed as a
// side effect of Option.
func ApplyOptions(opt ...Option) Options {
opts := Options{
func applyOptions(opt ...Option) options {
opts := options{
TraverseLinksOnlyOnce: false, // default: recurse until exhausted
MaxTraversalLinks: math.MaxInt64, // default: traverse all
}
Expand Down
15 changes: 7 additions & 8 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package car_test
package car

import (
"math"
"testing"

car "github.com/ipld/go-car"
"github.com/stretchr/testify/require"
)

func TestApplyOptions_SetsExpectedDefaults(t *testing.T) {
require.Equal(t, car.Options{
require.Equal(t, options{
MaxTraversalLinks: math.MaxInt64,
TraverseLinksOnlyOnce: false,
}, car.ApplyOptions())
}, applyOptions())
}

func TestApplyOptions_AppliesOptions(t *testing.T) {
require.Equal(t,
car.Options{
options{
MaxTraversalLinks: 123,
TraverseLinksOnlyOnce: true,
},
car.ApplyOptions(
car.MaxTraversalLinks(123),
car.TraverseLinksOnlyOnce(),
applyOptions(
MaxTraversalLinks(123),
TraverseLinksOnlyOnce(),
))
}
4 changes: 2 additions & 2 deletions selectivecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type SelectiveCar struct {
ctx context.Context
dags []Dag
store ReadStore
opts Options
opts options
}

// OnCarHeaderFunc is called during traversal when the header is created
Expand All @@ -68,7 +68,7 @@ func NewSelectiveCar(ctx context.Context, store ReadStore, dags []Dag, opts ...O
ctx: ctx,
store: store,
dags: dags,
opts: ApplyOptions(opts...),
opts: applyOptions(opts...),
}
}

Expand Down

0 comments on commit 3ff3c49

Please sign in to comment.