update replace field
This commit is contained in:
@@ -163,18 +163,7 @@ func (e *Entry) AddString(key, value string) *Entry {
|
||||
}
|
||||
|
||||
func (e *Entry) Replace(key string, value field.Value) *Entry {
|
||||
has := false
|
||||
|
||||
e.fields.Fields(func(f field.Field) bool {
|
||||
if f.Key == key {
|
||||
f.Value = value
|
||||
has = true
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
_, has := e.fields.Replace(field.Any(key, value))
|
||||
|
||||
if !has {
|
||||
e.AddAny(key, value)
|
||||
|
||||
42
entry/entry_test.go
Normal file
42
entry/entry_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package entry_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/log/entry"
|
||||
"gitoa.ru/go-4devs/log/field"
|
||||
)
|
||||
|
||||
func TestEntry_Replace(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ent := entry.New(entry.WithFields(
|
||||
field.Any("inti", "init date"),
|
||||
))
|
||||
|
||||
ent = ent.Replace("date", field.StringValue("some date"))
|
||||
ent = ent.Replace("date", field.TimeValue(time.Time{}))
|
||||
|
||||
fields := ent.Fields()
|
||||
|
||||
if len(fields) != 2 {
|
||||
t.Fatalf("count must be 2 got %v", len(fields))
|
||||
}
|
||||
|
||||
var has bool
|
||||
|
||||
fields.Fields(func(f field.Field) bool {
|
||||
if f.Key == "date" && !f.Value.AsTime().IsZero() {
|
||||
has = true
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
if !has {
|
||||
t.Fatal("failed reace value")
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,13 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var empty Field
|
||||
|
||||
func Empty() Field {
|
||||
return empty
|
||||
}
|
||||
|
||||
func Any(key string, value any) Field {
|
||||
return Field{
|
||||
Key: key,
|
||||
|
||||
@@ -32,3 +32,16 @@ func (f Fields) Set(idx int, field Field) {
|
||||
func (f Fields) Len() int {
|
||||
return len(f)
|
||||
}
|
||||
|
||||
func (f Fields) Replace(field Field) (Field, bool) {
|
||||
for idx := range f {
|
||||
if f[idx].Key == field.Key {
|
||||
old := f[idx]
|
||||
f[idx] = field
|
||||
|
||||
return old, true
|
||||
}
|
||||
}
|
||||
|
||||
return Empty(), false
|
||||
}
|
||||
|
||||
@@ -16,3 +16,22 @@ func TestFields_Append(t *testing.T) {
|
||||
t.Fatalf("require 2 field got %v", len(fields))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFields_Replace(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fields := field.Fields{
|
||||
field.Any("any", "any init"),
|
||||
field.Any("replace", "replace init"),
|
||||
}
|
||||
|
||||
old, ok := fields.Replace(field.Int64("replace", 42))
|
||||
if !ok || old.Key != "replace" || old.Value != field.StringValue("replace init") {
|
||||
t.Fatalf("failed replace value:%v", old)
|
||||
}
|
||||
|
||||
o2, ok2 := fields.Replace(field.Any("new", "new data"))
|
||||
if ok2 || o2.Key != "" {
|
||||
t.Fatalf("failed set new data:%v", o2)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user