### Install Prerequisites on Ubuntu Source: https://github.com/protocolbuffers/protobuf/blob/main/php/README.md Installs necessary tools for building the C extension on Ubuntu. Ensure these are installed before proceeding with the build. ```bash sudo apt-get install -y php-pear php-dev libtool make gcc ``` -------------------------------- ### Install Protobuf with CMake Source: https://github.com/protocolbuffers/protobuf/blob/main/cmake/README.md Install the compiled protobuf libraries, headers, and `protoc` binary to the specified installation directory. ```bash # Install the libraries and headers cmake --install build ``` -------------------------------- ### Configure Installation Source: https://github.com/protocolbuffers/protobuf/blob/main/third_party/utf8_range/CMakeLists.txt Sets up installation targets and files if 'utf8_range_ENABLE_INSTALL' is ON. This includes exporting targets, installing libraries and headers, and generating package configuration files. ```cmake # Configure installation. if (utf8_range_ENABLE_INSTALL) include(CMakePackageConfigHelpers) include(GNUInstallDirs) install(EXPORT ${PROJECT_NAME}-targets DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" NAMESPACE utf8_range:: ) install(TARGETS utf8_validity utf8_range EXPORT ${PROJECT_NAME}-targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) configure_package_config_file( cmake/${PROJECT_NAME}-config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/utf8_range.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/utf8_range.pc @ONLY) install( FILES ${CMAKE_CURRENT_BINARY_DIR}/utf8_range.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") # Install public headers explicitly. install(FILES utf8_range.h utf8_validity.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) endif () ``` -------------------------------- ### Using Proto2 Extensions (C#) Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/csharp/proto2.md Example of setting and getting proto2 extensions using the generated static classes and `using static` directive. ```csharp using static FooBar.FooExtensions; using static FooBar.Baz.Extensions; var foo = new Foo(); foo.SetExtension(FooExt, new Baz()); foo.GetOrInitializeExtension(RepeatedFooExt).Add(new Baz()); ``` -------------------------------- ### Install Protobuf Setuptools Extension Source: https://github.com/protocolbuffers/protobuf/blob/main/python/protobuf_distutils/README.md Install the extension using pip. For development, use 'python setup.py develop'. ```shell python setup.py build python -m pip install . ``` -------------------------------- ### Install Protocol Buffers on Linux (Directly with Makefiles) Source: https://github.com/protocolbuffers/protobuf/blob/main/cmake/README.md Alternatively, navigate to the build directory and use 'make install' with sudo to install Protocol Buffers on Linux. ```bash cd build sudo make install ``` -------------------------------- ### Build and Install upb using vcpkg Source: https://github.com/protocolbuffers/protobuf/blob/main/upb/README.md Steps to clone the vcpkg repository, bootstrap it, integrate it with your system, and then install upb using the vcpkg package manager. ```bash git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install ./vcpkg install upb ``` -------------------------------- ### Install Protocol Buffers Ruby Gem via Command Line Source: https://github.com/protocolbuffers/protobuf/blob/main/ruby/README.md Install the google-protobuf gem directly using the gem install command. Use the --prerelease flag if you need to install a pre-release version. ```bash $ gem install [--prerelease] google-protobuf ``` -------------------------------- ### Install C Extension from PECL Source: https://github.com/protocolbuffers/protobuf/blob/main/php/README.md Installs the pre-packaged Protocol Buffers C extension from PECL. This is a simpler installation method if a release is available. ```bash sudo pecl install protobuf-{VERSION} ``` -------------------------------- ### Build and Install Project with Generated Protobufs Source: https://github.com/protocolbuffers/protobuf/blob/main/python/protobuf_distutils/README.md Generate protobuf sources and then build and install your project. This ensures generated code is included. ```shell python setup.py generate_py_protobufs python setup.py build python -m pip install . ``` -------------------------------- ### Install Protobuf via vcpkg on Windows Source: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md Installs the Protobuf library and its dependencies using the vcpkg package manager. ```bash >vcpkg install protobuf protobuf:x64-windows ``` ```bash >vcpkg install protobuf[zlib] protobuf[zlib]:x64-windows ``` -------------------------------- ### Install google-protobuf for Ruby Source: https://github.com/protocolbuffers/protobuf/blob/main/upb/README.md Use RubyGems to install the google-protobuf gem for Ruby integration with upb. ```bash gem install google-protobuf ``` -------------------------------- ### Install Protocol Buffers on Linux (System-wide) Source: https://github.com/protocolbuffers/protobuf/blob/main/cmake/README.md After building, use this command to install Protocol Buffers system-wide on Linux. Ensure you have sudo privileges. ```bash sudo cmake --install build ``` -------------------------------- ### Install Protobuf Python with Pip Source: https://github.com/protocolbuffers/protobuf/blob/main/python/README.md Use pip to install the Protocol Buffers library for Python. This is the recommended method for most users. ```bash $ pip install protobuf ``` -------------------------------- ### Install protobuf for PHP Source: https://github.com/protocolbuffers/protobuf/blob/main/upb/README.md Use PECL to install the protobuf extension for PHP integration with upb. This command requires sudo privileges. ```bash sudo pecl install protobuf ``` -------------------------------- ### Install the compiler binary on Linux Source: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md Copies the compiled protoc binary to the system path. ```bash cp bazel-bin/protoc /usr/local/bin ``` -------------------------------- ### Migrate Proto Files to Editions Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/edition-zero-features.md Examples showing the transformation of proto2 and proto3 files into the edition-based syntax. ```protobuf // foo.proto syntax = "proto2" message Foo { repeated int32 x = 1; repeated int32 y = 2 [packed = true]; repeated int32 z = 3; } // bar.proto syntax = "proto3" message Foo { repeated int32 x = 1; repeated int32 y = 2 [packed = false]; repeated int32 z = 3; } ``` ```protobuf // foo.proto edition = "tbd" options features.repeated_field_encoding = EXPANDED; message Foo { repeated int32 x = 1; repeated int32 y = 2 [packed = true]; repeated int32 z = 3; } // bar.proto edition = "tbd" message Foo { repeated int32 x = 1; repeated int32 y = 2 [packed = false]; repeated int32 z = 3; } ``` -------------------------------- ### Build and Install Protocol Buffers Source: https://github.com/protocolbuffers/protobuf/blob/main/cmake/README.md Use this command to build and install Protocol Buffers to the directory specified by CMAKE_INSTALL_PREFIX. This creates bin, include, and lib folders. ```bash cmake --build --target install ``` -------------------------------- ### Install protobuf for Python Source: https://github.com/protocolbuffers/protobuf/blob/main/upb/README.md Use PyPI to install the protobuf package for Python integration with upb. This command may require sudo privileges. ```bash sudo pip install protobuf ``` -------------------------------- ### Install build dependencies on Ubuntu/Debian Source: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md Installs the necessary tools for building Protocol Buffers from source on Debian-based systems. ```bash sudo apt-get install g++ git bazel ``` -------------------------------- ### Install Bazel on macOS via Homebrew Source: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md Installs Bazel using the Homebrew package manager. ```bash brew install bazel ``` -------------------------------- ### Install Xcode command line tools on macOS Source: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md Installs the required command line developer tools for macOS. ```bash sudo xcode-select --install ``` -------------------------------- ### Install Bazel on macOS via MacPorts Source: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md Installs Bazel using the MacPorts package manager. ```bash sudo /opt/local/bin/port install bazel ``` -------------------------------- ### Build and Test Commands Source: https://github.com/protocolbuffers/protobuf/blob/main/ruby/README.md Commands for installing dependencies, building the gem, and running tests with different configurations. ```bash $ gem install bundler $ bundle ``` ```bash $ rake $ rake clobber_package gem $ gem install `ls pkg/google-protobuf-*.gem` ``` ```bash $ PROTOBUF_CONFIG=dbg rake ``` ```bash $ rake test ``` ```bash $ PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI rake test ``` ```bash $ bazel test //ruby/tests/... ``` ```bash $ bazel test //ruby/tests/... //ruby:ffi_enabled --test_env=PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI ``` -------------------------------- ### Install Dependencies for Native PHP Testing Source: https://github.com/protocolbuffers/protobuf/blob/main/php/README.md Installs dependencies required for testing the native PHP implementation of Protocol Buffers on Linux. Includes bazel, composer, and php-dev. ```bash # Install Dependencies (Linux) apt-get install bazel composer php-dev # Download protobuf git clone https://github.com/protocolbuffers/protobuf.git cd protobuf # Build protoc bazel build :protoc # Test native php cd php composer install composer test ``` -------------------------------- ### Proto2 Group Syntax Example Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/group-migration-issues.md Illustrates the transformation of proto2 group syntax into a message and a field with specific casing conventions. ```protobuf optional group MyGroup = 1 { ... } ``` ```protobuf message MyGroup { ... } optional MyGroup mygroup = 1; ``` -------------------------------- ### Define a proto_library with modifications Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/protobuf-editions-for-schema-producers.md Example of a Bazel rule configuration that incorporates semantic patches for Protobuf files. ```proto proto_library( name = "cloud_spanner_proto", modifications = ["cloud_spanner.change_spec"], deps = ["@com_google_cloud//spanner"], ) ``` -------------------------------- ### Build C Extension from Source Source: https://github.com/protocolbuffers/protobuf/blob/main/php/README.md Builds the Protocol Buffers C extension from source. This involves packaging the extension and then installing it using pecl. ```bash cd ext/google/protobuf pear package sudo pecl install protobuf-{VERSION}.tgz ``` -------------------------------- ### Generating C++ Code and Executable for Protobuf Examples Source: https://github.com/protocolbuffers/protobuf/blob/main/examples/CMakeLists.txt Iterates through example targets, defining source files and proto definitions. It uses `protobuf_generate_cpp` for code generation and `add_executable` to build the final program, linking against the protobuf library. ```cmake foreach(example add_person list_people) set(${example}_SRCS ${example}.cc) set(${example}_PROTOS addressbook.proto) #Code Generation if(protobuf_MODULE_COMPATIBLE) #Legacy Support protobuf_generate_cpp(${example}_PROTO_SRCS ${example}_PROTO_HDRS ${${example}_PROTOS}) list(APPEND ${example}_SRCS ${${example}_PROTO_SRCS} ${${example}_PROTO_HDRS}) endif() #Executable setup set(executable_name ${example}_cpp) add_executable(${executable_name} ${${example}_SRCS} ${${example}_PROTOS}) if(protobuf_MODULE_COMPATIBLE) #Legacy mode target_include_directories(${executable_name} PUBLIC ${PROTOBUF_INCLUDE_DIRS}) target_link_libraries(${executable_name} ${PROTOBUF_LIBRARIES}) else() target_link_libraries(${executable_name} protobuf::libprotobuf) protobuf_generate(TARGET ${executable_name}) endif() endforeach() ``` -------------------------------- ### Basic CMake Build Configuration Source: https://github.com/protocolbuffers/protobuf/blob/main/cmake/README.md Configure the protobuf build using CMake. This command sets up the build directory, specifies the installation prefix, and defines the build type as Release. ```bash # From the protobuf source directory cmake -S . -B build \ -DCMAKE_INSTALL_PREFIX=../install \ -DCMAKE_BUILD_TYPE=Release ``` -------------------------------- ### Legacy Proto2 and Proto3 Definitions Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/edition-zero-features.md Example of standard proto2 and proto3 message definitions before applying edition-based field presence features. ```protobuf // foo.proto syntax = "proto2" message Foo { required int32 x = 1; optional int32 y = 2; repeated int32 z = 3; } // bar.proto syntax = "proto3" message Bar { int32 x = 1; optional int32 y = 2; repeated int32 z = 3; } ``` -------------------------------- ### Add Protocol Buffers to Composer Source: https://github.com/protocolbuffers/protobuf/blob/main/php/README.md Installs the pure PHP implementation of Protocol Buffers using Composer. Add 'google/protobuf' to your project's composer.json file. ```json "require": { "google/protobuf": "^3.20" } ``` -------------------------------- ### Configure setup.py for Protobuf Generation Source: https://github.com/protocolbuffers/protobuf/blob/main/python/protobuf_distutils/README.md Configure your project's setup.py to use the protobuf_distutils extension. Specify source directories, output directories, and specific proto files. ```python from setuptools import setup setup( # ... name='example_project', # Require this package, but only for setup (not installation): setup_requires=['protobuf_distutils'], options={ # See below for details. 'generate_py_protobufs': { 'source_dir': 'path/to/protos', 'extra_proto_paths': ['path/to/other/project/protos'], 'output_dir': 'path/to/project/sources', # default '.' 'proto_files': ['relative/path/to/just_this_file.proto'], 'protoc': 'path/to/protoc.exe', }, }, ) ``` -------------------------------- ### Clone and initialize the repository Source: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md Downloads the source code and initializes required submodules. ```bash git clone https://github.com/protocolbuffers/protobuf.git cd protobuf git submodule update --init --recursive ``` -------------------------------- ### Initialize and populate a DescriptorPool Source: https://github.com/protocolbuffers/protobuf/blob/main/python/docs/google/protobuf/descriptor_pool.md Demonstrates how to instantiate a DescriptorPool, add FileDescriptorProto objects, and retrieve a message descriptor by name. ```default pool = DescriptorPool() file_descriptor_protos = [ ... ] for file_descriptor_proto in file_descriptor_protos: pool.Add(file_descriptor_proto) my_message_descriptor = pool.FindMessageTypeByName('some.package.MessageType') ``` -------------------------------- ### Specify Dependency Installation Path Source: https://github.com/protocolbuffers/protobuf/blob/main/cmake/README.md Inform CMake about the installation paths for dependencies like Abseil and Google Test. If not found, CMake will attempt to download them from GitHub. ```bash # Path to where Abseil and GTest are installed cmake . -DCMAKE_PREFIX_PATH=/path/to/my/dependencies ``` -------------------------------- ### Edition Ordering Example Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/edition-evolution.md Illustrates the total ordering of Protobuf editions, showing how year-based and revision-based editions are ordered. This ordering allows backends to determine if a proto file is older or newer than a specific edition. ```plaintext 2022 < 2022.0 < 2022.1 < ... < 2022.9 < 2022.10 < ... < 2023 < ... < 2024 < ... ``` -------------------------------- ### Example of Keyword Conflict in Protobuf Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/stricter-schemas-with-editions.md This example illustrates a potential parsing ambiguity where a keyword like 'int32' could be interpreted as a type name or a message name, highlighting the need for reserved keywords. ```protobuf message Foo { message int32 {} optional int32 foo = 1; } ``` -------------------------------- ### Proto2 Extension Initialization (C#) Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/csharp/proto2.md Generated static containers for proto2 extensions. Use `using static` for easier access to extensions. ```csharp public static partial class FooExtensions { public static readonly Extension FooExt = /* initialization */; } public partial class Baz { public partial static class Extensions { public static readonly RepeatedExtension RepeatedFooExt = /* initialization */; } } ``` -------------------------------- ### Define Enum Aliases Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/stricter-schemas-with-editions.md Example of enum aliases currently permitted in Protobuf. ```protobuf enum Foo { BAR = 5; BAZ = 5; } ``` -------------------------------- ### Apply retention and target attributes Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/protobuf-design-options-attributes.md Example of applying both retention and target attributes to a field option. ```protobuf message Cpp { enum StringType { STRING = 1; STRING_VIEW = 0; CORD = 2; } optional string namespace = 2 [ retention = RETENTION_SOURCE, target = TARGET_TYPE_FILE ]; } ``` -------------------------------- ### Basic CMake Configuration Source: https://github.com/protocolbuffers/protobuf/blob/main/third_party/utf8_range/CMakeLists.txt Sets up the minimum CMake version, project name, and handles policy settings. This is standard for most CMake projects. ```cmake cmake_minimum_required (VERSION 3.16) project (utf8_range C CXX) # option() honor variables if (POLICY CMP0077) cmake_policy (SET CMP0077 NEW) endif (POLICY CMP0077) ``` -------------------------------- ### Define a constant in Protobuf Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/minimum-required-edition.md Example of a novel constant definition that would require a descriptor change. ```protobuf const int32 MY_CONSTANT = 42; ``` -------------------------------- ### Build the Protocol Buffers compiler Source: https://github.com/protocolbuffers/protobuf/blob/main/objectivec/README.md Use this shell command to generate the protoc binary after cloning the repository. ```bash $ objectivec/DevTools/full_mac_build.sh ``` -------------------------------- ### Define Baz extension in proto Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/csharp/proto2.md Example of defining an extension within a message in a .proto file. ```proto message Baz { extend Foo { optional Baz foo_ext = 124; } } ``` -------------------------------- ### Build the compiler and runtime with Bazel Source: https://github.com/protocolbuffers/protobuf/blob/main/src/README.md Compiles the Protocol Buffer compiler and runtime library. ```bash bazel build :protoc :protobuf ``` -------------------------------- ### Define Proto2 and Proto3 Enum Files Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/edition-zero-features.md Initial state of proto2 and proto3 files before edition migration. ```protobuf // foo.proto syntax = "proto2" enum Foo { A = 2, B = 4, C = 6, } // bar.proto syntax = "proto3" enum Bar { A = 0, B = 1, C = 5, } ``` -------------------------------- ### Run PHP (C Extension) Conformance Tests with Bazel Source: https://github.com/protocolbuffers/protobuf/blob/main/conformance/README.md Runs conformance tests for the PHP Protocol Buffers implementation using its C extension. Requires GCC, libtool, make, pear, pecl, and phpize. ```bash which gcc || echo "gcc is required!" which libtool || echo "libtool is required!" which make || echo "make is required!" which pear || echo "pear is required! It might require a development version of PHP such as a php-dev package" which pecl || echo "pecl is required! It might require a development version of PHP such as a php-dev package" which phpize || echo "phpize is required! It might require a development version of PHP such as a php-dev package" bazel test //php:conformance_test_c ``` -------------------------------- ### Protobuf Schema Before Janitor Optimization Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/prototiller/editions-tooling.md An example of a Protobuf schema with redundant explicit feature declarations on multiple fields. ```protobuf edition = "2023"; message Foo { optional string a = 1 [features.(pb.cpp).string_type = VIEW]; optional string b = 2 [features.(pb.cpp).string_type = VIEW]; optional string c = 3 [features.(pb.cpp).string_type = VIEW]; optional string d = 4 [features.(pb.cpp).string_type = VIEW]; optional string e = 5 [features.(pb.cpp).string_type = VIEW]; } message Bar { optional string a = 1 [features.(pb.cpp).string_type = VIEW]; optional string b = 2; optional string c = 3; optional string d = 4; optional string e = 5; } ``` -------------------------------- ### Post-Edition Field Presence Migration Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/edition-zero-features.md Example of how proto files are updated to use the field_presence feature after migrating to editions. ```protobuf // foo.proto edition = "tbd" message Foo { int32 x = 1 [features.field_presence = LEGACY_REQUIRED]; int32 y = 2; repeated int32 z = 3; } // bar.proto edition = "tbd" option features.field_presence = NO_PRESENCE; message Bar { int32 x = 1; int32 y = 2 [features.field_presence = EXPLICIT_PRESENCE]; repeated int32 z = 3; } ``` -------------------------------- ### Allocate memory using upb_Arena Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/upb/design.md Demonstrates creating an arena, performing allocations, and freeing the entire arena at once. ```c upb_Arena* arena = upb_Arena_New(); // Perform some allocations. int* x = upb_Arena_Malloc(arena, sizeof(*x)); int* y = upb_Arena_Malloc(arena, sizeof(*y)); // We cannot free `x` and `y` separately, we can only free the arena // as a whole. upb_Arena_Free(arena); ``` -------------------------------- ### Build Options Configuration Source: https://github.com/protocolbuffers/protobuf/blob/main/third_party/utf8_range/CMakeLists.txt Defines build options for enabling tests and installation. These options can be controlled when configuring the CMake build. ```cmake option (utf8_range_ENABLE_TESTS "Build test suite" ON) option (utf8_range_ENABLE_INSTALL "Configure installation" ON) ``` -------------------------------- ### Proto3 Message Definition with Mixed Presence Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md Example Proto3 message demonstrating fields with both no presence and explicit presence semantics. ```protobuf syntax = "proto3"; package example; message MyMessage { // No presence: int32 not_tracked = 1; // Explicit presence: optional int32 tracked = 2; } ``` -------------------------------- ### Use ExtensionRegistry for parsing Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/csharp/proto2.md Demonstrates constructing an ExtensionRegistry to parse messages containing extensions. ```cs var registry = new ExtensionRegistry() { Baz.Extensions.FooExt }; var foo = Foo.Factory.WithExtensionRegistry(registry).ParseFrom(input); Assert.True(foo.HasExtension(Bas.Extensions.FooExt)); var fooNoRegistry = Foo.Factory.ParseFrom(input); Assert.False(foo.HasExtension(Bas.Extensions.FooExt)); ``` -------------------------------- ### Transform proto3 optional fields to edition 2023 Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/prototiller/prototiller-reqs-for-edition-zero.md Example of migrating proto3 optional fields to the edition 2023 model. ```protobuf syntax = "proto3"; message Foo { optional string bar = 1; optional string baz = 2; } ``` ```protobuf edition = "2023"; message Foo { string bar = 1; string baz = 2; } ``` -------------------------------- ### Sample Protobuf Features Usage Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/protobuf-editions-design-features.md Demonstrates how to use custom options for features like repeated field encoding and enum types within a .proto file. Note the use of `features.` and `features.().` syntax. ```protobuf edition = "2023"; package experimental.users.kfm.editions; import "net/proto2/proto/features_cpp.proto"; option features.repeated_field_encoding = EXPANDED; option features.enum = OPEN; option features.(pb.cpp).string_field_type = STRING; option features.(pb.cpp).namespace = "kfm::proto_experiments"; message Lab { // `Mouse` is open as it inherits the file's value. enum Mouse { UNKNOWN_MOUSE = 0; PINKY = 1; THE_BRAIN = 2; } repeated Mouse mice = 1 [features.repeated_field_encoding = PACKED]; string name = 2; string address = 3 [features.(pb.cpp).string_field_type = CORD]; string function = 4 [features.(pb.cpp).string_field_type = STRING_VIEW]; } enum ColorChannel { // Turn off the option from the surrounding file option features.enum = CLOSED; UNKNOWN_COLOR_CHANNEL = 0; RED = 1; BLUE = 2; GREEN = 3; ALPHA = 4; } ``` -------------------------------- ### IExtendableMessage Interface (C#) Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/csharp/proto2.md Interface for messages that support extensions. Provides methods to get, set, and check for the presence of extensions. ```csharp public interface IExtendableMessage : IMessage where T : IExtendableMessage { // Gets the value of a single value extension. If the extension isn't present, this returns the default value. TValue GetExtension(Extension extension); // Gets the value of a repeated extension. If the extension hasn't been set, this returns null to prevent unnecessary allocations. RepeatedField GetExtension(RepeatedExtension extension); // Gets the value of a repeated extension. This will initialize the value of the repeated field and will never return null. RepeatedField GetOrInitializeExtension(RepeatedExtension extension); // Sets the value of the extension void SetExtension(Extension extension, TValue value); // Returns whether the extension is present in the message bool HasExtension(Extension extension); // Clears the value of the extension, removing it from the message void ClearExtension(Extension extension); // Clears the value of the repeated extension, removing it from the message. Calling GetExtension after this will always return null. void ClearExtension(RepeatedExtension extension); } ``` -------------------------------- ### Run C# Conformance Tests with Bazel Source: https://github.com/protocolbuffers/protobuf/blob/main/conformance/README.md Runs conformance tests for the C# Protocol Buffers implementation. Requires the .NET SDK and sets environment variables for telemetry and globalization. ```bash which dotnet || echo "You must have dotnet installed!" bazel test //csharp:conformance_test \ --action_env=DOTNET_CLI_TELEMETRY_OPTOUT=1 --test_env=DOTNET_CLI_HOME=~ \ --action_env=DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 ``` -------------------------------- ### Require Protocol Buffers Library Source: https://github.com/protocolbuffers/protobuf/blob/main/ruby/README.md Include the necessary Protocol Buffers library in your Ruby script. This is typically done after installing the gem. ```ruby require 'google/protobuf' ``` -------------------------------- ### Generating CMake File Lists with Bazel Source: https://github.com/protocolbuffers/protobuf/blob/main/docs/cpp_build_systems.md This example shows how to define Bazel rules to generate CMake-compatible file lists. The `gen_cmake_file_lists` rule aggregates sources from various Bazel targets. ```bazel load("@protobuf//bazel:proto_library.bzl", "proto_library") load("//pkg:build_systems.bzl", "gen_cmake_file_lists") filegroup( name = "doc_files", srcs = [ "README.md", "english_paper.md", ], ) proto_library( name = "message", srcs = ["message.proto"], ) gen_cmake_file_lists( name = "source_lists", out = "source_lists.cmake", src_libs = { ":doc_files": "docs", ":message": "buff", "//cc_dist_library_example:c": "distlib", }, ) ``` ```bash $ bazel build gen_file_lists_example:source_lists ``` ```cmake # Auto-generated by //gen_file_lists_example:source_lists # # This file contains lists of sources based on Bazel rules. It should # be included from a hand-written CMake file that defines targets. # # Changes to this file will be overwritten based on Bazel definitions. if(${CMAKE_VERSION} VERSION_GREATER 3.10 OR ${CMAKE_VERSION} VERSION_EQUAL 3.10) include_guard() endif() # //gen_file_lists_example:doc_files set(docs_files gen_file_lists_example/README.md gen_file_lists_example/english_paper.md ) # //gen_file_lists_example:message set(buff_proto_srcs gen_file_lists_example/message.proto ) # //gen_file_lists_example:message set(buff_srcs gen_file_lists_example/message.proto.pb.cc ) # //gen_file_lists_example:message set(buff_hdrs gen_file_lists_example/message.proto.pb.h ) # //gen_file_lists_example:message set(buff_files gen_file_lists_example/message-descriptor-set.proto.bin ) # //cc_dist_library_example:c set(distlib_srcs cc_dist_library_example/a.cc cc_dist_library_example/b.cc ) # //cc_dist_library_example:c set(distlib_hdrs ) ```