-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
coreapi: Basic object API implementation #4492
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package options | ||
|
||
type ObjectNewSettings struct { | ||
Type string | ||
} | ||
|
||
type ObjectPutSettings struct { | ||
InputEnc string | ||
DataType string | ||
} | ||
|
||
type ObjectAddLinkSettings struct { | ||
Create bool | ||
} | ||
|
||
type ObjectNewOption func(*ObjectNewSettings) error | ||
type ObjectPutOption func(*ObjectPutSettings) error | ||
type ObjectAddLinkOption func(*ObjectAddLinkSettings) error | ||
|
||
func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) { | ||
options := &ObjectNewSettings{ | ||
Type: "empty", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default for this should be "unixfs-dir" as in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, nevermind, I understand :) |
||
} | ||
|
||
for _, opt := range opts { | ||
err := opt(options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return options, nil | ||
} | ||
|
||
func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) { | ||
options := &ObjectPutSettings{ | ||
InputEnc: "json", | ||
DataType: "text", | ||
} | ||
|
||
for _, opt := range opts { | ||
err := opt(options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return options, nil | ||
} | ||
|
||
func ObjectAddLinkOptions(opts ...ObjectAddLinkOption) (*ObjectAddLinkSettings, error) { | ||
options := &ObjectAddLinkSettings{ | ||
Create: false, | ||
} | ||
|
||
for _, opt := range opts { | ||
err := opt(options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return options, nil | ||
} | ||
|
||
type ObjectOptions struct{} | ||
|
||
func (api *ObjectOptions) WithType(t string) ObjectNewOption { | ||
return func(settings *ObjectNewSettings) error { | ||
settings.Type = t | ||
return nil | ||
} | ||
} | ||
|
||
func (api *ObjectOptions) WithInputEnc(e string) ObjectPutOption { | ||
return func(settings *ObjectPutSettings) error { | ||
settings.InputEnc = e | ||
return nil | ||
} | ||
} | ||
|
||
func (api *ObjectOptions) WithDataType(t string) ObjectPutOption { | ||
return func(settings *ObjectPutSettings) error { | ||
settings.DataType = t | ||
return nil | ||
} | ||
} | ||
|
||
func (api *ObjectOptions) WithCreate(create bool) ObjectAddLinkOption { | ||
return func(settings *ObjectAddLinkSettings) error { | ||
settings.Create = create | ||
return nil | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I wrap this into an interface for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine - given that the object API is quasi-frozen (it's the old merkledag/dag-pb structure), it's very unlikely this struct will ever have any functions attached to it.