Skip to content

Commit

Permalink
Exported / NonExported support.
Browse files Browse the repository at this point in the history
Breaking changes! New default is to *only* include Exported symbols.

Added option --nonexported to render private symbols.
  • Loading branch information
mariotoffia committed Oct 21, 2020
1 parent dcf199b commit 6e26bdd
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 706 deletions.
52 changes: 26 additions & 26 deletions asciidoc/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var templateImports = `=== Imports
var templateFunctions = `== Functions
{{range .File.StructMethods}}
{{- if notreceiver $ .}}{{render $ .}}{{end}}
{{- if notreceiver $ .}}{{if or .Exported $.Config.Private }}{{render $ .}}{{end}}{{end}}
{{end}}
`

Expand All @@ -47,63 +47,63 @@ var templateInterface = `=== {{ .Interface.Name }}
[source, go]
----
{{.Interface.Decl}} {
{{- range .Interface.Methods}}
{{tabifylast .Decl}}
{{- range .Interface.Methods}}{{if or .Exported $.Config.Private }}
{{tabifylast .Decl}}{{end}}
{{- end}}
}
----
{{.Interface.Doc}}
{{range .Interface.Methods}}
{{range .Interface.Methods}}{{if or .Exported $.Config.Private }}
==== {{.Decl}}
{{.Doc}}
{{end}}
{{end}}{{end}}
`

var templateInterfaces = `== Interfaces
{{range .File.Interfaces}}
{{- render $ .}}
{{range .File.Interfaces}}{{if or .Exported $.Config.Private }}
{{- render $ .}}{{end}}
{{end}}
`

var templateStruct = `=== {{.Struct.Name}}
[source, go]
----
{{.Struct.Decl}} {
{{- range .Struct.Fields}}
{{if .Nested}}{{.Nested.Name}}{{"\t"}}struct{{else}}{{tabify .Decl}}{{end}}
{{- range .Struct.Fields}}{{if or .Exported $.Config.Private }}
{{if .Nested}}{{.Nested.Name}}{{"\t"}}struct{{else}}{{tabify .Decl}}{{end}}{{end}}
{{- end}}
}
----
{{.Struct.Doc}}
{{range .Struct.Fields}}{{if not .Nested}}
{{range .Struct.Fields}}{{if not .Nested}}{{if or .Exported $.Config.Private }}
==== {{.Decl}}
{{.Doc}}
{{- end}}
{{end}}
{{range .Struct.Fields}}{{if .Nested}}{{render $ .Nested}}{{end}}{{end}}
{{end}}{{end}}
{{range .Struct.Fields}}{{if or .Exported $.Config.Private }}{{if .Nested}}{{render $ .Nested}}{{end}}{{end}}{{end}}
{{if hasReceivers . .Struct.Name}}{{renderReceivers . .Struct.Name}}{{end}}
`

var templateStructs = `== Structs
{{range .File.Structs}}
{{- render $ .}}
{{range .File.Structs}}{{if or .Exported $.Config.Private }}
{{- render $ .}}{{end}}
{{end}}
`

var templateReceivers = `==== Receivers
{{range .Receiver}}
{{range .Receiver}}{{if or .Exported $.Config.Private }}
===== {{.Name}}
[source, go]
----
{{ .Decl }}
----
{{.Doc}}
{{end}}
{{end}}{{end}}
`

var templateCustomTypeDefintion = `=== {{.TypeDefVar.Name}}
Expand All @@ -119,8 +119,8 @@ var templateCustomTypeDefintion = `=== {{.TypeDefVar.Name}}

var templateCustomTypeDefintions = `== Variable Typedefinitions
{{range .File.CustomTypes}}
{{- render $ .}}
{{range .File.CustomTypes}}{{if or .Exported $.Config.Private }}
{{- render $ .}}{{end}}
{{end}}
`

Expand All @@ -133,8 +133,8 @@ var templateVarAssignment = `=== {{.VarAssignment.Name}}
`

var templateVarAssignments = `== Variables
{{range .File.VarAssignments}}
{{render $ .}}
{{range .File.VarAssignments}}{{if or .Exported $.Config.Private }}
{{render $ .}}{{end}}
{{end}}
`

Expand All @@ -150,13 +150,13 @@ var templateConstAssignments = `=== Constants
[source, go]
----
const (
{{- range .File.ConstAssignments}}
{{tabify .Decl}}
{{- range .File.ConstAssignments}}{{if or .Exported $.Config.Private }}
{{tabify .Decl}}{{end}}
{{- end}}
)
----
{{range .File.ConstAssignments}}
{{render $ .}}
{{range .File.ConstAssignments}}{{if or .Exported $.Config.Private }}
{{render $ .}}{{end}}
{{end}}
`

Expand All @@ -170,7 +170,7 @@ var templateCustomFuncDefintion = `=== {{.TypeDefFunc.Name}}

