2024-02-29 03:20:28 -06:00
|
|
|
package codegen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-21 05:11:29 -05:00
|
|
|
"strings"
|
2024-02-29 03:20:28 -06:00
|
|
|
|
|
|
|
"cuelang.org/go/cue"
|
|
|
|
"github.com/dave/dst/dstutil"
|
|
|
|
"github.com/grafana/codejen"
|
2024-03-21 05:11:29 -05:00
|
|
|
"github.com/grafana/grafana/pkg/codegen/generators"
|
2024-02-29 03:20:28 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type GoSpecJenny struct {
|
|
|
|
ApplyFuncs []dstutil.ApplyFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jenny *GoSpecJenny) JennyName() string {
|
|
|
|
return "GoResourceTypes"
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:11:29 -05:00
|
|
|
func (jenny *GoSpecJenny) Generate(sfg ...SchemaForGen) (codejen.Files, error) {
|
|
|
|
files := make(codejen.Files, len(sfg))
|
|
|
|
for i, v := range sfg {
|
|
|
|
packageName := strings.ToLower(v.Name)
|
|
|
|
b, err := generators.GenerateTypesGo(v.CueFile,
|
|
|
|
&generators.GoConfig{
|
|
|
|
Config: &generators.OpenApiConfig{
|
|
|
|
IsGroup: false,
|
2024-02-29 03:20:28 -06:00
|
|
|
RootName: "Spec",
|
2024-03-21 05:11:29 -05:00
|
|
|
SubPath: cue.MakePath(cue.Str("spec")),
|
2024-02-29 03:20:28 -06:00
|
|
|
},
|
2024-03-21 05:11:29 -05:00
|
|
|
PackageName: packageName,
|
|
|
|
ApplyFuncs: append(jenny.ApplyFuncs, PrefixDropper(v.Name)),
|
2024-02-29 03:20:28 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:11:29 -05:00
|
|
|
files[i] = *codejen.NewFile(fmt.Sprintf("pkg/kinds/%s/%s_spec_gen.go", packageName, packageName), b, jenny)
|
2024-02-29 03:20:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|