// readType processes a type declaration. // func (r *reader) readType(decl *ast.GenDecl, spec *ast.TypeSpec) { typ := r.lookupType(spec.Name.Name) if typ == nil { return // no name or blank name - ignore the type } // A type should be added at most once, so typ.decl // should be nil - if it is not, simply overwrite it. typ.decl = decl // compute documentation doc := spec.Doc spec.Doc = nil // doc consumed - remove from AST if doc == nil { // no doc associated with the spec, use the declaration doc, if any doc = decl.Doc } decl.Doc = nil // doc consumed - remove from AST typ.doc = doc.Text() // record anonymous fields (they may contribute methods) // (some fields may have been recorded already when filtering // exports, but that's ok) var list []*ast.Field list, typ.isStruct = fields(spec.Type) for _, field := range list { if len(field.Names) == 0 { r.recordAnonymousField(typ, field.Type) } } }
// oneLineTypeDecl prints a type declaration as a single line. func (pkg *Package) oneLineTypeDecl(spec *ast.TypeSpec) { spec.Doc = nil spec.Comment = nil switch spec.Type.(type) { case *ast.InterfaceType: pkg.Printf("type %s interface { ... }\n", spec.Name) case *ast.StructType: pkg.Printf("type %s struct { ... }\n", spec.Name) default: pkg.Printf("type %s %s\n", spec.Name, pkg.formatNode(spec.Type)) } }
func newType(decl *ast.GenDecl, spec *ast.TypeSpec) *Type { if !ast.IsExported(spec.Name.Name) { return nil } t := new(Type) if spec.Doc != nil { t.Doc = doc.CommentText(spec.Doc) spec.Doc = nil } else { // if spec has no docs, try pulling comment from decl t.Doc = doc.CommentText(decl.Doc) } cleanUnexportedFields(spec.Type) t.Name = spec.Name.Name t.Decl = spec return t }
// readType processes a type declaration. // func (r *reader) readType(decl *ast.GenDecl, spec *ast.TypeSpec) { typ := r.lookupType(spec.Name.Name) if typ == nil { return // no name or blank name - ignore the type } // A type should be added at most once, so info.decl // should be nil - if it is not, simply overwrite it. typ.decl = decl // compute documentation doc := spec.Doc spec.Doc = nil // doc consumed - remove from AST if doc == nil { // no doc associated with the spec, use the declaration doc, if any doc = decl.Doc } decl.Doc = nil // doc consumed - remove from AST typ.doc = doc.Text() // look for anonymous fields that might contribute methods var list []*ast.Field list, typ.isStruct = fields(spec.Type) for _, field := range list { if len(field.Names) == 0 { // anonymous field - add corresponding field type to typ n, imp := baseTypeName(field.Type) if imp { // imported type - we don't handle this case // at the moment return } if embedded := r.lookupType(n); embedded != nil { _, ptr := field.Type.(*ast.StarExpr) typ.addEmbeddedType(embedded, ptr) } } } }