Don't use valid() pattern

This commit was moved from ipfs/go-ipfs-http-client@4d07c48f98
This commit is contained in:
Łukasz Magiera 2019-02-14 19:05:17 +01:00
parent e1b14d78c7
commit 42273cab06
4 changed files with 32 additions and 42 deletions

View File

@ -18,24 +18,16 @@ type BlockAPI HttpApi
type blockStat struct {
Key string
BSize int `json:"Size"`
cid cid.Cid
}
func (s *blockStat) Size() int {
return s.BSize
}
func (s *blockStat) valid() (iface.ResolvedPath, error) {
c, err := cid.Parse(s.Key)
if err != nil {
return nil, err
}
return iface.IpldPath(c), nil
}
func (s *blockStat) Path() iface.ResolvedPath {
p, _ := s.valid()
return p
return iface.IpldPath(s.cid)
}
func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.BlockPutOption) (iface.BlockStat, error) {
@ -60,7 +52,8 @@ func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.BlockP
if err := req.Exec(ctx, &out); err != nil {
return nil, err
}
if _, err := out.valid(); err != nil {
out.cid, err = cid.Parse(out.Key)
if err != nil {
return nil, err
}
@ -118,7 +111,8 @@ func (api *BlockAPI) Stat(ctx context.Context, p iface.Path) (iface.BlockStat, e
if err != nil {
return nil, err
}
if _, err := out.valid(); err != nil {
out.cid, err = cid.Parse(out.Key)
if err != nil {
return nil, err
}

View File

@ -14,6 +14,8 @@ type KeyAPI HttpApi
type keyOutput struct {
JName string `json:"Name"`
Id string
pid peer.ID
}
func (k *keyOutput) Name() string {
@ -26,13 +28,7 @@ func (k *keyOutput) Path() iface.Path {
}
func (k *keyOutput) ID() peer.ID {
p, _ := peer.IDB58Decode(k.Id)
return p
}
func (k *keyOutput) valid() error {
_, err := peer.IDB58Decode(k.Id)
return err
return k.pid
}
func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (iface.Key, error) {
@ -49,7 +45,8 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
if err != nil {
return nil, err
}
return &out, out.valid()
out.pid, err = peer.IDB58Decode(out.Id)
return &out, err
}
func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (iface.Key, bool, error) {
@ -72,7 +69,8 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
}
id := &keyOutput{JName: out.Now, Id: out.Id}
return id, out.Overwrite, id.valid()
id.pid, err = peer.IDB58Decode(id.Id)
return id, out.Overwrite, err
}
func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
@ -83,7 +81,9 @@ func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
res := make([]iface.Key, len(out.Keys))
for i, k := range out.Keys {
if err := k.valid(); err != nil {
var err error
k.pid, err = peer.IDB58Decode(k.Id)
if err != nil {
return nil, err
}
res[i] = k
@ -98,8 +98,10 @@ func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
return nil, err
}
var err error
out := keyOutput{JName: "self", Id: id.ID}
return &out, out.valid()
out.pid, err = peer.IDB58Decode(out.Id)
return &out, err
}
func (api *KeyAPI) Remove(ctx context.Context, name string) (iface.Key, error) {
@ -111,7 +113,9 @@ func (api *KeyAPI) Remove(ctx context.Context, name string) (iface.Key, error) {
return nil, errors.New("got unexpected number of keys back")
}
return &out.Keys[0], out.Keys[0].valid()
var err error
out.Keys[0].pid, err = peer.IDB58Decode(out.Keys[0].Id)
return &out.Keys[0], err
}
func (api *KeyAPI) core() *HttpApi {

View File

@ -16,10 +16,8 @@ type NameAPI HttpApi
type ipnsEntry struct {
JName string `json:"Name"`
JValue string `json:"Value"`
}
func (e *ipnsEntry) valid() (iface.Path, error) {
return iface.ParsePath(e.JValue)
path iface.Path
}
func (e *ipnsEntry) Name() string {
@ -27,8 +25,7 @@ func (e *ipnsEntry) Name() string {
}
func (e *ipnsEntry) Value() iface.Path {
p, _ := e.valid()
return p
return e.path
}
func (api *NameAPI) Publish(ctx context.Context, p iface.Path, opts ...caopts.NamePublishOption) (iface.IpnsEntry, error) {
@ -51,11 +48,8 @@ func (api *NameAPI) Publish(ctx context.Context, p iface.Path, opts ...caopts.Na
if err := req.Exec(ctx, &out); err != nil {
return nil, err
}
if _, err := out.valid(); err != nil {
return nil, err
}
return &out, nil
out.path, err = iface.ParsePath(out.JValue)
return &out, err
}
func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.NameResolveOption) (<-chan iface.IpnsResult, error) {

View File

@ -66,16 +66,12 @@ type pubsubMessage struct {
JData []byte `json:"data,omitempty"`
JSeqno []byte `json:"seqno,omitempty"`
JTopicIDs []string `json:"topicIDs,omitempty"`
}
func (msg *pubsubMessage) valid() error {
_, err := peer.IDFromBytes(msg.JFrom)
return err
from peer.ID
}
func (msg *pubsubMessage) From() peer.ID {
id, _ := peer.IDFromBytes(msg.JFrom)
return id
return msg.from
}
func (msg *pubsubMessage) Data() []byte {
@ -97,7 +93,9 @@ func (s *pubsubSub) Next(ctx context.Context) (iface.PubSubMessage, error) {
if err := s.dec.Decode(&msg); err != nil {
return nil, err
}
return &msg, msg.valid()
var err error
msg.from, err = peer.IDFromBytes(msg.JFrom)
return &msg, err
}
func (api *PubsubAPI) Subscribe(ctx context.Context, topic string, opts ...caopts.PubSubSubscribeOption) (iface.PubSubSubscription, error) {