Skip to content

Commit e535da4

Browse files
committed
many things: head, any, encodingemitter, copy, flushfwd
1 parent fc06f9b commit e535da4

13 files changed

+518
-97
lines changed

any_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cmds
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"strings"
7+
"testing"
8+
)
9+
10+
type Foo struct {
11+
Bar int
12+
}
13+
14+
type Bar struct {
15+
Foo string
16+
}
17+
18+
type ValueError struct {
19+
Error error
20+
Value interface{}
21+
}
22+
23+
type anyTestCase struct {
24+
Types []interface{}
25+
JSON string
26+
Decoded []ValueError
27+
}
28+
29+
func TestMaybe(t *testing.T) {
30+
testcases := []anyTestCase{
31+
anyTestCase{
32+
Types: []interface{}{Foo{}, &Bar{}},
33+
JSON: `{"Bar":2}{"Foo":"abc"}`,
34+
Decoded: []ValueError{
35+
ValueError{Error: nil, Value: Foo{2}},
36+
ValueError{Error: nil, Value: Bar{"abc"}},
37+
},
38+
},
39+
}
40+
41+
for _, tc := range testcases {
42+
a := &Any{}
43+
44+
for _, t := range tc.Types {
45+
a.Add(t)
46+
}
47+
48+
r := strings.NewReader(tc.JSON)
49+
d := json.NewDecoder(r)
50+
51+
var err error
52+
53+
for _, dec := range tc.Decoded {
54+
err = d.Decode(a)
55+
if err != dec.Error {
56+
t.Fatalf("error is %v, expected %v", err, dec.Error)
57+
}
58+
59+
if a.Interface() != dec.Value {
60+
t.Fatalf("value is %#v, expected %#v", a.Interface(), dec.Value)
61+
}
62+
}
63+
64+
err = d.Decode(a)
65+
if err != io.EOF {
66+
t.Fatal("data left in decoder:", a.Interface())
67+
}
68+
}
69+
}

chan.go

+28-3
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,34 @@ type chanResponse struct {
3434
}
3535

3636
func (r *chanResponse) Request() *Request {
37+
if r == nil {
38+
return nil
39+
}
40+
3741
return r.req
3842
}
3943

4044
func (r *chanResponse) Error() *cmdsutil.Error {
45+
if r == nil {
46+
return nil
47+
}
48+
4149
return r.err
4250
}
4351

4452
func (r *chanResponse) Length() uint64 {
53+
if r == nil {
54+
return 0
55+
}
56+
4557
return r.length
4658
}
4759

4860
func (r *chanResponse) Next() (interface{}, error) {
61+
if r == nil {
62+
return nil, io.EOF
63+
}
64+
4965
v, ok := <-r.ch
5066
if ok {
5167
return v, nil
@@ -65,9 +81,11 @@ type chanResponseEmitter struct {
6581

6682
func (re *chanResponseEmitter) SetError(err interface{}, t cmdsutil.ErrorType) {
6783
// don't change value after emitting
68-
if re.emitted {
69-
return
70-
}
84+
/*
85+
if re.emitted {
86+
return
87+
}
88+
*/
7189

7290
*re.err = &cmdsutil.Error{Message: fmt.Sprint(err), Code: t}
7391
}
@@ -81,6 +99,13 @@ func (re *chanResponseEmitter) SetLength(l uint64) {
8199
*re.length = l
82100
}
83101

102+
func (re *chanResponseEmitter) Head() Head {
103+
return Head{
104+
Len: *re.length,
105+
Err: *re.err,
106+
}
107+
}
108+
84109
func (re *chanResponseEmitter) Close() error {
85110
close(re.ch)
86111
re.ch = nil

cmdsutil/option_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func TestOptionValueExtractBoolNotFound(t *testing.T) {
99
t.Log("ensure that no error is returned when value is not found")
10-
optval := &OptionValue{found: false}
10+
optval := &OptionValue{ValueFound: false}
1111
_, _, err := optval.Bool()
1212
if err != nil {
1313
t.Fatal("Found was false. Err should have been nil")
@@ -18,13 +18,13 @@ func TestOptionValueExtractWrongType(t *testing.T) {
1818

1919
t.Log("ensure that error is returned when value if of wrong type")
2020

21-
optval := &OptionValue{value: "wrong type: a string", found: true}
21+
optval := &OptionValue{Value: "wrong type: a string", ValueFound: true}
2222
_, _, err := optval.Bool()
2323
if err == nil {
2424
t.Fatal("No error returned. Failure.")
2525
}
2626

27-
optval = &OptionValue{value: "wrong type: a string", found: true}
27+
optval = &OptionValue{Value: "wrong type: a string", ValueFound: true}
2828
_, _, err = optval.Int()
2929
if err == nil {
3030
t.Fatal("No error returned. Failure.")

command.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
2222
)
2323

24-
var log = logging.Logger("command")
24+
var log = logging.Logger("cmds")
2525

2626
// Function is the type of function that Commands use.
2727
// It reads from the Request, and writes results to the ResponseEmitter.
@@ -39,7 +39,7 @@ type Command struct {
3939
// after writing when using multipart requests. The request body will not be
4040
// available for reading after the HTTP connection has been written to.
4141
Run Function
42-
PostRun interface{}
42+
PostRun map[EncodingType]func(Request, Response) Response
4343
Encoders map[EncodingType]func(io.Writer) Encoder
4444
Helptext cmdsutil.HelpText
4545

@@ -85,6 +85,22 @@ func (c *Command) Call(req Request, re ResponseEmitter) error {
8585
return err
8686
}
8787

88+
// wat
89+
if re_, ok := re.(EncodingEmitter); ok {
90+
if encType, found, err := req.Option(cmdsutil.EncShort).String(); found && err == nil {
91+
if enc, ok := cmd.Encoders[EncodingType(encType)]; ok {
92+
re_.SetEncoder(enc)
93+
log.Debug("updated encoder to", enc)
94+
} else {
95+
log.Debug("no encoder found for encoding", encType)
96+
}
97+
} else {
98+
log.Debug("no encoding found in request. err:", err)
99+
}
100+
} else {
101+
log.Debugf("responseemitter is not an EncodingEmitter, but a %T", re)
102+
}
103+
88104
cmd.Run(req, re)
89105

90106
return nil
@@ -96,7 +112,10 @@ func (c *Command) fakeOldCall(cmd *oldcmds.Command, req Request, re ResponseEmit
96112
}
97113

98114
// fake old response
99-
res := FakeOldResponse(re)
115+
res := &fakeResponse{
116+
re: re,
117+
req: &requestWrapper{req},
118+
}
100119

101120
optDefs := make(map[string]cmdsutil.Option)
102121

@@ -300,7 +319,12 @@ func (c *Command) Subcommand(id string) *Command {
300319
return cmd
301320
}
302321

303-
return NewCommand(c.OldSubcommands[id])
322+
oldcmd := c.OldSubcommands[id]
323+
if oldcmd != nil {
324+
return NewCommand(oldcmd)
325+
}
326+
327+
return nil
304328
}
305329

306330
type CommandVisitor func(*Command)

0 commit comments

Comments
 (0)