chore: use go generate to create Validate and Merge funcs on RoleConfigs
This commit is contained in:
parent
28b7993be4
commit
c9e93802eb
10 changed files with 236 additions and 200 deletions
196
internal/codegen/roleconfig/main.go
Normal file
196
internal/codegen/roleconfig/main.go
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/packages"
|
||||
"golang.org/x/tools/imports"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultMerge = `
|
||||
if s.%[1]s != %[2]s {
|
||||
s.%[1]s = other.%[1]s
|
||||
}
|
||||
`
|
||||
defaultValidator = `
|
||||
if s.%[1]s == %[2]s {
|
||||
return errors.New("missing %[3]s")
|
||||
}
|
||||
`
|
||||
ipValidator = `
|
||||
if net.ParseIP(s.%[1]s) == nil {
|
||||
return fmt.Errorf("invalid %[3]s: %%q", s.%[1]s)
|
||||
}
|
||||
`
|
||||
positiveValidator = `
|
||||
if s.%[1]s < 1 {
|
||||
return fmt.Errorf("invalid %[3]s: %%q", s.%[1]s)
|
||||
}
|
||||
`
|
||||
)
|
||||
|
||||
var (
|
||||
structName string
|
||||
packageName string
|
||||
fileName string
|
||||
)
|
||||
|
||||
type Validator struct {
|
||||
code string
|
||||
}
|
||||
|
||||
type Field struct {
|
||||
name string
|
||||
prettyName string
|
||||
defaultValue string
|
||||
validators []string
|
||||
}
|
||||
|
||||
func NewField(name, prettyName, defaultValue, tag string) *Field {
|
||||
validators := make([]string, 0, 2)
|
||||
|
||||
switch tag {
|
||||
case "":
|
||||
validators = append(validators, defaultValidator)
|
||||
case "ip":
|
||||
validators = append(validators, defaultValidator, ipValidator)
|
||||
case "positive":
|
||||
validators = append(validators, positiveValidator)
|
||||
case "no":
|
||||
break
|
||||
default:
|
||||
panic(fmt.Sprintf("invalid tag %v", tag))
|
||||
}
|
||||
|
||||
return &Field{
|
||||
name: name,
|
||||
prettyName: prettyName,
|
||||
defaultValue: defaultValue,
|
||||
validators: validators,
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
namePtr := flag.String("name", "", "name of the struct")
|
||||
flag.Parse()
|
||||
if namePtr == nil || *namePtr == "" {
|
||||
fmt.Fprintln(os.Stderr, "Invalid name")
|
||||
os.Exit(1)
|
||||
}
|
||||
structName = *namePtr
|
||||
|
||||
packageName = os.Getenv("GOPACKAGE")
|
||||
|
||||
fileName = fmt.Sprintf("%s_gen.go", strings.TrimSuffix(os.Getenv("GOFILE"), ".go"))
|
||||
}
|
||||
|
||||
func defaultValue(t types.Type) string {
|
||||
b, ok := t.Underlying().(*types.Basic)
|
||||
if !ok {
|
||||
return "nil"
|
||||
}
|
||||
|
||||
switch b.Kind() {
|
||||
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
|
||||
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
|
||||
return "0"
|
||||
case types.String:
|
||||
return `""`
|
||||
case types.Bool:
|
||||
return "false"
|
||||
default:
|
||||
return "nil"
|
||||
}
|
||||
}
|
||||
|
||||
type Package struct {
|
||||
fields []*Field
|
||||
imports map[string]struct{}
|
||||
}
|
||||
|
||||
func enumerateFields(root *types.Struct) []*Field {
|
||||
fields := make([]*Field, 0, 16)
|
||||
|
||||
for i := range root.NumFields() {
|
||||
field := root.Field(i)
|
||||
tag := reflect.StructTag(root.Tag(i))
|
||||
|
||||
extraValidators := tag.Get("gen")
|
||||
prettyName := tag.Get("toml")
|
||||
if embed, ok := field.Type().Underlying().(*types.Struct); ok {
|
||||
fields = append(fields, enumerateFields(embed)...)
|
||||
} else {
|
||||
fields = append(fields, NewField(field.Name(), prettyName, defaultValue(field.Type()), extraValidators))
|
||||
}
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
func writeMerge(w io.Writer, name string, fields []*Field) {
|
||||
fmt.Fprintf(w, "func (s *%[1]s) Merge(other %[1]s) {", name)
|
||||
for _, f := range fields {
|
||||
fmt.Fprintf(w, defaultMerge, f.name, f.defaultValue)
|
||||
}
|
||||
fmt.Fprintf(w, "}\n\n")
|
||||
}
|
||||
|
||||
func writeValidators(w io.Writer, name string, fields []*Field) {
|
||||
fmt.Fprintf(w, "func (s %s) Validate() error {", name)
|
||||
for _, f := range fields {
|
||||
for _, v := range f.validators {
|
||||
fmt.Fprintf(w, v, f.name, f.defaultValue, f.prettyName)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, "\nreturn nil\n}\n\n")
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg := &packages.Config{
|
||||
Mode: packages.NeedName |
|
||||
packages.NeedTypes |
|
||||
packages.NeedTypesInfo |
|
||||
packages.NeedSyntax |
|
||||
packages.NeedFiles,
|
||||
}
|
||||
pkgs, _ := packages.Load(cfg, ".")
|
||||
config := *pkgs[0]
|
||||
for _, v := range config.Syntax {
|
||||
ast.Inspect(v, func(node ast.Node) bool {
|
||||
decl, ok := node.(*ast.GenDecl)
|
||||
if !ok || decl.Tok != token.TYPE {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, spec := range decl.Specs {
|
||||
ts := spec.(*ast.TypeSpec)
|
||||
if ts.Name.String() != structName {
|
||||
continue
|
||||
}
|
||||
|
||||
t := config.TypesInfo.TypeOf(ts.Type.(*ast.StructType)).(*types.Struct)
|
||||
fields := enumerateFields(t)
|
||||
|
||||
buf := bytes.NewBuffer(make([]byte, 0))
|
||||
fmt.Fprintf(buf, "// Code generated by roleconfig; DO NOT EDIT.\n\npackage %s\n", packageName)
|
||||
writeValidators(buf, structName, fields)
|
||||
writeMerge(buf, structName, fields)
|
||||
x, _ := imports.Process(fileName, buf.Bytes(), nil)
|
||||
os.WriteFile(fileName, x, 0644)
|
||||
|
||||
break
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +1,8 @@
|
|||
package config
|
||||
|
||||
//go:generate -command roleconfig go run git.wzray.com/homelab/hivemind/internal/codegen/roleconfig
|
||||
//go:generate roleconfig -name DnsConfig
|
||||
type DnsConfig struct {
|
||||
UseSystemd bool `toml:"use_systemd"`
|
||||
UseSystemd bool `toml:"use_systemd" gen:"no"`
|
||||
baseRoleConfig
|
||||
}
|
||||
|
||||
func (c DnsConfig) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *DnsConfig) Merge(other DnsConfig) {
|
||||
if other.set {
|
||||
c.set = other.set
|
||||
}
|
||||
|
||||
if other.UseSystemd {
|
||||
c.UseSystemd = other.UseSystemd
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,79 +1,13 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
//go:generate -command roleconfig go run git.wzray.com/homelab/hivemind/internal/codegen/roleconfig
|
||||
//go:generate roleconfig -name HostConfig
|
||||
type HostConfig struct {
|
||||
Domain string `toml:"domain"`
|
||||
IpAddress string `toml:"ip"`
|
||||
IpAddress string `toml:"ip" gen:"ip"`
|
||||
LocalAddress string `toml:"local_address"`
|
||||
InternalEntrypoint string `toml:"internal_entrypoint"`
|
||||
ExternalEntrypoint string `toml:"external_entrypoint"`
|
||||
ListenAddress string `toml:"listen_address"`
|
||||
baseRoleConfig
|
||||
}
|
||||
|
||||
func (c HostConfig) Validate() error {
|
||||
if c.Domain == "" {
|
||||
return errors.New("missing domain")
|
||||
}
|
||||
|
||||
if c.IpAddress == "" {
|
||||
return errors.New("missing ip")
|
||||
}
|
||||
|
||||
if net.ParseIP(c.IpAddress) == nil {
|
||||
return fmt.Errorf("invalid ip: %q", c.IpAddress)
|
||||
}
|
||||
|
||||
if c.LocalAddress == "" {
|
||||
return errors.New("missing local_address")
|
||||
}
|
||||
|
||||
if c.InternalEntrypoint == "" {
|
||||
return errors.New("missing internal_entrypoint")
|
||||
}
|
||||
|
||||
if c.ExternalEntrypoint == "" {
|
||||
return errors.New("missing external_entrypoint")
|
||||
}
|
||||
|
||||
if c.ListenAddress == "" {
|
||||
return errors.New("missing listen_address")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *HostConfig) Merge(other HostConfig) {
|
||||
if other.set {
|
||||
c.set = other.set
|
||||
}
|
||||
|
||||
if other.Domain != "" {
|
||||
c.Domain = other.Domain
|
||||
}
|
||||
|
||||
if other.IpAddress != "" {
|
||||
c.IpAddress = other.IpAddress
|
||||
}
|
||||
|
||||
if other.LocalAddress != "" {
|
||||
c.LocalAddress = other.LocalAddress
|
||||
}
|
||||
|
||||
if other.InternalEntrypoint != "" {
|
||||
c.InternalEntrypoint = other.InternalEntrypoint
|
||||
}
|
||||
|
||||
if other.ExternalEntrypoint != "" {
|
||||
c.ExternalEntrypoint = other.ExternalEntrypoint
|
||||
}
|
||||
|
||||
if other.ListenAddress != "" {
|
||||
c.ListenAddress = other.ListenAddress
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,54 +1,12 @@
|
|||
package config
|
||||
|
||||
import "errors"
|
||||
|
||||
//go:generate -command roleconfig go run git.wzray.com/homelab/hivemind/internal/codegen/roleconfig
|
||||
//go:generate roleconfig -name MasterConfig
|
||||
type MasterConfig struct {
|
||||
ObserverInterval int `toml:"observer_interval"`
|
||||
BackoffSeconds int `toml:"backoff_seconds"`
|
||||
BackoffCount int `toml:"backoff_count"`
|
||||
NodeTimeout int `toml:"node_timeout"`
|
||||
ObserverInterval int `toml:"observer_interval" gen:"positive"`
|
||||
BackoffSeconds int `toml:"backoff_seconds" gen:"positive"`
|
||||
BackoffCount int `toml:"backoff_count" gen:"positive"`
|
||||
NodeTimeout int `toml:"node_timeout" gen:"positive"`
|
||||
|
||||
baseRoleConfig
|
||||
}
|
||||
|
||||
func (c MasterConfig) Validate() error {
|
||||
if c.ObserverInterval < 1 {
|
||||
return errors.New("invalid observer_interval")
|
||||
}
|
||||
|
||||
if c.BackoffSeconds < 1 {
|
||||
return errors.New("invalid backoff_seconds")
|
||||
}
|
||||
|
||||
if c.BackoffCount < 1 {
|
||||
return errors.New("invalid backoff_count")
|
||||
}
|
||||
|
||||
if c.NodeTimeout < 1 {
|
||||
return errors.New("invalid node_timeout")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MasterConfig) Merge(other MasterConfig) {
|
||||
if other.set {
|
||||
c.set = true
|
||||
}
|
||||
|
||||
if other.ObserverInterval != 0 {
|
||||
c.ObserverInterval = other.ObserverInterval
|
||||
}
|
||||
|
||||
if other.BackoffSeconds != 0 {
|
||||
c.BackoffSeconds = other.BackoffSeconds
|
||||
}
|
||||
|
||||
if other.BackoffCount != 0 {
|
||||
c.BackoffCount = other.BackoffCount
|
||||
}
|
||||
|
||||
if other.NodeTimeout != 0 {
|
||||
c.NodeTimeout = other.NodeTimeout
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -28,68 +26,16 @@ func (l *LogLevel) UnmarshalText(data []byte) error {
|
|||
}
|
||||
}
|
||||
|
||||
//go:generate -command roleconfig go run git.wzray.com/homelab/hivemind/internal/codegen/roleconfig
|
||||
//go:generate roleconfig -name NodeConfig
|
||||
type NodeConfig struct {
|
||||
Hostname string `toml:"hostname"`
|
||||
Address string `toml:"address"`
|
||||
Port int `toml:"port"`
|
||||
|
||||
KeepaliveInterval int `toml:"keepalive_interval"`
|
||||
KeepaliveInterval int `toml:"keepalive_interval" gen:"positive"`
|
||||
LogLevel LogLevel `toml:"log_level"`
|
||||
|
||||
BootstrapMaster string `toml:"bootstrap_master"`
|
||||
ListenOn string `toml:"listen_on"`
|
||||
}
|
||||
|
||||
func (c NodeConfig) Validate() error {
|
||||
if c.Address == "" {
|
||||
return errors.New("missing address")
|
||||
}
|
||||
|
||||
if c.Hostname == "" {
|
||||
return errors.New("missing hostname")
|
||||
}
|
||||
|
||||
if c.KeepaliveInterval < 1 {
|
||||
return errors.New("invalid keepalive_interval")
|
||||
}
|
||||
|
||||
if c.ListenOn == "" {
|
||||
return errors.New("missing listen_on")
|
||||
}
|
||||
|
||||
if net.ParseIP(c.ListenOn) == nil {
|
||||
return fmt.Errorf("invalid listen_on: %v", c.ListenOn)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *NodeConfig) Merge(other NodeConfig) {
|
||||
if other.Hostname != "" {
|
||||
c.Hostname = other.Hostname
|
||||
}
|
||||
|
||||
if other.Address != "" {
|
||||
c.Address = other.Address
|
||||
}
|
||||
|
||||
if other.BootstrapMaster != "" {
|
||||
c.BootstrapMaster = other.BootstrapMaster
|
||||
}
|
||||
|
||||
if other.ListenOn != "" {
|
||||
c.ListenOn = other.ListenOn
|
||||
}
|
||||
|
||||
if other.Port != 0 {
|
||||
c.Port = other.Port
|
||||
}
|
||||
|
||||
if other.KeepaliveInterval != 0 {
|
||||
c.KeepaliveInterval = other.KeepaliveInterval
|
||||
}
|
||||
|
||||
if other.LogLevel != "" {
|
||||
c.LogLevel = other.LogLevel
|
||||
}
|
||||
BootstrapMaster string `toml:"bootstrap_master" gen:"no"`
|
||||
ListenOn string `toml:"listen_on" gen:"ip"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
package config
|
||||
|
||||
type baseRoleConfig struct {
|
||||
set bool
|
||||
set bool `gen:"no"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue