generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
62 lines (52 loc) 路 1.45 KB
/
content.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
package fileloader
import (
"io"
"io/fs"
"path"
"strings"
"github.com/dogmatiq/aureus/internal/loader"
)
// A ContentLoader is a function that returns content read from a file.
//
// name is the "effective" or "sanitized" name of the file, after any special
// characters have been removed by the loader. For example, a leading
// underscore. The f.Stat() method can be used to get the actual file name.
//
// If the returned content's role is [loader.NoRole], it is ignored.
type ContentLoader func(name string, f fs.File) (loader.Content, error)
// LoadContent is the default [ContentLoader] implementation.
func LoadContent(name string, f fs.File) (loader.Content, error) {
base := path.Base(name)
atoms := strings.Split(base, ".")
for i, x := range atoms {
if i == 0 {
// We don't look for the input/output marker in the first atom, as
// there must be at least one atom before it from which we deduce
// the test name.
continue
}
var role loader.ContentRole
if strings.EqualFold(x, "input") {
role = loader.Input
} else if strings.EqualFold(x, "output") {
role = loader.Output
} else {
continue
}
data, err := io.ReadAll(f)
if err != nil {
return loader.Content{}, err
}
lang := ""
if n := len(atoms) - 1; i < n {
lang = atoms[n]
}
return loader.Content{
Language: lang,
Data: string(data),
Group: strings.Join(atoms[:i], "."),
Role: role,
}, nil
}
return loader.Content{}, nil
}