-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
coreapi: functional options for DagAPI
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
- Loading branch information
Showing
5 changed files
with
151 additions
and
26 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
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,83 @@ | ||
package options | ||
|
||
import ( | ||
"math" | ||
|
||
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid" | ||
) | ||
|
||
type DagPutSettings struct { | ||
InputEnc string | ||
Codec uint64 | ||
MhType uint64 | ||
MhLength int | ||
} | ||
|
||
type DagTreeSettings struct { | ||
Depth int | ||
} | ||
|
||
type DagPutOption func(*DagPutSettings) error | ||
type DagTreeOption func(*DagTreeSettings) error | ||
|
||
func DagPutOptions(opts ...DagPutOption) (*DagPutSettings, error) { | ||
options := &DagPutSettings{ | ||
InputEnc: "json", | ||
Codec: cid.DagCBOR, | ||
MhType: math.MaxUint64, | ||
MhLength: -1, | ||
} | ||
|
||
for _, opt := range opts { | ||
err := opt(options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return options, nil | ||
} | ||
|
||
func DagTreeOptions(opts ...DagTreeOption) (*DagTreeSettings, error) { | ||
options := &DagTreeSettings{ | ||
Depth: -1, | ||
} | ||
|
||
for _, opt := range opts { | ||
err := opt(options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return options, nil | ||
} | ||
|
||
type DagOptions struct{} | ||
|
||
func (api *DagOptions) WithInputEnc(enc string) DagPutOption { | ||
return func(settings *DagPutSettings) error { | ||
settings.InputEnc = enc | ||
return nil | ||
} | ||
} | ||
|
||
func (api *DagOptions) WithCodec(codec uint64) DagPutOption { | ||
return func(settings *DagPutSettings) error { | ||
settings.Codec = codec | ||
return nil | ||
} | ||
} | ||
|
||
func (api *DagOptions) WithHash(mhType uint64, mhLen int) DagPutOption { | ||
return func(settings *DagPutSettings) error { | ||
settings.MhType = mhType | ||
settings.MhLength = mhLen | ||
return nil | ||
} | ||
} | ||
|
||
func (api *DagOptions) WithDepth(depth int) DagTreeOption { | ||
return func(settings *DagTreeSettings) error { | ||
settings.Depth = depth | ||
return nil | ||
} | ||
} |