pkg.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package templates
  2. import (
  3. "text/template"
  4. "github.com/lyft/protoc-gen-star"
  5. "github.com/lyft/protoc-gen-star/lang/go"
  6. "github.com/envoyproxy/protoc-gen-validate/templates/cc"
  7. "github.com/envoyproxy/protoc-gen-validate/templates/go"
  8. "github.com/envoyproxy/protoc-gen-validate/templates/java"
  9. "github.com/envoyproxy/protoc-gen-validate/templates/shared"
  10. )
  11. type RegisterFn func(tpl *template.Template, params pgs.Parameters)
  12. type FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath
  13. func makeTemplate(ext string, fn RegisterFn, params pgs.Parameters) *template.Template {
  14. tpl := template.New(ext)
  15. shared.RegisterFunctions(tpl, params)
  16. fn(tpl, params)
  17. return tpl
  18. }
  19. func Template(params pgs.Parameters) map[string][]*template.Template {
  20. return map[string][]*template.Template{
  21. "cc": {makeTemplate("h", cc.RegisterHeader, params), makeTemplate("cc", cc.RegisterModule, params)},
  22. "go": {makeTemplate("go", golang.Register, params)},
  23. "java": {makeTemplate("java", java.Register, params)},
  24. }
  25. }
  26. func FilePathFor(tpl *template.Template) FilePathFn {
  27. switch tpl.Name() {
  28. case "h":
  29. return cc.CcFilePath
  30. case "cc":
  31. return cc.CcFilePath
  32. case "java":
  33. return java.JavaFilePath
  34. default:
  35. return func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath {
  36. out := ctx.OutputPath(f)
  37. out = out.SetExt(".validate." + tpl.Name())
  38. return &out
  39. }
  40. }
  41. }