Skip to content

Commit

Permalink
updating mod and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rbroggi committed Jul 22, 2024
1 parent 731d494 commit 852aa5a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
64 changes: 60 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,70 @@ repositories. There may be a slight delay.
deployment of new configuration versions.
* **Fast Local Retrieval**: Getting configuration data locally is fast as it's retrieved
from memory, not requiring remote queries.
* **Input validation**: User-provided configuration changes validation through the `Update` method.

## Usage

checkout the [example](./example/server/main.go).
checkout the [example](./example/server/main.go) folder for a more real-world scenario.

1. Define a configuration with `json` field tags (and optionally with `default` field tags):
2. Make sure that your configuration type implements the `streamingconfig.Config` interface:
3. Instantiate and start the repository and use it.
As a library user, you will have to:

1. Define a configuration with `json` field tags (and optionally with `default` field tags);
2. Make sure that your configuration type implements the `streamingconfig.Config` interface;
> **_NOTE:_** Within the `Update` method you can implement configuration validation see example below.
3. Instantiate and start the repository and use it;

```go
package main

import (
"errors"
config "github.com/rbroggi/streamingconfig"
)

type conf struct {
Name string `json:"name" default:"john"`
Age int `json:"age"`
}

func (c *conf) Update(new config.Config) error {
newCfg, ok := new.(*conf)
if !ok {
return errors.New("wrong configuration")
}
c.Name = newCfg.Name
c.Age = newCfg.Age
return c.validate()
}

// validation should not disallow zero-values as the `Update`
// method is called on the struct without it's default values.
func (c *conf) validate() error {
if c.Age < 0 {
return errors.New("age must not be negative")
}
return nil
}

func main() {
repo, err := config.NewWatchedRepo[*conf](
config.Args{
Logger: getLogger(),
DB: getDatabase(),
})
if err != nil {
log.Fatal(err)
}
ctx, cnl := context.WithCancel(context.Background())
done, err := repo.Start(ctx)
if err != nil {
log.Fatal(err)
}
// use repo
cnl()
<-done
}
```

## Test

Expand Down
2 changes: 1 addition & 1 deletion example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"syscall"
"time"

config "streamingconfig"
config "github.com/rbroggi/streamingconfig"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down
2 changes: 1 addition & 1 deletion example/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"strconv"

config "streamingconfig"
config "github.com/rbroggi/streamingconfig"
)

type server struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module streamingconfig
module github.com/rbroggi/streamingconfig

go 1.22

Expand Down
2 changes: 1 addition & 1 deletion streamingconfig_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

config "streamingconfig"
config "github.com/rbroggi/streamingconfig"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down

0 comments on commit 852aa5a

Please sign in to comment.