From 197ddceac7d4c683d285b5371852f96abfab9b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20K=C3=B6ser?= Date: Fri, 9 Aug 2024 23:35:40 +0200 Subject: [PATCH] docs(examples): apply latest changes --- examples/complex/main.go | 7 +++---- examples/koanf/main.go | 6 +++--- examples/simple/main.go | 12 ++++++------ examples/viper/main.go | 6 +++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/examples/complex/main.go b/examples/complex/main.go index 1fc859e..c4fce47 100644 --- a/examples/complex/main.go +++ b/examples/complex/main.go @@ -1,7 +1,7 @@ package main import ( - "fmt" + "log" "time" "github.com/nikoksr/konfetty" @@ -92,7 +92,7 @@ func main() { } // Use konfetty to process the config - cfg, err := konfetty.FromConfig(cfg). + cfg, err := konfetty.FromStruct(cfg). WithDefaults( // Default for all BaseDevice instances BaseDevice{ @@ -141,8 +141,7 @@ func main() { }). Build() if err != nil { - fmt.Printf("Error processing config: %v\n", err) - return + log.Fatalf("Error building config: %v", err) } // The processed config would look like this: diff --git a/examples/koanf/main.go b/examples/koanf/main.go index 3a4da12..ee01d98 100644 --- a/examples/koanf/main.go +++ b/examples/koanf/main.go @@ -1,7 +1,7 @@ package main import ( - "fmt" + "errors" "log" "github.com/knadh/koanf/parsers/yaml" @@ -51,7 +51,7 @@ func main() { // Simply pass the config to konfetty and let it handle the rest. // - cfg, err = konfetty.FromConfig(cfg). + cfg, err = konfetty.FromStruct(cfg). WithDefaults( AppConfig{ Database: DatabaseConfig{ @@ -71,7 +71,7 @@ func main() { }). WithValidator(func(c *AppConfig) error { if c.Database.Username == "" || c.Database.Password == "" { - return fmt.Errorf("database credentials are required") + return errors.New("database credentials are required") } return nil }). diff --git a/examples/simple/main.go b/examples/simple/main.go index abed07a..4c1239f 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -1,7 +1,7 @@ package main import ( - "fmt" + "errors" "log" "github.com/nikoksr/konfetty" @@ -31,10 +31,12 @@ func main() { Crust: "thin", }, Quantity: 1, + Delivery: true, + Address: "A-123, 4th Street, New York", } // Use konfetty to process the config - cfg, err := konfetty.FromConfig(cfg). + cfg, err := konfetty.FromStruct(cfg). WithDefaults( OrderConfig{ Pizza: PizzaConfig{ @@ -54,7 +56,7 @@ func main() { }). WithValidator(func(c *OrderConfig) error { if c.Delivery && c.Address == "" { - return fmt.Errorf("delivery address is required for delivery orders") + return errors.New("delivery address is required for delivery orders") } return nil }). @@ -80,8 +82,6 @@ func main() { // }, // Quantity: 1, // Delivery: true, - // Address: "", + // Address: "A-123, 4th Street, New York", // } - // - // Note: The above configuration would fail validation due to missing address for delivery. } diff --git a/examples/viper/main.go b/examples/viper/main.go index c3faa49..51df57c 100644 --- a/examples/viper/main.go +++ b/examples/viper/main.go @@ -1,7 +1,7 @@ package main import ( - "fmt" + "errors" "log" "github.com/spf13/viper" @@ -47,7 +47,7 @@ func main() { } // Use konfetty to handle the rest - cfg, err = konfetty.FromConfig(cfg). + cfg, err = konfetty.FromStruct(cfg). WithDefaults( AppConfig{ Database: DatabaseConfig{ @@ -67,7 +67,7 @@ func main() { }). WithValidator(func(c *AppConfig) error { if c.Database.Username == "" || c.Database.Password == "" { - return fmt.Errorf("database credentials are required") + return errors.New("database credentials are required") } return nil }).