Skip to content

Commit

Permalink
Implement suggestions; Change cfggen
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode committed Aug 23, 2021
1 parent 31d6978 commit cd9a223
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 156 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/efficientgo/tools/core v0.0.0-20210609125236-d73259166f20
github.com/efficientgo/tools/extkingpin v0.0.0-20210609125236-d73259166f20
github.com/felixge/fgprof v0.9.1
github.com/fatih/structtag v1.2.0
github.com/go-kit/kit v0.10.0
github.com/gobwas/glob v0.2.3
github.com/gocolly/colly/v2 v2.1.1-0.20201013153555-8252c346cfb0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/felixge/fgprof v0.9.1 h1:E6FUJ2Mlv043ipLOCFqo8+cHo9MhQ203E2cdEK/isEs=
github.com/felixge/fgprof v0.9.1/go.mod h1:7/HK6JFtFaARhIljgP2IV8rJLIoHDoOYoUphsnGvqxE=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
Expand Down
2 changes: 1 addition & 1 deletion pkg/mdformatter/linktransformer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

type Config struct {
Version int
Version int `yaml:"version"`

Validators []ValidatorConfig `yaml:"validators"`
Timeout string `yaml:"timeout"`
Expand Down
65 changes: 0 additions & 65 deletions pkg/yamlgen/cfggen.go

This file was deleted.

99 changes: 12 additions & 87 deletions pkg/yamlgen/yamlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,11 @@ func GenGoCode(src []byte) (string, error) {
jen.Qual("fmt", "Println").Call(jen.Lit("---")),
jen.Qual("fmt", "Println").Call(jen.Id("k")),
// TODO(saswatamcode): Replace with import from mdox itself once merged.
// jen.Qual("github.com/bwplotka/mdox/yamlgen", "Generate").Call(jen.Id("config"), jen.Qual("os", "Stderr")),
jen.Qual("structgen/cfggen", "Generate").Call(jen.Id("config"), jen.Qual("os", "Stderr")),
jen.Qual("github.com/bwplotka/mdox/pkg/yamlgen", "Generate").Call(jen.Id("config"), jen.Qual("os", "Stderr")),
))

// Generate main function in new module.
generatedCode.Func().Id("main").Params().Block(
init...,
)
generatedCode.Func().Id("main").Params().Block(init...)
return fmt.Sprintf("%#v", generatedCode), nil
}

Expand All @@ -123,23 +120,6 @@ func ExecGoCode(ctx context.Context, mainGo string) ([]byte, error) {
}
defer os.RemoveAll(tmpDir)

// TODO(saswatamcode): Remove once merged.
// This is weird but need it for getting Generate function inside tmpDir in PR.
// Once merged this can be removed and can be replaced with just an import in tmpDir/main.go.
err = os.Mkdir(filepath.Join(tmpDir, "cfggen"), 0700)
if err != nil {
return nil, err
}
code, err := os.Create(filepath.Join(tmpDir, "cfggen/cfggen.go"))
if err != nil {
return nil, err
}
defer code.Close()
_, err = code.Write([]byte(cfggenFile))
if err != nil {
return nil, err
}

// Copy generated code to main.go.
main, err := os.Create(filepath.Join(tmpDir, "main.go"))
if err != nil {
Expand All @@ -156,14 +136,22 @@ func ExecGoCode(ctx context.Context, mainGo string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "go", "mod", "init", "structgen")
cmd.Dir = tmpDir
if err := cmd.Run(); err != nil {
return nil, errors.Wrapf(err, "run %v", cmd)
return nil, errors.Wrapf(err, "mod init %v", cmd)
}

// Replace for unreleased mdox yamlgen so don't need to copy cfggen code to new dir and compile.
// Currently in github.com/saswatamcode/[email protected]. Replace once #79 is merged.
cmd = exec.CommandContext(ctx, "go", "mod", "edit", "-replace", "github.com/bwplotka/mdox=github.com/saswatamcode/[email protected]")
cmd.Dir = tmpDir
if err := cmd.Run(); err != nil {
return nil, errors.Wrapf(err, "mod edit %v", cmd)
}

// Import required packages(generate go.sum).
cmd = exec.CommandContext(ctx, "go", "mod", "tidy")
cmd.Dir = tmpDir
if err := cmd.Run(); err != nil {
return nil, errors.Wrapf(err, "run %v", cmd)
return nil, errors.Wrapf(err, "mod tidy %v", cmd)
}

// Execute generate code and return output.
Expand All @@ -178,66 +166,3 @@ func ExecGoCode(ctx context.Context, mainGo string) ([]byte, error) {

return b.Bytes(), nil
}

// TODO(saswatamcode): Remove once merged.
// This is weird but need it for getting Generate function inside tmpDir in PR.
// Could also do with commit hash and go.mod, but it would change on each commit in PR).
// Once merged this can be removed and can be replaced with just an import in tmpDir/main.go.
const cfggenFile = `package cfggen
import (
"io"
"reflect"
"github.com/fatih/structtag"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
func Generate(obj interface{}, w io.Writer) error {
// We forbid omitempty option. This is for simplification for doc generation.
if err := checkForOmitEmptyTagOption(obj); err != nil {
return errors.Wrap(err, "invalid type")
}
return yaml.NewEncoder(w).Encode(obj)
}
func checkForOmitEmptyTagOption(obj interface{}) error {
return checkForOmitEmptyTagOptionRec(reflect.ValueOf(obj))
}
func checkForOmitEmptyTagOptionRec(v reflect.Value) error {
switch v.Kind() {
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
tags, err := structtag.Parse(string(v.Type().Field(i).Tag))
if err != nil {
return errors.Wrapf(err, "%s: failed to parse tag %q", v.Type().Field(i).Name, v.Type().Field(i).Tag)
}
tag, err := tags.Get("yaml")
if err != nil {
return errors.Wrapf(err, "%s: failed to get tag %q", v.Type().Field(i).Name, v.Type().Field(i).Tag)
}
for _, opts := range tag.Options {
if opts == "omitempty" {
return errors.Errorf("omitempty is forbidden for config, but spotted on field '%s'", v.Type().Field(i).Name)
}
}
if err := checkForOmitEmptyTagOptionRec(v.Field(i)); err != nil {
return errors.Wrapf(err, "%s", v.Type().Field(i).Name)
}
}
case reflect.Ptr:
return errors.New("nil pointers are not allowed in configuration")
case reflect.Interface:
return checkForOmitEmptyTagOptionRec(v.Elem())
}
return nil
}
`

0 comments on commit cd9a223

Please sign in to comment.