var templateCustomFuncDefintions = `== Function Definitions
{{range .File.CustomFuncs}}
{{render $ .}}
{{range .File.CustomFuncs}}{{if or .Exported $.Config.Private }}
{{render $ .}}{{end}}
{{end}}
`
10 changes: 10 additions & 0 deletions asciidoc/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Producer struct {
// overviewpaths is which paths to search for overview ascii doc document.
// It defaults to overview.adoc, _design/overview.adoc.
overviewpaths []string
// private when set to true all symbols are rendered.
private bool
}

// NewProducer creates a new instance of a producer.
Expand All @@ -52,6 +54,14 @@ func (p *Producer) StdOut() *Producer {
return p
}

// NonExported will set renderer to render all Symobols both
// exported and non exported. By default only exported symbols
// are rendered.
func (p *Producer) NonExported() *Producer {
p.private = true
return p
}

// Writer sets a custom writer where *everything* gets written to.
func (p *Producer) Writer(w io.Writer) *Producer {
p.writer = w
Expand Down
1 change: 1 addition & 0 deletions asciidoc/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (p *Producer) Generate() {
tc := t.NewContextWithConfig(&pkg.GoFile, pkg, &TemplateContextConfig{
IncludeMethodCode: false,
PackageOverviewPaths: overviewpaths,
Private: p.private,
})

if !indexdone {
Expand Down
38 changes: 38 additions & 0 deletions asciidoc/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,44 @@ func TestRenderSingleFunction(t *testing.T) {
assert.Equal(t, "=== Bar\n[source, go]\n----\nfunc Bar() int\n----\n\t\t\nBar is a public function that outputs\ncurrent time and return zero.", buf.String())
}

func TestIncludePrivateFunctions(t *testing.T) {
src := `
package mypkg
import (
"fmt"
"time"
)
type Kalle struct {
private int
Public string
}
// bar is a private function that outputs
// current time and return zero.
func bar() int {
fmt.Println(time.Now())
return 0
}
// This is always exported.
func ExportedFunc() {}`

m := dummyModule()
f, err := goparser.ParseInlineFile(m, m.Base+"/mypkg/file.go", src)
assert.NoError(t, err)

var buf bytes.Buffer

x := NewTemplateWithOverrides(nil).NewContext(f) //.RenderPrivate()

//x.RenderFunction(&buf, f.StructMethods[0])
//x.RenderFunction(&buf, f.StructMethods[1])
x.RenderStructs(&buf)

fmt.Println(buf.String())
}

func TestRenderSingleFunctionWithCode(t *testing.T) {
src := `
package mypkg
Expand Down
13 changes: 13 additions & 0 deletions asciidoc/templatecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type TemplateContextConfig struct {
//
// |===
PackageOverviewPaths []string
// Private indicates if it shall include private as well. By default only Exported is rendered.
Private bool
}

// IndexConfig is configuration to use when generating index template
Expand Down Expand Up @@ -186,6 +188,17 @@ func (t *TemplateContext) Creator() *Template {
return t.creator
}

// RenderPrivate will enable non exported to be rendered.
func (t *TemplateContext) RenderPrivate() *TemplateContext {

if nil == t.Config {
panic("Config is nil while trying to configure it!")
}

t.Config.Private = true
return t
}

// RenderPackage will render the package defintion onto the provided writer.
//
// Depending on if a package overview asciidoc document is found it will prioritize that before
Expand Down
Loading

0 comments on commit 6e26bdd

Please sign in to comment.