@@ -37,14 +37,70 @@ repositories. There may be a slight delay.
3737deployment of new configuration versions.
3838* ** Fast Local Retrieval** : Getting configuration data locally is fast as it's retrieved
3939from memory, not requiring remote queries.
40+ * ** Input validation** : User-provided configuration changes validation through the ` Update ` method.
4041
4142## Usage
4243
43- checkout the [ example] ( ./example/server/main.go ) .
44+ checkout the [ example] ( ./example/server/main.go ) folder for a more real-world scenario.
4445
45- 1 . Define a configuration with ` json ` field tags (and optionally with ` default ` field tags):
46- 2 . Make sure that your configuration type implements the ` streamingconfig.Config ` interface:
47- 3 . Instantiate and start the repository and use it.
46+ As a library user, you will have to:
47+
48+ 1 . Define a configuration with ` json ` field tags (and optionally with ` default ` field tags);
49+ 2 . Make sure that your configuration type implements the ` streamingconfig.Config ` interface;
50+ > ** _ NOTE:_ ** Within the ` Update ` method you can implement configuration validation see example below.
51+ 3 . Instantiate and start the repository and use it;
52+
53+ ``` go
54+ package main
55+
56+ import (
57+ " errors"
58+ config " github.com/rbroggi/streamingconfig"
59+ )
60+
61+ type conf struct {
62+ Name string ` json:"name" default:"john"`
63+ Age int ` json:"age"`
64+ }
65+
66+ func (c *conf ) Update (new config .Config ) error {
67+ newCfg , ok := new .(*conf)
68+ if !ok {
69+ return errors.New (" wrong configuration" )
70+ }
71+ c.Name = newCfg.Name
72+ c.Age = newCfg.Age
73+ return c.validate ()
74+ }
75+
76+ // validation should not disallow zero-values as the `Update`
77+ // method is called on the struct without it's default values.
78+ func (c *conf ) validate () error {
79+ if c.Age < 0 {
80+ return errors.New (" age must not be negative" )
81+ }
82+ return nil
83+ }
84+
85+ func main () {
86+ repo , err := config.NewWatchedRepo [*conf](
87+ config.Args {
88+ Logger: getLogger (),
89+ DB: getDatabase (),
90+ })
91+ if err != nil {
92+ log.Fatal (err)
93+ }
94+ ctx , cnl := context.WithCancel (context.Background ())
95+ done , err := repo.Start (ctx)
96+ if err != nil {
97+ log.Fatal (err)
98+ }
99+ // use repo
100+ cnl ()
101+ <- done
102+ }
103+ ```
48104
49105## Test
50106
0 commit comments