Skip to content

Commit

Permalink
Adds encoding and decoding of cast in trasnformer#trasnform
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciro S. Costa committed Jul 4, 2018
1 parent 9b8c2e2 commit 774443d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
26 changes: 10 additions & 16 deletions commands/speed.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,27 @@ func (t *SpeedTransformation) Transform(c *cast.Cast) (err error) {

func speedAction(c *cli.Context) (err error) {
var (
input = c.Args().First()
output = c.String("out")
input = c.Args().First()
output = c.String("out")
transformation = &SpeedTransformation{
factor: c.Float64("factor"),
from: c.Float64("start"),
to: c.Float64("end"),
}
)

transformation := &SpeedTransformation{
factor: c.Float64("factor"),
from: c.Float64("start"),
to: c.Float64("end"),
}

t, err := transformer.New(transformation, input, output)
if err != nil {
err = cli.NewExitError(
"failed to create transformation: " +
err.Error(), 1)
err = cli.NewExitError(err, 1)
return
}

err = t.Apply()
err = t.Transform()
if err != nil {
err = cli.NewExitError(
"failed to apply transformation: " +
err.Error(), 1)
err = cli.NewExitError(err, 1)
return
}

t.Close()

return
}
31 changes: 26 additions & 5 deletions commands/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ type Transformation interface {
type Transformer struct {
input *os.File
output *os.File
cast *cast.Cast
Transformation Transformation
transformation Transformation
}

func New(t Transformation, input, output string) (m *Transformer, err error) {
Expand All @@ -27,7 +26,7 @@ func New(t Transformation, input, output string) (m *Transformer, err error) {
m = &Transformer{
input: os.Stdin,
output: os.Stdout,
Transformation: t,
transformation: t,
}

if input != "" {
Expand Down Expand Up @@ -66,8 +65,30 @@ func New(t Transformation, input, output string) (m *Transformer, err error) {
return
}

func (m *Transformer) Apply() (err error) {
err = m.Transformation.Transform(m.cast)
func (m *Transformer) Transform() (err error) {
var decodedCast *cast.Cast

decodedCast, err = cast.Decode(m.input)
if err != nil {
err = errors.Wrapf(err,
"failed to decode cast from input")
return
}

err = m.transformation.Transform(decodedCast)
if err != nil {
err = errors.Wrapf(err,
"failed to transform cast")
return
}

err = cast.Encode(m.output, decodedCast)
if err != nil {
err = errors.Wrapf(err,
"failed to save modified cast")
return
}

return
}

Expand Down

0 comments on commit 774443d

Please sign in to comment.