mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
Seperate Definitions by Sub System
This commit is contained in:
parent
14c8da64cc
commit
72bf96295d
31 changed files with 120 additions and 108 deletions
56
internal/definitions/config/config.go
Normal file
56
internal/definitions/config/config.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/internal/definitions/firewall"
|
||||
"nfsense.net/nfsense/internal/definitions/network"
|
||||
"nfsense.net/nfsense/internal/definitions/object"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ConfigVersion uint64 `json:"config_version" validate:"required,eq=1"`
|
||||
Firewall firewall.Firewall `json:"firewall" validate:"required,dive"`
|
||||
Object object.Object `json:"object" validate:"required,dive"`
|
||||
Network network.Network `json:"network" validate:"required,dive"`
|
||||
}
|
||||
|
||||
// Clone TODO find a better way to deep copy
|
||||
func (c *Config) Clone() *Config {
|
||||
data, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Marshal Error: %w", err))
|
||||
}
|
||||
var clone Config
|
||||
err = json.Unmarshal(data, &clone)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Unmarshal Error: %w", err))
|
||||
}
|
||||
return &clone
|
||||
}
|
||||
|
||||
func ValidateConfig(conf *Config) error {
|
||||
val := validator.New()
|
||||
val.RegisterValidation("test", nilIfOtherNil)
|
||||
return val.Struct(conf)
|
||||
}
|
||||
|
||||
func nilIfOtherNil(fl validator.FieldLevel) bool {
|
||||
slog.Info("Start", "field", fl.FieldName(), "param", fl.Param())
|
||||
if !fl.Field().IsNil() {
|
||||
slog.Info("Field is not nil", "field", fl.FieldName())
|
||||
f := fl.Parent().FieldByName(fl.Param())
|
||||
if f.IsZero() {
|
||||
panic(fmt.Errorf("Param %v is not a Valid Field", fl.Param()))
|
||||
}
|
||||
if !f.IsNil() {
|
||||
slog.Info("Fail", "field", fl.FieldName(), "param", fl.Param())
|
||||
return false
|
||||
}
|
||||
}
|
||||
slog.Info("Success", "field", fl.FieldName(), "param", fl.Param())
|
||||
return true
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue