From f73ec5a7ce82b3f9fb47f01d4629aa4617247af5 Mon Sep 17 00:00:00 2001 From: Duco van Amstel Date: Tue, 24 Dec 2019 15:54:53 +0100 Subject: [PATCH] Finalise Bazel support --- .gitignore | 2 + BUILD.bazel | 78 +++++++++++++++++++++++++++---- WORKSPACE | 67 +++++++++++++++++++++++++- go.mod | 2 + plugin/BUILD.bazel | 6 +-- test/BUILD.bazel | 36 ++++++++++++++ test/gogo/BUILD.bazel | 28 +++++++++++ test/golang/BUILD.bazel | 31 ++++++++++++ test/validator_proto3_map.proto | 3 -- test/validator_proto3_oneof.proto | 1 - 10 files changed, 237 insertions(+), 17 deletions(-) create mode 100644 test/BUILD.bazel create mode 100644 test/gogo/BUILD.bazel create mode 100644 test/golang/BUILD.bazel diff --git a/.gitignore b/.gitignore index fb1a95c..678e8bb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ # Build and script artefacts. deps/ +bazel-* + diff --git a/BUILD.bazel b/BUILD.bazel index ba5f112..b0e6965 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,9 +1,17 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") load("@rules_proto//proto:defs.bzl", "proto_library") -proto_library( - name = "validator_proto", +filegroup( + name = "validators", srcs = ["validator.proto"], + visibility = ["//visibility:public"], +) + +proto_library( + name = "validators_proto", + srcs = [":validators"], import_prefix = "github.com/mwitkow/go-proto-validators", deps = [ "@com_google_protobuf//:descriptor_proto", @@ -11,16 +19,70 @@ proto_library( visibility = ["//visibility:public"], ) -go_library( - name = "go_default_library", - srcs = [ - "helper.go", - "validator.pb.go", +go_proto_compiler( + name = "go_proto_validators", + plugin = "//protoc-gen-govalidators", + options = ["gogoimport=false"], + suffix = ".validator.pb.go", + valid_archive = False, + deps = [ + ":validators_golang", + "@com_github_golang_protobuf//protoc-gen-go/descriptor:go_default_library", + ], + visibility = ["//visibility:public"], +) + +go_proto_compiler( + name = "gogo_proto_validators", + plugin = "//protoc-gen-govalidators", + options = ["gogoimport=true"], + suffix = ".validator.pb.go", + valid_archive = False, + deps = [ + ":validators_gogo", + "@com_github_gogo_protobuf//protoc-gen-gogo/descriptor:go_default_library", ], + visibility = ["//visibility:public"], +) + +go_proto_library( + name = "_validators_gogo", importpath = "github.com/mwitkow/go-proto-validators", + proto = ":validators_proto", + compilers = [ + "@io_bazel_rules_go//proto:gogo_proto", + ], deps = [ - "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_gogo_protobuf//protoc-gen-gogo/descriptor:go_default_library", ], + visibility = ["//:__pkg__"], +) + +go_proto_library( + name = "_validators_golang", + importpath = "github.com/mwitkow/go-proto-validators", + proto = ":validators_proto", + compilers = [ + "@io_bazel_rules_go//proto:go_proto", + ], + deps = [ + "@com_github_golang_protobuf//protoc-gen-go/descriptor:go_default_library", + ], + visibility = ["//:__pkg__"], +) + +go_library( + name = "validators_gogo", + importpath = "github.com/mwitkow/go-proto-validators", + srcs = ["helper.go"], + embed = [":_validators_gogo"], + visibility = ["//visibility:public"], +) + +go_library( + name = "validators_golang", + importpath = "github.com/mwitkow/go-proto-validators", + srcs = ["helper.go"], + embed = [":_validators_golang"], visibility = ["//visibility:public"], ) diff --git a/WORKSPACE b/WORKSPACE index d0b78e8..35acb4b 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -2,7 +2,7 @@ workspace(name = "com_github_mwitkow_go_proto_validators") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -GO_VERSION="1.13.5" +GO_VERSION = "1.13.5" http_archive( name = "rules_proto", @@ -15,10 +15,15 @@ http_archive( http_archive( name = "com_google_protobuf", + sha256 = "678d91d8a939a1ef9cb268e1f20c14cd55e40361dc397bb5881e4e1e532679b1", strip_prefix = "protobuf-3.10.1", url = "https://github.com/protocolbuffers/protobuf/archive/v3.10.1.zip", ) +load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") + +protobuf_deps() + http_archive( name = "io_bazel_rules_go", sha256 = "d5de13e9a994527b6dc41f39ad9ceee3214974dacb18f73a5fa2a4458ae6d3c9", @@ -34,6 +39,7 @@ go_register_toolchains(go_version = GO_VERSION) http_archive( name = "bazel_gazelle", + sha256 = "d987004a72697334a095bbaa18d615804a28280201a50ed6c234c40ccc41e493", strip_prefix = "bazel-gazelle-0.19.1", urls = [ "https://github.com/bazelbuild/bazel-gazelle/archive/v0.19.1.tar.gz", @@ -47,5 +53,62 @@ gazelle_dependencies() go_repository( name = "com_github_gogo_protobuf", importpath = "github.com/gogo/protobuf", - version = "v1.3.1", + sum = "h1:G8O7TerXerS4F6sx9OV7/nRfJdnXgHZu/S/7F2SN+UE=", + version = "v1.3.0", +) + +go_repository( + name = "com_github_davecgh_go_spew", + importpath = "github.com/davecgh/go-spew", + sum = "h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=", + version = "v1.1.0", +) + +go_repository( + name = "com_github_golang_protobuf", + importpath = "github.com/golang/protobuf", + sum = "h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=", + version = "v1.3.2", +) + +go_repository( + name = "com_github_kisielk_errcheck", + importpath = "github.com/kisielk/errcheck", + sum = "h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E=", + version = "v1.2.0", +) + +go_repository( + name = "com_github_kisielk_gotool", + importpath = "github.com/kisielk/gotool", + sum = "h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg=", + version = "v1.0.0", +) + +go_repository( + name = "com_github_pmezard_go_difflib", + importpath = "github.com/pmezard/go-difflib", + sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=", + version = "v1.0.0", +) + +go_repository( + name = "com_github_stretchr_objx", + importpath = "github.com/stretchr/objx", + sum = "h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=", + version = "v0.1.0", +) + +go_repository( + name = "com_github_stretchr_testify", + importpath = "github.com/stretchr/testify", + sum = "h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=", + version = "v1.3.0", +) + +go_repository( + name = "org_golang_x_tools", + importpath = "golang.org/x/tools", + sum = "h1:NIou6eNFigscvKJmsbyez16S2cIS6idossORlFtSt2E=", + version = "v0.0.0-20181030221726-6c7e314b6563", ) diff --git a/go.mod b/go.mod index 3a23ae7..1668aa1 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ module github.com/mwitkow/go-proto-validators +go 1.13 + require ( github.com/gogo/protobuf v1.3.0 github.com/golang/protobuf v1.3.2 diff --git a/plugin/BUILD.bazel b/plugin/BUILD.bazel index 24fbb44..7539580 100644 --- a/plugin/BUILD.bazel +++ b/plugin/BUILD.bazel @@ -2,15 +2,15 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", - srcs = ["plugin.go"], importpath = "github.com/mwitkow/go-proto-validators/plugin", - visibility = ["//visibility:public"], + srcs = ["plugin.go"], deps = [ - "//:go_default_library", + "//:validators_gogo", "@com_github_gogo_protobuf//gogoproto:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_gogo_protobuf//protoc-gen-gogo/descriptor:go_default_library", "@com_github_gogo_protobuf//protoc-gen-gogo/generator:go_default_library", "@com_github_gogo_protobuf//vanity:go_default_library", ], + visibility = ["//visibility:public"], ) diff --git a/test/BUILD.bazel b/test/BUILD.bazel new file mode 100644 index 0000000..83cf0d7 --- /dev/null +++ b/test/BUILD.bazel @@ -0,0 +1,36 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "proto2", + srcs = ["validator_proto2.proto"], + deps = [ + "//:validators_proto", + "@gogo_special_proto//github.com/gogo/protobuf/gogoproto", + ], + visibility = ["//test:__subpackages__"], +) + +proto_library( + name = "proto3", + srcs = ["validator_proto3.proto"], + deps = [ + "//:validators_proto", + "@gogo_special_proto//github.com/gogo/protobuf/gogoproto", + ], + visibility = ["//test:__subpackages__"], +) + +proto_library( + name = "proto3_oneof", + srcs = ["validator_proto3_oneof.proto"], + deps = [ + "//:validators_proto", + ], + visibility = ["//test:__subpackages__"], +) + +proto_library( + name = "proto3_map", + srcs = ["validator_proto3_map.proto"], + visibility = ["//test:__subpackages__"], +) diff --git a/test/gogo/BUILD.bazel b/test/gogo/BUILD.bazel new file mode 100644 index 0000000..9da8480 --- /dev/null +++ b/test/gogo/BUILD.bazel @@ -0,0 +1,28 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") + +go_proto_library( + name = "gogo_proto", + importpath = "dummy", + protos = [ + "//test:proto2", + "//test:proto3", + "//test:proto3_oneof", + "//test:proto3_map", + ], + compilers = [ + "//:gogo_proto_validators", + "@io_bazel_rules_go//proto:gogo_proto", + ], + visibility = [":__pkg__"] +) + +go_test( + name = "go_default_test", + importpath = "dummy", + srcs = ["validator_test.go"], + embed = [":gogo_proto"], + deps = [ + "@com_github_stretchr_testify//assert:go_default_library", + ], +) diff --git a/test/golang/BUILD.bazel b/test/golang/BUILD.bazel new file mode 100644 index 0000000..57ffc7b --- /dev/null +++ b/test/golang/BUILD.bazel @@ -0,0 +1,31 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") + +go_proto_library( + name = "go_proto", + importpath = "dummy", + protos = [ + "//test:proto2", + "//test:proto3", + "//test:proto3_oneof", + "//test:proto3_map", + ], + compilers = [ + "//:go_proto_validators", + "@io_bazel_rules_go//proto:go_proto", + ], + deps = [ + "@com_github_gogo_protobuf//gogoproto:go_default_library", + ], + visibility = [":__pkg__"] +) + +go_test( + name = "go_default_test", + importpath = "dummy", + srcs = ["validator_test.go"], + embed = [":go_proto"], + deps = [ + "@com_github_stretchr_testify//assert:go_default_library", + ], +) diff --git a/test/validator_proto3_map.proto b/test/validator_proto3_map.proto index f8799b1..ad74629 100644 --- a/test/validator_proto3_map.proto +++ b/test/validator_proto3_map.proto @@ -4,9 +4,6 @@ syntax = "proto3"; package validatortest; -import "github.com/gogo/protobuf/gogoproto/gogo.proto"; -import "github.com/mwitkow/go-proto-validators/validator.proto"; - message ValueType { string something = 1 ; } diff --git a/test/validator_proto3_oneof.proto b/test/validator_proto3_oneof.proto index 736dd82..8042963 100644 --- a/test/validator_proto3_oneof.proto +++ b/test/validator_proto3_oneof.proto @@ -4,7 +4,6 @@ syntax = "proto3"; package validatortest; -import "github.com/gogo/protobuf/gogoproto/gogo.proto"; import "github.com/mwitkow/go-proto-validators/validator.proto"; message ExternalMsg {