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

Initial correctifier code. #164

Closed
wants to merge 1 commit into from
Closed

Initial correctifier code. #164

wants to merge 1 commit into from

Conversation

schmidtw
Copy link
Member

This is a code doodle that shows what I'm thinking for the options based validator & normalizer code. I think this should support the metrics use cases we have & probably more if we want.

I would like to chat about this tomorrow if possible.

@schmidtw schmidtw requested review from denopink and johnabass March 12, 2024 04:10
Copy link

codecov bot commented Mar 12, 2024

Codecov Report

Attention: Patch coverage is 0% with 148 lines in your changes are missing coverage. Please review.

Project coverage is 50.10%. Comparing base (ac04be7) to head (cf93c70).
Report is 2 commits behind head on main.

Files Patch % Lines
wrpcorrectify/options.go 0.00% 114 Missing ⚠️
wrpcorrectify/metrics.go 0.00% 21 Missing ⚠️
wrpcorrectify/correctify.go 0.00% 13 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #164      +/-   ##
==========================================
- Coverage   51.66%   50.10%   -1.57%     
==========================================
  Files          31       34       +3     
  Lines        4746     4894     +148     
==========================================
  Hits         2452     2452              
- Misses       2104     2252     +148     
  Partials      190      190              
Flag Coverage Δ
unittests 50.10% <0.00%> (-1.57%) ⬇️

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.

)

// Correctifier applies a series of normalizing options to a WRP message.
type Correctifier struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

This type stutters: wrpcorrectify.Correctifier. The usual way to do this in golang is to call this type T: wrpcorrectify.T. The idea is that it's a central type to the package. I'm dubious about this, but it is what's done.

Probably a better way is to rename either the type or the package to something that doesn't stutter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, these were just working names.

// Option is a functional option for normalizing a WRP message.
type Option interface {
// Correctify applies the option to the given message.
Correctify(context.Context, *wrp.Message) error
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 intention to allow code outside this package to create options? If so, some additional bulletproofing might be in order. However, if no code outside this package should be creating Option implementations, then this interface should be sealed: no exported methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking that by making the Option something outside code could do, it would make the functionality more extensible & useful for one-off places.

What kind of bulletproofing did you have in mind?


// CounterMetric provides a counter metric that can be used to track the number
// of times a specific error occurs.
type CounterMetric struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

This code might benefit from inverting the metric function. You can avoid any dependency on prometheus. Instead, expose methods that return whatever state you want to track, then require application code to use the *Func versions of metrics. For example: https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#NewCounterFunc.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like your suggestion better.

// ReplaceAnySelfLocator replaces any `self:` based locator with the scheme and
// authority of the given locator. If the given locator is not valid, the
// option returns an error.
func ReplaceAnySelfLocator(me string) Option {
Copy link
Contributor

Choose a reason for hiding this comment

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

You might be able to cut down the code repetition by have fewer or just one option that was parameterized. For example:

func ReplaceAnyLocator(scheme string, me string) Option { ...}

You could also use struct fields via the reflect package to bind together a replacement value with the wrp.Message fields it applies to.

@schmidtw
Copy link
Member Author

Closing in favor of #167

@schmidtw schmidtw closed this Mar 15, 2024
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