Skip to content

Commit

Permalink
Merge pull request #49 from rodionstolyarov/json-format
Browse files Browse the repository at this point in the history
Add support for json output format
  • Loading branch information
g4s8 authored Jan 9, 2025
2 parents c6280ce + 2af50da commit 185c18c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Config struct {
* `-files` (glob string, *optional*) - File glob pattern to specify file names to process. Default is the single file with `go:generate`.
* `-types` (glob string, *optional*) - Type glob pattern for type names to process. If not specified, the next type after `go:generate` is used.
* `-output` (path string, **required**) - Output file name for generated documentation.
* `-format` (`enum(markdown, plaintext, html, dotenv)` string, *optional*) - Output format for documentation. Default is `markdown`.
* `-format` (`enum(markdown, plaintext, html, dotenv, json)` string, *optional*) - Output format for documentation. Default is `markdown`.
* `-no-styles` (`bool`, *optional*) - If true, CSS styles will not be included for `html` format.
* `-env-prefix` (`string`, *optional*) - Sets additional global prefix for all environment variables.
* `-tag-name` (string, *optional*, default: `env`) - Use custom tag name instead of `env`.
Expand Down
1 change: 1 addition & 0 deletions _examples/simple/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
//go:generate go run ../../ -output doc.md -format markdown
//go:generate go run ../../ -output doc.html -format html
//go:generate go run ../../ -output doc.env -format dotenv
//go:generate go run ../../ -output doc.json -format json
type Config struct {
// Hosts name of hosts to listen on.
Hosts []string `env:"HOST,required", envSeparator:";"`
Expand Down
23 changes: 23 additions & 0 deletions _examples/simple/doc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"env_name": "HOST",
"doc": "Hosts name of hosts to listen on.",
"env_separator": ";",
"required": true
},
{
"env_name": "PORT",
"doc": "Port to listen on.",
"required": true,
"non_empty": true
},
{
"env_name": "DEBUG",
"doc": "Debug mode enabled.",
"env_default": "false"
},
{
"env_name": "PREFIX",
"doc": "Prefix for something."
}
]
4 changes: 4 additions & 0 deletions render/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ var configs = map[types.OutFormat]renderConfig{
},
tmpl: newTmplText("dotenv.tmpl"),
},
types.OutFormatJSON: {
Item: renderItemConfig{},
tmpl: newTmplText("json.tmpl"),
},
}
18 changes: 9 additions & 9 deletions render/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ type renderSection struct {
}

type renderItem struct {
EnvName string
Doc string
EnvDefault string
EnvSeparator string
EnvName string `json:"env_name"`
Doc string `json:"doc"`
EnvDefault string `json:"env_default,omitempty"`
EnvSeparator string `json:"env_separator,omitempty"`

Required bool
Expand bool
NonEmpty bool
FromFile bool
Required bool `json:"required,omitempty"`
Expand bool `json:"expand,omitempty"`
NonEmpty bool `json:"non_empty,omitempty"`
FromFile bool `json:"from_file,omitempty"`

children []renderItem
Indent int
Indent int `json:"-"`
}

func (i renderItem) Children(indentInc int) []renderItem {
Expand Down
3 changes: 3 additions & 0 deletions render/templ/json.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- range .Sections }}
{{- marshalIndent .Items }}
{{- end }}
5 changes: 5 additions & 0 deletions render/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package render

import (
"embed"
"encoding/json"
"path"
"strings"

Expand Down Expand Up @@ -31,6 +32,10 @@ var tplFuncs = map[string]any{
}
return sum
},
"marshalIndent": func(v any) (string, error) {
a, err := json.MarshalIndent(v, "", " ")
return string(a), err
},
}

const (
Expand Down
1 change: 1 addition & 0 deletions types/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
OutFormatHTML OutFormat = "html"
OutFormatTxt OutFormat = "plaintext"
OutFormatEnv OutFormat = "dotenv"
OutFormatJSON OutFormat = "json"
)

// EnvDocItem is a documentation item for one environment variable.
Expand Down

0 comments on commit 185c18c

Please sign in to comment.