-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
50 lines (41 loc) · 1 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package config
import (
"context"
"crypto/md5"
"fmt"
)
// Snapshot contains point of time loaded configuration
type Snapshot struct {
Data []byte
checksum string
}
// Checksum return md5 checksum of snapshot data
func (s *Snapshot) Checksum() string {
if s.checksum == "" {
s.checksum = checksum(s.Data)
}
return s.checksum
}
// Loader is config source loader
type Loader interface {
Load() (*Snapshot, error)
// assign stream decoder for config loader
// Loader implementation must aware and use
// decoder before unmarshalling
SetDecoder(Decoder)
}
// Decoder decode config stream before loaded
// which can be used to decrypt encoded stream
type Decoder interface {
Decode([]byte) []byte
}
// Watchable indicate source is support watch changes
type Watchable interface {
// Watch run watcher with given context
// implementer should listen to context cancellation
// to stop watching process
Watch(context.Context)
}
func checksum(b []byte) string {
return fmt.Sprintf("%x", md5.Sum(b))
}