-
Notifications
You must be signed in to change notification settings - Fork 9
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
feat: implement verbose
#89
base: main
Are you sure you want to change the base?
Conversation
I'm not sure we need this 🤔 Early versions of On the other hand, I don't mind implementing it as an option, e.g.: musttag:
settings:
verbose: true It would be useful at least in @ldez maybe you have some thoughts? |
musttag.go
Outdated
@@ -216,21 +213,22 @@ func (c *checker) checkStruct(styp *types.Struct, tag string) (valid bool) { | |||
if !ok { | |||
// tag is not required for embedded types; see issue #12. | |||
if !field.Embedded() { | |||
return false | |||
pass.Reportf(pos, "the given struct should be annotated with the `%s` tag: %s.%s (%s)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving this here would cause the same call to be reported multiple times. We don't want that.
The original problem was the difficulty of knowing where the Marshall/Decode was. I think a more detailed report can be useful in some cases but reporting several issues on the same line is a problem because golangci-lint will only keep one of them. The
|
Hello, I've added the
|
What happens if there are multiple missing tags? If it reports only one field, then multiple runs will be required, it doesn't seem like a good behavior. |
Currently, each Marshal call is analyzed independently, so a struct type may be parsed multiple times to check for different tags. This could be prevented by introducing some kind of cache to parse each type only once. I see two possible implementations of
type T struct {
Foo struct {
Bar string
Baz string
}
}
var t T
json.Marshal(t) // musttag: the fields of the given struct should be annotated with the `json` tag: Foo.Bar, Foo.Baz
yaml.Marshal(t) // musttag: the fields of the given struct should be annotated with the `yaml` tag: Foo.Bar, Foo.Baz
type T struct {
Foo struct {
Bar string // musttag: the field should be annotated with the `json`, `yaml` tag(s)
Baz string // musttag: the field should be annotated with the `json`, `yaml` tag(s)
}
}
var t T
json.Marshal(t) // musttag: the given struct should be annotated with the `json` tag
yaml.Marshal(t) // musttag: the given struct should be annotated with the `yaml` tag The second one is tricky: to report a field with multiple tags just once we would have to change the implementation to analyze all Marshal calls first saving untagged fields into something like |
verbose
Hello, I have updated it to support the first implementation. For the second implementation, it should not be difficult. Currently, we already have Regarding "introducing some kind of cache to parse each type only once", I found that this may be more difficult than imagined, especially to obtain a unique identifier of a type. For example, in the two funcs below, the package main
func f1() {
type Foo struct {
...
}
}
func f2() {
type Foo struct {
...
}
} In addition, due to the existence of |
This PR contains the following three changes:
For example, we have a file
foo.go
:Previously, this would only output a single line:
When the structure is deeply nested, it will be very difficult to find all non-tag fields.
So we can try to output more information like:
Each line has two file locations. The first is the same as the original, which is the location of
json.Marshal
, and the second is the location where the non-tag fields are defined.However, repeated structures are not checked or outputted twice, for example, if
Foo
was changed to:The output will be unchanged, although
.FieldB.NoTagFieldB
is a non-tag field, theNestedB
structure has already been checked in.FieldA.NestedAField
, so it will be skipped.