-
Notifications
You must be signed in to change notification settings - Fork 603
make templates self-contained and pluginable #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ go_library( | |
| "msg.go", | ||
| "none.go", | ||
| "num.go", | ||
| "plugin.go", | ||
| "register.go", | ||
| "repeated.go", | ||
| "string.go", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package cc | ||
|
|
||
| import ( | ||
| "text/template" | ||
|
|
||
| "github.com/envoyproxy/protoc-gen-validate/templates/shared" | ||
| pgs "github.com/lyft/protoc-gen-star" | ||
| ) | ||
|
|
||
| const PluginName = "cc" | ||
|
|
||
| func MakePlugin(params pgs.Parameters) *shared.TemplatePlugin { | ||
| return &shared.TemplatePlugin{ | ||
| Templates: []*template.Template{ | ||
| shared.MakeTemplate("h", registerHeader, params), | ||
| shared.MakeTemplate("cc", registerModule, params), | ||
| }, | ||
| PathFunction: ccFilePath, | ||
| Name: PluginName, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package golang | ||
|
|
||
| import ( | ||
| "text/template" | ||
|
|
||
| "github.com/envoyproxy/protoc-gen-validate/templates/shared" | ||
| pgs "github.com/lyft/protoc-gen-star" | ||
| pgsgo "github.com/lyft/protoc-gen-star/lang/go" | ||
| ) | ||
|
|
||
| const PluginName = "go" | ||
|
|
||
| func MakePlugin(params pgs.Parameters) *shared.TemplatePlugin { | ||
| return &shared.TemplatePlugin{ | ||
| Templates: []*template.Template{shared.MakeTemplate("go", register, params)}, | ||
| PathFunction: func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath { | ||
| out := ctx.OutputPath(f) | ||
| out = out.SetExt(".validate." + tpl.Name()) | ||
| return &out | ||
| }, | ||
| Name: PluginName, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package java | ||
|
|
||
| import ( | ||
| "text/template" | ||
|
|
||
| "github.com/envoyproxy/protoc-gen-validate/templates/shared" | ||
| pgs "github.com/lyft/protoc-gen-star" | ||
| ) | ||
|
|
||
| const PluginName = "java" | ||
|
|
||
| func MakePlugin(params pgs.Parameters) *shared.TemplatePlugin { | ||
| return &shared.TemplatePlugin{ | ||
| Templates: []*template.Template{shared.MakeTemplate("java", register, params)}, | ||
| PathFunction: javaFilePath, | ||
| Name: PluginName, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,46 @@ | ||
| package templates | ||
|
|
||
| import ( | ||
| "text/template" | ||
| "errors" | ||
| "plugin" | ||
|
|
||
| "github.com/lyft/protoc-gen-star" | ||
| "github.com/lyft/protoc-gen-star/lang/go" | ||
| "github.com/envoyproxy/protoc-gen-validate/templates/cc" | ||
| "github.com/envoyproxy/protoc-gen-validate/templates/go" | ||
| golang "github.com/envoyproxy/protoc-gen-validate/templates/go" | ||
| "github.com/envoyproxy/protoc-gen-validate/templates/java" | ||
| "github.com/envoyproxy/protoc-gen-validate/templates/shared" | ||
| pgs "github.com/lyft/protoc-gen-star" | ||
| ) | ||
|
|
||
| type RegisterFn func(tpl *template.Template, params pgs.Parameters) | ||
| type FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath | ||
| type makePluginFn func(params pgs.Parameters) *shared.TemplatePlugin | ||
|
|
||
| func makeTemplate(ext string, fn RegisterFn, params pgs.Parameters) *template.Template { | ||
| tpl := template.New(ext) | ||
| shared.RegisterFunctions(tpl, params) | ||
| fn(tpl, params) | ||
| return tpl | ||
| func MakeTemplateForLang(params pgs.Parameters, lang string) *shared.TemplatePlugin { | ||
| switch lang { | ||
| case cc.PluginName: | ||
| return cc.MakePlugin(params) | ||
| case golang.PluginName: | ||
| return golang.MakePlugin(params) | ||
| case java.PluginName: | ||
| return java.MakePlugin(params) | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func Template(params pgs.Parameters) map[string][]*template.Template { | ||
| return map[string][]*template.Template{ | ||
| "cc": {makeTemplate("h", cc.RegisterHeader, params), makeTemplate("cc", cc.RegisterModule, params)}, | ||
| "go": {makeTemplate("go", golang.Register, params)}, | ||
| "java": {makeTemplate("java", java.Register, params)}, | ||
| func MakeTemplateFromPlugin(path string, params pgs.Parameters) (*shared.TemplatePlugin, error) { | ||
| plug, err := plugin.Open(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| func FilePathFor(tpl *template.Template) FilePathFn { | ||
| switch tpl.Name() { | ||
| case "h": | ||
| return cc.CcFilePath | ||
| case "cc": | ||
| return cc.CcFilePath | ||
| case "java": | ||
| return java.JavaFilePath | ||
| default: | ||
| return func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath { | ||
| out := ctx.OutputPath(f) | ||
| out = out.SetExt(".validate." + tpl.Name()) | ||
| return &out | ||
| } | ||
| symPlugin, err := plug.Lookup("Plugin") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| makePlugin, ok := symPlugin.(makePluginFn) | ||
| if !ok { | ||
| return nil, errors.New("loaded object has an incorrect type, expected: *TemplatePlugin") | ||
| } | ||
|
|
||
| return makePlugin(params), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package shared | ||
|
|
||
| import ( | ||
| "text/template" | ||
|
|
||
| pgs "github.com/lyft/protoc-gen-star" | ||
| pgsgo "github.com/lyft/protoc-gen-star/lang/go" | ||
| ) | ||
|
|
||
| type RegisterFn func(tpl *template.Template, params pgs.Parameters) | ||
| type FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath | ||
|
|
||
| type TemplatePlugin struct { | ||
| Templates []*template.Template | ||
| PathFunction FilePathFn | ||
| Name string | ||
| } | ||
|
|
||
| func MakeTemplate(ext string, fn RegisterFn, params pgs.Parameters) *template.Template { | ||
| tpl := template.New(ext) | ||
| RegisterFunctions(tpl, params) | ||
| fn(tpl, params) | ||
| return tpl | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an important change for go plugins to work. see more info about this on: bazel-contrib/rules_go#2994
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the rules_go fix is now merged!