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 RegisterEncoder functionality #348

Merged
merged 9 commits into from
Mar 10, 2017
Merged

Add RegisterEncoder functionality #348

merged 9 commits into from
Mar 10, 2017

Conversation

bufdev
Copy link
Contributor

@bufdev bufdev commented Mar 6, 2017

For #330

This is just a pass at this, let me know of any changes. I purposefully didn't do the underscores in front of private variables (I don't want to do this anymore) but I can add this for consistency. We could reuse the global mutex in global.go as well.

@mention-bot
Copy link

@peter-edge, thanks for your PR! By analyzing the history of the files in this pull request, we identified @akshayjshah and @suyash to be potential reviewers.

encoder.go Outdated
return errNoEncoderNameSpecified
}
if _, ok := encoderNameToConstructor[name]; ok {
return fmt.Errorf("encoder already registered for name %s", name)
Copy link
Contributor

Choose a reason for hiding this comment

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

%q may be a moderately more useful formatting code here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

encoder.go Outdated
if !ok {
return nil, fmt.Errorf("no encoder registered for name %s", name)
}
return constructor(encoderConfig), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems a missed opportunity to not let the encoder constructor return an error (e.g. config validation if needed).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya I was thinking the same thing, didn't know what to do. I updated to this.

encoder_test.go Outdated
"github.com/stretchr/testify/assert"
)

var (
Copy link
Contributor

Choose a reason for hiding this comment

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

is the var binding truly needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Heh no.

@bufdev
Copy link
Contributor Author

bufdev commented Mar 9, 2017

All comments addressed

Copy link
Contributor

@jcorbin jcorbin left a comment

Choose a reason for hiding this comment

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

This looks okay to me, but I bet @akshayjshah will really want those _ prefixes on your globals ;-)

At any rate, leaving actual stamp bit for him.

@bufdev
Copy link
Contributor Author

bufdev commented Mar 9, 2017

Ya I bet he will too, let's hope he doesn't notice :) Fat chance :)

encoder.go Outdated
var (
errNoEncoderNameSpecified = errors.New("no encoder name specified ")
encoderNameToConstructor = make(map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error))
encoderMutex sync.RWMutex
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: please underscore-prefix globals (except for errors).

I'm not wild about this convention either, but let's keep everything consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

encoder.go Outdated
if err := RegisterEncoder(name, constructor); err != nil {
panic(err.Error())
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Peter, I know you're not wild about this Must* pattern; in this case, I'm not a fan either. Can we drop this and encourage users to handle errors properly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

encoder.go Outdated
return func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
return constructor(encoderConfig), nil
}
}
Copy link
Contributor

@akshayjshah akshayjshah Mar 9, 2017

Choose a reason for hiding this comment

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

Could we simplify a bit by declaring encoderNameToConstructor as a map literal (or the output of a defaultEncoders() function)? I think that'll let us drop this wrapper function, MustRegisterEncoder, registerDefaultEncoders, and init.

(This is actually a question, not a requirement - I'm fine with the current approach too.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@akshayjshah
Copy link
Contributor

Haha, didn't see the discussion about underscores 😄

@bufdev
Copy link
Contributor Author

bufdev commented Mar 9, 2017

All comments addressed

@akshayjshah
Copy link
Contributor

Last nit: please add messages in assertions.

encoder.go Outdated
return constructor(encoderConfig)
}

func defaultEncoders() map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we even need this function now?

@bufdev
Copy link
Contributor Author

bufdev commented Mar 9, 2017

Both comments should be addressed

@bufdev
Copy link
Contributor Author

bufdev commented Mar 9, 2017

@akshayjshah note you have to merge this :)

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.

5 participants