-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
135 lines (115 loc) · 2.9 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package config
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"errors"
"fmt"
"os"
"github.com/carbonable-labs/indexer/internal/storage"
"gopkg.in/yaml.v3"
)
var (
ErrParseConfig = errors.New("unable to parse config")
ErrUnmarshalYamlFailed = errors.New("unable to unmarshal yaml config")
ErrUnmarshalJsonFailed = errors.New("unable to unmarshal json config")
)
type ContractRepository interface {
SaveConfig(Config) error
GetConfigs() ([]Config, error)
GetConfig(string) (*Config, error)
}
type NatsContractRepository struct {
storage storage.Storage
}
func NewPebbleContractRepository(s storage.Storage) *NatsContractRepository {
return &NatsContractRepository{storage: s}
}
func (r *NatsContractRepository) SaveConfig(c Config) error {
key := fmt.Sprintf("config.%s", c.AppName)
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
if err := encoder.Encode(c); err != nil {
return err
}
if err := r.storage.Set([]byte(key), buf.Bytes()); err != nil {
return err
}
return nil
}
func (r *NatsContractRepository) GetConfigs() ([]Config, error) {
var configs []Config
data := r.storage.Scan([]byte("config."))
for _, d := range data {
if len(d) == 0 {
continue
}
var c Config
decoder := gob.NewDecoder(bytes.NewReader(d))
if err := decoder.Decode(&c); err != nil {
return nil, err
}
configs = append(configs, c)
}
return configs, nil
}
func (r *NatsContractRepository) GetConfig(name string) (*Config, error) {
var c Config
data := r.storage.Get([]byte(fmt.Sprintf("config.%s", name)))
decoder := gob.NewDecoder(bytes.NewReader(data))
if err := decoder.Decode(&c); err != nil {
return nil, err
}
return &c, nil
}
type Contract struct {
Events map[string]string `yaml:"events" json:"events"`
Address string `yaml:"address" json:"address"`
Name string `yaml:"name" json:"name"`
}
type Config struct {
AppName string `yaml:"app_name" json:"app_name"`
Hash string
Contracts []Contract `yaml:"contracts" json:"contracts"`
StartBlock uint64 `yaml:"start_block" json:"start_block"`
}
func (c *Config) GetContract(address string) *Contract {
for _, contract := range c.Contracts {
if contract.Address == address {
return &contract
}
}
return nil
}
func (c *Config) ComputeHash() *Config {
b, _ := yaml.Marshal(c)
h := sha256.Sum256(b)
c.Hash = fmt.Sprintf("%x", h)
return c
}
func NewCongig() *Config {
var c Config
return &c
}
// Create config from yaml input
func FromYamlFile(file string) (*Config, error) {
cfg := Config{}
data, err := os.ReadFile(file)
if err != nil {
return nil, ErrParseConfig
}
err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, ErrUnmarshalYamlFailed
}
return &cfg, nil
}
// Create config from json input
func FromJson(data []byte) (*Config, error) {
cfg := Config{}
err := yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, ErrUnmarshalJsonFailed
}
return &cfg, nil
}