Permalink
Browse files

get proto working

  • Loading branch information...
peter-edge committed Jan 20, 2016
1 parent 09426b4 commit fdd1193ab4e1d3f0888897725eca3dc81434f858
Showing with 142 additions and 61 deletions.
  1. +21 −0 encoding.go
  2. +10 −1 proto/logger.go
  3. +83 −38 proto/protolion.go
  4. +4 −5 proto/util.go
  5. +24 −17 testing/testing_test.go
View
@@ -92,6 +92,9 @@ func checkNoRegisteredEncoding(encoding string) error {
/// *** util ***
func encodeEntry(entry *Entry) (*EncodedEntry, error) {
if entry == nil {
return nil, nil
}
encodedContexts, err := encodeEntryMessages(entry.Contexts)
if err != nil {
return nil, err
@@ -113,6 +116,9 @@ func encodeEntry(entry *Entry) (*EncodedEntry, error) {
}
func encodeEntryMessages(entryMessages []*EntryMessage) ([]*EncodedEntryMessage, error) {
if entryMessages == nil {
return nil, nil
}
encodedEntryMessages := make([]*EncodedEntryMessage, len(entryMessages))
for i, entryMessage := range entryMessages {
encodedEntryMessage, err := encodeEntryMessage(entryMessage)
@@ -125,6 +131,9 @@ func encodeEntryMessages(entryMessages []*EntryMessage) ([]*EncodedEntryMessage,
}
func encodeEntryMessage(entryMessage *EntryMessage) (*EncodedEntryMessage, error) {
if entryMessage == nil {
return nil, nil
}
encoder, err := getEncoder(entryMessage.Encoding)
if err != nil {
return nil, err
@@ -133,6 +142,9 @@ func encodeEntryMessage(entryMessage *EntryMessage) (*EncodedEntryMessage, error
}
func decodeEncodedEntry(encodedEntry *EncodedEntry) (*Entry, error) {
if encodedEntry == nil {
return nil, nil
}
contexts, err := decodeEncodedEntryMessages(encodedEntry.Contexts)
if err != nil {
return nil, err
@@ -154,6 +166,9 @@ func decodeEncodedEntry(encodedEntry *EncodedEntry) (*Entry, error) {
}
func decodeEncodedEntryMessages(encodedEntryMessages []*EncodedEntryMessage) ([]*EntryMessage, error) {
if encodedEntryMessages == nil {
return nil, nil
}
entryMessages := make([]*EntryMessage, len(encodedEntryMessages))
for i, encodedEntryMessage := range encodedEntryMessages {
entryMessage, err := decodeEncodedEntryMessage(encodedEntryMessage)
@@ -166,6 +181,9 @@ func decodeEncodedEntryMessages(encodedEntryMessages []*EncodedEntryMessage) ([]
}
func decodeEncodedEntryMessage(encodedEntryMessage *EncodedEntryMessage) (*EntryMessage, error) {
if encodedEntryMessage == nil {
return nil, nil
}
decoder, err := getDecoder(encodedEntryMessage.Encoding)
if err != nil {
return nil, err
@@ -174,6 +192,9 @@ func decodeEncodedEntryMessage(encodedEntryMessage *EncodedEntryMessage) (*Entry
}
func entryMessageName(entryMessage *EntryMessage) (string, error) {
if entryMessage == nil {
return "", nil
}
encoder, err := getEncoder(entryMessage.Encoding)
if err != nil {
return "", err
View
@@ -1,8 +1,9 @@
package protolion
import (
"github.com/gogo/protobuf/proto"
"go.pedge.io/lion"
"github.com/golang/protobuf/proto"
)
type logger struct {
@@ -17,6 +18,14 @@ func (l *logger) WithProtoContext(context proto.Message) Logger {
return newLogger(l.WithEntryMessageContext(newEntryMessage(context)))
}
func (l *logger) WithProtoField(key string, value interface{}) Logger {
return newLogger(l.WithField(key, value))
}
func (l *logger) WithProtoFields(fields map[string]interface{}) Logger {
return newLogger(l.WithFields(fields))
}
func (l *logger) ProtoDebug(event proto.Message) {
l.LogEntryMessage(lion.LevelDebug, newEntryMessage(event))
}
View
@@ -3,8 +3,9 @@ package protolion // import "go.pedge.io/lion/proto"
import (
"sync"
"github.com/gogo/protobuf/proto"
"go.pedge.io/lion"
"github.com/golang/protobuf/proto"
)
var (
@@ -24,7 +25,9 @@ var (
)
func init() {
lion.RegisterEncoderDecoder(Encoding, newEncoderDecoder())
if err := lion.RegisterEncoderDecoder(Encoding, newEncoderDecoder()); err != nil {
panic(err.Error())
}
lion.AddGlobalHook(setGlobalLogger)
}
@@ -34,47 +37,13 @@ func setGlobalLogger(logger lion.Logger) {
globalLogger = NewLogger(logger)
}
// GolangFirst says to check both golang and gogo for message names and types, but golang first.
func GolangFirst() {
globalLock.Lock()
defer globalLock.Unlock()
globalPrimaryPackage = "golang"
globalSecondaryPackage = "gogo"
globalOnlyPrimaryPackage = false
}
// GolangOnly says to check only golang for message names and types, but not gogo.
func GolangOnly() {
globalLock.Lock()
defer globalLock.Unlock()
globalPrimaryPackage = "golang"
globalSecondaryPackage = "gogo"
globalOnlyPrimaryPackage = true
}
// GogoFirst says to check both gogo and golang for message names and types, but gogo first.
func GogoFirst() {
globalLock.Lock()
defer globalLock.Unlock()
globalPrimaryPackage = "gogo"
globalSecondaryPackage = "golang"
globalOnlyPrimaryPackage = false
}
// GogoOnly says to check only gogo for message names and types, but not golang.
func GogoOnly() {
globalLock.Lock()
defer globalLock.Unlock()
globalPrimaryPackage = "gogo"
globalSecondaryPackage = "golang"
globalOnlyPrimaryPackage = true
}
// Logger is a lion.Logger that also has proto logging methods.
type Logger interface {
lion.Logger
WithProtoContext(context proto.Message) Logger
WithProtoField(key string, value interface{}) Logger
WithProtoFields(fields map[string]interface{}) Logger
ProtoDebug(event proto.Message)
ProtoInfo(event proto.Message)
@@ -89,3 +58,79 @@ type Logger interface {
func NewLogger(delegate lion.Logger) Logger {
return newLogger(delegate)
}
// WithContext calls WithProtoContext on the global Logger.
func WithContext(context proto.Message) Logger {
return globalLogger.WithProtoContext(context)
}
// Debug calls ProtoDebug on the global Logger.
func Debug(event proto.Message) {
globalLogger.ProtoDebug(event)
}
// Info calls ProtoInfo on the global Logger.
func Info(event proto.Message) {
globalLogger.ProtoInfo(event)
}
// Warn calls ProtoWarn on the global Logger.
func Warn(event proto.Message) {
globalLogger.ProtoWarn(event)
}
// Error calls ProtoError on the global Logger.
func Error(event proto.Message) {
globalLogger.ProtoError(event)
}
// Fatal calls ProtoFatal on the global Logger.
func Fatal(event proto.Message) {
globalLogger.ProtoFatal(event)
}
// Panic calls ProtoPanic on the global Logger.
func Panic(event proto.Message) {
globalLogger.ProtoPanic(event)
}
// Print calls ProtoPrint on the global Logger.
func Print(event proto.Message) {
globalLogger.ProtoPrint(event)
}
//// GolangFirst says to check both golang and gogo for message names and types, but golang first.
//func GolangFirst() {
//globalLock.Lock()
//defer globalLock.Unlock()
//globalPrimaryPackage = "golang"
//globalSecondaryPackage = "gogo"
//globalOnlyPrimaryPackage = false
//}
//// GolangOnly says to check only golang for message names and types, but not gogo.
//func GolangOnly() {
//globalLock.Lock()
//defer globalLock.Unlock()
//globalPrimaryPackage = "golang"
//globalSecondaryPackage = "gogo"
//globalOnlyPrimaryPackage = true
//}
//// GogoFirst says to check both gogo and golang for message names and types, but gogo first.
//func GogoFirst() {
//globalLock.Lock()
//defer globalLock.Unlock()
//globalPrimaryPackage = "gogo"
//globalSecondaryPackage = "golang"
//globalOnlyPrimaryPackage = false
//}
//// GogoOnly says to check only gogo for message names and types, but not golang.
//func GogoOnly() {
//globalLock.Lock()
//defer globalLock.Unlock()
//globalPrimaryPackage = "gogo"
//globalSecondaryPackage = "golang"
//globalOnlyPrimaryPackage = true
//}
View
@@ -4,7 +4,6 @@ import (
"fmt"
"reflect"
gogo "github.com/gogo/protobuf/proto"
golang "github.com/golang/protobuf/proto"
)
@@ -40,8 +39,8 @@ func messageTypeForPackage(pkg string, name string) (reflect.Type, error) {
switch pkg {
case "golang":
return golang.MessageType(name), nil
case "gogo":
return gogo.MessageType(name), nil
//case "gogo":
//return gogo.MessageType(name), nil
default:
return nil, fmt.Errorf("lion: unknown package: %s", pkg)
}
@@ -71,8 +70,8 @@ func messageNameForPackage(pkg string, message golang.Message) (string, error) {
switch pkg {
case "golang":
return golang.MessageName(message), nil
case "gogo":
return gogo.MessageName(message), nil
//case "gogo":
//return gogo.MessageName(message), nil
default:
return "", fmt.Errorf("lion: unknown package: %s", pkg)
}
View
@@ -9,27 +9,31 @@ import (
"time"
"go.pedge.io/lion"
"go.pedge.io/lion/proto"
"github.com/stretchr/testify/require"
)
func TestRoundtripAndTextMarshaller(t *testing.T) {
buffer := bytes.NewBuffer(nil)
fakeTimer := newFakeTimer(0)
logger := lion.NewLogger(
lion.NewWritePusher(
buffer,
),
lion.LoggerWithIDAllocator(newFakeIDAllocator()),
lion.LoggerWithTimer(fakeTimer),
).AtLevel(lion.LevelDebug)
logger.Debug(
logger := protolion.NewLogger(
lion.NewLogger(
lion.NewWritePusher(
buffer,
protolion.DelimitedMarshaller,
),
lion.LoggerWithIDAllocator(newFakeIDAllocator()),
lion.LoggerWithTimer(fakeTimer),
).AtLevel(lion.LevelDebug),
)
logger.ProtoDebug(
&Foo{
StringField: "one",
Int32Field: 2,
},
)
logger.Info(
logger.ProtoInfo(
&Baz{
Bat: &Baz_Bat{
Ban: &Baz_Bat_Ban{
@@ -39,7 +43,7 @@ func TestRoundtripAndTextMarshaller(t *testing.T) {
},
},
)
logger.Info(&Empty{})
logger.ProtoInfo(&Empty{})
writer := logger.InfoWriter()
for _, s := range []string{
"hello",
@@ -58,14 +62,17 @@ func TestRoundtripAndTextMarshaller(t *testing.T) {
puller := lion.NewReadPuller(
buffer,
protolion.DelimitedUnmarshaller,
)
writeBuffer := bytes.NewBuffer(nil)
writePusher := lion.NewTextWritePusher(
writeBuffer,
lion.TextMarshallerDisableTime(),
)
for entry, pullErr := puller.Pull(); pullErr != io.EOF; entry, pullErr = puller.Pull() {
for encodedEntry, pullErr := puller.Pull(); pullErr != io.EOF; encodedEntry, pullErr = puller.Pull() {
require.NoError(t, pullErr)
entry, err := encodedEntry.Decode()
require.NoError(t, err)
require.NoError(t, writePusher.Push(entry))
}
require.Equal(
@@ -88,17 +95,17 @@ WARN a warning line {"someKey":"someValue"}
}
func TestPrintSomeStuff(t *testing.T) {
testPrintSomeStuff(t, lion.DefaultLogger)
testPrintSomeStuff(t, protolion.NewLogger(lion.DefaultLogger))
}
func testPrintSomeStuff(t *testing.T, logger lion.Logger) {
logger.Debug(
func testPrintSomeStuff(t *testing.T, logger protolion.Logger) {
logger.ProtoDebug(
&Foo{
StringField: "one",
Int32Field: 2,
},
)
logger.Info(
logger.ProtoInfo(
&Baz{
Bat: &Baz_Bat{
Ban: &Baz_Bat_Ban{
@@ -122,8 +129,8 @@ func testPrintSomeStuff(t *testing.T, logger lion.Logger) {
writer = logger.Writer()
_, _ = writer.Write([]byte("none"))
logger.Infoln("a normal line")
logger.WithField("someKey", "someValue").WithField("someOtherKey", 1).Warnln("a warning line")
logger.WithField("someKey", "someValue").WithField("someOtherKey", 1).Info(
logger.WithProtoField("someKey", "someValue").WithProtoField("someOtherKey", 1).Warnln("a warning line")
logger.WithProtoField("someKey", "someValue").WithProtoField("someOtherKey", 1).ProtoInfo(
&Baz{
Bat: &Baz_Bat{
Ban: &Baz_Bat_Ban{

0 comments on commit fdd1193

Please sign in to comment.