Don't use valid() pattern
This commit was moved from ipfs/go-ipfs-http-client@4d07c48f98b0af0b18c6c13f80f04c966fc43bdc
Łukasz Magiera committed
Feb 14, 2019 at 19:05 UTC
42273cab06348b19a813dee77f7cbe5ed7cc78d6
4 files changed
+32
-42
client/httpapi/block.go
+7
-13
@@ -18,24 +18,16 @@ type BlockAPI HttpApi
18
type blockStat struct {
19
Key string
20
BSize int `json:"Size"`
21
+
22
+ cid cid.Cid
23
}
24
25
func (s *blockStat) Size() int {
26
return s.BSize
27
}
28
27
-func (s *blockStat) valid() (iface.ResolvedPath, error) {
28
- c, err := cid.Parse(s.Key)
29
- if err != nil {
30
- return nil, err
31
- }
32
-
33
- return iface.IpldPath(c), nil
34
-}
35
-
29
func (s *blockStat) Path() iface.ResolvedPath {
37
- p, _ := s.valid()
38
- return p
30
+ return iface.IpldPath(s.cid)
31
}
32
33
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
52
if err := req.Exec(ctx, &out); err != nil {
53
return nil, err
54
}
63
- if _, err := out.valid(); err != nil {
55
+ out.cid, err = cid.Parse(out.Key)
56
+ if err != nil {
57
return nil, err
58
}
59
@@ -118,7 +111,8 @@ func (api *BlockAPI) Stat(ctx context.Context, p iface.Path) (iface.BlockStat, e
111
if err != nil {
112
return nil, err
113
}
121
- if _, err := out.valid(); err != nil {
114
+ out.cid, err = cid.Parse(out.Key)
115
+ if err != nil {
116
return nil, err
117
}
118
client/httpapi/key.go
+16
-12
@@ -14,6 +14,8 @@ type KeyAPI HttpApi
14
type keyOutput struct {
15
JName string `json:"Name"`
16
Id string
17
+
18
+ pid peer.ID
19
}
20
21
func (k *keyOutput) Name() string {
@@ -26,13 +28,7 @@ func (k *keyOutput) Path() iface.Path {
28
}
29
30
func (k *keyOutput) ID() peer.ID {
29
- p, _ := peer.IDB58Decode(k.Id)
30
- return p
31
-}
32
-
33
-func (k *keyOutput) valid() error {
34
- _, err := peer.IDB58Decode(k.Id)
35
- return err
31
+ return k.pid
32
}
33
34
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
45
if err != nil {
46
return nil, err
47
}
52
- return &out, out.valid()
48
+ out.pid, err = peer.IDB58Decode(out.Id)
49
+ return &out, err
50
}
51
52
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
69
}
70
71
id := &keyOutput{JName: out.Now, Id: out.Id}
75
- return id, out.Overwrite, id.valid()
72
+ id.pid, err = peer.IDB58Decode(id.Id)
73
+ return id, out.Overwrite, err
74
}
75
76
func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
@@ -83,7 +81,9 @@ func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
81
82
res := make([]iface.Key, len(out.Keys))
83
for i, k := range out.Keys {
86
- if err := k.valid(); err != nil {
84
+ var err error
85
+ k.pid, err = peer.IDB58Decode(k.Id)
86
+ if err != nil {
87
return nil, err
88
}
89
res[i] = k
@@ -98,8 +98,10 @@ func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
98
return nil, err
99
}
100
101
+ var err error
102
out := keyOutput{JName: "self", Id: id.ID}
102
- return &out, out.valid()
103
+ out.pid, err = peer.IDB58Decode(out.Id)
104
+ return &out, err
105
}
106
107
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) {
113
return nil, errors.New("got unexpected number of keys back")
114
}
115
114
- return &out.Keys[0], out.Keys[0].valid()
116
+ var err error
117
+ out.Keys[0].pid, err = peer.IDB58Decode(out.Keys[0].Id)
118
+ return &out.Keys[0], err
119
}
120
121
func (api *KeyAPI) core() *HttpApi {
client/httpapi/name.go
+4
-10
@@ -16,10 +16,8 @@ type NameAPI HttpApi
16
type ipnsEntry struct {
17
JName string `json:"Name"`
18
JValue string `json:"Value"`
19
-}
19
21
-func (e *ipnsEntry) valid() (iface.Path, error) {
22
- return iface.ParsePath(e.JValue)
20
+ path iface.Path
21
}
22
23
func (e *ipnsEntry) Name() string {
@@ -27,8 +25,7 @@ func (e *ipnsEntry) Name() string {
25
}
26
27
func (e *ipnsEntry) Value() iface.Path {
30
- p, _ := e.valid()
31
- return p
28
+ return e.path
29
}
30
31
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
48
if err := req.Exec(ctx, &out); err != nil {
49
return nil, err
50
}
54
- if _, err := out.valid(); err != nil {
55
- return nil, err
56
- }
57
-
58
- return &out, nil
51
+ out.path, err = iface.ParsePath(out.JValue)
52
+ return &out, err
53
}
54
55
func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.NameResolveOption) (<-chan iface.IpnsResult, error) {
client/httpapi/pubsub.go
+5
-7
@@ -66,16 +66,12 @@ type pubsubMessage struct {
66
JData []byte `json:"data,omitempty"`
67
JSeqno []byte `json:"seqno,omitempty"`
68
JTopicIDs []string `json:"topicIDs,omitempty"`
69
-}
69
71
-func (msg *pubsubMessage) valid() error {
72
- _, err := peer.IDFromBytes(msg.JFrom)
73
- return err
70
+ from peer.ID
71
}
72
73
func (msg *pubsubMessage) From() peer.ID {
77
- id, _ := peer.IDFromBytes(msg.JFrom)
78
- return id
74
+ return msg.from
75
}
76
77
func (msg *pubsubMessage) Data() []byte {
@@ -97,7 +93,9 @@ func (s *pubsubSub) Next(ctx context.Context) (iface.PubSubMessage, error) {
93
if err := s.dec.Decode(&msg); err != nil {
94
return nil, err
95
}
100
- return &msg, msg.valid()
96
+ var err error
97
+ msg.from, err = peer.IDFromBytes(msg.JFrom)
98
+ return &msg, err
99
}
100
101
func (api *PubsubAPI) Subscribe(ctx context.Context, topic string, opts ...caopts.PubSubSubscribeOption) (iface.PubSubSubscription, error) {