stringify a field type name
(e ast.Expr)
| 662 | |
| 663 | // stringify a field type name |
| 664 | func stringify(e ast.Expr) string { |
| 665 | switch e := e.(type) { |
| 666 | case *ast.Ident: |
| 667 | return e.Name |
| 668 | case *ast.StarExpr: |
| 669 | return "*" + stringify(e.X) |
| 670 | case *ast.SelectorExpr: |
| 671 | return stringify(e.X) + "." + e.Sel.Name |
| 672 | case *ast.ArrayType: |
| 673 | if e.Len == nil { |
| 674 | return "[]" + stringify(e.Elt) |
| 675 | } |
| 676 | return fmt.Sprintf("[%s]%s", stringify(e.Len), stringify(e.Elt)) |
| 677 | case *ast.InterfaceType: |
| 678 | if e.Methods == nil || e.Methods.NumFields() == 0 { |
| 679 | return "interface{}" |
| 680 | } |
| 681 | case *ast.BasicLit: |
| 682 | return e.Value |
| 683 | case *ast.IndexExpr: |
| 684 | // Single type argument: Generic[T] |
| 685 | return fmt.Sprintf("%s[%s]", stringify(e.X), stringify(e.Index)) |
| 686 | case *ast.IndexListExpr: |
| 687 | // Multiple type arguments: Generic[A,B,...] |
| 688 | args := make([]string, 0, len(e.Indices)) |
| 689 | for _, ix := range e.Indices { |
| 690 | args = append(args, stringify(ix)) |
| 691 | } |
| 692 | return fmt.Sprintf("%s[%s]", stringify(e.X), strings.Join(args, ",")) |
| 693 | } |
| 694 | return "<BAD>" |
| 695 | } |
| 696 | |
| 697 | // recursively translate ast.Expr to gen.Elem; nil means type not supported |
| 698 | // expected input types: |
no outgoing calls
no test coverage detected
searching dependent graphs…