-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
go/doc: add fields to Example struct to indicate type/method/suffix #23864
Comments
Some evidence that this is an issue: the two most common godoc implementations don't handle this the same way:
Arguably the gddo approach is more correct. The point is that the divergence in behavior is a why the standard library should do this association for the user. |
Change https://golang.org/cl/94855 mentions this issue: |
Why do you want to modify go/doc? Is it just so godoc and gddo can share something? |
To have a canonical implementation of the syntax laid on in the
and the additional godoc-like tools that are springing up.
I'm not sure what you mean by that. Are you saying we shouldn't alter the |
I don't think we expect go/doc structs in their current form to be guaranteed to be helpful for any use of template. I do think that something like
could be useful for godoc and cmd/doc and other things to share. That would still give you a canonical implementation of the syntax. Or add a few fields to the doc.Example struct:
? |
That is not sufficient. You need a list of top-level type, function, and method identifiers to disambiguate whether Another ambiguity is whether |
The ambiguity that @dsnet mentions strikes out the ParseExample func, but it seems like the doc.Example field additions are still fine, since they're returned as part of something that looks at the whole package and knows its types and methods. |
OK, it sounds like we should add the fields to the Example struct (the second half of my message from March 5). And maybe also we should provide the Example slice in the overall Package struct instead of having a second call, although there may be some concerns around opening files during doc.New that were previously not opened (*_test.go). @dsnet and @griesemer, if you both agree about adding the fields to the Example struct, please mark this proposal-accepted. |
Ping @dsnet and @griesemer, waiting on your acceptance. |
Spoke to @griesemer, adding fields to the This will require some adjustments to how
I support this, but can be a future addition. |
#27442 was closed as a duplicate of this. I should note that the unified behavior for example parsing may require an update to the documentation in |
There is a
So I think we can safely update |
Please take the opportunity to relax that restriction. The suffix used here is for human consumption and is not a Go idenfier label. Making it !uppercase rather than lowercase would be much better. |
Thanks for feedback @kortschak. I've looked over #27442 for the rationale presented more closely and will take it into account. |
I have talked with @dsnet about the status of CL 94855, and he gave me permission to take its code and make the necessary changes to address code review comments and make progress on resolving this issue. (I will be including a note in the commit message of my CL that the code is based on his CL.) I have addressed the code review comments I left on the original CL (here and here). We've been testing the code in that CL as part of documentation rendering for the discovery site (#33654), which has allowed me to detect a problem with the API it attempted to implement. From its commit message:
It turned out to be problematic to try to provide test source files to the files := map[string]*ast.File{...} // all .go files, including test source files
apkg, _ := ast.NewPackage(fset, files, ...)
p := doc.New(apkg, ...) Will result in We've used a workaround like this: apkg, _ := ast.NewPackage(fset, goFiles, ...)
for name, f := range testGoFiles {
apkg.Files[name] = f
}
p := doc.New(apkg, ...) But that does not seem to be a good API, and it requires I tried to come up with the least bad new API to resolve this problem, and I have created a new CL 204830 that implements it in order to resolve this issue. Review is welcome. |
Change https://golang.org/cl/204830 mentions this issue: |
Change https://golang.org/cl/217125 mentions this issue: |
Updates #23864 Updates #36878 Change-Id: I6efdaafbe5207c625643f201a5931ad735941365 Reviewed-on: https://go-review.googlesource.com/c/go/+/217125 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Change https://golang.org/cl/218477 mentions this issue: |
The most well known and important build constraints to take into account when rendering package documentation are the GOOS/GOARCH values. Make it more clear in the NewFromFiles documentation that they are a part of all build constraints that the caller is responsible for filtering out. Also suggest the "go/build".Context.MatchFile method for performing file matching. The logic to perform build context file matching is subtle and has many rules that aren't well known (for example, taking the gc or gccgo compiler into account). It is currently the only exported API in the standard library that implements this logic, and it would be unfortunate if people attempt to re-create it because they don't realize it is already available. Updates #23864 Change-Id: I3c5901e7081acf79125b2d429ec3aa3b58416ed7 Reviewed-on: https://go-review.googlesource.com/c/go/+/218477 Reviewed-by: Robert Griesemer <gri@golang.org>
The
doc
package currently provides:Given a set of source files, this only parses out the examples as a flat list.
However, the testing package documents a syntax of how to associate examples with:
Example
orExample_suffix
)ExampleF
orExampleF_suffix
)ExampleT
orExampleT_suffix
)ExampleT_M
orExampleT_M_suffix
)As the number of tools that provide godoc-like functionality increases, the manual logic to parse these example names and associate it with the appropriate top-level declaration is somewhat tedious and easy to get wrong.
Given that this syntax is documented, I propose that the
doc
package provide first-class support for doing this association. The additional functionality is as follows:New
uses theast.Package.Files
input to produce an internal[]Example
.Examples []Example
field is added to thePackage
,Type
, andFunc
types. They are populated as appropriate according to what the relevant examples are associated with.\cc @griesemer
The text was updated successfully, but these errors were encountered: