-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
109 lines (93 loc) · 2.67 KB
/
plugin.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
package copyfile
import (
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/rrgmc/debefix"
)
// CopyFile is a debefix plugin to configure files to be copied during the data generation process.
type CopyFile struct {
debefix.ValueImpl
sourcePath string
destinationPath string
getPathsCallback GetPathsCallback
getValueCallback GetValueCallback
copyFileCallback CopyFileCallback
}
var (
_ debefix.ValueParser = (*CopyFile)(nil)
_ debefix.RowResolvedCallback = (*CopyFile)(nil)
)
func (c *CopyFile) ParseValue(tag *ast.TagNode) (bool, any, error) {
// parse "!copyfile" tag
if tag.Start.Value != "!copyfile" {
return false, nil, nil
}
var fileData FileData
err := yaml.NodeToValue(tag.Value, &fileData, yaml.Strict())
if err != nil {
return false, nil, err
}
// return a [debefix.Value] to be processed later.
return true, ©FileValue{cf: c, fileData: fileData}, nil
}
func (c *CopyFile) RowResolved(ctx debefix.ValueResolveContext) error {
// after row was resolved, call the callback to copy the file
md := getMetadata(ctx.Row().Metadata)
for fieldname, file := range md.Fields {
getPathsCallback := c.getPathsCallback
if getPathsCallback == nil {
getPathsCallback = DefaultGetPathsCallback
}
source, destination, err := getPathsCallback(ctx, fieldname, file)
if err != nil {
return err
}
copyFileCallback := c.copyFileCallback
if copyFileCallback == nil {
copyFileCallback = DefaultCopyFileCallback
}
err = copyFileCallback(c.sourcePath, source, c.destinationPath, destination)
if err != nil {
return err
}
}
return nil
}
type copyFileValue struct {
debefix.ValueImpl
cf *CopyFile
fileData FileData
}
var (
_ debefix.ValueCallback = (*copyFileValue)(nil)
)
func (c *copyFileValue) GetValueCallback(ctx debefix.ValueCallbackResolveContext) (resolvedValue any, addField bool, err error) {
// copy the metadata to the row being processed, so it is available to [CopyFile.RowResolved].
setMetadata(ctx, c.fileData)
getValueCallback := c.cf.getValueCallback
if getValueCallback == nil {
getValueCallback = DefaultGetValueCallback
}
return getValueCallback(ctx, c.fileData)
}
const (
metadataName = "__copyfile__"
)
type fileDataList struct {
Fields map[string]FileData `json:"fields"`
}
func getMetadata(metadata map[string]any) *fileDataList {
if md, ok := metadata[metadataName]; ok {
if mdfl, ok := md.(*fileDataList); ok {
return mdfl
}
}
return &fileDataList{
Fields: map[string]FileData{},
}
}
func setMetadata(ctx debefix.ValueCallbackResolveContext, fileData FileData) {
md := getMetadata(ctx.Metadata())
md.Fields[ctx.FieldName()] = fileData
ctx.SetMetadata(metadataName, md)
}