diff --git a/pkg/log/log.go b/pkg/log/log.go index bb34948efdead28fce58c5a6211bdb547c8be042..9e07a32d435e5bb137e67b713e03b9c2100ec41e 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -28,16 +28,16 @@ func (l Level) String() string { type Entry struct { ID string `json:"id" bson:"id" mapstructure:"id"` - Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp,omitempty" mapstructure:"timestamp"` - LogLevel Level `json:"log_level,omitempty" bson:"log_level,omitempty" mapstructure:"log_level"` - Message string `json:"message,omitempty" bson:"message,omitempty" mapstructure:"message"` - Category string `json:"category,omitempty" bson:"category,omitempty" mapstructure:"category"` - Component string `json:"component,omitempty" bson:"component,omitempty" mapstructure:"component"` - Event string `json:"event,omitempty" bson:"event,omitempty" mapstructure:"event"` - Object string `json:"object,omitempty" bson:"object,omitempty" mapstructure:"object"` - Caller string `json:"caller,omitempty" bson:"caller,omitempty" mapstructure:"caller"` - Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty" mapstructure:"attr"` - Tags []string `json:"tags,omitempty" bson:"tags,omitempty" mapstructure:"tags"` + Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp,omitempty" mapstructure:"timestamp,omitempty"` + LogLevel Level `json:"log_level,omitempty" bson:"log_level,omitempty" mapstructure:"log_level,omitempty"` + Message string `json:"message,omitempty" bson:"message,omitempty" mapstructure:"message,omitempty"` + Category string `json:"category,omitempty" bson:"category,omitempty" mapstructure:"category,omitempty"` + Component string `json:"component,omitempty" bson:"component,omitempty" mapstructure:"component,omitempty"` + Event string `json:"event,omitempty" bson:"event,omitempty" mapstructure:"event,omitempty"` + Object string `json:"object,omitempty" bson:"object,omitempty" mapstructure:"object,omitempty"` + Caller string `json:"caller,omitempty" bson:"caller,omitempty" mapstructure:"caller,omitempty"` + Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty" mapstructure:"attr,omitempty"` + Tags []string `json:"tags,omitempty" bson:"tags,omitempty" mapstructure:"tags,omitempty"` } //func convertInterfaceToAny(v interface{}) (*any.Any, error) { @@ -83,8 +83,11 @@ func EntryFromPB(request *pb.LogEntry) *Entry { } } -func (e *Entry) ToMap() map[string]string { - res := make(map[string]string) - mapstructure.Decode(e, &res) +func (e *Entry) ToMap() map[string]interface{} { + res := make(map[string]interface{}) + err := mapstructure.Decode(e, &res) + if err != nil { + return nil + } return res } diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go new file mode 100644 index 0000000000000000000000000000000000000000..2f63f5353a1c3a3a0b1cc3dfcc807e52c0b13a28 --- /dev/null +++ b/pkg/log/log_test.go @@ -0,0 +1,65 @@ +package log + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestEntry_ToMap(t *testing.T) { + type fields struct { + ID string + Timestamp time.Time + LogLevel Level + Message string + Category string + Component string + Event string + Object string + Caller string + Attr interface{} + Tags []string + } + tests := []struct { + name string + fields fields + want map[string]interface{} + }{ + { + "#1", + fields{ + ID: "1", + Timestamp: time.Time{}, + Message: "message", + Object: "/spaces/<space_id>/envs/<env_id>", + Caller: "/users/<user_id>", + }, + map[string]interface{}{ + "id": "1", + "timestamp": map[string]interface{}{}, + "message": "message", + "object": "/spaces/<space_id>/envs/<env_id>", + "caller": "/users/<user_id>", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := &Entry{ + ID: tt.fields.ID, + Timestamp: tt.fields.Timestamp, + LogLevel: tt.fields.LogLevel, + Message: tt.fields.Message, + Category: tt.fields.Category, + Component: tt.fields.Component, + Event: tt.fields.Event, + Object: tt.fields.Object, + Caller: tt.fields.Caller, + Attr: tt.fields.Attr, + Tags: tt.fields.Tags, + } + assert.Equal(t, e.ToMap(), tt.want) + }) + } +}