deps: Update thema, use CUE fork (#60496)
* deps: Update thema, use CUE fork * Remove superfluous openapi-codegen output * Update thema again to catch cuetsy changes * Rerun codegen with latest * Latest thema, removes cuetsy update * Latest of cuetsy * Update type usage * One last cuetsy rollback * Update playlist summary tests * More lint fixes * Remove TS veneer changes for VariableHide Co-authored-by: Clarity-89 <homes89@ukr.net>
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package codegen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/dave/dst/decorator"
|
||||
"github.com/dave/dst/dstutil"
|
||||
"github.com/grafana/codejen"
|
||||
"github.com/grafana/thema/encoding/gocode"
|
||||
"golang.org/x/tools/go/ast/astutil"
|
||||
@@ -8,7 +12,9 @@ import (
|
||||
|
||||
// GoTypesJenny creates a [OneToOne] that produces Go types for the provided
|
||||
// [thema.Schema].
|
||||
type GoTypesJenny struct{}
|
||||
type GoTypesJenny struct {
|
||||
ApplyFuncs []astutil.ApplyFunc
|
||||
}
|
||||
|
||||
func (j GoTypesJenny) JennyName() string {
|
||||
return "GoTypesJenny"
|
||||
@@ -19,7 +25,28 @@ func (j GoTypesJenny) Generate(sfg SchemaForGen) (*codejen.File, error) {
|
||||
b, err := gocode.GenerateTypesOpenAPI(sfg.Schema, &gocode.TypeConfigOpenAPI{
|
||||
// TODO will need to account for sanitizing e.g. dashes here at some point
|
||||
PackageName: sfg.Schema.Lineage().Name(),
|
||||
ApplyFuncs: []astutil.ApplyFunc{PrefixDropper(sfg.Name)},
|
||||
ApplyFuncs: append(j.ApplyFuncs, PrefixDropper(sfg.Name)),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO switch to dst completely in thema so this can be made an ApplyFuncs element
|
||||
fb, err := decorator.Parse(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dstutil.Apply(fb, DecoderCompactor(), nil)
|
||||
buf := new(bytes.Buffer)
|
||||
err = decorator.Fprint(buf, fb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b, err = postprocessGoFile(genGoFile{
|
||||
path: "",
|
||||
walker: nil,
|
||||
in: buf.Bytes(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/dave/dst"
|
||||
"github.com/dave/dst/dstutil"
|
||||
"golang.org/x/tools/go/ast/astutil"
|
||||
"golang.org/x/tools/imports"
|
||||
)
|
||||
@@ -163,3 +165,75 @@ func (d prefixmod) do(n *ast.Ident) {
|
||||
n.Name = d.replace
|
||||
}
|
||||
}
|
||||
|
||||
func isSingleTypeDecl(gd *dst.GenDecl) bool {
|
||||
if gd.Tok == token.TYPE && len(gd.Specs) == 1 {
|
||||
_, is := gd.Specs[0].(*dst.TypeSpec)
|
||||
return is
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isAdditionalPropertiesStruct(tspec *dst.TypeSpec) (dst.Expr, bool) {
|
||||
strct, is := tspec.Type.(*dst.StructType)
|
||||
if is && len(strct.Fields.List) == 1 && strct.Fields.List[0].Names[0].Name == "AdditionalProperties" {
|
||||
return strct.Fields.List[0].Type, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func DecoderCompactor() dstutil.ApplyFunc {
|
||||
return func(c *dstutil.Cursor) bool {
|
||||
f, is := c.Node().(*dst.File)
|
||||
if !is {
|
||||
return false
|
||||
}
|
||||
|
||||
compact := make(map[string]bool)
|
||||
// walk the file decls
|
||||
for _, decl := range f.Decls {
|
||||
if fd, is := decl.(*dst.FuncDecl); is {
|
||||
compact[ddepoint(fd.Recv.List[0].Type).(*dst.Ident).Name] = true
|
||||
}
|
||||
}
|
||||
if len(compact) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
replace := make(map[string]dst.Expr)
|
||||
// Walk again, looking for types we found
|
||||
for _, decl := range f.Decls {
|
||||
if gd, is := decl.(*dst.GenDecl); is && isSingleTypeDecl(gd) {
|
||||
if tspec := gd.Specs[0].(*dst.TypeSpec); compact[tspec.Name.Name] {
|
||||
if expr, is := isAdditionalPropertiesStruct(tspec); is {
|
||||
replace[tspec.Name.Name] = expr
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dstutil.Apply(f, func(c *dstutil.Cursor) bool {
|
||||
switch x := c.Node().(type) {
|
||||
case *dst.FuncDecl:
|
||||
c.Delete()
|
||||
case *dst.GenDecl:
|
||||
if isSingleTypeDecl(x) && compact[x.Specs[0].(*dst.TypeSpec).Name.Name] {
|
||||
c.Delete()
|
||||
}
|
||||
case *dst.Field:
|
||||
if id, is := x.Type.(*dst.Ident); is {
|
||||
if expr, has := replace[id.Name]; has {
|
||||
x.Type = expr
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}, nil)
|
||||
return false
|
||||
}
|
||||
}
|
||||
func ddepoint(e dst.Expr) dst.Expr {
|
||||
if star, is := e.(*dst.StarExpr); is {
|
||||
return star.X
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user