NeutrinoCorp / quark

:zap: A Reliable and fully customizable event router for Event-Driven systems written in Go.
https://quark.neutrinocorp.org
MIT License
3 stars 1 forks source link

Implement io.Writer and io.Reader for easier data encoding and decoding #9

Closed maestre3d closed 3 years ago

maestre3d commented 3 years ago

Is your feature request related to a problem? Please describe. When a developer want to encode/decode his responses and incoming events using encoders like Go's encoding/json, the developer must do it manually.

func(w quark.EventWriter, e *quark.Event) {
  msgJSON, _ := json.Marshal(e.Body)
  _, _ = w.Write(e.Context, msgJSON, "foo.created")
}

Describe the solution you'd like Implement the io.Writer interface on EventWriter and io.Reader interface on the Event struct so any encoder that uses these interfaces may be used like this:

func(w quark.EventWriter, e *quark.Event) {
  err := json.NewDecoder(e).Decode() // will write parse and set data to given quark.Event
  err = json.NewEncoder(w).Encode(e.Body) // will write message
}
maestre3d commented 3 years ago

Not required since an encoder must be custom (not just JSON). In addition, a decoder would override the current EventWriter.Write() implementation and would remove the Context.Context variable required for scenarios such as distributed tracing.