-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Being the first lib implemented in baseplate.go and long predates generics, the API of filewatcher lacks type safety and can cause other annoyances. Add filewatcher/v2 that provides: * Generics/type safety * Use io.Closer over Stop() * Use With- options over a Config struct * Use slog at error level over log wrapper And change original filewatcher lib to a thin wrapper of v2. Also add filewatcher/v2/fwtest and move the fakes there. This change intentionally does not change any of its users (experiments, secrets, etc.), as a proof that we did not break v0 API. The next change will switch them to use v2.
- Loading branch information
Showing
10 changed files
with
1,261 additions
and
348 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package filewatcher provides a go implementation of baseplate's FileWatcher: | ||
// https://baseplate.readthedocs.io/en/stable/api/baseplate/lib/file_watcher.html | ||
package filewatcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package filewatcher_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"time" | ||
|
||
"github.com/reddit/baseplate.go/filewatcher/v2" | ||
"github.com/reddit/baseplate.go/log" | ||
) | ||
|
||
// This example demonstrates how to use filewatcher. | ||
func Example() { | ||
const ( | ||
// The path to the file. | ||
path = "/opt/data.json" | ||
// Timeout on the initial read. | ||
timeout = time.Second * 30 | ||
) | ||
|
||
// The type of the parsed data | ||
type dataType map[string]any | ||
|
||
// Wrap a json decoder as parser | ||
parser := func(f io.Reader) (dataType, error) { | ||
var data dataType | ||
err := json.NewDecoder(f).Decode(&data) | ||
return data, err | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
data, err := filewatcher.New(ctx, path, parser) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Whenever you need to use the parsed data, just call data.Get(): | ||
_ = data.Get() | ||
} |
Oops, something went wrong.