forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathignore.go
91 lines (70 loc) · 1.82 KB
/
ignore.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
package provider
import (
"context"
"strings"
"github.com/evcc-io/evcc/util"
)
type ignoreProvider struct {
ctx context.Context
err string
set Config
}
func init() {
registry.AddCtx("ignore", NewIgnoreFromConfig)
}
// NewIgnoreFromConfig creates const provider
func NewIgnoreFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) {
var cc struct {
Error string
Set Config
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
o := &ignoreProvider{
ctx: ctx,
err: cc.Error,
set: cc.Set,
}
return o, nil
}
var _ SetIntProvider = (*ignoreProvider)(nil)
func ignoreError[T any](fun func(T) error, match string) func(T) error {
return func(val T) error {
if err := fun(val); err != nil && !strings.HasPrefix(err.Error(), match) {
return err
}
return nil
}
}
func (o *ignoreProvider) IntSetter(param string) (func(int64) error, error) {
set, err := NewIntSetterFromConfig(o.ctx, param, o.set)
if err != nil {
return nil, err
}
return ignoreError(set, o.err), nil
}
var _ SetFloatProvider = (*ignoreProvider)(nil)
func (o *ignoreProvider) FloatSetter(param string) (func(float64) error, error) {
set, err := NewFloatSetterFromConfig(o.ctx, param, o.set)
if err != nil {
return nil, err
}
return ignoreError(set, o.err), nil
}
var _ SetBoolProvider = (*ignoreProvider)(nil)
func (o *ignoreProvider) BoolSetter(param string) (func(bool) error, error) {
set, err := NewBoolSetterFromConfig(o.ctx, param, o.set)
if err != nil {
return nil, err
}
return ignoreError(set, o.err), nil
}
var _ SetBytesProvider = (*ignoreProvider)(nil)
func (o *ignoreProvider) BytesSetter(param string) (func([]byte) error, error) {
set, err := NewBytesSetterFromConfig(o.ctx, param, o.set)
if err != nil {
return nil, err
}
return ignoreError(set, o.err), nil
}