Skip to content

Commit 69f75b6

Browse files
committed
template: Add oneof support
1 parent ffd194b commit 69f75b6

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ func (o *GenOpts) templateFuncMap() template.FuncMap {
123123
typ := anchor(fmt.Sprint(f.Message.Desc.FullName()))
124124
return fmt.Sprintf(`{{< relref "%s#%s" >}}`, fn, typ)
125125
}
126+
if f.Enum != nil {
127+
fn := fmt.Sprint(f.Enum.Desc.ParentFile().Path())
128+
fn = filepath.Base(fn)
129+
fn = strings.TrimSuffix(fn, filepath.Ext(fn))
130+
typ := anchor(fmt.Sprint(f.Enum.Desc.FullName()))
131+
return fmt.Sprintf(`{{< relref "%s#%s" >}}`, fn, typ)
132+
}
126133
return fmt.Sprintf(`#%s`, anchor(f.Desc.FullName()))
127134
},
128135
"description": func(s interface{}) string {

templates/markdown.tmpl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ Message template
8484
{{if .Fields}}
8585
| Field | Type | Description |
8686
| ----- | ---- | ----------- |
87-
{{range .Fields}}{{template "field" .}}{{end}}
88-
{{end}}
87+
{{range .Fields}}{{ if (not .Desc.ContainingOneof) }}{{template "field" .}}{{end}}{{end}}
88+
{{- end -}}
89+
{{range .Oneofs}}{{template "oneof" .}}{{end}}
8990

9091
{{if .Extensions}}
9192
| Extension | Type | Base | Number | Description |
@@ -109,15 +110,23 @@ Message template
109110
Field template
110111
***************************************************************/}}
111112
{{define "field" -}}
112-
| {{.Desc.Name}} |
113+
| {{.Desc.Name}}{{ if .Desc.IsList }}[]{{ end }} |
113114
{{- if (is_primitive .) -}}
114-
{{ if .Desc.IsList }}repeated {{ end }}{{ field_type . }}
115+
{{ field_type . }}
115116
{{- else -}}
116-
[{{ .| field_type }}]({{ type_link . }})
117+
[{{ .| field_type }}]({{ type_link . }})
117118
{{- end -}}
118119
| {{ .Comments.Leading | description | nobr}} {{ .Comments.Trailing | description | nobr }} |
119120
{{end}}
120121

122+
{{/***************************************************************
123+
Oneof template
124+
This is kind of gross since GFM doesn't support colspan.
125+
***************************************************************/}}
126+
{{define "oneof" -}}
127+
|<tr><td colspan=2>Union field `{{ .Desc.Name }}`. {{ .Comments.Leading | description | nobr}} {{ .Comments.Trailing | description | nobr }} `{{ .Desc.Name }}` can be only one of the following:</td></tr>|
128+
{{range .Fields}}{{template "field" .}}{{end}}
129+
{{end}}
121130

122131
{{/***************************************************************
123132
Enum template

0 commit comments

Comments
 (0)