forked from git-ecosystem/trace2receiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_yml.go
43 lines (34 loc) · 946 Bytes
/
parse_yml.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
package trace2receiver
import (
"fmt"
"os"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
type MyYmlFileTypes interface {
RulesetDefinition | FilterSettings | PiiSettings
}
type MyYmlParseBufferFn[T MyYmlFileTypes] func(data []byte, path string) (*T, error)
func parseYmlFile[T MyYmlFileTypes](path string, fnPB MyYmlParseBufferFn[T]) (*T, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read YML '%s': '%s'",
path, err.Error())
}
return fnPB(data, path)
}
func parseYmlBuffer[T MyYmlFileTypes](data []byte, path string) (*T, error) {
m := make(map[interface{}]interface{})
err := yaml.Unmarshal(data, &m)
if err != nil {
return nil, fmt.Errorf("could not parse YAML '%s': '%s'",
path, err.Error())
}
p := new(T)
err = mapstructure.Decode(m, p)
if err != nil {
return nil, fmt.Errorf("could not decode '%s': '%s'",
path, err.Error())
}
return p, nil
}