-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from ipld/rvagg/selective-traversal-options
Expose selector traversal options for SelectiveCar
- Loading branch information
Showing
7 changed files
with
333 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package car | ||
|
||
import "math" | ||
|
||
// options holds the configured options after applying a number of | ||
// Option funcs. | ||
type options struct { | ||
TraverseLinksOnlyOnce bool | ||
MaxTraversalLinks uint64 | ||
} | ||
|
||
// Option describes an option which affects behavior when | ||
// interacting with the interface. | ||
type Option func(*options) | ||
|
||
// TraverseLinksOnlyOnce prevents the traversal engine from repeatedly visiting | ||
// the same links more than once. | ||
// | ||
// This can be an efficient strategy for an exhaustive selector where it's known | ||
// that repeat visits won't impact the completeness of execution. However it | ||
// should be used with caution with most other selectors as repeat visits of | ||
// links for different reasons during selector execution can be valid and | ||
// necessary to perform full traversal. | ||
func TraverseLinksOnlyOnce() Option { | ||
return func(sco *options) { | ||
sco.TraverseLinksOnlyOnce = true | ||
} | ||
} | ||
|
||
// MaxTraversalLinks changes the allowed number of links a selector traversal | ||
// can execute before failing. | ||
// | ||
// 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) { | ||
sco.MaxTraversalLinks = MaxTraversalLinks | ||
} | ||
} | ||
|
||
// applyOptions applies given opts and returns the resulting options. | ||
func applyOptions(opt ...Option) options { | ||
opts := options{ | ||
TraverseLinksOnlyOnce: false, // default: recurse until exhausted | ||
MaxTraversalLinks: math.MaxInt64, // default: traverse all | ||
} | ||
for _, o := range opt { | ||
o(&opts) | ||
} | ||
return opts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package car | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestApplyOptions_SetsExpectedDefaults(t *testing.T) { | ||
require.Equal(t, options{ | ||
MaxTraversalLinks: math.MaxInt64, | ||
TraverseLinksOnlyOnce: false, | ||
}, applyOptions()) | ||
} | ||
|
||
func TestApplyOptions_AppliesOptions(t *testing.T) { | ||
require.Equal(t, | ||
options{ | ||
MaxTraversalLinks: 123, | ||
TraverseLinksOnlyOnce: true, | ||
}, | ||
applyOptions( | ||
MaxTraversalLinks(123), | ||
TraverseLinksOnlyOnce(), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.