generator

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TemplateConfig = template.Must(template.New("").Parse(`
// Code generated by "go-gen-config"; DO NOT EDIT.

package config

import (
	"fmt"
	"os"

	"github.com/evald24/go-gen-config/pkg/helpers"
	"gopkg.in/yaml.v3"
)

{{define "struct_item"}}{{.Name}} {{if .IsEnum}}{{.Type}}{{else}}{{.Type}}{{end}}{{.Tags}}{{end}}

// Conifg - Basic structure with configuration
type Config struct {
{{range .}}  // {{.Name}} - {{.Description}}
	{{template "struct_item" .}}
{{end}}}



{{define "enum"}}{{$item := .}}
	// {{.Type}} - {{.Description}}
	type {{.Type}} = string
	const (
		{{range $j, $enum := .Enums}}// {{.Name}} - {{$item.Description}}
		{{$enum.Name}} {{if eq $j 0}} {{$item.Type}} = "{{$enum.Value}}"{{else}} = "{{$enum.Value}}"{{end}}
	{{end}})
{{end}}

{{define "struct"}}
	// {{.Type}} - {{.Description}}
	type {{.Type}} struct {
	{{range .Items}}// {{.Name}} - {{.Description}}
		{{template "struct_item" .}}
	{{end}}}

	{{template "gen" .Items}}
{{end}}

{{define "gen"}}
	{{range .}}
		{{if .IsEnum}}
			{{template "enum" .}}
		{{end}}
		{{if .IsStruct}}
			{{template "struct" .}}
		{{end}}
	{{end}}
{{end}}

{{template "gen" .}}

// GetConfig - get the configuration
func GetConfig() *Config {
	return cfg
}

var fileConfig string
var cfg *Config

// Init - initializing the configuration
func Init(configPath string) (*Config, error) {
	fileConfig = configPath

	if cfg != nil {
		return nil, fmt.Errorf("The configuration has already been initialized")
	}

	if err := UpdateConfig(); err != nil {
		return nil, fmt.Errorf("Configuration initialization failed: %v", err)
	}

	return cfg, nil
}

// UpdateConfig - Updates the configuration by rereading
func UpdateConfig() error {
	file, err := os.Open(fileConfig)
	if err != nil {
		return err
	}
	defer file.Close()

	decoder := yaml.NewDecoder(file)
	if err := decoder.Decode(&cfg); err != nil {
		return err
	}

	// read environment and replace
	return helpers.ReadEnvAndSet(cfg)
}

`))

Functions

func New

func New(templatePath, outputPath, configPath string) *generator

Types

type ConfigItem

type ConfigItem struct {
	Name        string
	Description string
	Type        string
	Tags        string
	Default     string

	IsNumber bool
	IsEnum   bool
	IsStruct bool
	Env      string
	Enums    []EnumKV
	Items    map[string]ConfigItem
}

type EnumKV

type EnumKV struct {
	Name  string
	Value string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL