Skip to content

Commit

Permalink
Add option to include the exported types (#95)
Browse files Browse the repository at this point in the history
* Add option  to include the exported types

* Add test packages with types
  • Loading branch information
romainbou authored Oct 19, 2020
1 parent 02292f3 commit ce92331
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/goreadme/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func init() {
flag.StringVar(&cfg.Title, "title", "", "Override readme title. Default is package name.")
flag.BoolVar(&cfg.RecursiveSubPackages, "recursive", false, "Load docs recursively.")
flag.BoolVar(&cfg.Functions, "functions", false, "Write functions section.")
flag.BoolVar(&cfg.Types, "types", false, "Write types section.")
flag.BoolVar(&cfg.SkipExamples, "skip-examples", false, "Skip the examples section.")
flag.BoolVar(&cfg.SkipSubPackages, "skip-sub-packages", false, "Skip the sub packages section.")
flag.BoolVar(&cfg.Badges.TravisCI, "badge-travisci", false, "Show TravisCI badge.")
Expand Down
18 changes: 18 additions & 0 deletions goreadme.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ type Config struct {
ImportPath string `json:"import_path"`
// Functions will make functions documentation to be added to the README.
Functions bool `json:"functions"`
// Types will make types documentation to be added to the README.
Types bool `json:"types"`
// SkipExamples will omit the examples section from the README.
SkipExamples bool `json:"skip_examples"`
// SkipSubPackages will omit the sub packages section from the README.
Expand Down Expand Up @@ -208,6 +210,22 @@ func (r *GoReadme) get(ctx context.Context, name string) (*pkg, error) {
}
}

// If types were not requested to be added to the readme, add their
// examples to the main readme.
if !r.config.Types {
for _, f := range p.Types {
for _, e := range f.Examples {
if e.Name == "" {
e.Name = f.Name
}
if e.Doc == "" {
e.Doc = f.Doc
}
p.Examples = append(p.Examples, e)
}
}
}

if p.IsCmd {
// TODO: make this better
p.Name = filepath.Base(name)
Expand Down
31 changes: 31 additions & 0 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package template
import (
"bytes"
"io"
"regexp"
"strings"
"text/template"

Expand All @@ -29,6 +30,11 @@ var base = template.New("base").Funcs(
"inlineCode": func(s string) string {
return "`" + s + "`"
},
"inlineCodeEllipsis": func(s string) string {
r := regexp.MustCompile(`\{[^[]*\}`)
s = r.ReplaceAllString(s, "{ ... }")
return "`" + s + "`"
},
"importPath": func(p *doc.Package) string {
return p.ImportPath
},
Expand Down Expand Up @@ -73,6 +79,10 @@ var main = template.Must(base.Parse(`# {{.Package.Name}}
{{ template "functions" .Package }}
{{ end }}
{{ if .Config.Types }}
{{ template "types" .Package }}
{{ end }}
{{ if (not .Config.SkipSubPackages) }}
{{ template "subpackages" . }}
{{ end }}
Expand Down Expand Up @@ -107,6 +117,27 @@ var functions = template.Must(base.Parse(`
{{ end }}
`))

var types = template.Must(base.Parse(`
{{ define "types" }}
{{ if .Types }}
## Types
{{ range .Types }}
### type [{{ .Name }}]({{ urlOrName (index $.Files .Pos.File) }}#L{{ .Pos.Line }})
{{ inlineCodeEllipsis .Decl.Text }}
{{ doc .Doc }}
{{ template "examplesNoHeading" .Examples }}
{{ end }}
{{ end }}
{{ end }}
`))

var examples = template.Must(base.Parse(`
{{ define "examples" }}
{{ if . }}
Expand Down
11 changes: 11 additions & 0 deletions testdata/pkg1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,14 @@ Func()
```
hello
```

### Assignment

ExampleExampleType tests using the type ExampleType

```golang

example := new(ExampleType)
example.val = 1

```
5 changes: 5 additions & 0 deletions testdata/pkg1/pkg1.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ import "fmt"
func Func() {
fmt.Println("hello")
}

type ExampleType struct {
val int
ExampleInterface interface{}
}
6 changes: 6 additions & 0 deletions testdata/pkg1/pkg1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ func ExampleFunc_withName() {
Func()
// Output: hello
}

// ExampleExampleType tests using the type ExampleType
func ExampleExampleType_assignment() {
example := new(ExampleType)
example.val = 1
}
22 changes: 22 additions & 0 deletions testdata/pkg8_types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# pkg1

Package pkg1 is a testing package.

## Types

### type [ExampleType](/pkg.go#L5)

`type ExampleType struct { ... }`

ExampleType is a type

### Assignment

ExampleExampleType tests using the type ExampleType

```golang

example := new(ExampleType)
example.val = 1

```
3 changes: 3 additions & 0 deletions testdata/pkg8_types/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module pkg6

go 1.15
3 changes: 3 additions & 0 deletions testdata/pkg8_types/goreadme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"types": true
}
8 changes: 8 additions & 0 deletions testdata/pkg8_types/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Package pkg1 is a testing package.
package pkg1

// ExampleType is a type
type ExampleType struct {
val int
ExampleInterface interface{}
}
7 changes: 7 additions & 0 deletions testdata/pkg8_types/pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pkg1

// ExampleExampleType tests using the type ExampleType
func ExampleExampleType_assignment() {
example := new(ExampleType)
example.val = 1
}

0 comments on commit ce92331

Please sign in to comment.