d: debug, targets: make(map[string]File, len(req.GetFileToGenerate())), packages: make(map[string]Package), entities: make(map[string]Entity), extensions: []Extension{}, } for _, f := range req.GetFileToGenerate() { g.targets[f] = nil } for _, f := range req.GetProtoFile() { pkg := g.hydratePackage(f) pkg.addFile(g.hydrateFile(pkg, f)) } for _, e := range g.extensions { e.addType(g.hydrateFieldType(e)) extendee := g.mustSeen(e.Descriptor().GetExtendee()).(Message) e.setExtendee(extendee) if extendee != nil { extendee.addExtension(e) } } return g } Generate AST func (g *graph) hydrateFile(pkg Package, f *descriptor.FileDescriptorProto) File { fl := &file{ pkg: pkg, desc: f, } if pkg := f.GetPackage(); pkg != "" { fl.fqn = "." + pkg } else { fl.fqn = "" } g.add(fl) for _, dep := range f.GetDependency() { // the AST is built in topological order so a file's dependencies are always hydrated first d := g.mustSeen(dep).(File) fl.addFileDependency(d) d.addDependent(fl) } if _, fl.buildTarget = g.targets[f.GetName()]; fl.buildTarget { g.targets[f.GetName()] = fl } enums := f.GetEnumType() fl.enums = make([]Enum, 0, len(enums)) for _, e := range enums { fl.addEnum(g.hydrateEnum(fl, e)) } exts := f.GetExtension() fl.defExts = make([]Extension, 0, len(exts)) for _, ext := range exts { e := g.hydrateExtension(fl, ext) fl.addDefExtension(e) } msgs := f.GetMessageType() fl.msgs = make([]Message, 0, len(f.GetMessageType())) for _, msg := range msgs { fl.addMessage(g.hydrateMessage(fl, msg)) } srvs := f.GetService() fl.srvs = make([]Service, 0, len(srvs)) for _, sd := range srvs { fl.addService(g.hydrateService(fl, sd)) } for _, m := range fl.AllMessages() { for _, me := range m.MapEntries() { for _, fld := range me.Fields() { fld.addType(g.hydrateFieldType(fld)) } } for _, fld := range m.Fields() { fld.addType(g.hydrateFieldType(fld)) } } g.hydrateSourceCodeInfo(fl, f) return fl } 気合い