package types
import (
)
func ( ast.Expr) string {
var bytes.Buffer
WriteExpr(&, )
return .String()
}
func ( *bytes.Buffer, ast.Expr) {
switch x := .(type) {
default:
.WriteString(fmt.Sprintf("(ast: %T)", ))
case *ast.Ident:
.WriteString(.Name)
case *ast.Ellipsis:
.WriteString("...")
if .Elt != nil {
(, .Elt)
}
case *ast.BasicLit:
.WriteString(.Value)
case *ast.FuncLit:
.WriteByte('(')
(, .Type)
.WriteString(" literal)")
case *ast.CompositeLit:
.WriteByte('(')
(, .Type)
.WriteString(" literal)")
case *ast.ParenExpr:
.WriteByte('(')
(, .X)
.WriteByte(')')
case *ast.SelectorExpr:
(, .X)
.WriteByte('.')
.WriteString(.Sel.Name)
case *ast.IndexExpr, *ast.IndexListExpr:
:= typeparams.UnpackIndexExpr()
(, .X)
.WriteByte('[')
writeExprList(, .Indices)
.WriteByte(']')
case *ast.SliceExpr:
(, .X)
.WriteByte('[')
if .Low != nil {
(, .Low)
}
.WriteByte(':')
if .High != nil {
(, .High)
}
if .Slice3 {
.WriteByte(':')
if .Max != nil {
(, .Max)
}
}
.WriteByte(']')
case *ast.TypeAssertExpr:
(, .X)
.WriteString(".(")
(, .Type)
.WriteByte(')')
case *ast.CallExpr:
(, .Fun)
.WriteByte('(')
writeExprList(, .Args)
if .Ellipsis.IsValid() {
.WriteString("...")
}
.WriteByte(')')
case *ast.StarExpr:
.WriteByte('*')
(, .X)
case *ast.UnaryExpr:
.WriteString(.Op.String())
(, .X)
case *ast.BinaryExpr:
(, .X)
.WriteByte(' ')
.WriteString(.Op.String())
.WriteByte(' ')
(, .Y)
case *ast.ArrayType:
.WriteByte('[')
if .Len != nil {
(, .Len)
}
.WriteByte(']')
(, .Elt)
case *ast.StructType:
.WriteString("struct{")
writeFieldList(, .Fields.List, "; ", false)
.WriteByte('}')
case *ast.FuncType:
.WriteString("func")
writeSigExpr(, )
case *ast.InterfaceType:
.WriteString("interface{")
writeFieldList(, .Methods.List, "; ", true)
.WriteByte('}')
case *ast.MapType:
.WriteString("map[")
(, .Key)
.WriteByte(']')
(, .Value)
case *ast.ChanType:
var string
switch .Dir {
case ast.SEND:
= "chan<- "
case ast.RECV:
= "<-chan "
default:
= "chan "
}
.WriteString()
(, .Value)
}
}
func ( *bytes.Buffer, *ast.FuncType) {
.WriteByte('(')
writeFieldList(, .Params.List, ", ", false)
.WriteByte(')')
:= .Results
:= .NumFields()
if == 0 {
return
}
.WriteByte(' ')
if == 1 && len(.List[0].Names) == 0 {
WriteExpr(, .List[0].Type)
return
}
.WriteByte('(')
writeFieldList(, .List, ", ", false)
.WriteByte(')')
}
func ( *bytes.Buffer, []*ast.Field, string, bool) {
for , := range {
if > 0 {
.WriteString()
}
writeIdentList(, .Names)
if , := .Type.(*ast.FuncType); != nil && {
writeSigExpr(, )
continue
}
if len(.Names) > 0 {
.WriteByte(' ')
}
WriteExpr(, .Type)
}
}
func ( *bytes.Buffer, []*ast.Ident) {
for , := range {
if > 0 {
.WriteString(", ")
}
.WriteString(.Name)
}
}
func ( *bytes.Buffer, []ast.Expr) {
for , := range {
if > 0 {
.WriteString(", ")
}
WriteExpr(, )
}
}