Skip to content

Instantly share code, notes, and snippets.

@tomtwinkle
Created April 5, 2021 06:30
Show Gist options
  • Save tomtwinkle/074bfc9cd4db53b58bb4ff368157f3ed to your computer and use it in GitHub Desktop.
Save tomtwinkle/074bfc9cd4db53b58bb4ff368157f3ed to your computer and use it in GitHub Desktop.
Generate Protocol Buffer Markdown with Validation
# Generate Protocol Buffer Markdown with Validation
<a name="top"></a>
## Services
{{- range .Files -}}
{{- $file := . -}}
{{- $srivicesLength := len $file.Services -}}
{{- if ne $srivicesLength 0 -}}
{{range $file.Services -}}
{{- $service := .}}
<a name="{{$service.FullName}}"></a>
### {{$service.Name}}
{{$service.Description}}
| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
{{- range $service.Methods}}
| {{.Name}} | [{{.RequestLongType}}](#{{.RequestFullType}}){{if .RequestStreaming}} stream{{end}} | [{{.ResponseLongType}}](#{{.ResponseFullType}}){{if .ResponseStreaming}} stream{{end}} | {{nobr .Description}} |
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- range .Files}}
{{- $file := .}}
---
> {{$file.Name}}
## Types
{{- range $file.Messages}}
{{- $message := .}}
<a name="{{$message.FullName}}"></a>
### {{$message.LongName}}
{{- range $file.Services}}
[Back](#{{.FullName}})
{{- end}}
{{$message.Description}}
{{- if $message.HasFields}}
| Field | Type | Label | Validations | Description |
| ----- | ---- | ----- | ----------- | ----------- |
{{- range $message.Fields}}
{{- $field := .}}
| {{$field.Name}} | [{{$field.LongType}}](#{{$field.FullType}}) | {{$field.Label}} |
{{- range $message.FieldOptions -}}
{{- $option := . -}}
{{- if eq $option "validator.field" "validate.rules" -}}
{{- range $i, $v := $message.FieldsWithOption $option -}}
{{- if eq $field.Name $v.Name -}}
{{range ($v.Option $option).Rules}}{{.Name}}:{{.Value}} {{end}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
| {{nobr .Description}}{{if .DefaultValue}} Default: {{.DefaultValue}}{{end}} |
{{- end}}
{{- end}}
{{- if $message.HasExtensions}}
| Extension | Type | Base | Number | Description |
| --------- | ---- | ---- | ------ | ----------- |
{{- range $message.Extensions}}
| {{.Name}} | {{.LongType}} | {{.ContainingLongType}} | {{.Number}} | {{nobr .Description}}{{if .DefaultValue}} Default: {{.DefaultValue}}{{end}} |
{{- end}}
{{- end}}
{{- end}}
---
## Enums
{{- range $file.Enums}}
{{- $enum := .}}
<a name="{{$enum.FullName}}"></a>
### {{$enum.LongName}}
{{- range $file.Services}}
[Back](#{{.FullName}})
{{- end}}
{{$enum.Description}}
| Name | Number | Description |
| ---- | ------ | ----------- |
{{- range $enum.Values}}
| {{.Name}} | {{.Number}} | {{nobr .Description}} |
{{- end}}
{{- end -}} <!-- end enums -->
{{if $file.HasExtensions}}
<a name="{{$file.Name}}-extensions"></a>
---
#### File-level Extensions
| Extension | Type | Base | Number | Description |
| --------- | ---- | ---- | ------ | ----------- |
{{- range $file.Extensions}}
| {{.Name}} | {{.LongType}} | {{.ContainingLongType}} | {{.Number}} | {{nobr .Description}}{{if .DefaultValue}} Default: `{{.DefaultValue}}`{{end}} |
{{- end}}
{{- end -}}
{{- end -}}
---
## Scalars
| .proto Type | Notes | Go Type | C++ Type | Java Type | Python Type |
| ----------- | ----- | -------- | -------- | --------- | ----------- |
{{range .Scalars -}}
| <a name="{{.ProtoType}}"/> {{.ProtoType}} | {{.Notes}} | {{.GoType}} | {{.CppType}} | {{.JavaType}} | {{.PythonType}} |
{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment