Skip to content

Commit 185c18c

Browse files
authored
Merge pull request #49 from rodionstolyarov/json-format
Add support for json output format
2 parents c6280ce + 2af50da commit 185c18c

File tree

8 files changed

+47
-10
lines changed

8 files changed

+47
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Config struct {
4848
* `-files` (glob string, *optional*) - File glob pattern to specify file names to process. Default is the single file with `go:generate`.
4949
* `-types` (glob string, *optional*) - Type glob pattern for type names to process. If not specified, the next type after `go:generate` is used.
5050
* `-output` (path string, **required**) - Output file name for generated documentation.
51-
* `-format` (`enum(markdown, plaintext, html, dotenv)` string, *optional*) - Output format for documentation. Default is `markdown`.
51+
* `-format` (`enum(markdown, plaintext, html, dotenv, json)` string, *optional*) - Output format for documentation. Default is `markdown`.
5252
* `-no-styles` (`bool`, *optional*) - If true, CSS styles will not be included for `html` format.
5353
* `-env-prefix` (`string`, *optional*) - Sets additional global prefix for all environment variables.
5454
* `-tag-name` (string, *optional*, default: `env`) - Use custom tag name instead of `env`.

_examples/simple/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package main
88
//go:generate go run ../../ -output doc.md -format markdown
99
//go:generate go run ../../ -output doc.html -format html
1010
//go:generate go run ../../ -output doc.env -format dotenv
11+
//go:generate go run ../../ -output doc.json -format json
1112
type Config struct {
1213
// Hosts name of hosts to listen on.
1314
Hosts []string `env:"HOST,required", envSeparator:";"`

_examples/simple/doc.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"env_name": "HOST",
4+
"doc": "Hosts name of hosts to listen on.",
5+
"env_separator": ";",
6+
"required": true
7+
},
8+
{
9+
"env_name": "PORT",
10+
"doc": "Port to listen on.",
11+
"required": true,
12+
"non_empty": true
13+
},
14+
{
15+
"env_name": "DEBUG",
16+
"doc": "Debug mode enabled.",
17+
"env_default": "false"
18+
},
19+
{
20+
"env_name": "PREFIX",
21+
"doc": "Prefix for something."
22+
}
23+
]

render/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ var configs = map[types.OutFormat]renderConfig{
6565
},
6666
tmpl: newTmplText("dotenv.tmpl"),
6767
},
68+
types.OutFormatJSON: {
69+
Item: renderItemConfig{},
70+
tmpl: newTmplText("json.tmpl"),
71+
},
6872
}

render/renderer.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ type renderSection struct {
4141
}
4242

4343
type renderItem struct {
44-
EnvName string
45-
Doc string
46-
EnvDefault string
47-
EnvSeparator string
44+
EnvName string `json:"env_name"`
45+
Doc string `json:"doc"`
46+
EnvDefault string `json:"env_default,omitempty"`
47+
EnvSeparator string `json:"env_separator,omitempty"`
4848

49-
Required bool
50-
Expand bool
51-
NonEmpty bool
52-
FromFile bool
49+
Required bool `json:"required,omitempty"`
50+
Expand bool `json:"expand,omitempty"`
51+
NonEmpty bool `json:"non_empty,omitempty"`
52+
FromFile bool `json:"from_file,omitempty"`
5353

5454
children []renderItem
55-
Indent int
55+
Indent int `json:"-"`
5656
}
5757

5858
func (i renderItem) Children(indentInc int) []renderItem {

render/templ/json.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{- range .Sections }}
2+
{{- marshalIndent .Items }}
3+
{{- end }}

render/templates.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package render
22

33
import (
44
"embed"
5+
"encoding/json"
56
"path"
67
"strings"
78

@@ -31,6 +32,10 @@ var tplFuncs = map[string]any{
3132
}
3233
return sum
3334
},
35+
"marshalIndent": func(v any) (string, error) {
36+
a, err := json.MarshalIndent(v, "", " ")
37+
return string(a), err
38+
},
3439
}
3540

3641
const (

types/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const (
88
OutFormatHTML OutFormat = "html"
99
OutFormatTxt OutFormat = "plaintext"
1010
OutFormatEnv OutFormat = "dotenv"
11+
OutFormatJSON OutFormat = "json"
1112
)
1213

1314
// EnvDocItem is a documentation item for one environment variable.

0 commit comments

Comments
 (0)