Add Config Clone

This commit is contained in:
Samuel Lorch 2023-04-01 18:15:37 +02:00
parent 29bfd7f708
commit f6bcffd8df

View file

@ -1,6 +1,7 @@
package definitions
import (
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
@ -14,6 +15,20 @@ type Config struct {
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)