-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from tdakkota/feature/mtprint/more-formats-str…
…eaming mtprint: Add JSON and pretty-print formats, stream reading
- Loading branch information
Showing
5 changed files
with
161 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/k0kubun/pp" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/gotd/td/bin" | ||
"github.com/gotd/td/internal/mt" | ||
"github.com/gotd/td/internal/proto/codec" | ||
"github.com/gotd/td/internal/tmap" | ||
"github.com/gotd/td/tg" | ||
"github.com/gotd/td/transport" | ||
) | ||
|
||
// Formatter formats given bin.Object and prints it to io.Writer. | ||
type Formatter interface { | ||
Format(w io.Writer, i bin.Object) error | ||
} | ||
|
||
// FormatterFunc is functional adapter for Formatter. | ||
type FormatterFunc func(w io.Writer, i bin.Object) error | ||
|
||
// Format implements Formatter. | ||
func (f FormatterFunc) Format(w io.Writer, i bin.Object) error { | ||
return f(w, i) | ||
} | ||
|
||
func formats(name string) Formatter { | ||
switch name { | ||
case "json": | ||
return FormatterFunc(func(w io.Writer, i bin.Object) error { | ||
e := json.NewEncoder(w) | ||
e.SetIndent("", "\t") | ||
return e.Encode(i) | ||
}) | ||
case "pp": | ||
return FormatterFunc(func(w io.Writer, i bin.Object) error { | ||
_, err := pp.Fprintln(w, i) | ||
return err | ||
}) | ||
default: // "go" format | ||
return FormatterFunc(func(w io.Writer, i bin.Object) error { | ||
_, err := fmt.Fprintln(w, i) | ||
return err | ||
}) | ||
} | ||
} | ||
|
||
// Printer decodes messages from given reader and prints is using Formatter. | ||
type Printer struct { | ||
src io.Reader | ||
codec transport.Codec | ||
format Formatter | ||
} | ||
|
||
// NewPrinter creates new Printer. | ||
// If format is nil, "go" format will be used. | ||
// If c is nil, codec.Intermediate will be use. | ||
func NewPrinter(src io.Reader, format Formatter, c transport.Codec) Printer { | ||
if c == nil { | ||
c = codec.Intermediate{} | ||
} | ||
if format == nil { | ||
format = formats("go") | ||
} | ||
return Printer{ | ||
src: src, | ||
codec: c, | ||
format: format, | ||
} | ||
} | ||
|
||
// Print prints decoded messages to output. | ||
func (p Printer) Print(output io.Writer) error { | ||
b := &bin.Buffer{} | ||
|
||
m := tmap.NewConstructor( | ||
tg.TypesConstructorMap(), | ||
mt.TypesConstructorMap(), | ||
) | ||
for { | ||
b.Reset() | ||
if err := p.codec.Read(p.src, b); err != nil { | ||
if errors.Is(err, io.EOF) { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
id, err := b.PeekID() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
v := m.New(id) | ||
if v == nil { | ||
return xerrors.Errorf("failed to find type 0x%x", id) | ||
} | ||
|
||
if err := v.Decode(b); err != nil { | ||
return err | ||
} | ||
|
||
if err := p.format.Format(output, v); err != nil { | ||
return err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/gotd/td/bin" | ||
"github.com/gotd/td/internal/mt" | ||
"github.com/gotd/td/internal/proto/codec" | ||
"github.com/gotd/td/tg" | ||
) | ||
|
||
func Test_readAndPrint(t *testing.T) { | ||
c := codec.Intermediate{} | ||
|
||
input := &bytes.Buffer{} | ||
buf := &bin.Buffer{} | ||
|
||
objects := []bin.Object{ | ||
&mt.RPCResult{}, | ||
&mt.RPCError{}, | ||
&tg.CodeSettings{}, | ||
} | ||
for _, o := range objects { | ||
buf.Reset() | ||
require.NoError(t, o.Encode(buf)) | ||
require.NoError(t, c.Write(input, buf)) | ||
} | ||
|
||
output := &bytes.Buffer{} | ||
require.NoError(t, NewPrinter(input, formats("go"), c).Print(output)) | ||
out := output.String() | ||
require.Contains(t, out, "RPCResult") | ||
require.Contains(t, out, "RPCError") | ||
require.Contains(t, out, "CodeSettings") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters