Permalink
Browse files

fix json marshal func for protobuf

Signed-off-by: Peter Edge <peter.edge@gmail.com>
  • Loading branch information...
peter-edge committed Jan 28, 2016
1 parent 5168c8e commit bce8ff6a73e3de54a5f6b973ed8c117e63a66cda
Showing with 10 additions and 24 deletions.
  1. +5 −13 lion.go
  2. +5 −11 text_marshaller.go
18 lion.go
@@ -401,6 +401,11 @@ func NewReadPuller(reader io.Reader, unmarshaller Unmarshaller) Puller {
return newReadPuller(reader, unmarshaller)
}

// JSONMarshalFunc marshals JSON for a TextMarshaller.
//
// It is used internally in a TextMarshaller, and is not a Marshaller itself.
type JSONMarshalFunc func(writer io.Writer, value interface{}) error

// TextMarshaller is a Marshaller used for text.
type TextMarshaller interface {
Marshaller
@@ -439,19 +444,6 @@ func TextMarshallerDisableNewlines() TextMarshallerOption {
}
}

// JSONMarshalFunc marshals JSON for a TextMarshaller.
//
// It is used internally in a TextMarshaller, and is not a Marshaller itself.
type JSONMarshalFunc func(writer io.Writer, value interface{}) error

// TextMarshallerWithJSONMarshalFunc uses the given JSONMarshalFunc for JSON marshalling. The default
// behavior is to use the global JSONMarshalFunc.
func TextMarshallerWithJSONMarshalFunc(jsonMarshalFunc JSONMarshalFunc) TextMarshallerOption {
return func(textMarshaller *textMarshaller) {
textMarshaller.jsonMarshalFunc = jsonMarshalFunc
}
}

// NewTextMarshaller constructs a new Marshaller that produces human-readable
// marshalled Entry objects. This Marshaller is currently inefficient.
func NewTextMarshaller(options ...TextMarshallerOption) TextMarshaller {
@@ -32,7 +32,6 @@ type textMarshaller struct {
disableContexts bool
disableNewlines bool
colorize bool
jsonMarshalFunc JSONMarshalFunc
}

func newTextMarshaller(options ...TextMarshallerOption) *textMarshaller {
@@ -42,7 +41,6 @@ func newTextMarshaller(options ...TextMarshallerOption) *textMarshaller {
false,
false,
false,
globalJSONMarshalFunc,
}
for _, option := range options {
option(textMarshaller)
@@ -57,7 +55,6 @@ func (t *textMarshaller) WithColors() TextMarshaller {
t.disableContexts,
t.disableNewlines,
true,
t.jsonMarshalFunc,
}
}

@@ -68,7 +65,6 @@ func (t *textMarshaller) WithoutColors() TextMarshaller {
t.disableContexts,
t.disableNewlines,
false,
t.jsonMarshalFunc,
}
}

@@ -80,7 +76,6 @@ func (t *textMarshaller) Marshal(entry *Entry) ([]byte, error) {
t.disableContexts,
t.disableNewlines,
t.colorize,
t.jsonMarshalFunc,
)
}

@@ -91,7 +86,6 @@ func textMarshalEntry(
disableContexts bool,
disableNewlines bool,
colorize bool,
jsonMarshalFunc JSONMarshalFunc,
) ([]byte, error) {
buffer := bytes.NewBuffer(nil)
if entry.ID != "" {
@@ -119,7 +113,7 @@ func textMarshalEntry(
// TODO(pedge): verify only one of Event, Message, WriterOutput?
if entry.Event != nil {
eventSeen = true
if err := textMarshalMessage(jsonMarshalFunc, buffer, entry.Event); err != nil {
if err := textMarshalMessage(buffer, entry.Event); err != nil {
return nil, err
}
}
@@ -138,7 +132,7 @@ func textMarshalEntry(
eventSeen = true
lenContexts := len(entry.Contexts)
for i, context := range entry.Contexts {
if err := textMarshalMessage(jsonMarshalFunc, buffer, context); err != nil {
if err := textMarshalMessage(buffer, context); err != nil {
return nil, err
}
if i != lenContexts-1 {
@@ -150,7 +144,7 @@ func textMarshalEntry(
if eventSeen {
_ = buffer.WriteByte(' ')
}
if err := jsonMarshalFunc(buffer, entry.Fields); err != nil {
if err := globalJSONMarshalFunc(buffer, entry.Fields); err != nil {
return nil, err
}
}
@@ -163,7 +157,7 @@ func textMarshalEntry(
return data, nil
}

func textMarshalMessage(jsonMarshalFunc JSONMarshalFunc, buffer *bytes.Buffer, message *EntryMessage) error {
func textMarshalMessage(buffer *bytes.Buffer, message *EntryMessage) error {
if message == nil {
return nil
}
@@ -173,7 +167,7 @@ func textMarshalMessage(jsonMarshalFunc JSONMarshalFunc, buffer *bytes.Buffer, m
}
_, _ = buffer.WriteString(name)
_ = buffer.WriteByte(' ')
return jsonMarshalFunc(buffer, message.Value)
return globalJSONMarshalFunc(buffer, message.Value)
}

func trimRightSpaceBytes(b []byte) []byte {

0 comments on commit bce8ff6

Please sign in to comment.