diff --git a/log/log.go b/log/log.go index 4b9454b4f9dcce3eb7cfd4acaf948473d0c36cc8..d95ecbed0e12372a9d53f7c367144df112c21e73 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 2f63f5353a1c3a3a0b1cc3dfcc807e52c0b13a28..52fea5be0c1f661b99cfc2f5883e476ee32282b4 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()) }) } }