1package module 2 3import ( 4 "github.com/envoyproxy/protoc-gen-validate/templates" 5 "github.com/envoyproxy/protoc-gen-validate/templates/java" 6 pgs "github.com/lyft/protoc-gen-star" 7 pgsgo "github.com/lyft/protoc-gen-star/lang/go" 8) 9 10const ( 11 validatorName = "validator" 12 langParam = "lang" 13) 14 15type Module struct { 16 *pgs.ModuleBase 17 ctx pgsgo.Context 18} 19 20func Validator() pgs.Module { return &Module{ModuleBase: &pgs.ModuleBase{}} } 21 22func (m *Module) InitContext(ctx pgs.BuildContext) { 23 m.ModuleBase.InitContext(ctx) 24 m.ctx = pgsgo.InitContext(ctx.Parameters()) 25} 26 27func (m *Module) Name() string { return validatorName } 28 29func (m *Module) Execute(targets map[string]pgs.File, pkgs map[string]pgs.Package) []pgs.Artifact { 30 lang := m.Parameters().Str(langParam) 31 m.Assert(lang != "", "`lang` parameter must be set") 32 33 // Process file-level templates 34 tpls := templates.Template(m.Parameters())[lang] 35 m.Assert(tpls != nil, "could not find templates for `lang`: ", lang) 36 37 for _, f := range targets { 38 m.Push(f.Name().String()) 39 40 for _, msg := range f.AllMessages() { 41 m.CheckRules(msg) 42 } 43 44 for _, tpl := range tpls { 45 out := templates.FilePathFor(tpl)(f, m.ctx, tpl) 46 // A nil path means no output should be generated for this file - as controlled by 47 // implementation-specific FilePathFor implementations. 48 // Ex: Don't generate Java validators for files that don't reference PGV. 49 if out != nil { 50 if opts := f.Descriptor().GetOptions(); opts != nil && opts.GetJavaMultipleFiles() { 51 // TODO: Only Java supports multiple file generation. If more languages add multiple file generation 52 // support, the implementation should be made more inderect. 53 for _, msg := range f.Messages() { 54 m.AddGeneratorTemplateFile(java.JavaMultiFilePath(f, msg).String(), tpl, msg) 55 } 56 } else { 57 m.AddGeneratorTemplateFile(out.String(), tpl, f) 58 } 59 } 60 } 61 62 m.Pop() 63 } 64 65 return m.Artifacts() 66} 67 68var _ pgs.Module = (*Module)(nil) 69