Skip to content
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 NoSynopsis option to AddGoComments #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 27 additions & 2 deletions comment_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ import (
"go/token"
)

// ExtractGoCommentsOption represents an option that can be passed to ExtractGoComments
// in order to modify its behavior.
type ExtractGoCommentsOption func(e *extractOptions)
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

I do like this approach for option handling! But I think at the moment it is too inconsistent with the current approach (unfortunately). I have a plan in mind to refactor all configuration towards config methods.


// NoSynopsis prevents ExtractGoComments from calling doc.Synopsis on the comment text.
// This is used to preserve the full text of the original comment, including newlines.
//
// Example usage:
// if err := r.AddGoComments("github.com/example/wails", "./schema/", jsonschema.NoSynopsis()); err != nil { ... }
func NoSynopsis() ExtractGoCommentsOption {
return func(e *extractOptions) {
e.noSynopsis = true
}
}

type extractOptions struct {
noSynopsis bool
}

// ExtractGoComments will read all the go files contained in the provided path,
// including sub-directories, in order to generate a dictionary of comments
// associated with Types and Fields. The results will be added to the `commentsMap`
Expand All @@ -24,7 +43,11 @@ 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, path string, commentMap map[string]string, opts ...ExtractGoCommentsOption) error {
eopts := &extractOptions{}
for _, opt := range opts {
opt(eopts)
}
fset := token.NewFileSet()
dict := make(map[string][]*ast.Package)
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
Expand Down Expand Up @@ -64,7 +87,9 @@ func ExtractGoComments(base, path string, commentMap map[string]string) error {
txt = gtxt
gtxt = ""
}
txt = doc.Synopsis(txt)
if !eopts.noSynopsis {
txt = doc.Synopsis(txt)
}
commentMap[fmt.Sprintf("%s.%s", pkg, typ)] = strings.TrimSpace(txt)
}
case *ast.Field:
Expand Down
53 changes: 53 additions & 0 deletions comment_extractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package jsonschema

import (
"testing"
)

const (
baseURL = "github.com/invopop/jsonschema"
)

// WantStruct is a test struct
// for testing reflection and
// ensuring that comment newlines
// are preserved when using
// jsonschema.NoSynopsis().
type WantStruct struct {
}

func TestDescription(t *testing.T) {
r := new(Reflector)
r.DoNotReference = true
if err := r.AddGoComments(baseURL, "./"); err != nil {
t.Fatal(err)
}
v := &WantStruct{}
s := r.Reflect(v)

want := `WantStruct is a test struct for testing reflection and ensuring that comment newlines are preserved when using jsonschema.NoSynopsis().`

if got := s.Description; got != want {
t.Errorf("s.Description =\n%v\nwant:\n%v", got, want)
}
}

func TestDescription_NoSynopsis(t *testing.T) {
r := new(Reflector)
r.DoNotReference = true
if err := r.AddGoComments(baseURL, "./", NoSynopsis()); err != nil {
t.Fatal(err)
}
v := &WantStruct{}
s := r.Reflect(v)

want := `WantStruct is a test struct
for testing reflection and
ensuring that comment newlines
are preserved when using
jsonschema.NoSynopsis().`

if got := s.Description; got != want {
t.Errorf("s.Description =\n%v\nwant:\n%v", got, want)
}
}
4 changes: 2 additions & 2 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,9 +1125,9 @@ func fullyQualifiedTypeName(t reflect.Type) string {
// AddGoComments will update the reflectors comment map with all the comments
// found in the provided source directories. See the #ExtractGoComments method
// for more details.
func (r *Reflector) AddGoComments(base, path string) error {
func (r *Reflector) AddGoComments(base, path string, opts ...ExtractGoCommentsOption) error {
if r.CommentMap == nil {
r.CommentMap = make(map[string]string)
}
return ExtractGoComments(base, path, r.CommentMap)
return ExtractGoComments(base, path, r.CommentMap, opts...)
}