-
Notifications
You must be signed in to change notification settings - Fork 0
/
preproc.go
45 lines (36 loc) · 1.07 KB
/
preproc.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
package config
import (
"golang.org/x/xerrors"
yaml "gopkg.in/yaml.v3"
"github.com/kozmod/progen/internal/entity"
)
type RawPreprocessor struct {
templateName string
templateVars map[string]any
templateFns map[string]any
templateOptions []string
}
func NewRawPreprocessor(templateName string, templateVars, templateFns map[string]any, templateOptions []string) *RawPreprocessor {
return &RawPreprocessor{
templateName: templateName,
templateVars: templateVars,
templateFns: templateFns,
templateOptions: templateOptions,
}
}
func (p *RawPreprocessor) Process(data []byte) ([]byte, map[string]any, error) {
var (
conf map[string]any
name = p.templateName
)
err := yaml.Unmarshal(data, &conf)
if err != nil {
return nil, nil, xerrors.Errorf("parse config to map: %w", err)
}
conf = entity.MergeKeys(conf, p.templateVars)
res, err := entity.NewTemplateProc(conf, p.templateFns, p.templateOptions).Process(name, string(data))
if err != nil {
return nil, nil, xerrors.Errorf("config data: %w", err)
}
return []byte(res), conf, nil
}