Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
0452a5f7
Commit
0452a5f7
authored
10 years ago
by
Matt Bell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands: Made default JSON marshaler support channel output
parent
cf1e770e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
6 deletions
+50
-6
commands/channelmarshaler.go
commands/channelmarshaler.go
+34
-0
commands/response.go
commands/response.go
+16
-6
No files found.
commands/channelmarshaler.go
0 → 100644
View file @
0452a5f7
package
commands
import
"io"
type
ChannelMarshaler
struct
{
Channel
<-
chan
interface
{}
Marshaler
func
(
interface
{})
(
io
.
Reader
,
error
)
reader
io
.
Reader
}
func
(
cr
*
ChannelMarshaler
)
Read
(
p
[]
byte
)
(
int
,
error
)
{
if
cr
.
reader
==
nil
{
val
,
more
:=
<-
cr
.
Channel
if
!
more
{
return
0
,
io
.
EOF
}
r
,
err
:=
cr
.
Marshaler
(
val
)
if
err
!=
nil
{
return
0
,
err
}
cr
.
reader
=
r
}
n
,
err
:=
cr
.
reader
.
Read
(
p
)
if
err
!=
nil
&&
err
!=
io
.
EOF
{
return
n
,
err
}
if
n
==
0
{
cr
.
reader
=
nil
}
return
n
,
nil
}
This diff is collapsed.
Click to expand it.
commands/response.go
View file @
0452a5f7
...
...
@@ -40,20 +40,30 @@ const (
// TODO: support more encoding types
)
func
marshalJson
(
value
interface
{})
(
io
.
Reader
,
error
)
{
b
,
err
:=
json
.
MarshalIndent
(
value
,
""
,
" "
)
if
err
!=
nil
{
return
nil
,
err
}
return
bytes
.
NewReader
(
b
),
nil
}
var
marshallers
=
map
[
EncodingType
]
Marshaler
{
JSON
:
func
(
res
Response
)
(
io
.
Reader
,
error
)
{
if
ch
,
ok
:=
res
.
Output
()
.
(
chan
interface
{});
ok
{
return
&
ChannelMarshaler
{
Channel
:
ch
,
Marshaler
:
marshalJson
,
},
nil
}
var
value
interface
{}
if
res
.
Error
()
!=
nil
{
value
=
res
.
Error
()
}
else
{
value
=
res
.
Output
()
}
b
,
err
:=
json
.
MarshalIndent
(
value
,
""
,
" "
)
if
err
!=
nil
{
return
nil
,
err
}
return
bytes
.
NewReader
(
b
),
nil
return
marshalJson
(
value
)
},
XML
:
func
(
res
Response
)
(
io
.
Reader
,
error
)
{
var
value
interface
{}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment