From 40780f6747966b2536250d3f2414285cdadd4fe9 Mon Sep 17 00:00:00 2001 From: Privacy Sandbox Team Date: Wed, 16 Oct 2024 16:50:08 -0400 Subject: [PATCH] feat: Add generated proto for sample BYOB UDF to repo Bug: b/374288740 Change-Id: I7a9160726a760b3f4ff904572facfd61e862c2da GitOrigin-RevId: ac4654084f80a145d4c4462a649ef67ee6f00d91 --- src/roma/byob/sample_udf/BUILD.bazel | 9 +- .../byob/sample_udf/generated/sample.proto | 175 ++++++++++++++++++ src/roma/byob/sample_udf/sample.proto | 5 +- src/roma/tools/api_plugin/roma_api.bzl | 12 ++ .../tmpl/byob/app/udf_interface.proto.tmpl | 92 +++++---- 5 files changed, 237 insertions(+), 56 deletions(-) create mode 100644 src/roma/byob/sample_udf/generated/sample.proto diff --git a/src/roma/byob/sample_udf/BUILD.bazel b/src/roma/byob/sample_udf/BUILD.bazel index e47dd5b1..13194f3a 100644 --- a/src/roma/byob/sample_udf/BUILD.bazel +++ b/src/roma/byob/sample_udf/BUILD.bazel @@ -36,9 +36,7 @@ proto_library( cc_proto_library( name = "sample_cc_proto", visibility = ["//src/roma/byob:__subpackages__"], - deps = [ - ":sample_proto", - ], + deps = [":sample_proto"], ) buf_lint_test( @@ -58,9 +56,7 @@ proto_library( cc_proto_library( name = "sample_callback_cc_proto", visibility = ["//src/roma/byob:__subpackages__"], - deps = [ - ":sample_callback_proto", - ], + deps = [":sample_callback_proto"], ) buf_lint_test( @@ -95,6 +91,7 @@ sample_api = declare_roma_api( roma_byob_sdk( name = "sample_byob_sdk", + generated_proto_path = "generated/sample.proto", roma_app_api = sample_api, visibility = ["//visibility:public"], ) diff --git a/src/roma/byob/sample_udf/generated/sample.proto b/src/roma/byob/sample_udf/generated/sample.proto new file mode 100644 index 00000000..2afb2786 --- /dev/null +++ b/src/roma/byob/sample_udf/generated/sample.proto @@ -0,0 +1,175 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +// Generated from: src/roma/byob/sample_udf/sample.proto +// +// See /docs/Guide to the SDK.md for information on using this spec. +// +// +// SampleServer UDF Spec +// A sample server showcasing declarative APIs for BYOB +// + +syntax = "proto3"; + +package privacy_sandbox.roma_byob.example; + + + + +// +// UDF rpc: Sample +// The Sample method MethodAnnotation. Add some information about this rpc +// method. +// request: SampleRequest +// response: SampleResponse + +// SampleRequest specifies a single function to execute. +message SampleRequest { + // The function to be executed + FunctionType function = 1; +} + +// SampleResponse returns the function's output. +message SampleResponse { + // The payload generated by the function. + string greeting = 1; + // A list of prime numbers. + repeated int32 prime_number = 2; +} + +// +// UDF rpc: ReadPayload +// MethodAnnotation. Add some information about this rpc method. +// request: ReadPayloadRequest +// response: ReadPayloadResponse + + +message ReadPayloadRequest { + // The payloads sent into the UDF. + repeated bytes payloads = 1; +} + + +message ReadPayloadResponse { + // The size of the payloads sent to the UDF. + uint64 payload_size = 1; +} + +// +// UDF rpc: GeneratePayload +// MethodAnnotation. Add some information about this rpc method. +// request: GeneratePayloadRequest +// response: GeneratePayloadResponse + + +message GeneratePayloadRequest { + // The size of the payload elements for the UDF to generate. + uint64 element_size = 1; + // The number of the payload elements for the UDF to generate. + uint64 element_count = 2; +} + + +message GeneratePayloadResponse { + // The payloads generated by the UDF. + repeated bytes payloads = 1; +} + +// +// UDF rpc: ReadCallbackPayload +// MethodAnnotation +// request: ReadCallbackPayloadRequest +// response: ReadCallbackPayloadResponse + + +message ReadCallbackPayloadRequest { + // The size of the payload elements to generate as input to the Host API + // function. + uint64 element_size = 1; + // The number of the payload elements to generate as input to the Host API + // function. + uint64 element_count = 2; +} + + +message ReadCallbackPayloadResponse { + // The size of the payloads sent to the Host API function. + uint64 payload_size = 1; +} + +// +// UDF rpc: WriteCallbackPayload +// MethodAnnotation +// request: WriteCallbackPayloadRequest +// response: WriteCallbackPayloadResponse + + +message WriteCallbackPayloadRequest { + // The size of the payload elements for the Host API function to generate. + uint64 element_size = 1; + // The number of the payload elements for the Host API function to generate. + uint64 element_count = 2; +} + + +message WriteCallbackPayloadResponse { + // The size of the payloads returned by the Host API function. + uint64 payload_size = 1; +} + +// +// UDF rpc: RunPrimeSieve +// MethodAnnotation +// request: RunPrimeSieveRequest +// response: RunPrimeSieveResponse + + +message RunPrimeSieveRequest { + // The count of prime numbers to generate. + int32 prime_count = 1; +} + + +message RunPrimeSieveResponse { + // The Nth (and largest) of the prime numbers generated. + int32 largest_prime = 1; +} + +// +// UDF rpc: SortList +// MethodAnnotation +// request: SortListRequest +// response: SortListResponse + + +message SortListRequest { +} + + +message SortListResponse { +} + + +// Functions that can be executed by the Sample UDF. +enum FunctionType { + FUNCTION_UNSPECIFIED = 0; + // The hello world function. + FUNCTION_HELLO_WORLD = 1; + // The sieve-based prime generator function. + FUNCTION_PRIME_SIEVE = 2; + FUNCTION_CALLBACK = 3; + FUNCTION_TEN_CALLBACK_INVOCATIONS = 4; +} diff --git a/src/roma/byob/sample_udf/sample.proto b/src/roma/byob/sample_udf/sample.proto index 8b13c210..493a081b 100644 --- a/src/roma/byob/sample_udf/sample.proto +++ b/src/roma/byob/sample_udf/sample.proto @@ -21,10 +21,9 @@ import "apis/privacysandbox/apis/roma/app_api/v1/options.proto"; service SampleService { option (privacysandbox.apis.roma.app_api.v1.roma_svc_annotation) = { name: 'Sample Server', - code_id: "app_api_roma_app_test_v1", + roma_app_name: 'SampleServer', description: 'A sample server showcasing declarative APIs for BYOB', - cpp_namespace: 'privacy_sandbox::roma_byob::example', - roma_app_name: 'SampleServer' + cpp_namespace: 'privacy_sandbox::roma_byob::example' }; rpc Sample(SampleRequest) returns (SampleResponse) { diff --git a/src/roma/tools/api_plugin/roma_api.bzl b/src/roma/tools/api_plugin/roma_api.bzl index ceaef8c9..71b5342a 100644 --- a/src/roma/tools/api_plugin/roma_api.bzl +++ b/src/roma/tools/api_plugin/roma_api.bzl @@ -14,6 +14,7 @@ """Macro for the Roma Application API.""" +load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file") load("@bazel_skylib//rules:copy_file.bzl", "copy_file") load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@com_google_googleapis_imports//:imports.bzl", "cc_proto_library") @@ -861,6 +862,7 @@ def roma_byob_sdk( extra_docs = [], exclude_tools = False, guide_intro_text = _default_guide_intro, + generated_proto_path = "", **kwargs): """ Top-level macro for the Roma BYOB SDK. @@ -873,6 +875,9 @@ def roma_byob_sdk( extra_docs: a list of declare_doc-created structs exclude_tools: bool controlling inclusion of SDK tools guide_intro_text: string containing markdown text for the guide introduction + generated_proto_path: path to the workspace source location to store the + generated protobuf spec. Must be in the same package as the sdk + build target. **kwargs: attributes common to bazel build rules. Targets: @@ -912,6 +917,13 @@ def roma_byob_sdk( config = Label("//src:buf.yaml"), targets = [":{}_proto".format(name)], ) + if generated_proto_path: + write_source_file( + name = name + "_write_proto", + check_that_out_file_exists = False, + in_file = ":{}.proto".format(name), + out_file = generated_proto_path, + ) pkg_files( name = name + "_specs", srcs = [":{}.proto".format(name)], diff --git a/src/roma/tools/api_plugin/tmpl/byob/app/udf_interface.proto.tmpl b/src/roma/tools/api_plugin/tmpl/byob/app/udf_interface.proto.tmpl index c1b3b348..d4be0965 100644 --- a/src/roma/tools/api_plugin/tmpl/byob/app/udf_interface.proto.tmpl +++ b/src/roma/tools/api_plugin/tmpl/byob/app/udf_interface.proto.tmpl @@ -21,23 +21,23 @@ // // See /docs/Guide to the SDK.md for information on using this spec. // -{{- $svc := index $file.Services 0}} -{{- $svcopts := index $svc.Options "privacysandbox.apis.roma.app_api.v1.roma_svc_annotation"}} +{{- $svc := index $file.Services 0 }} +{{- $svcopts := index $svc.Options "privacysandbox.apis.roma.app_api.v1.roma_svc_annotation" }} // // {{$svcopts.RomaAppName}} UDF Spec -{{ if len $svcopts.Description}}// {{$svcopts.Description | wrapWith 75 "\n// "}}{{- end}} +{{ if len $svcopts.Description }}// {{ $svcopts.Description | wrapWith 75 "\n// " }}{{- end }} // syntax = "proto3"; package {{$svcopts.CppNamespace | replace "::" "." }}; -{{if len $svcopts.CsharpNamespace}}option csharp_namespace = "{{$svcopts.CsharpNamespace}}";{{end}} -{{if len $svcopts.GoPackage}}option go_package = "{{$svcopts.GoPackage}}";{{end}} +{{ if len $svcopts.CsharpNamespace }}option csharp_namespace = "{{ $svcopts.CsharpNamespace }}";{{ end }} +{{ if len $svcopts.GoPackage }}option go_package = "{{ $svcopts.GoPackage }}";{{ end }} {{- range $rpc := $svc.MethodsWithOption "privacysandbox.apis.roma.app_api.v1.roma_rpc_annotation"}} -{{- $rpcopts := index $rpc.Options "privacysandbox.apis.roma.app_api.v1.roma_rpc_annotation"}} +{{- $rpcopts := index $rpc.Options "privacysandbox.apis.roma.app_api.v1.roma_rpc_annotation" }} {{- $reqType := $rpc.RequestType }} {{- $respType := $rpc.ResponseType }} {{- $reqFullType := $rpc.RequestFullType | printf "proto.%s" }} @@ -54,78 +54,76 @@ package {{$svcopts.CppNamespace | replace "::" "." }}; {{- $msgopts := index $msg.Options "privacysandbox.apis.roma.app_api.v1.roma_mesg_annotation"}} {{- if eq $msg.LongName $reqType}} {{- $_ := set $rpcMsgs $reqType $reqType}} -{{- if and $msgopts (len $msgopts.Description)}}// {{$msgopts.Description | wrapWith 75 "\n// "}}{{- end}} +{{- if and $msgopts (len $msgopts.Description)}}// {{$msgopts.Description | wrapWith 75 "\n// "}}{{- end }} message {{$msg.LongName}} { {{- if $msg.HasFields}} {{- range $fldNum, $fld := $msg.Fields}} {{- $fldopts := index $fld.Options "privacysandbox.apis.roma.app_api.v1.roma_field_annotation"}} {{- if and $fldopts (len $fldopts.Description)}} // {{$fldopts.Description | wrapWith 75 "\n // "}} -{{- end}} - {{if eq $fld.Label "repeated"}}repeated {{end}}{{$fld.LongType}} {{$fld.Name}} = {{add $fldNum 1}}; -{{- end}} -{{- end}}{{/*fields*/}} +{{- end }} + {{if eq $fld.Label "repeated"}}repeated {{end }}{{$fld.LongType}} {{$fld.Name}} = {{add $fldNum 1}}; +{{- end }} +{{- end }}{{/*fields*/}} } -{{- end}} -{{- end}}{{/*messages*/}} +{{- end }} +{{- end }}{{/*messages*/}} {{/*response*/}} {{- range $msg := $file.Messages}} {{- $msgopts := index $msg.Options "privacysandbox.apis.roma.app_api.v1.roma_mesg_annotation"}} {{- if eq $msg.LongName $respType}} {{- $_ := set $rpcMsgs $respType $respType}} -{{- if and $msgopts (len $msgopts.Description)}}// {{$msgopts.Description | wrapWith 75 "\n// "}}{{- end}} +{{- if and $msgopts (len $msgopts.Description)}}// {{$msgopts.Description | wrapWith 75 "\n// "}}{{- end }} message {{$msg.LongName}} { {{- if $msg.HasFields}} {{- range $fldNum, $fld := $msg.Fields}} {{- $fldopts := index $fld.Options "privacysandbox.apis.roma.app_api.v1.roma_field_annotation"}} {{- if and $fldopts (len $fldopts.Description)}} // {{$fldopts.Description | wrapWith 75 "\n // "}} -{{- end}} - {{if eq $fld.Label "repeated"}}repeated {{end}}{{$fld.LongType}} {{$fld.Name}} = {{add $fldNum 1}}; -{{- end}} -{{- end}}{{/*fields*/}} +{{- end }} + {{if eq $fld.Label "repeated"}}repeated {{end }}{{$fld.LongType}} {{$fld.Name}} = {{add $fldNum 1}}; +{{- end }} +{{- end }}{{/*fields*/}} } -{{- end}} -{{- end}}{{/*messages*/}} -{{- end}}{{/*rpcs*/}} - +{{- end }} +{{- end }}{{/*messages*/}} +{{- end }}{{/*rpcs*/}} {{- if len $file.Messages}} + {{- range $msg := $file.Messages}} {{- $msgopts := index $msg.Options "privacysandbox.apis.roma.app_api.v1.roma_mesg_annotation"}} {{- if eq (len (get $rpcMsgs $msg.LongName)) 0}} -{{- if and $msgopts (len $msgopts.Description)}}// {{$msgopts.Description | wrapWith 75 "\n// "}}{{- end}} +{{- if and $msgopts (len $msgopts.Description)}}// {{$msgopts.Description | wrapWith 75 "\n// "}}{{- end }} message {{$msg.LongName}} { {{- if $msg.HasFields}} {{- range $fldNum, $fld := $msg.Fields}} {{- $fldopts := index $fld.Options "privacysandbox.apis.roma.app_api.v1.roma_field_annotation"}} {{- if and $fldopts (len $fldopts.Description)}} // {{$fldopts.Description | wrapWith 75 "\n // "}} -{{- end}} - {{if eq $fld.Label "repeated"}}repeated {{end}}{{$fld.LongType}} {{$fld.Name}} = {{add $fldNum 1}}; -{{end}} -{{- end}}{{/*fields*/}} +{{- end }} + {{if eq $fld.Label "repeated"}}repeated {{end }}{{$fld.LongType}} {{$fld.Name}} = {{add $fldNum 1}}; +{{end }} +{{- end }}{{/*fields*/}} } -{{end}} -{{- end}} -{{- end}}{{/*messages*/}} +{{end }} +{{- end }} +{{- end }}{{/*messages*/}} +{{- if len $file.Enums}} -{{/*enums*/}} -{{- if len $file.Enums}} -{{- range $enum := $file.Enums}} -{{- $enumopts := index $enum.Options "privacysandbox.apis.roma.app_api.v1.roma_enum_annotation"}} -{{- if and $enumopts (len $enumopts.Description)}}// {{$enumopts.Description | wrapWith 75 "\n// "}}{{- end}} -enum {{$enum.LongName}} { -{{- range $val := $enum.Values}} -{{- $valopts := index $val.Options "privacysandbox.apis.roma.app_api.v1.roma_enumval_annotation"}} -{{- if and $valopts (len $valopts.Description)}} - // {{$valopts.Description | wrapWith 75 "\n // "}} -{{- end}} - {{$val.Name}} = {{$val.Number}}; -{{- end}}{{/*vals*/}} +{{ range $enum := $file.Enums }} +{{- $enumopts := index $enum.Options "privacysandbox.apis.roma.app_api.v1.roma_enum_annotation" }} +{{- if and $enumopts (len $enumopts.Description) }}// {{ $enumopts.Description | wrapWith 75 "\n// " }}{{- end }} +enum {{ $enum.LongName }} { +{{- range $val := $enum.Values }} +{{- $valopts := index $val.Options "privacysandbox.apis.roma.app_api.v1.roma_enumval_annotation" }} +{{- if and $valopts (len $valopts.Description) }} + // {{$valopts.Description | wrapWith 75 "\n // " }} +{{- end }} + {{ $val.Name }} = {{ $val.Number }}; +{{- end }}{{/*vals*/}} } -{{end}} -{{- end}}{{/*enums*/}} - -{{end}}{{/*files*/}} +{{- end -}} +{{- end -}}{{/*enums*/}} +{{- end -}}{{/*files*/}}