Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otelzap: Implement Uint methods on encoder #5609

Merged
merged 12 commits into from
May 21, 2024
Merged
39 changes: 33 additions & 6 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@
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 All @@ -94,11 +98,34 @@
func (m *objectEncoder) AddInt16(k string, v int16) { m.AddInt64(k, int64(v)) }
func (m *objectEncoder) AddInt8(k string, v int8) { m.AddInt64(k, int64(v)) }

func (m *objectEncoder) AddUint(k string, v uint) {
m.AddUint64(k, uint64(v))

Check warning on line 102 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}

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))
}

// TODO.
func (m *objectEncoder) AddComplex64(k string, v complex64) {}
func (m *objectEncoder) AddTime(k string, v time.Time) {}
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 assignUintValue(v uint64) log.Value {
const maxInt64 = ^uint64(0) >> 1
if v > maxInt64 {
return log.Float64Value(float64(v))
}
return log.Int64Value(int64(v))
}
25 changes: 25 additions & 0 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ 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", ^uint64(0)) },
expected: float64(^uint64(0)),
},
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
{
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),
},
}

for _, tt := range tests {
Expand Down
Loading