Skip to content

Commit 852aa5a

Browse files
committed
updating mod and docs
1 parent 731d494 commit 852aa5a

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

README.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,70 @@ repositories. There may be a slight delay.
3737
deployment of new configuration versions.
3838
* **Fast Local Retrieval**: Getting configuration data locally is fast as it's retrieved
3939
from 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

example/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"syscall"
1313
"time"
1414

15-
config "streamingconfig"
15+
config "github.com/rbroggi/streamingconfig"
1616

1717
"go.mongodb.org/mongo-driver/mongo"
1818
"go.mongodb.org/mongo-driver/mongo/options"

example/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/http"
99
"strconv"
1010

11-
config "streamingconfig"
11+
config "github.com/rbroggi/streamingconfig"
1212
)
1313

1414
type server struct {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module streamingconfig
1+
module github.com/rbroggi/streamingconfig
22

33
go 1.22
44

streamingconfig_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99
"time"
1010

11-
config "streamingconfig"
11+
config "github.com/rbroggi/streamingconfig"
1212

1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"

0 commit comments

Comments
 (0)