Skip to content

Commit

Permalink
use Encoder value receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed May 21, 2024
1 parent 579fb74 commit 4fb89e6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 49 deletions.
2 changes: 1 addition & 1 deletion convert/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func BenchmarkJSONConvert(b *testing.B) {

appendMap := func(b []byte, kvs ...interface{}) []byte {
b = e.AppendMap(b, -1)
b = tlog.AppendKVs(b, kvs)
b = tlog.AppendKVs(e, b, kvs)
b = e.AppendBreak(b)

return b
Expand Down
3 changes: 2 additions & 1 deletion convert/logfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ func TestLogfmtRename(t *testing.T) {
}

func TestLogfmtKeyWithSpace(t *testing.T) {
var e tlwire.Encoder
var b low.Buf

j := NewLogfmt(&b)
j.QuoteEmptyValue = true

_, err := j.Write(tlog.AppendKVs(nil, []interface{}{tlog.RawTag(tlwire.Map, 1), "key with spaces", "value"}))
_, err := j.Write(tlog.AppendKVs(e, nil, []interface{}{tlog.RawTag(tlwire.Map, 1), "key with spaces", "value"}))
assert.NoError(t, err)
assert.Equal(t, `"key with spaces"=value`+"\n", string(b))
}
Expand Down
8 changes: 5 additions & 3 deletions convert/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func TestRewriter(t *testing.T) {
var e tlwire.Encoder
var obj, b low.Buf
rew := NewRewriter(&b)
rew.Rule = RewriterFunc(func(b, p []byte, path []tlog.RawMessage, kst, st int) ([]byte, int, error) {
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestRewriter(t *testing.T) {
assert.Equal(t, obj, b)

b = b[:0]
obj = tlog.AppendKVs(obj[:0], []interface{}{
obj = tlog.AppendKVs(e, obj[:0], []interface{}{
tlog.RawTag(tlwire.Map, -1),
"a", "b",
"d", tlog.NextIs(tlwire.Duration), 1,
Expand All @@ -59,6 +60,7 @@ func TestRewriter(t *testing.T) {
}

func TestKeyRenamer(t *testing.T) {
var e tlwire.Encoder
var obj, exp, b low.Buf

rew := NewRewriter(&b)
Expand Down Expand Up @@ -96,14 +98,14 @@ func TestKeyRenamer(t *testing.T) {
b = b[:0]
t.Logf("case 1")

obj = tlog.AppendKVs(obj[:0], []interface{}{
obj = tlog.AppendKVs(e, obj[:0], []interface{}{
tlog.RawTag(tlwire.Map, -1),
tlog.KeyTimestamp, time.Unix(100000000, 0),
tlog.KeyCaller, loc.Caller(0),
tlog.Break,
})

exp = tlog.AppendKVs(exp[:0], []interface{}{
exp = tlog.AppendKVs(e, exp[:0], []interface{}{
tlog.RawTag(tlwire.Map, -1),
"time", time.Unix(100000000, 0),
tlog.Break,
Expand Down
3 changes: 2 additions & 1 deletion examples/charmlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"time"

"tlog.app/go/tlog"
"tlog.app/go/tlog/ext/tlwtag"
"tlog.app/go/tlog/tlwire"
)

const (
Cups = tlog.SemanticUserBase + iota
Cups = tlwtag.SemanticUserBase + iota
)

type cup int
Expand Down
9 changes: 4 additions & 5 deletions tlio/writers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestReWriter(t *testing.T) {
err: errors.New("some"),
}

var b low.Buf
var e tlwire.Encoder
var b low.Buf

w := NewReWriter(func(have io.Writer, err error) (io.Writer, error) {
var b low.Buf
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestReWriter(t *testing.T) {
}()

b = e.AppendMap(b, -1)
b = tlog.AppendKVs(b, kvs)
b = tlog.AppendKVs(e, b, kvs)
b = e.AppendBreak(b)

_, err = w.Write(b)
Expand Down Expand Up @@ -141,13 +141,12 @@ func TestReWriter(t *testing.T) {
}

func newfile(events [][]interface{}) *low.Buf {
var b low.Buf

var e tlwire.Encoder
var b low.Buf

for _, evs := range events {
b = e.AppendMap(b, -1)
b = tlog.AppendKVs(b, evs)
b = tlog.AppendKVs(e, b, evs)
b = e.AppendBreak(b)
}

Expand Down
40 changes: 21 additions & 19 deletions tlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,51 +130,53 @@ func message(l *Logger, id ID, d int, msg interface{}, kvs []interface{}) {
return
}

e := l.Encoder

defer l.Unlock()

Check failure on line 135 in tlog.go

View workflow job for this annotation

GitHub Actions / lint

l.Unlock undefined (type *Logger has no field or method Unlock) (typecheck)
l.Lock()

Check failure on line 136 in tlog.go

View workflow job for this annotation

GitHub Actions / lint

l.Lock undefined (type *Logger has no field or method Lock) (typecheck)

l.b = l.Encoder.AppendMap(l.b[:0], -1)
l.b = e.AppendMap(l.b[:0], -1)

if id != (ID{}) {
l.b = l.Encoder.AppendString(l.b, KeySpan)
l.b = e.AppendString(l.b, KeySpan)
l.b = id.TlogAppend(l.b)
}

if l.nano != nil {
now := l.nano()

l.b = l.Encoder.AppendString(l.b, KeyTimestamp)
l.b = l.Encoder.AppendTimestamp(l.b, now)
l.b = e.AppendString(l.b, KeyTimestamp)
l.b = e.AppendTimestamp(l.b, now)
}

var c loc.PC

if d >= 0 && l.callers != nil && l.callers(2+d+l.callersSkip, (*loc.PC)(noescape(unsafe.Pointer(&c))), 1, 1) != 0 {
l.b = l.Encoder.AppendKey(l.b, KeyCaller)
l.b = l.Encoder.AppendCaller(l.b, c)
l.b = e.AppendKey(l.b, KeyCaller)
l.b = e.AppendCaller(l.b, c)
}

if msg != nil {
l.b = l.Encoder.AppendKey(l.b, KeyMessage)
l.b = l.Encoder.AppendSemantic(l.b, WireMessage)
l.b = e.AppendKey(l.b, KeyMessage)
l.b = e.AppendSemantic(l.b, WireMessage)

switch msg := msg.(type) {
case string:
l.b = l.Encoder.AppendString(l.b, msg)
l.b = e.AppendString(l.b, msg)
case []byte:
l.b = l.Encoder.AppendTagBytes(l.b, tlwire.String, msg)
l.b = e.AppendTagBytes(l.b, tlwire.String, msg)
case format:
l.b = l.Encoder.AppendFormat(l.b, msg.Fmt, msg.Args...)
l.b = e.AppendFormat(l.b, msg.Fmt, msg.Args...)
default:
l.b = l.Encoder.AppendFormat(l.b, "%v", msg)
l.b = e.AppendFormat(l.b, "%v", msg)
}
}

l.b = AppendKVs(&l.Encoder, l.b, kvs)
l.b = AppendKVs(e, l.b, kvs)

l.b = append(l.b, l.ls...)

l.b = l.Encoder.AppendBreak(l.b)
l.b = e.AppendBreak(l.b)

_, _ = l.Writer.Write(l.b)
}
Expand All @@ -190,7 +192,7 @@ func newspan(l *Logger, par ID, d int, n string, kvs []interface{}) (s Span) {
s.StartedAt = l.now()
}

e := &l.Encoder
e := l.Encoder

defer l.Unlock()

Check failure on line 197 in tlog.go

View workflow job for this annotation

GitHub Actions / lint

l.Unlock undefined (type *Logger has no field or method Unlock) (typecheck)
l.Lock()

Check failure on line 198 in tlog.go

View workflow job for this annotation

GitHub Actions / lint

l.Lock undefined (type *Logger has no field or method Lock) (typecheck)
Expand Down Expand Up @@ -244,7 +246,7 @@ func (s Span) Finish(kvs ...interface{}) {
}

l := s.Logger
e := &s.Logger.Encoder
e := l.Encoder

defer l.Unlock()

Check failure on line 251 in tlog.go

View workflow job for this annotation

GitHub Actions / lint

l.Unlock undefined (type *Logger has no field or method Unlock) (typecheck)
l.Lock()

Check failure on line 252 in tlog.go

View workflow job for this annotation

GitHub Actions / lint

l.Lock undefined (type *Logger has no field or method Lock) (typecheck)
Expand Down Expand Up @@ -293,7 +295,7 @@ func (l *Logger) SetLabels(kvs ...interface{}) {
defer l.Unlock()
l.Lock()

l.ls = AppendLabels(&l.Encoder, l.ls[:0], kvs)
l.ls = AppendLabels(l.Encoder, l.ls[:0], kvs)
}

func (l *Logger) Labels() RawMessage {
Expand Down Expand Up @@ -330,7 +332,7 @@ func (l *Logger) Event(kvs ...interface{}) (err error) {

l.b = l.AppendMap(l.b[:0], -1)

Check failure on line 333 in tlog.go

View workflow job for this annotation

GitHub Actions / lint

l.AppendMap undefined (type *Logger has no field or method AppendMap) (typecheck)

l.b = AppendKVs(&l.Encoder, l.b, kvs)
l.b = AppendKVs(l.Encoder, l.b, kvs)

l.b = append(l.b, l.ls...)

Expand All @@ -347,7 +349,7 @@ func (s Span) Event(kvs ...interface{}) (err error) {
}

l := s.Logger
e := &l.Encoder
e := l.Encoder

defer l.Unlock()
l.Lock()
Expand Down
26 changes: 12 additions & 14 deletions tlwire/encoder_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,26 @@ func init() {
})
}

func (e *Encoder) AppendKeyValue(b []byte, key string, v interface{}) []byte {
func (e Encoder) AppendKeyValue(b []byte, key string, v interface{}) []byte {
b = e.AppendKey(b, key)
b = e.AppendValue(b, v)
return b
}

//go:linkname appendValue tlog.app/go/tlog/tlwire.(*Encoder).appendValue
//go:linkname appendValue tlog.app/go/tlog/tlwire.Encoder.appendValue
//go:noescape
func appendValue(e *Encoder, b []byte, v interface{}) []byte
func appendValue(e Encoder, b []byte, v interface{}) []byte

func (e *Encoder) AppendValue(b []byte, v interface{}) []byte {
func (e Encoder) AppendValue(b []byte, v interface{}) []byte {
return appendValue(e, b, v)
}

func (e *Encoder) AppendValueSafe(b []byte, v interface{}) []byte {
func (e Encoder) AppendValueSafe(b []byte, v interface{}) []byte {
return e.appendValue(b, v)
}

// Called through linkname hack as appendValue from (Encoder).AppendValue.
func (e *Encoder) appendValue(b []byte, v interface{}) []byte {
func (e Encoder) appendValue(b []byte, v interface{}) []byte {
if v == nil {
return append(b, Special|Nil)
}
Expand All @@ -126,7 +126,7 @@ func (e *Encoder) appendValue(b []byte, v interface{}) []byte {
return e.appendRaw(b, r, ptrSet{})
}

func (e *Encoder) appendRaw(b []byte, r reflect.Value, visited ptrSet) []byte { //nolint:gocognit,cyclop
func (e Encoder) appendRaw(b []byte, r reflect.Value, visited ptrSet) []byte { //nolint:gocognit,cyclop
if r.CanInterface() {
// v := r.Interface()
v := valueInterface(r)
Expand All @@ -137,14 +137,12 @@ func (e *Encoder) appendRaw(b []byte, r reflect.Value, visited ptrSet) []byte {

ef := raweface(v)

if e != nil {
if enc, ok := e.custom[ef.typ]; ok {
return enc(e, b, v)
}
if enc, ok := e.custom[ef.typ]; ok {
return enc(&e, b, v)
}

if enc, ok := defaultEncoders[ef.typ]; ok {
return enc(e, b, v)
return enc(&e, b, v)
}

switch v := v.(type) {
Expand Down Expand Up @@ -250,7 +248,7 @@ func (e *Encoder) appendRaw(b []byte, r reflect.Value, visited ptrSet) []byte {
}
}

func (e *Encoder) appendStruct(b []byte, r reflect.Value, visited ptrSet) []byte {
func (e Encoder) appendStruct(b []byte, r reflect.Value, visited ptrSet) []byte {
t := r.Type()

b = append(b, Map|LenBreak)
Expand All @@ -262,7 +260,7 @@ func (e *Encoder) appendStruct(b []byte, r reflect.Value, visited ptrSet) []byte
return b
}

func (e *Encoder) appendStructFields(b []byte, t reflect.Type, r reflect.Value, visited ptrSet) []byte {
func (e Encoder) appendStructFields(b []byte, t reflect.Type, r reflect.Value, visited ptrSet) []byte {
// fmt.Fprintf(os.Stderr, "appendStructFields: %v ctx %p %d\n", t, visited, len(visited))

s := parseStruct(t)
Expand Down
10 changes: 5 additions & 5 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const (
SemanticCommunityBase = tlwire.SemanticTlogBase + 10
)

func AppendLabels(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte {
func AppendLabels(e tlwire.Encoder, b []byte, kvs []interface{}) []byte {
const tag = tlwire.Semantic | WireLabel

var d tlwire.LowDecoder
Expand Down Expand Up @@ -85,7 +85,7 @@ func AppendLabels(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte {
return b[:w]
}

func AppendKVs(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte {
func AppendKVs(e tlwire.Encoder, b []byte, kvs []interface{}) []byte {
return appendKVs0(e, b, kvs)
}

Expand All @@ -103,13 +103,13 @@ func Special(value int) RawMessage {

//go:linkname appendKVs0 tlog.app/go/tlog.appendKVs
//go:noescape
func appendKVs0(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte
func appendKVs0(e tlwire.Encoder, b []byte, kvs []interface{}) []byte

func init() { // prevent deadcode warnings
appendKVs(nil, nil, nil)
appendKVs(tlwire.Encoder{}, nil, nil)
}

func appendKVs(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte {
func appendKVs(e tlwire.Encoder, b []byte, kvs []interface{}) []byte {
for i := 0; i < len(kvs); {
var k string

Expand Down

0 comments on commit 4fb89e6

Please sign in to comment.