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 ability to ignore fields that match given regex pattern(s) #199

Merged
merged 3 commits into from
Aug 28, 2021

Conversation

dselans
Copy link
Contributor

@dselans dselans commented Aug 27, 2021

@alecthomas this adds functionality to allow kong to ignore fields that match a certain pattern.

I needed this in order to be able to ensure that kong does not parse fields like XXX_ that are automatically generated via protoc.

The regexp is kind of a big hammer but since this is for CLI generation, I figure the perf hit is OK. LMK.

Copy link
Owner

@alecthomas alecthomas left a comment

Choose a reason for hiding this comment

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

A couple of comments, but I like the idea.

options.go Outdated
//
// Example: When referencing protoc generated structs, you will likely want to
// ignore/skip XXX_* fields.
func IgnoreFieldsRegex(regexes ...*regexp.Regexp) Option {
Copy link
Owner

Choose a reason for hiding this comment

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

Please change this accept strings, and compile the regexes in the OptionFunc. It would be tedious to require users to compile their patterns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing!

build.go Outdated
for _, field := range fields {
for _, r := range k.ignoreFieldsRegex {
if r.Match([]byte(field.field.Name)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Use MatchString() instead of casting.

Copy link
Owner

Choose a reason for hiding this comment

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

Please match against v.Type().Name + "." + field.field.Name so that it can match against the containing struct as well. It's fairly common in deeply nested CLI structures to repeat fields and being able to discriminate by the containing "command" will future proof this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha. Will do.

build.go Outdated
for _, field := range fields {
for _, r := range k.ignoreFieldsRegex {
if r.Match([]byte(field.field.Name)) {
fmt.Println("skipping field: ", field.field.Name)
Copy link
Owner

Choose a reason for hiding this comment

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

Remove.

@alecthomas
Copy link
Owner

Thanks for this, I love getting contributions! :)

@dselans
Copy link
Contributor Author

dselans commented Aug 28, 2021

Thanks for this, I love getting contributions! :)

Thank YOU - you did all the hard work! I added an extra test for options - noticed something was untested when running a coverage report for my own stuff.

All feedback should be addressed.

@dselans dselans requested a review from alecthomas August 28, 2021 01:41
@alecthomas alecthomas merged commit 07faa2e into alecthomas:master Aug 28, 2021
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