/
validate.go
93 lines (74 loc) · 2.74 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package module
import (
"strings"
pgs "github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
"github.com/curl-li/protoc-gen-validate/templates"
"github.com/curl-li/protoc-gen-validate/templates/java"
)
const (
validatorName = "validator"
langParam = "lang"
moduleParam = "module"
formatParam = "format"
)
type Module struct {
*pgs.ModuleBase
ctx pgsgo.Context
formatCode bool
}
func Validator() pgs.Module { return &Module{ModuleBase: &pgs.ModuleBase{}} }
func (m *Module) InitContext(ctx pgs.BuildContext) {
m.ModuleBase.InitContext(ctx)
m.ctx = pgsgo.InitContext(ctx.Parameters())
}
func (m *Module) Name() string { return validatorName }
func (m *Module) Execute(targets map[string]pgs.File, pkgs map[string]pgs.Package) []pgs.Artifact {
lang := m.Parameters().Str(langParam)
m.Assert(lang != "", "`lang` parameter must be set")
format, err := m.Parameters().Bool(formatParam)
m.Assert(err == nil, "`format` parameter must be set true or false, default is false")
m.formatCode = format
module := m.Parameters().Str(moduleParam)
// Process file-level templates
tpls := templates.Template(m.Parameters())[lang]
m.Assert(tpls != nil, "could not find templates for `lang`: ", lang)
for _, f := range targets {
m.Push(f.Name().String())
for _, msg := range f.AllMessages() {
m.CheckRules(msg)
}
for _, tpl := range tpls {
out := templates.FilePathFor(tpl)(f, m.ctx, tpl)
// A nil path means no output should be generated for this file - as controlled by
// implementation-specific FilePathFor implementations.
// Ex: Don't generate Java validators for files that don't reference PGV.
if out != nil {
outPath := strings.TrimLeft(strings.ReplaceAll(out.String(), module, ""), "/")
if opts := f.Descriptor().GetOptions(); opts != nil && opts.GetJavaMultipleFiles() && lang == "java" {
// TODO: Only Java supports multiple file generation. If more languages add multiple file generation
// support, the implementation should be made more inderect.
for _, msg := range f.Messages() {
m.AddGeneratorTemplateFile(java.JavaMultiFilePath(f, msg).String(), tpl, msg)
}
} else {
m.AddGeneratorTemplateFile(outPath, tpl, f)
}
}
}
m.Pop()
}
return m.Artifacts()
}
// AddGeneratorTemplateFile behaves the same as AddGeneratorFile, however the
// contents are rendered from the provided tpl and data.
func (m *Module) AddGeneratorTemplateFile(name string, tpl pgs.Template, data interface{}) {
m.AddArtifact(pgs.GeneratorTemplateFile{
TemplateArtifact: pgs.TemplateArtifact{
Template: &templates.WrapTemplate{Template: tpl, ModuleBase: m.ModuleBase, Format: m.formatCode},
Data: data,
},
Name: name,
})
}
var _ pgs.Module = (*Module)(nil)