From 46b4defc9b4d6abb515ad8c4391c5d89033b1e2b Mon Sep 17 00:00:00 2001 From: ko_oler <kooler89@gmail.com> Date: Thu, 8 Feb 2024 17:05:34 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=B2?= =?UTF-8?q?=D0=BB=D0=B8=D1=82=D0=B8=D1=8F=20=D0=B3=D0=BB=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=B8=D0=B4=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0=D1=82=D0=BE=D1=80=D0=B0,?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/log.go | 37 +++++++++++++++++++++++++++---------- log/log_test.go | 35 ++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/log/log.go b/log/log.go index 4b9454b4..d95ecbed 100644 --- a/log/log.go +++ b/log/log.go @@ -3,8 +3,8 @@ package log import ( "time" + "git.perx.ru/perxis/perxis-go/id" pb "git.perx.ru/perxis/perxis-go/proto/log" - "github.com/mitchellh/mapstructure" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -34,8 +34,8 @@ type Entry struct { 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"` - ObjectID *id.ID `json:"object,omitempty" bson:"object,omitempty" mapstructure:"object,omitempty"` - CallerID *id.ID `json:"caller,omitempty" bson:"caller,omitempty" mapstructure:"caller,omitempty"` + ObjectID *id.ID `json:"object_id,omitempty" bson:"object_id,omitempty" mapstructure:"object_id,omitempty"` + CallerID *id.ID `json:"caller_id,omitempty" bson:"caller_id,omitempty" mapstructure:"caller_id,omitempty"` Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty" mapstructure:"attr,omitempty"` Tags []string `json:"tags,omitempty" bson:"tags,omitempty" mapstructure:"tags,omitempty"` } @@ -59,8 +59,8 @@ func EntryToPB(entry *Entry) *pb.LogEntry { Category: entry.Category, Component: entry.Component, Event: entry.Event, - Object: entry.Object, - Caller: entry.Caller, + ObjectId: entry.ObjectID.String(), + CallerId: entry.CallerID.String(), Attr: nil, //implement Tags: entry.Tags, } @@ -68,6 +68,8 @@ func EntryToPB(entry *Entry) *pb.LogEntry { } func EntryFromPB(request *pb.LogEntry) *Entry { + objectID, _ := id.Parse(request.ObjectId) + callerID, _ := id.Parse(request.CallerId) return &Entry{ ID: request.Id, Timestamp: request.Timestamp.AsTime(), @@ -76,15 +78,30 @@ func EntryFromPB(request *pb.LogEntry) *Entry { Category: request.Category, Component: request.Component, Event: request.Event, - Object: request.Object, - Caller: request.Caller, + ObjectID: objectID, + CallerID: callerID, Attr: request.Attr, // todo: как с этим работать? Tags: request.Tags, } } -func (e *Entry) ToMap() map[string]interface{} { - res := make(map[string]interface{}) - _ = mapstructure.Decode(e, &res) +func (e *Entry) ToMap() map[string]any { + res := map[string]any{ + "id": e.ID, + "timestamp": e.Timestamp, + "log_level": e.LogLevel, + "message": e.Message, + "category": e.Category, + "component": e.Component, + "event": e.Event, + "object_id": e.ObjectID.String(), + "caller_id": e.CallerID.String(), + } + if e.Attr != nil { + res["attr"] = e.Attr + } + if e.Tags != nil { + res["tags"] = e.Tags + } return res } diff --git a/log/log_test.go b/log/log_test.go index 2f63f535..52fea5be 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "git.perx.ru/perxis/perxis-go/id" "github.com/stretchr/testify/assert" ) @@ -16,8 +17,8 @@ func TestEntry_ToMap(t *testing.T) { Category string Component string Event string - Object string - Caller string + ObjectId *id.ID + CallerId *id.ID Attr interface{} Tags []string } @@ -29,19 +30,19 @@ func TestEntry_ToMap(t *testing.T) { { "#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>", + "1", + time.Time{}, + 0, + "message", + "", + "", + "", + id.NewEnvironmentID("<space_id>", "<env_id>"), + id.NewUserID("<user_id>"), + nil, + nil, }, + map[string]interface{}{"caller_id": "/users/<user_id>", "category": "", "component": "", "event": "", "id": "1", "log_level": Level(0), "message": "message", "object_id": "/spaces/<space_id>/envs/<env_id>", "timestamp": time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC)}, }, } for _, tt := range tests { @@ -54,12 +55,12 @@ func TestEntry_ToMap(t *testing.T) { Category: tt.fields.Category, Component: tt.fields.Component, Event: tt.fields.Event, - Object: tt.fields.Object, - Caller: tt.fields.Caller, + ObjectID: tt.fields.ObjectId, + CallerID: tt.fields.CallerId, Attr: tt.fields.Attr, Tags: tt.fields.Tags, } - assert.Equal(t, e.ToMap(), tt.want) + assert.Equal(t, tt.want, e.ToMap()) }) } } -- GitLab