From 78a63b53c45953eb34494b78e687cb365f0e3124 Mon Sep 17 00:00:00 2001 From: Davide Nabais Date: Mon, 11 Sep 2023 15:34:07 +0100 Subject: [PATCH 1/3] - fallback to inline comment --- comment_extractor.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/comment_extractor.go b/comment_extractor.go index 0088b41..a4bf1ef 100644 --- a/comment_extractor.go +++ b/comment_extractor.go @@ -2,15 +2,14 @@ package jsonschema import ( "fmt" - "io/fs" - gopath "path" - "path/filepath" - "strings" - "go/ast" "go/doc" "go/parser" "go/token" + "io/fs" + gopath "path" + "path/filepath" + "strings" ) // ExtractGoComments will read all the go files contained in the provided path, @@ -69,6 +68,9 @@ func ExtractGoComments(base, path string, commentMap map[string]string) error { } case *ast.Field: txt := x.Doc.Text() + if txt == "" { + txt = x.Comment.Text() + } if typ != "" && txt != "" { for _, n := range x.Names { if ast.IsExported(n.String()) { From 93d9fcdc0e13cd60b1881f72f3d681a3d342dc9f Mon Sep 17 00:00:00 2001 From: Davide Nabais Date: Tue, 12 Sep 2023 17:16:31 +0100 Subject: [PATCH 2/3] - fix unit test --- examples/nested/nested.go | 4 +++- fixtures/go_comments.json | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/nested/nested.go b/examples/nested/nested.go index 886c5cf..bfdceb3 100644 --- a/examples/nested/nested.go +++ b/examples/nested/nested.go @@ -16,6 +16,8 @@ type ( // Plant represents the plants the user might have and serves as a test // of structs inside a `type` set. Plant struct { - Variant string `json:"variant" jsonschema:"title=Variant"` // This comment will be ignored + Variant string `json:"variant" jsonschema:"title=Variant"` // This comment will be used + // Multicellular is true if the plant is multicellular + Multicellular bool `json:"multicellular,omitempty" jsonschema:"title=Multicellular"` // This comment will be ignored } ) diff --git a/fixtures/go_comments.json b/fixtures/go_comments.json index 2e675cc..bfe5e61 100644 --- a/fixtures/go_comments.json +++ b/fixtures/go_comments.json @@ -36,7 +36,13 @@ "properties": { "variant": { "type": "string", - "title": "Variant" + "title": "Variant", + "description": "This comment will be used" + }, + "multicellular": { + "type": "boolean", + "title": "Multicellular", + "description": "Multicellular is true if the plant is multicellular" } }, "additionalProperties": false, From 2b4f56029248963b4812a88e5604b6cb22926992 Mon Sep 17 00:00:00 2001 From: Davide Nabais Date: Tue, 12 Sep 2023 17:23:14 +0100 Subject: [PATCH 3/3] - revert imports formatting --- comment_extractor.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/comment_extractor.go b/comment_extractor.go index a4bf1ef..e157837 100644 --- a/comment_extractor.go +++ b/comment_extractor.go @@ -2,14 +2,15 @@ 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" ) // ExtractGoComments will read all the go files contained in the provided path,