From c757a750448905cb66fc8ac2217532e6e5abc80a Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Mon, 29 Jan 2024 15:35:02 +0300
Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?=
 =?UTF-8?q?=D0=BD=20=D1=82=D0=B5=D0=B3=20omitempty,=20=D0=B4=D0=BE=D0=B1?=
 =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D1=82=D0=B5=D1=81=D1=82=20?=
 =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20ToM?=
 =?UTF-8?q?ap()?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/log/log.go      | 29 +++++++++++---------
 pkg/log/log_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 13 deletions(-)
 create mode 100644 pkg/log/log_test.go

diff --git a/pkg/log/log.go b/pkg/log/log.go
index bb34948e..9e07a32d 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 00000000..2f63f535
--- /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)
+		})
+	}
+}
-- 
GitLab