Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
remove configuration model in prebuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 16, 2019
1 parent 79ec5a8 commit cffb24d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 51 deletions.
47 changes: 0 additions & 47 deletions EXPERIMENTAL/typicmd/prebuilder/configuration.go

This file was deleted.

38 changes: 34 additions & 4 deletions EXPERIMENTAL/typicmd/prebuilder/configuration_generator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package prebuilder

import (
"fmt"
"reflect"

"github.com/typical-go/typical-rest-server/EXPERIMENTAL/bash"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typicmd/prebuilder/golang"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typicmd/prebuilder/walker"
Expand All @@ -24,23 +27,50 @@ func (g *ConfigurationGenerator) Generate() (err error) {

func (g *ConfigurationGenerator) generate() (err error) {
defer elapsed("Generate Configuration")()
conf := createConfiguration(g.Context)
model, contructors := g.create()
pkg := typienv.Dependency.Package
src := golang.NewSourceCode(pkg).AddStruct(conf.Struct)
src := golang.NewSourceCode(pkg).AddStruct(model)
src.AddImport("", "github.com/kelseyhightower/envconfig")
for _, imp := range g.ContextFile.Imports {
src.AddImport(imp.Name, imp.Path)
}
src.AddConstructors(conf.Constructors...)
src.AddConstructors(contructors...)
target := dependency + "/configurations.go"
err = src.Cook(target)
if err != nil {
return
}
return bash.GoImports(target)
return
}

func (g *ConfigurationGenerator) check() bool {
return true
}

func (g *ConfigurationGenerator) create() (model golang.Struct, constructors []string) {
structName := "Config"
model.Name = structName
constructors = append(constructors, g.configDef())
for _, acc := range g.ConfigAccessors() {
key := acc.GetKey()
typ := reflect.TypeOf(acc.GetConfigSpec()).String()
model.AddField(key, typ)
constructors = append(constructors, g.subConfigDef(key, typ))

}
return
}

func (g *ConfigurationGenerator) configDef() string {
return `func() (*Config, error) {
var cfg Config
err := envconfig.Process("", &cfg)
return &cfg, err
}`
}

func (g *ConfigurationGenerator) subConfigDef(name, typ string) string {
return fmt.Sprintf(`func(cfg *Config) %s {
return cfg.%s
}`, typ, name)
}

0 comments on commit cffb24d

Please sign in to comment.