grafana/pkg/codegen/latest_jenny.go
Kristin Laemmert 8745d7ef1b
extract kindsys to external library (#64562)
* extract kindsys

* reinstate kindsys report

This may end up living somewhere else (or not! who knows!), but the important part is that I don't get rid of it right now :)

I hate the package layout (kindsysreport/codegen) for the main function and will take pretty much any alternative suggestion, but we can change also change it later.

Note that the generated report.json is in a different location - anything using this (ops something) needs to be updated.

* kindsysreport in codeowners
2023-03-15 12:04:28 -04:00

55 lines
1.3 KiB
Go

package codegen
import (
"fmt"
"path/filepath"
"github.com/grafana/codejen"
"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/") and with a directory based on the kind's
// machine name (e.g. "dashboard/").
func LatestJenny(parentdir string, inner codejen.OneToOne[SchemaForGen]) 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[SchemaForGen]
}
func (j *latestj) JennyName() string {
return "LatestJenny"
}
func (j *latestj) Generate(kind kindsys.Kind) (*codejen.File, error) {
comm := kind.Props().Common()
sfg := 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, comm.MachineName, f.RelativePath)
f.From = append(f.From, j)
return f, nil
}