Skip to content

fix: resolve LogValuer before processing in attribute helpers#39

Merged
samber merged 1 commit into
mainfrom
fix/resolve-log-valuer-in-attribute-helpers
Apr 3, 2026
Merged

fix: resolve LogValuer before processing in attribute helpers#39
samber merged 1 commit into
mainfrom
fix/resolve-log-valuer-in-attribute-helpers

Conversation

@samber
Copy link
Copy Markdown
Owner

@samber samber commented Apr 3, 2026

Call v.Resolve() at the start of AnyValueToString, anyValueToStringValue,
and ValueToFloat64, and use Resolve() when extracting error values in
ReplaceAttrFunctions. Fixes incorrect dispatch when values are wrapped
in a LogValuer.

Call v.Resolve() at the start of AnyValueToString, anyValueToStringValue,
and ValueToFloat64, and use Resolve() when extracting error values in
ReplaceAttrFunctions. Fixes incorrect dispatch when values are wrapped
in a LogValuer.
Copilot AI review requested due to automatic review settings April 3, 2026 00:07
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.18%. Comparing base (694d1fd) to head (0afc0b4).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #39      +/-   ##
==========================================
+ Coverage   96.15%   96.18%   +0.02%     
==========================================
  Files           4        4              
  Lines         260      262       +2     
==========================================
+ Hits          250      252       +2     
  Misses          9        9              
  Partials        1        1              
Flag Coverage Δ
unittests 96.18% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@samber samber merged commit 5de6b94 into main Apr 3, 2026
12 checks passed
@samber samber deleted the fix/resolve-log-valuer-in-attribute-helpers branch April 3, 2026 00:10
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the attribute/value helper functions to call slog.Value.Resolve() before inspecting or formatting values, improving handling of slog.LogValuer-wrapped values during conversion and error replacement.

Changes:

  • Resolve slog.LogValuer values early in AttrToValue, AnyValueToString, and ValueToString.
  • Use Resolve() when extracting error values in ReplaceError to better support LogValuer-wrapped errors.
Comments suppressed due to low confidence (1)

attributes.go:165

  • The new v = v.Resolve() behavior in AnyValueToString/ValueToString/AttrToValue changes how slog.LogValuer values are rendered (and is the core purpose of this PR), but existing tests don’t cover LogValuer inputs for these helpers. Add test cases that pass slog.Any("k", stubLogValuer) (and a LogValuer that returns a non-group like testMaskedLogValuer) through these helpers to ensure the resolved output is used and no raw LogValuer structs leak into logs.
func AnyValueToString(v slog.Value) string {
	v = v.Resolve()

	if tm, ok := v.Any().(encoding.TextMarshaler); ok {
		data, err := tm.MarshalText()
		if err != nil {
			return ""
		}

		return string(data)
	}

	return fmt.Sprintf("%+v", v.Any())
}

func AttrsToString(attrs ...slog.Attr) map[string]string {
	output := make(map[string]string, len(attrs))

	for i := range attrs {
		attr := attrs[i]
		k, v := attr.Key, attr.Value
		output[k] = ValueToString(v)
	}

	return output
}

func ValueToString(v slog.Value) string {
	v = v.Resolve()

	switch v.Kind() {
	case slog.KindAny, slog.KindLogValuer, slog.KindGroup:
		return AnyValueToString(v)
	case slog.KindInt64, slog.KindUint64, slog.KindFloat64, slog.KindString, slog.KindBool, slog.KindDuration:

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread attributes.go
Comment on lines 180 to 184
for i := range errorKeys {
if a.Key == errorKeys[i] {
if err, ok := a.Value.Any().(error); ok {
if err, ok := a.Value.Resolve().Any().(error); ok {
return slog.Any(a.Key, FormatError(err))
}
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReplaceError now checks a.Value.Resolve().Any().(error). This breaks replacement for values where the attribute itself is an error that also implements slog.LogValuer: Resolve() converts it to the result of LogValue() (e.g., a string/group), so the type assertion to error will fail and the error won’t be formatted. Consider checking a.Value.Any() for error first (preserving LogValuer errors), and only falling back to Resolve().Any() if the direct assertion fails (to support wrapper LogValuers).

Copilot uses AI. Check for mistakes.
Comment thread attributes.go
Comment on lines 101 to +106
func AttrToValue(attr slog.Attr) (string, any) {
k := attr.Key
v := attr.Value
kind := v.Kind()
v = v.Resolve()

switch kind {
switch v.Kind() {
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description references anyValueToStringValue, ValueToFloat64, and "ReplaceAttrFunctions", but those functions don’t exist in this repo and the changes here also update ValueToString/AttrToValue. Please update the PR description to reflect the actual modified helpers so reviewers/users can match the intent to the diff.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants