Skip to content

Make snakecase the default in Apply#119

Merged
findleyr merged 1 commit intofatih:mainfrom
madelinekalil:snakecase-default-apply
Apr 23, 2025
Merged

Make snakecase the default in Apply#119
findleyr merged 1 commit intofatih:mainfrom
madelinekalil:snakecase-default-apply

Conversation

@madelinekalil
Copy link
Contributor

Currently, when calling Apply() without providing a Transform value, gomodifytags throws an error "unknown transform option". We should instead have a default value of snakecase, which is consistent with the command line execution of gomodifytags.

name = fieldName
default:
unknown = true
// Use snakecase as the default.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use fallthrough to avoid duplicating logic here.

Also, any observable behavior change like this should have a corresponding change in documentation.

@madelinekalil madelinekalil force-pushed the snakecase-default-apply branch 2 times, most recently from f3147e1 to f28c0a5 Compare April 16, 2025 14:53
Copy link
Collaborator

@findleyr findleyr left a comment

Choose a reason for hiding this comment

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

Madeline, there's something I'm not clear on. In the documentation for the gomodifytags command, it doesn't document "notransform" as an available option. Therefore, is there any need for the NoTransform constant at all? Can SnakeCase be the zero value? I think we should only make this change if SnakeCase can be the zero value.

key = split[0]
name = strings.Join(split[1:], "")
} else if unknown {
// the user didn't pass any value but want to use an unknown
Copy link
Collaborator

Choose a reason for hiding this comment

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

How would we have hit this code before? Looks like it wasn't exercised by any tests, since no tests had to change when this was removed. Would we have hit a parse error before getting here if the given transform was invalid?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! There were not any tests with invalid transform values, which is when we would reach this unknown case. I think it makes more sense to throw an error if the user passes an invalid transform value, and remove the NoTransform constant, so then SnakeCase can be the zero value. I added a test case for this error behavior in parseFlags.

@madelinekalil madelinekalil force-pushed the snakecase-default-apply branch from f28c0a5 to a18d651 Compare April 18, 2025 15:42
}

if cfg.fset == nil {
return errors.New("no file set")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check isn't necessary here because we create a new fset for the file later in parse(), which is called after validate()

@fatih
Copy link
Owner

fatih commented Apr 21, 2025

@madelinekalil let me know if you have anything else waiting, otherwise I'll merge it.

@findleyr findleyr merged commit f3939df into fatih:main Apr 23, 2025
1 check passed
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.

3 participants