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

Add Objects and ObjectValues field constructors #1071

Merged
merged 4 commits into from Mar 22, 2022
Merged

Add Objects and ObjectValues field constructors #1071

merged 4 commits into from Mar 22, 2022

Commits on Mar 19, 2022

  1. Add Objects field constructor for array of objects

    Add a new Zap field constructor that encodes an array of zero or more
    Zap-marshalable objects into a Zap array.
    
    Usage:
    
        // Given,
        //
        // func (*User) MarshalLogObject(zapcore.ObjectEncoder) error
    
        zap.Objects("foo", []*User{u1, u2, u3})
    
    Before this, users would have to resort to constructing a `[]*User`
    wrapper, and use Zap's `Array` constructor:
    
        // Before:
    
        type users []*User
    
        func (uu users) MarshalLogArray(enc zapcore.ArrayEncoder) error a
            for _, u := range uu {
                if err := enc.AppendObject(u); err != nil {
                    return err
                }
            }
            return nil
        }
    
        zap.Array("foo", users{u1, u2, u3})
    
    Credit to ztstewart for the suggestion.
    
    Refs GO-618
    
    Reviewed-At: D7294727
    Reviewed-By: ztstewart
    abhinav committed Mar 19, 2022
    Copy the full SHA
    1ab8406 View commit details
    Browse the repository at this point in the history

Commits on Mar 21, 2022

  1. Add ObjectValues field constructor

    In the previous change, we added a Objects field constructor to support
    logging arrays of any object supported by zap.Object.
    
        func (*User) MarshalLogObject(...) error { ... }
        var users []*User = ...
        zap.Object("users", users)
    
    That solution is incomplete because it does not cover the case when,
    instead of a `[]*User`, we have a `[]User` despite the method being on
    `*User`.
    
    This change adds a similar ObjectValues field constructor that handles the
    case where we have a `[]T` but the MarshalLogObject is on `*T`.
    
    This works by declaring a new type constraint objectMarshalerPtr.
    `objectMarshalerPtr[T]` is true for a type that is a pointer to `T`,
    and implements zapcore.ObjectMarshaler.
    
    Note that we still do not cover the case where the method is on the value
    receiver (`func (User) MarshalLogObject(..) error`) and we have a
    `[]*User`. We can add that when it becomes necessary.
    
    Reviewed-At: D7295739
    Reviewed-By: ztstewart
    abhinav committed Mar 21, 2022
    Copy the full SHA
    cac228a View commit details
    Browse the repository at this point in the history

Commits on Mar 22, 2022

  1. ObjectValues: Avoid copying

    Minor optimization to avoid copying the struct to a variable.
    
    Refs GO-618
    
    Reviewed-At: D7297933
    Reviewed-By: abhinav
    kishoresenji authored and abhinav committed Mar 22, 2022
    Copy the full SHA
    fb6027f View commit details
    Browse the repository at this point in the history
  2. CI: Temporarily disable staticcheck

    staticcheck is expected to have support for generics-based code in a few
    weeks. Disable it until then.
    abhinav committed Mar 22, 2022
    Copy the full SHA
    854d895 View commit details
    Browse the repository at this point in the history