-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage_config.go
71 lines (61 loc) · 1.42 KB
/
storage_config.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package eventstream
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/geniusrabbit/eventstream/internal/utils"
)
var (
errStorageEmptyConnection = errors.New("[storage] empty connection")
errStorageUndefinedDriver = errors.New("[storage] undefined driver")
)
// StorageConfig of the storage
type StorageConfig struct {
Debug bool
Connect string
Driver string
Buffer uint
Raw json.RawMessage
}
// Decode raw data to the target object
func (c *StorageConfig) Decode(v any) error {
if err := json.Unmarshal(c.Raw, v); err != nil {
return fmt.Errorf("decode storage config: %s", err.Error())
}
return nil
}
// UnmarshalJSON data
func (c *StorageConfig) UnmarshalJSON(data []byte) (err error) {
var confData struct {
Connect string `json:"connect"`
Driver string `json:"driver"`
Buffer uint `json:"buffer"`
}
if err = json.Unmarshal(data, &confData); err != nil {
return err
}
if confData.Buffer <= 0 {
confData.Buffer = 1000
}
c.Connect = utils.PrepareValue(confData.Connect)
c.Driver = confData.Driver
c.Buffer = confData.Buffer
c.Raw = json.RawMessage(data)
if c.Driver == `` {
if urlData := strings.Split(c.Connect, `://`); len(urlData) > 1 {
c.Driver = urlData[0]
}
}
return err
}
// Validate config
func (c *StorageConfig) Validate() error {
if c.Connect == "" {
return errStorageEmptyConnection
}
if c.Driver == "" {
return errStorageUndefinedDriver
}
return nil
}