-
Notifications
You must be signed in to change notification settings - Fork 456
/
mapping.go
77 lines (64 loc) · 1.99 KB
/
mapping.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
// SPDX-License-Identifier: AGPL-3.0-only
package config
import (
"github.com/grafana/dskit/multierror"
)
type Mapper interface {
DoMap(source, target Parameters) error
}
type Mapping func(oldPath string, oldVal Value) (newPath string, newVal Value)
// BestEffortDirectMapper implement Mapper and naively maps the values of all parameters form the source to the
// same name parameter in the target. It ignores all errors while setting the values.
type BestEffortDirectMapper struct{}
func (BestEffortDirectMapper) DoMap(source, target Parameters) error {
err := source.Walk(func(path string, value Value) error {
_ = target.SetValue(path, value)
return nil
})
if err != nil {
panic("walk returned an error, even though the walking function didn't return any, not sure what to do: " + err.Error())
}
return nil
}
// PathMapper applies the mappings to specific parameters of the source.
type PathMapper struct {
PathMappings map[string]Mapping
}
// DoMap applies the Mappings from source to target.
// The error DoMap returns are the combined errors that all Mappings returned. If no
// Mappings returned an error, then DoMap returns nil.
func (m PathMapper) DoMap(source, target Parameters) error {
errs := multierror.New()
for path, mapping := range m.PathMappings {
oldVal, err := source.GetValue(path)
if err != nil {
errs.Add(err)
continue
}
newPath, newVal := mapping(path, oldVal)
err = target.SetValue(newPath, newVal)
if err != nil {
errs.Add(err)
}
}
return errs.Err()
}
type MultiMapper []Mapper
func (m MultiMapper) DoMap(source, target Parameters) error {
errs := multierror.New()
for _, mapper := range m {
errs.Add(mapper.DoMap(source, target))
}
return errs.Err()
}
type MapperFunc func(source, target Parameters) error
func (m MapperFunc) DoMap(source, target Parameters) error {
return m(source, target)
}
func RenameMapping(to string) Mapping {
return func(_ string, oldVal Value) (newPath string, newVal Value) {
newPath = to
newVal = oldVal
return
}
}