Chore: Remove DecoderCompactor() (#61706)

* Remove ApplyFunc and set NoOptionalPointers to true

* Update go mod
This commit is contained in:
Selene
2023-01-23 15:00:34 +01:00
committed by GitHub
parent f62f3cb0e9
commit 1e5eae5fca
4 changed files with 5 additions and 77 deletions

View File

@@ -30,7 +30,7 @@ func (j GoTypesJenny) Generate(sfg SchemaForGen) (*codejen.File, error) {
},
},
PackageName: sfg.Schema.Lineage().Name(),
ApplyFuncs: append(j.ApplyFuncs, PrefixDropper(sfg.Name), DecoderCompactor()),
ApplyFuncs: append(j.ApplyFuncs, PrefixDropper(sfg.Name)),
})
if err != nil {

View File

@@ -172,75 +172,3 @@ func (d prefixmod) do(n *dst.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 := ddepoint(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
}