Skip to content

Conversation

@alshopov
Copy link
Contributor

@alshopov alshopov commented Jul 6, 2025

ArrayMarshalerType, ObjectMarshalerType, InlineMarshalerType all call
user provided methods on user provided values.
They may fail/panic and have to be guarded.

Use guard similar to the one for stringer with the following caveats:

  • ObjectMarshalerType in case of nil-ness will be logged as string rather than map.
  • ArrayMarshalerType - we have to insert a synthetic empty marshaller to log an empty slice.
    Otherwise the default shape of map shines through
  • InlineMarshalerType - in case of any error it will be de-inlined to show exact cause of error.
    Otherwise error will be attached to the wrapping object which will make debugging and fixing harder

While we introduce enough coverage, the test harness is not using currently subtests.
This patch does not address this to keep changes to a minimum and give time to discuss if this is necessary.
Thus there are 3 tests that were changed - the error cases, the success cases and inline case (separate).

ArrayMarshalerType, ObjectMarshalerType, InlineMarshalerType all call
user provided methods on user provided values.
They may fail/panic and have to be guarded.

Use guard similar to the one for stringer with the following caveats:

- ObjectMarshalerType in case of nil-ness will be logged as string rather than map.
- ArrayMarshalerType - we have to insert a synthetic empty marshaller to log an empty slice.
  Otherwise the default shape of map shines through
- InlineMarshalerType - in case of any error it will be de-inlined to show exact cause of error.
  Otherwise error will be attached to the wrapping object which will make debugging and fixing harder

While we introduce enough coverage, the test harness is not using currently subtests.
This patch does not address this to keep changes to a minimum and give time to discuss if this is necessary.
Thus there are 3 tests that were changed - the error cases, the success cases and inline case (separate).
@alshopov
Copy link
Contributor Author

alshopov commented Jul 7, 2025

@JacobOaks, @tchung1118: as discussed on #1501, I am proposing a fix for #1504.
The commit messages should outline decisions made in implementing this. Have a look.

@alshopov
Copy link
Contributor Author

alshopov commented Jul 7, 2025

@prashantv Have a look at the implementation I propose for fixing problems you outlined here: #1501 (comment)

Copy link

@tchung1118 tchung1118 left a comment

Choose a reason for hiding this comment

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

Can we also add benchmarks to compare performances of before and after this change?
(on behalf of @sywhang from group code review)

enc.AddString(key, "<nil>")
return
}
_ = enc.AddArray(key, emptyArrayMarshaler{})

Choose a reason for hiding this comment

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

Why are we adding this?

Comment on lines +244 to +247
if v := reflect.ValueOf(objectMarshaller); v.Kind() == reflect.Ptr && v.IsNil() {
enc.AddString(key, "<nil>")
return
}

Choose a reason for hiding this comment

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

It doesn't seem necessary to log nil here separately because the error returned from here will already indicate there's a nil panic without this. Let's not do this because reflection is expensive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants