Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GenDocs method to drivers and Manager #9

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'

- name: Build
run: go build -v ./...
Expand Down
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func main() {
if err != nil {
panic(err)
}
fmt.Println(config.GenDoc("yaml"))
err = config.Parse(&c)
if err != nil {
panic(err)
Expand Down
87 changes: 84 additions & 3 deletions drivers/env/env.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package env

import (
"cmp"
"fmt"
"os"

"github.com/insei/tinyconf"
"reflect"
"slices"
"strings"

"github.com/insei/cast"
"github.com/insei/fmap/v2"
"github.com/insei/fmap/v3"
"github.com/insei/tinyconf"
)

type envDriver struct {
Expand All @@ -34,6 +37,84 @@ func (d envDriver) GetName() string {
return d.name
}

type field struct {
path string
value any
depth int
tag reflect.StructTag
}

func (f field) genDoc(driver string) string {
if tagDriver, ok := f.tag.Lookup(driver); ok {
tagDoc := f.tag.Get("doc")
return fmt.Sprintf("#%s\n#%s=%v\n", tagDoc, tagDriver, f.value)
}
return ""
}

func (d envDriver) getUniqueFields(registers []tinyconf.Registered) []field {
var fields []field
for _, register := range registers {
for _, path := range register.Storage.GetAllPaths() {
fld := register.Storage.MustFind(path)

tag := fld.GetTag()
tagDriver, ok := tag.Lookup(d.name)
if !ok {
continue
}

member := field{
path: strings.Split(tagDriver, "_")[0],
value: fld.Get(register.Config),
depth: strings.Count(tagDriver, "_"),
tag: tag,
}

if slices.ContainsFunc(fields, func(item field) bool {
return item.tag.Get(d.name) == member.tag.Get(d.name)
}) {
continue
}
fields = append(fields, member)
}
}
return fields
}

func (d envDriver) getRootMap(fields []field) map[int]map[string]string {
roots := make(map[int]map[string]string)
root := make(map[string]string)

for _, field := range fields {
root[field.path] += field.genDoc(d.name)
roots[field.depth] = root
}
return roots
}

func (d envDriver) GenDoc(registers ...tinyconf.Registered) string {
uniqueFields := d.getUniqueFields(registers)

sortedFields := slices.Clone(uniqueFields)
slices.SortStableFunc(sortedFields, func(i, j field) int {
return cmp.Compare(j.depth, i.depth)
})

roots := d.getRootMap(sortedFields)
marks := make([]string, 0)

var doc string
for _, field := range uniqueFields {
if slices.Contains(marks, field.path) {
continue
}
marks = append(marks, field.path)
doc += roots[field.depth][field.path] + "\n"
}
return doc
}

func New() (tinyconf.Driver, error) {
return envDriver{
name: "env",
Expand Down
Loading
Loading