Skip to content

Commit

Permalink
refactor(logger): remove one layer of json
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Feb 3, 2021
1 parent d589bf8 commit 3adf676
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
14 changes: 4 additions & 10 deletions src/logger/cmd/test_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@ package main

import (
"bufio"
"encoding/json"
"bytes"
"fmt"
"os"
"path/filepath"
)

func log(w *bufio.Writer, event []byte, meta []byte, data []byte) {
entry, err := json.Marshal(map[string][]byte{
"event": event,
"meta": meta,
"data": data,
})
if err != nil {
panic(err)
}
entry := append(bytes.Join([][]byte{event, meta, data}, []byte("\t")),
byte('\n'))
fmt.Printf("entry = '%s'\n", string(entry))
_, err = w.Write(append(entry, byte('\n')))
_, err := w.Write(entry)
if err != nil {
panic(err)
}
Expand Down
22 changes: 3 additions & 19 deletions src/logger/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func NewLogger() *Logger {
}

type Log struct {
Event []byte `json:"event"`
Meta []byte `json:"meta"`
Data []byte `json:"data"`
Entry []byte
}

func (_ Log) Message() {}
Expand All @@ -48,10 +46,7 @@ func (l *Logger) Receive(at time.Time, from string, event lib.InEvent) []lib.Out
case *lib.InternalMessage:
switch msg := (*ev).Message.(type) {
case Log:
// XXX: msg.Event and msg.Meta should also be enqueued!
enqueue(l.Queue,
bytes.Join([][]byte{msg.Event, msg.Meta, msg.Data},
[]byte("\t")))
enqueue(l.Queue, msg.Entry)
default:
panic(fmt.Errorf("Unknown message type: %s\n", msg))
}
Expand Down Expand Up @@ -187,18 +182,8 @@ func NewMarshaler() *Marshaler {
func (_ *Marshaler) UnmarshalMessage(message string, raw json.RawMessage, msg *lib.Message) error {
switch strings.ToLower(message) {
case "log":
var op struct {
Event []byte `json:"event"`
Meta []byte `json:"meta"`
Data []byte `json:"data"`
}
if err := json.Unmarshal(raw, &op); err != nil {
panic(err)
}
*msg = Log{
Event: op.Event,
Meta: op.Meta,
Data: op.Data,
Entry: raw,
}
default:
panic(fmt.Errorf("Unknown message type: %s\n%s", message, raw))
Expand Down Expand Up @@ -237,7 +222,6 @@ func DeployReadOnlyPipe(pipeName string, reactor lib.Reactor, m lib.Marshaler) {
// The client could prepend the line with the operation
// and then add a tab to separate it from the data?
if line != nil {
log.Printf("ReadBytes: '%s'\n", line)
var msg lib.Message
err := m.UnmarshalMessage("log", line, &msg)
if err != nil {
Expand Down

0 comments on commit 3adf676

Please sign in to comment.