-
Notifications
You must be signed in to change notification settings - Fork 1
/
jenny_major.go
55 lines (46 loc) · 1.35 KB
/
jenny_major.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
package jen
import (
"fmt"
"path/filepath"
"github.com/grafana/codejen"
"github.com/grafana/grafana/pkg/codegen"
"github.com/grafana/kindsys"
)
// LatestJenny returns a jenny that runs another jenny for only the latest
// schema in a DefForGen, and prefixes the resulting file with the provided
// parentdir (e.g. "pkg/kinds/").
// TODO remove this once there's a standard jenny for this...somewhere in core.
func LatestJenny(parentdir string, inner codejen.OneToOne[codegen.SchemaForGen]) codegen.OneToOne {
if inner == nil {
panic("inner jenny must not be nil")
}
return &latestj{
parentdir: parentdir,
inner: inner,
}
}
type latestj struct {
parentdir string
inner codejen.OneToOne[codegen.SchemaForGen]
}
func (j *latestj) JennyName() string {
return "LatestJenny"
}
func (j *latestj) Generate(kind kindsys.Kind) (*codejen.File, error) {
comm := kind.Props().Common()
sfg := codegen.SchemaForGen{
Name: comm.Name,
Schema: kind.Lineage().Latest(),
IsGroup: comm.LineageIsGroup,
}
f, err := j.inner.Generate(sfg)
if err != nil {
return nil, fmt.Errorf("%s jenny failed on %s schema for %s: %w", j.inner.JennyName(), sfg.Schema.Version(), kind.Props().Common().Name, err)
}
if f == nil || !f.Exists() {
return nil, nil
}
f.RelativePath = filepath.Join(j.parentdir, f.RelativePath)
f.From = append(f.From, j)
return f, nil
}