Skip to content

Commit

Permalink
doc: add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Feb 15, 2024
1 parent d892dd6 commit 338f912
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions xload/type/example_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 338f912

Please sign in to comment.