From 338f9128d6cca7e6819d93be6ed4734c9c37b230 Mon Sep 17 00:00:00 2001 From: ajatprabha Date: Thu, 15 Feb 2024 13:13:30 +0530 Subject: [PATCH] doc: add examples --- xload/type/example_test.go | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 xload/type/example_test.go diff --git a/xload/type/example_test.go b/xload/type/example_test.go new file mode 100644 index 0000000..92544cd --- /dev/null +++ b/xload/type/example_test.go @@ -0,0 +1,47 @@ +package xloadtype_test + +import ( + "context" + "fmt" + "github.com/gojekfarm/xtools/xload" + xloadtype "github.com/gojekfarm/xtools/xload/type" +) + +var testValues = map[string]string{ + "LISTENER": "[::1]:8080", + "ENDPOINT": "example.com:80", +} + +var loader = xload.LoaderFunc(func(ctx context.Context, key string) (string, error) { + return testValues[key], nil +}) + +func ExampleEndpoint() { + type Server struct { + Endpoint xloadtype.Endpoint `env:"ENDPOINT"` + } + + var srv Server + if err := xload.Load(context.Background(), &srv, loader); err != nil { + panic(err) + } + + fmt.Println(srv.Endpoint.String()) + + // Output: example.com:80 +} + +func ExampleListener() { + type Server struct { + Listener xloadtype.Listener `env:"LISTENER"` + } + + var srv Server + if err := xload.Load(context.Background(), &srv, loader); err != nil { + panic(err) + } + + fmt.Println(srv.Listener.String()) + + // Output: [::1]:8080 +}