Skip to content
Snippets Groups Projects
Commit c757a750 authored by ko_oler's avatar ko_oler
Browse files

добавлен тег omitempty, добавлен тест на функцию ToMap()

parent 0a927de3
No related branches found
No related tags found
No related merge requests found
...@@ -28,16 +28,16 @@ func (l Level) String() string { ...@@ -28,16 +28,16 @@ func (l Level) String() string {
type Entry struct { type Entry struct {
ID string `json:"id" bson:"id" mapstructure:"id"` ID string `json:"id" bson:"id" mapstructure:"id"`
Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp,omitempty" mapstructure:"timestamp"` 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"` LogLevel Level `json:"log_level,omitempty" bson:"log_level,omitempty" mapstructure:"log_level,omitempty"`
Message string `json:"message,omitempty" bson:"message,omitempty" mapstructure:"message"` Message string `json:"message,omitempty" bson:"message,omitempty" mapstructure:"message,omitempty"`
Category string `json:"category,omitempty" bson:"category,omitempty" mapstructure:"category"` Category string `json:"category,omitempty" bson:"category,omitempty" mapstructure:"category,omitempty"`
Component string `json:"component,omitempty" bson:"component,omitempty" mapstructure:"component"` Component string `json:"component,omitempty" bson:"component,omitempty" mapstructure:"component,omitempty"`
Event string `json:"event,omitempty" bson:"event,omitempty" mapstructure:"event"` Event string `json:"event,omitempty" bson:"event,omitempty" mapstructure:"event,omitempty"`
Object string `json:"object,omitempty" bson:"object,omitempty" mapstructure:"object"` Object string `json:"object,omitempty" bson:"object,omitempty" mapstructure:"object,omitempty"`
Caller string `json:"caller,omitempty" bson:"caller,omitempty" mapstructure:"caller"` Caller string `json:"caller,omitempty" bson:"caller,omitempty" mapstructure:"caller,omitempty"`
Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty" mapstructure:"attr"` Attr interface{} `json:"attr,omitempty" bson:"attr,omitempty" mapstructure:"attr,omitempty"`
Tags []string `json:"tags,omitempty" bson:"tags,omitempty" mapstructure:"tags"` Tags []string `json:"tags,omitempty" bson:"tags,omitempty" mapstructure:"tags,omitempty"`
} }
//func convertInterfaceToAny(v interface{}) (*any.Any, error) { //func convertInterfaceToAny(v interface{}) (*any.Any, error) {
...@@ -83,8 +83,11 @@ func EntryFromPB(request *pb.LogEntry) *Entry { ...@@ -83,8 +83,11 @@ func EntryFromPB(request *pb.LogEntry) *Entry {
} }
} }
func (e *Entry) ToMap() map[string]string { func (e *Entry) ToMap() map[string]interface{} {
res := make(map[string]string) res := make(map[string]interface{})
mapstructure.Decode(e, &res) err := mapstructure.Decode(e, &res)
if err != nil {
return nil
}
return res return res
} }
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)
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment