Skip to content

fix: Key construction in ExtractGoComments #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

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions comment_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package jsonschema

import (
"fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"io/fs"
gopath "path"
"path/filepath"
"strings"

"go/ast"
"go/doc"
"go/parser"
"go/token"
"golang.org/x/exp/maps"
)

// ExtractGoComments will read all the go files contained in the provided path,
Expand All @@ -24,24 +25,33 @@ import (
//
// When parsing type comments, we use the `go/doc`'s Synopsis method to extract the first phrase
// only. Field comments, which tend to be much shorter, will include everything.
func ExtractGoComments(base, path string, commentMap map[string]string) error {
func ExtractGoComments(base, rootPath string, commentMap map[string]string) error {
root, err := filepath.Abs(rootPath)
if err != nil {
return err
}

fset := token.NewFileSet()
dict := make(map[string][]*ast.Package)
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
err = filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
d, err := parser.ParseDir(fset, path, nil, parser.ParseComments)
if err != nil {
return err
}
for _, v := range d {
// paths may have multiple packages, like for tests
k := gopath.Join(base, path)
dict[k] = append(dict[k], v)
}
if !info.IsDir() {
return nil
}

d, err := parser.ParseDir(fset, path, nil, parser.ParseComments)
if err != nil {
return err
}

// key should consist of a base path + the path relative to the root
// so that we can treat rootPath of "../" properly
k := gopath.Join(base, strings.TrimPrefix(path, root))
// paths may have multiple packages, like for tests
dict[k] = append(dict[k], maps.Values(d)...)

return nil
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/stretchr/testify v1.8.1
github.com/wk8/go-ordered-map/v2 v2.1.8
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
2 changes: 1 addition & 1 deletion reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func TestSchemaGeneration(t *testing.T) {
func prepareCommentReflector(t *testing.T) *Reflector {
t.Helper()
r := new(Reflector)
err := r.AddGoComments("github.com/invopop/jsonschema", "./examples")
err := r.AddGoComments("github.com/invopop/jsonschema/examples", "./examples")
require.NoError(t, err, "did not expect error while adding comments")
return r
}
Expand Down