Skip to content

Commit

Permalink
otelzap: Implement Uint methods on encoder (#5609)
Browse files Browse the repository at this point in the history
Part of
#5191

Pre-work
#5279

This PR implements `uInt` methods `objectEncoder`

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
  • Loading branch information
khushijain21 and pellared authored May 21, 2024
1 parent a91e60b commit 52ee3c4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
39 changes: 32 additions & 7 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ func (m *objectEncoder) AddString(k string, v string) {
m.kv = append(m.kv, log.String(k, v))
}

// TODO.
func (m *objectEncoder) AddUint64(k string, v uint64) {
m.kv = append(m.kv,
log.KeyValue{
Key: k,
Value: assignUintValue(v),
})
}

// TODO.
Expand Down Expand Up @@ -115,9 +119,30 @@ func (m *objectEncoder) AddInt8(k string, v int8) {
m.AddInt64(k, int64(v))
}

// TODO.
func (m *objectEncoder) AddUint(k string, v uint) {}
func (m *objectEncoder) AddUint32(k string, v uint32) {}
func (m *objectEncoder) AddUint16(k string, v uint16) {}
func (m *objectEncoder) AddUint8(k string, v uint8) {}
func (m *objectEncoder) AddUintptr(k string, v uintptr) {}
func (m *objectEncoder) AddUint(k string, v uint) {
m.AddUint64(k, uint64(v))
}

func (m *objectEncoder) AddUint32(k string, v uint32) {
m.AddInt64(k, int64(v))
}

func (m *objectEncoder) AddUint16(k string, v uint16) {
m.AddInt64(k, int64(v))
}

func (m *objectEncoder) AddUint8(k string, v uint8) {
m.AddInt64(k, int64(v))
}

func (m *objectEncoder) AddUintptr(k string, v uintptr) {
m.AddUint64(k, uint64(v))
}

func assignUintValue(v uint64) log.Value {
const maxInt64 = ^uint64(0) >> 1
if v > maxInt64 {
return log.Float64Value(float64(v))
}
return log.Int64Value(int64(v))
}
35 changes: 35 additions & 0 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ func TestObjectEncoder(t *testing.T) {
f: func(e zapcore.ObjectEncoder) { e.AddString("k", "v") },
expected: "v",
},
{
desc: "AddUint64",
f: func(e zapcore.ObjectEncoder) { e.AddUint64("k", 42) },
expected: int64(42),
},
{
desc: "AddUint64-Overflow",
f: func(e zapcore.ObjectEncoder) { e.AddUint64("k", ^uint64(0)) },
expected: float64(^uint64(0)),
},
{
desc: "AddUint",
f: func(e zapcore.ObjectEncoder) { e.AddUint("k", 42) },
expected: int64(42),
},
{
desc: "AddUint32",
f: func(e zapcore.ObjectEncoder) { e.AddUint32("k", 42) },
expected: int64(42),
},
{
desc: "AddUint16",
f: func(e zapcore.ObjectEncoder) { e.AddUint16("k", 42) },
expected: int64(42),
},
{
desc: "AddUint8",
f: func(e zapcore.ObjectEncoder) { e.AddUint8("k", 42) },
expected: int64(42),
},
{
desc: "AddUintptr",
f: func(e zapcore.ObjectEncoder) { e.AddUintptr("k", 42) },
expected: int64(42),
},
{
desc: "AddDuration",
f: func(e zapcore.ObjectEncoder) { e.AddDuration("k", time.Millisecond) },
Expand Down

0 comments on commit 52ee3c4

Please sign in to comment.