2024-05-14 13:07:09 +00:00
|
|
|
//go:build go1.18
|
|
|
|
// +build go1.18
|
|
|
|
|
|
|
|
package swag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/go-openapi/spec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type genericTypeSpec struct {
|
|
|
|
TypeSpec *TypeSpecDef
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Name string
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type formalParamType struct {
|
|
|
|
Name string
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *genericTypeSpec) TypeName() string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if t.TypeSpec != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return t.TypeSpec.TypeName()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return t.Name
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeGenericTypeName(name string) string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return strings.Replace(name, ".", "_", -1)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pkgDefs *PackagesDefinitions) getTypeFromGenericParam(genericParam string, file *ast.File) (typeSpecDef *TypeSpecDef) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if strings.HasPrefix(genericParam, "[]") {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
typeSpecDef = pkgDefs.getTypeFromGenericParam(genericParam[2:], file)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if typeSpecDef == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
var expr ast.Expr
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
switch typeSpecDef.TypeSpec.Type.(type) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.ArrayType, *ast.MapType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
expr = typeSpecDef.TypeSpec.Type
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
default:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
name := typeSpecDef.TypeName()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
expr = ast.NewIdent(name)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if _, ok := pkgDefs.uniqueDefinitions[name]; !ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
pkgDefs.uniqueDefinitions[name] = typeSpecDef
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return &TypeSpecDef{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
TypeSpec: &ast.TypeSpec{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Name: ast.NewIdent(string(IgnoreNameOverridePrefix) + "array_" + typeSpecDef.TypeName()),
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Type: &ast.ArrayType{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Elt: expr,
|
|
|
|
},
|
|
|
|
},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Enums: typeSpecDef.Enums,
|
|
|
|
|
|
|
|
PkgPath: typeSpecDef.PkgPath,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
ParentSpec: typeSpecDef.ParentSpec,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
NotUnique: false,
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(genericParam, "map[") {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
parts := strings.SplitN(genericParam[4:], "]", 2)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if len(parts) != 2 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
typeSpecDef = pkgDefs.getTypeFromGenericParam(parts[1], file)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if typeSpecDef == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
var expr ast.Expr
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
switch typeSpecDef.TypeSpec.Type.(type) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.ArrayType, *ast.MapType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
expr = typeSpecDef.TypeSpec.Type
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
default:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
name := typeSpecDef.TypeName()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
expr = ast.NewIdent(name)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if _, ok := pkgDefs.uniqueDefinitions[name]; !ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
pkgDefs.uniqueDefinitions[name] = typeSpecDef
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return &TypeSpecDef{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
TypeSpec: &ast.TypeSpec{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Name: ast.NewIdent(string(IgnoreNameOverridePrefix) + "map_" + parts[0] + "_" + typeSpecDef.TypeName()),
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Type: &ast.MapType{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Key: ast.NewIdent(parts[0]), //assume key is string or integer
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Value: expr,
|
|
|
|
},
|
|
|
|
},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Enums: typeSpecDef.Enums,
|
|
|
|
|
|
|
|
PkgPath: typeSpecDef.PkgPath,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
ParentSpec: typeSpecDef.ParentSpec,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
NotUnique: false,
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if IsGolangPrimitiveType(genericParam) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return &TypeSpecDef{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
TypeSpec: &ast.TypeSpec{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Name: ast.NewIdent(genericParam),
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Type: ast.NewIdent(genericParam),
|
|
|
|
},
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return pkgDefs.FindTypeSpec(genericParam, file)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pkgDefs *PackagesDefinitions) parametrizeGenericType(file *ast.File, original *TypeSpecDef, fullGenericForm string) *TypeSpecDef {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if original == nil || original.TypeSpec.TypeParams == nil || len(original.TypeSpec.TypeParams.List) == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return original
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name, genericParams := splitGenericsTypeName(fullGenericForm)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if genericParams == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//generic[x,y any,z any] considered, TODO what if the type is not `any`, but a concrete one, such as `int32|int64` or an certain interface{}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
var formals []formalParamType
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
for _, field := range original.TypeSpec.TypeParams.List {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
for _, ident := range field.Names {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
formal := formalParamType{Name: ident.Name}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if ident, ok := field.Type.(*ast.Ident); ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
formal.Type = ident.Name
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
formals = append(formals, formal)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if len(genericParams) != len(formals) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
genericParamTypeDefs := map[string]*genericTypeSpec{}
|
|
|
|
|
|
|
|
for i, genericParam := range genericParams {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
var typeDef *TypeSpecDef
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if !IsGolangPrimitiveType(genericParam) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
typeDef = pkgDefs.getTypeFromGenericParam(genericParam, file)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if typeDef != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
genericParam = typeDef.TypeName()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if _, ok := pkgDefs.uniqueDefinitions[genericParam]; !ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
pkgDefs.uniqueDefinitions[genericParam] = typeDef
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
genericParamTypeDefs[formals[i].Name] = &genericTypeSpec{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
TypeSpec: typeDef,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Name: genericParam,
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name = fmt.Sprintf("%s%s-", string(IgnoreNameOverridePrefix), original.TypeName())
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
var nameParts []string
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
for _, def := range formals {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if specDef, ok := genericParamTypeDefs[def.Name]; ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
nameParts = append(nameParts, specDef.TypeName())
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name += normalizeGenericTypeName(strings.Join(nameParts, "-"))
|
|
|
|
|
|
|
|
if typeSpec, ok := pkgDefs.uniqueDefinitions[name]; ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return typeSpec
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parametrizedTypeSpec := &TypeSpecDef{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
File: original.File,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
PkgPath: original.PkgPath,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
TypeSpec: &ast.TypeSpec{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Name: &ast.Ident{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Name: name,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
NamePos: original.TypeSpec.Name.NamePos,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Obj: original.TypeSpec.Name.Obj,
|
2024-05-14 13:07:09 +00:00
|
|
|
},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Doc: original.TypeSpec.Doc,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Assign: original.TypeSpec.Assign,
|
|
|
|
},
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
pkgDefs.uniqueDefinitions[name] = parametrizedTypeSpec
|
|
|
|
|
|
|
|
parametrizedTypeSpec.TypeSpec.Type = pkgDefs.resolveGenericType(original.File, original.TypeSpec.Type, genericParamTypeDefs)
|
|
|
|
|
|
|
|
return parametrizedTypeSpec
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// splitGenericsTypeName splits a generic struct name in his parts
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
func splitGenericsTypeName(fullGenericForm string) (string, []string) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
//remove all spaces character
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
fullGenericForm = strings.Map(func(r rune) rune {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if unicode.IsSpace(r) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return -1
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return r
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}, fullGenericForm)
|
|
|
|
|
|
|
|
// split only at the first '[' and remove the last ']'
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if fullGenericForm[len(fullGenericForm)-1] != ']' {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
genericParams := strings.SplitN(fullGenericForm[:len(fullGenericForm)-1], "[", 2)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if len(genericParams) == 1 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generic type name
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
genericTypeName := genericParams[0]
|
|
|
|
|
|
|
|
depth := 0
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
genericParams = strings.FieldsFunc(genericParams[1], func(r rune) bool {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if r == '[' {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
depth++
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
} else if r == ']' {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
depth--
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
} else if r == ',' && depth == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return true
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return false
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if depth != 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return genericTypeName, genericParams
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pkgDefs *PackagesDefinitions) getParametrizedType(genTypeSpec *genericTypeSpec) ast.Expr {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if genTypeSpec.TypeSpec != nil && strings.Contains(genTypeSpec.Name, ".") {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
parts := strings.SplitN(genTypeSpec.Name, ".", 2)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return &ast.SelectorExpr{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
X: &ast.Ident{Name: parts[0]},
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Sel: &ast.Ident{Name: parts[1]},
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//a primitive type name or a type name in current package
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return &ast.Ident{Name: genTypeSpec.Name}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pkgDefs *PackagesDefinitions) resolveGenericType(file *ast.File, expr ast.Expr, genericParamTypeDefs map[string]*genericTypeSpec) ast.Expr {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
switch astExpr := expr.(type) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.Ident:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if genTypeSpec, ok := genericParamTypeDefs[astExpr.Name]; ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return pkgDefs.getParametrizedType(genTypeSpec)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.ArrayType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return &ast.ArrayType{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Elt: pkgDefs.resolveGenericType(file, astExpr.Elt, genericParamTypeDefs),
|
|
|
|
|
|
|
|
Len: astExpr.Len,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Lbrack: astExpr.Lbrack,
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.MapType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return &ast.MapType{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Map: astExpr.Map,
|
|
|
|
|
|
|
|
Key: pkgDefs.resolveGenericType(file, astExpr.Key, genericParamTypeDefs),
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Value: pkgDefs.resolveGenericType(file, astExpr.Value, genericParamTypeDefs),
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.StarExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return &ast.StarExpr{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Star: astExpr.Star,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
X: pkgDefs.resolveGenericType(file, astExpr.X, genericParamTypeDefs),
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.IndexExpr, *ast.IndexListExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
fullGenericName, _ := getGenericFieldType(file, expr, genericParamTypeDefs)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
typeDef := pkgDefs.FindTypeSpec(fullGenericName, file)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if typeDef != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return typeDef.TypeSpec.Name
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.StructType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
newStructTypeDef := &ast.StructType{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Struct: astExpr.Struct,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Incomplete: astExpr.Incomplete,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Fields: &ast.FieldList{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Opening: astExpr.Fields.Opening,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Closing: astExpr.Fields.Closing,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range astExpr.Fields.List {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
newField := &ast.Field{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Type: field.Type,
|
|
|
|
|
|
|
|
Doc: field.Doc,
|
|
|
|
|
|
|
|
Names: field.Names,
|
|
|
|
|
|
|
|
Tag: field.Tag,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
Comment: field.Comment,
|
|
|
|
}
|
|
|
|
|
|
|
|
newField.Type = pkgDefs.resolveGenericType(file, field.Type, genericParamTypeDefs)
|
|
|
|
|
|
|
|
newStructTypeDef.Fields.List = append(newStructTypeDef.Fields.List, newField)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return newStructTypeDef
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return expr
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getExtendedGenericFieldType(file *ast.File, field ast.Expr, genericParamTypeDefs map[string]*genericTypeSpec) (string, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
switch fieldType := field.(type) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.ArrayType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
fieldName, err := getExtendedGenericFieldType(file, fieldType.Elt, genericParamTypeDefs)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "[]" + fieldName, err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.StarExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return getExtendedGenericFieldType(file, fieldType.X, genericParamTypeDefs)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.Ident:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if genericParamTypeDefs != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if typeSpec, ok := genericParamTypeDefs[fieldType.Name]; ok {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return typeSpec.Name, nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if fieldType.Obj == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return fieldType.Name, nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tSpec := &TypeSpecDef{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
File: file,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
TypeSpec: fieldType.Obj.Decl.(*ast.TypeSpec),
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
PkgPath: file.Name.Name,
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return tSpec.TypeName(), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
default:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return getFieldType(file, field, genericParamTypeDefs)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getGenericFieldType(file *ast.File, field ast.Expr, genericParamTypeDefs map[string]*genericTypeSpec) (string, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
var fullName string
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
var baseName string
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
var err error
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
switch fieldType := field.(type) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.IndexListExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
baseName, err = getGenericTypeName(file, fieldType.X)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
fullName = baseName + "["
|
|
|
|
|
|
|
|
for _, index := range fieldType.Indices {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
fieldName, err := getExtendedGenericFieldType(file, index, genericParamTypeDefs)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fullName += fieldName + ","
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fullName = strings.TrimRight(fullName, ",") + "]"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.IndexExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
baseName, err = getGenericTypeName(file, fieldType.X)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
indexName, err := getExtendedGenericFieldType(file, fieldType.Index, genericParamTypeDefs)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", err
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fullName = fmt.Sprintf("%s[%s]", baseName, indexName)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fullName == "" {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", fmt.Errorf("unknown field type %#v", field)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var packageName string
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if !strings.Contains(baseName, ".") {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if file.Name == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", errors.New("file name is nil")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
packageName, _ = getFieldType(file, file.Name, genericParamTypeDefs)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimLeft(fmt.Sprintf("%s.%s", packageName, fullName), "."), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getGenericTypeName(file *ast.File, field ast.Expr) (string, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
switch fieldType := field.(type) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.Ident:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if fieldType.Obj == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return fieldType.Name, nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tSpec := &TypeSpecDef{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
File: file,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
TypeSpec: fieldType.Obj.Decl.(*ast.TypeSpec),
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
PkgPath: file.Name.Name,
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return tSpec.TypeName(), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.ArrayType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
tSpec := &TypeSpecDef{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
File: file,
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
TypeSpec: fieldType.Elt.(*ast.Ident).Obj.Decl.(*ast.TypeSpec),
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
PkgPath: file.Name.Name,
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return tSpec.TypeName(), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.SelectorExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return fmt.Sprintf("%s.%s", fieldType.X.(*ast.Ident).Name, fieldType.Sel.Name), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return "", fmt.Errorf("unknown type %#v", field)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *Parser) parseGenericTypeExpr(file *ast.File, typeExpr ast.Expr) (*spec.Schema, error) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
switch expr := typeExpr.(type) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// suppress debug messages for these types
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.InterfaceType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.StructType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.Ident:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.StarExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.SelectorExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.ArrayType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.MapType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.FuncType:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
case *ast.IndexExpr, *ast.IndexListExpr:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
name, err := getExtendedGenericFieldType(file, expr, nil)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if err == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
if schema, err := parser.getTypeSchema(name, file, false); err == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return schema, nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parser.debug.Printf("Type definition of type '%T' is not supported yet. Using 'object' instead. (%s)\n", typeExpr, err)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
default:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
parser.debug.Printf("Type definition of type '%T' is not supported yet. Using 'object' instead.\n", typeExpr)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PrimitiveSchema(OBJECT), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|