github.com/cosmos/cosmos-proto@v1.0.0-beta.3/generator/generator.go (about) 1 // Copyright (c) 2021 PlanetScale Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package generator 6 7 import ( 8 "google.golang.org/protobuf/compiler/protogen" 9 "google.golang.org/protobuf/reflect/protoreflect" 10 ) 11 12 type featureHelpers struct { 13 path protogen.GoImportPath 14 feature int 15 } 16 17 type Extensions struct { 18 Poolable map[protogen.GoIdent]bool 19 } 20 21 type Generator struct { 22 seen map[featureHelpers]bool 23 ext *Extensions 24 features []Feature 25 local map[string]bool 26 } 27 28 func NewGenerator(allFiles []*protogen.File, featureNames []string, ext *Extensions) (*Generator, error) { 29 features, err := findFeatures(featureNames) 30 31 if err != nil { 32 return nil, err 33 } 34 35 local := make(map[string]bool) 36 for _, f := range allFiles { 37 if f.Generate { 38 local[string(f.Desc.Package())] = true 39 } 40 } 41 42 return &Generator{ 43 seen: make(map[featureHelpers]bool), 44 ext: ext, 45 features: features, 46 local: local, 47 }, nil 48 } 49 50 func (gen *Generator) GenerateFile(plugin *protogen.Plugin, gf *protogen.GeneratedFile, file *protogen.File) bool { 51 if file.Desc.Syntax() != protoreflect.Proto3 { 52 return false 53 } 54 55 p := &GeneratedFile{ 56 GeneratedFile: gf, 57 Ext: gen.ext, 58 LocalPackages: gen.local, 59 } 60 61 // DEPRECATED: this was used for our fork/copy of protoc-gen-go 62 // GenerateProtocGenGo(plugin, p, file) 63 64 var generated bool 65 for fidx, feat := range gen.features { 66 featGenerator := feat(p, plugin) 67 if featGenerator.GenerateFile(file, plugin) { 68 generated = true 69 70 helpersForPlugin := featureHelpers{ 71 path: file.GoImportPath, 72 feature: fidx, 73 } 74 if !gen.seen[helpersForPlugin] { 75 featGenerator.GenerateHelpers() 76 gen.seen[helpersForPlugin] = true 77 } 78 } 79 } 80 81 return generated 82 }