Skip to content

introduce a NOP template for cc #533

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

Merged
merged 3 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

config_setting(
name = "windows_x86_64",
values = {"cpu": "x64_windows"},
visibility = ["//visibility:public"],
)

string_flag(
name = "template-flavor",
build_setting_default = "",
visibility = ["//visibility:public"],
)

5 changes: 4 additions & 1 deletion bazel/protobuf.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_tools//tools/jdk:toolchain_utils.bzl", "find_java_runtime_toolchain", "find_java_toolchain")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")

Expand Down Expand Up @@ -44,6 +45,7 @@ def _output_dir(ctx):

def _protoc_gen_validate_cc_impl(ctx):
"""Generate C++ protos using protoc-gen-validate plugin"""
flavor = ctx.attr._flavor[BuildSettingInfo].value or ""
protos = _proto_sources(ctx)
out_files = []

Expand All @@ -54,7 +56,7 @@ def _protoc_gen_validate_cc_impl(ctx):
dir_out = _output_dir(ctx)

args = [
"--validate_out=lang=cc:" + dir_out,
"--validate_out=lang=cc" + flavor + ":" + dir_out,
]

return _protoc_gen_validate_impl(
Expand Down Expand Up @@ -119,6 +121,7 @@ cc_proto_gen_validate = rule(
allow_files = True,
executable = True,
),
"_flavor": attr.label(default = ":template-flavor"),
},
output_to_genfiles = True,
implementation = _protoc_gen_validate_cc_impl,
Expand Down
1 change: 1 addition & 0 deletions templates/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//templates/cc",
"//templates/ccnop",
"//templates/go",
"//templates/java",
"//templates/shared",
Expand Down
15 changes: 15 additions & 0 deletions templates/ccnop/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "ccnop",
srcs = [
"file.go",
"register.go",
],
importpath = "github.com/envoyproxy/protoc-gen-validate/templates/ccnop",
visibility = ["//visibility:public"],
deps = [
"//templates/cc",
"@com_github_lyft_protoc_gen_star//:protoc-gen-star",
],
)
56 changes: 56 additions & 0 deletions templates/ccnop/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ccnop

const headerFileTpl = `// Code generated by protoc-gen-validate
// source: {{ .InputPath }}
// DO NOT EDIT!!!

#pragma once

#include <string>

#include "validate/validate.h"
#include "{{ output .File ".h" }}"

{{ range .Package.ProtoName.Split }}
namespace {{ . }} {
{{- end }}

using std::string;

{{ range .AllMessages }}
extern inline bool Validate(__attribute__((unused)) const {{ class . }}& m, __attribute__((unused)) pgv::ValidationMsg* err) { return true; }
{{ end }}

{{ range .Package.ProtoName.Split -}}
} // namespace
{{ end }}

#define X_{{ .Package.ProtoName.ScreamingSnakeCase }}_{{ .File.InputPath.BaseName | screaming_snake_case }}(X) \
{{ range .AllMessages -}}
{{- if not (ignored .) -}}
X({{class . }}) \
{{ end -}}
{{ end }}
`

const moduleFileTpl = `// Code generated by protoc-gen-validate
// source: {{ .InputPath }}
// DO NOT EDIT!!!

#include "{{ output .File ".validate.h" }}"

namespace pgv {
namespace validate {
using std::string;

{{ range .AllMessages }}
{{- if not (ignored .) -}}
{{- if not (disabled .) -}}
pgv::Validator<{{ class . }}> {{ staticVarName . }}(static_cast<bool(*)(const {{ class .}}&, pgv::ValidationMsg*)>({{ package .}}::Validate));
{{- end -}}
{{ end }}
{{ end }}

} // namespace validate
} // namespace pgv
`
18 changes: 18 additions & 0 deletions templates/ccnop/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ccnop

import (
"text/template"

"github.com/envoyproxy/protoc-gen-validate/templates/cc"
pgs "github.com/lyft/protoc-gen-star"
)

func RegisterModule(tpl *template.Template, params pgs.Parameters) {
cc.RegisterModule(tpl, params)
template.Must(tpl.Parse(moduleFileTpl))
}

func RegisterHeader(tpl *template.Template, params pgs.Parameters) {
cc.RegisterHeader(tpl, params)
template.Must(tpl.Parse(headerFileTpl))
}
10 changes: 7 additions & 3 deletions templates/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"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/ccnop"
"github.com/envoyproxy/protoc-gen-validate/templates/go"
"github.com/envoyproxy/protoc-gen-validate/templates/java"
"github.com/envoyproxy/protoc-gen-validate/templates/shared"
Expand All @@ -23,16 +24,19 @@ func makeTemplate(ext string, fn RegisterFn, params pgs.Parameters) *template.Te

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)},
"cc": {makeTemplate("h", cc.RegisterHeader, params), makeTemplate("cc", cc.RegisterModule, params)},
"ccnop": {makeTemplate("h", ccnop.RegisterHeader, params), makeTemplate("cc", ccnop.RegisterModule, params)},
"go": {makeTemplate("go", golang.Register, params)},
"java": {makeTemplate("java", java.Register, params)},
}
}

func FilePathFor(tpl *template.Template) FilePathFn {
switch tpl.Name() {
case "h":
return cc.CcFilePath
case "ccnop":
return cc.CcFilePath
case "cc":
return cc.CcFilePath
case "java":
Expand Down