-
Notifications
You must be signed in to change notification settings - Fork 94
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 a custom mechanism for looking up comments. #159
Conversation
@misberner this is a great idea! Thanks for submitting! Just by chance, I've also been working on the comments handling and incorporated a Do you think it would be possible to match that approach instead of adding yet more options to the Reflect struct? Thanks! |
@samlown thanks for the quick response! I have to admit I am not fully sure I understand the ask:
|
@misberner you're right, sorry about that. I looked at the example and assumed that the I'm super-short on time for refactors. If you feel up for an experiment, great! Otherwise if you could sync with the current main branch and move as much of the logic to the |
e86bcf6
to
48af7e0
Compare
@samlown no worries! I also don't have a ton of time on my hands so I went with the simple option. And yes, agreed on (a) using func CombineCommentLookups(lookupFns ...func(reflect.Type, string) string) func(reflect.Type, string) string {
return func(t reflect.Type, field string) string {
for _, lookupFn := range lookupFns {
if comment := lookupFn(t, field); comment != "" {
return comment
}
}
return ""
}
} if the need for this arrives. I'd like to keep it simple for now. If you disagree, I can easily change the option to be of slice type directly. |
48af7e0
to
20dafe1
Compare
Locally getting the same 92.1% coverage number on my branch as on main |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #159 +/- ##
==========================================
+ Coverage 88.34% 88.39% +0.05%
==========================================
Files 4 4
Lines 858 862 +4
==========================================
+ Hits 758 762 +4
Misses 80 80
Partials 20 20 ☔ View full report in Codecov by Sentry. |
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.
Awesome, thanks for this!
This PR adds a custom mechanism to
Reflector
for looking up comments, in addition toCommentsMap
.A
LookupComment(reflect.Type, string)
function can be defined, that, when set, gets invoked with the same arguments as the internallookupComment
method ofReflector
. If it returns a non-empty string, that will be used as the type/field comment; otherwise, if the returned string is emtpy or theLookupComment
function is not set, theCommentsMap
will be consulted. The precedence of an explicit description defined via struct tags remains unaffected.The motivation for this is that we cannot use the mechanism provided by the package for parsing Go comments, as it would require access to the source at runtime. Instead, we rely on code generation to provide Go comments at runtime. The data structure that we use is different from how the
CommentsMap
is structured. While we could create aCommentsMap
in the required format, this would require a priori knowledge of all the types that will be referenced in the JSON schema (or we'd have to generate it for all our types, which is a much larger set as the ones we want to obtain JSON schemas for.