-
Notifications
You must be signed in to change notification settings - Fork 30
Generate a protobuf schema artifact from ElasticGraph schemas #1080
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
Open
jwils
wants to merge
1
commit into
main
Choose a base branch
from
joshuaw/protobuf-schema-generation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| # ElasticGraph::ProtoIngestion | ||
|
|
||
| An ElasticGraph extension that supports ingesting Protocol Buffer data into ElasticGraph. | ||
| Currently it generates `proto3` Protocol Buffers schema artifacts from ElasticGraph schemas. | ||
|
|
||
| ## Dependency Diagram | ||
|
|
||
|
|
@@ -15,3 +16,130 @@ graph LR; | |
| elasticgraph-proto_ingestion --> elasticgraph-support; | ||
| class elasticgraph-support otherEgGemStyle; | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| First, add `elasticgraph-proto_ingestion` to your `Gemfile`, alongside the other ElasticGraph gems: | ||
|
|
||
| ```diff | ||
| diff --git a/Gemfile b/Gemfile | ||
| index 4a5ef1e..5c16c2b 100644 | ||
| --- a/Gemfile | ||
| +++ b/Gemfile | ||
| @@ -8,6 +8,7 @@ gem "elasticgraph-query_registry", *elasticgraph_details | ||
|
|
||
| # Can be elasticgraph-elasticsearch or elasticgraph-opensearch based on the datastore you want to use. | ||
| gem "elasticgraph-opensearch", *elasticgraph_details | ||
| +gem "elasticgraph-proto_ingestion", *elasticgraph_details | ||
|
|
||
| gem "httpx", "~> 1.3" | ||
|
|
||
| ``` | ||
|
|
||
| Next, update your `Rakefile` so that `ElasticGraph::ProtoIngestion::SchemaDefinition::APIExtension` is | ||
| included in the schema-definition extension modules: | ||
|
|
||
| ```diff | ||
| diff --git a/Rakefile b/Rakefile | ||
| index 2943335..26633c3 100644 | ||
| --- a/Rakefile | ||
| +++ b/Rakefile | ||
| @@ -3,5 +3,6 @@ | ||
| require "elastic_graph/json_ingestion/schema_definition/api_extension" | ||
| require "elastic_graph/local/rake_tasks" | ||
| +require "elastic_graph/proto_ingestion/schema_definition/api_extension" | ||
| require "elastic_graph/query_registry/rake_tasks" | ||
| require "rspec/core/rake_task" | ||
| require "standard/rake" | ||
| @@ -16,6 +17,7 @@ ElasticGraph::Local::RakeTasks.new( | ||
| # Determines casing of field names. Can be either `:camelCase` or `:snake_case`. | ||
| tasks.schema_element_name_form = :camelCase | ||
| tasks.schema_definition_extension_modules << ElasticGraph::JSONIngestion::SchemaDefinition::APIExtension | ||
| + tasks.schema_definition_extension_modules << ElasticGraph::ProtoIngestion::SchemaDefinition::APIExtension | ||
|
|
||
| # Customizes the names of fields generated by ElasticGraph. | ||
| tasks.schema_element_name_overrides = { | ||
| ``` | ||
|
|
||
| Then opt into proto generation from your schema definition: | ||
|
|
||
| ```ruby | ||
| # in config/schema/protobuf.rb | ||
|
|
||
| ElasticGraph.define_schema do |schema| | ||
| schema.proto_schema_artifacts package_name: "myapp.events.v1" | ||
| end | ||
| ``` | ||
|
|
||
| After running `bundle exec rake schema_artifacts:dump`, ElasticGraph will generate `schema.proto`. | ||
|
|
||
| ## Schema Definition Options | ||
|
|
||
| ### Custom Scalar Types | ||
|
|
||
| Built-in ElasticGraph scalar types are automatically mapped to proto scalar types. | ||
| For custom scalar types, use `protobuf` to define the proto scalar type: | ||
|
|
||
| ```ruby | ||
| # in config/schema/money.rb | ||
|
|
||
| ElasticGraph.define_schema do |schema| | ||
| schema.scalar_type "Money" do |t| | ||
| t.mapping type: "long" | ||
| t.json_schema type: "integer" | ||
| t.protobuf type: "int64" | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| ## Type Mappings | ||
|
|
||
| The generated `schema.proto` uses these built-in scalar mappings: | ||
|
|
||
| | ElasticGraph Type | Protobuf Type | | ||
| |-------------------|------------| | ||
| | `Boolean` | `bool` | | ||
| | `Cursor` | `string` | | ||
| | `Date` | `string` | | ||
| | `DateTime` | `string` | | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use https://protobuf.dev/reference/protobuf/google.protobuf/#timestamp for this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| | `Float` | `double` | | ||
| | `ID` | `string` | | ||
| | `Int` | `int32` | | ||
| | `JsonSafeLong` | `int64` | | ||
| | `LocalTime` | `string` | | ||
| | `LongString` | `int64` | | ||
| | `String` | `string` | | ||
| | `TimeZone` | `string` | | ||
| | `Untyped` | `string` | | ||
|
|
||
| Additionally: | ||
| - List types become `repeated` fields. | ||
| - Lists of lists (e.g. `[[Float!]!]!`) generate wrapper messages, because protobuf has no | ||
| `repeated repeated` equivalent. Each inner nesting level becomes a generated message with a | ||
| single `repeated values = 1;` field. For example, the `values` field here: | ||
|
|
||
| ```ruby | ||
| # in config/schema/matrix.rb | ||
|
|
||
| ElasticGraph.define_schema do |schema| | ||
| schema.object_type "Matrix" do |t| | ||
| t.field "id", "ID" | ||
| t.field "values", "[[Float!]!]!" | ||
| t.index "matrices" | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| generates: | ||
|
|
||
| ```text | ||
| message Matrix { | ||
| string id = 1; | ||
| repeated MatrixValuesListLevel1 values = 2; | ||
| } | ||
|
|
||
| message MatrixValuesListLevel1 { | ||
| repeated double values = 1; | ||
| } | ||
| ``` | ||
| - Enum types generate `enum` definitions whose values are prefixed with the enum type name in `UPPER_SNAKE_CASE`, including a zero-valued `*_UNSPECIFIED` entry. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/factory_extension.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # Copyright 2024 - 2026 Block, Inc. | ||
| # | ||
| # Use of this source code is governed by an MIT-style | ||
| # license that can be found in the LICENSE file or at | ||
| # https://opensource.org/licenses/MIT. | ||
| # | ||
| # frozen_string_literal: true | ||
|
|
||
| require "elastic_graph/proto_ingestion/schema_definition/results_extension" | ||
| require "elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension" | ||
| require "elastic_graph/proto_ingestion/schema_definition/schema_elements/enum_type_extension" | ||
| require "elastic_graph/proto_ingestion/schema_definition/schema_elements/object_interface_and_union_extension" | ||
| require "elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension" | ||
|
|
||
| module ElasticGraph | ||
| module ProtoIngestion | ||
| module SchemaDefinition | ||
| # Extension module applied to Factory to add proto support. | ||
| module FactoryExtension | ||
| # Creates a new enum type with proto extensions. | ||
| # | ||
| # @param name [String] enum type name | ||
| # @yield [ElasticGraph::SchemaDefinition::SchemaElements::EnumType] | ||
| # @return [ElasticGraph::SchemaDefinition::SchemaElements::EnumType] | ||
| def new_enum_type(name) | ||
| super(name) do |type| | ||
| type.extend SchemaElements::EnumTypeExtension | ||
| yield type if block_given? | ||
| end | ||
| end | ||
|
|
||
| # Creates a new interface type with proto extensions. | ||
| # | ||
| # @param name [String] interface type name | ||
| # @yield [ElasticGraph::SchemaDefinition::SchemaElements::InterfaceType] | ||
| # @return [ElasticGraph::SchemaDefinition::SchemaElements::InterfaceType] | ||
| def new_interface_type(name) | ||
| super(name) do |type| | ||
| type.extend SchemaElements::ObjectInterfaceAndUnionExtension | ||
| yield type if block_given? | ||
| end | ||
| end | ||
|
|
||
| # Creates a new object type with proto extensions. | ||
| # | ||
| # @param name [String] object type name | ||
| # @yield [ElasticGraph::SchemaDefinition::SchemaElements::ObjectType] | ||
| # @return [ElasticGraph::SchemaDefinition::SchemaElements::ObjectType] | ||
| def new_object_type(name) | ||
| super(name) do |type| | ||
| type.extend SchemaElements::ObjectInterfaceAndUnionExtension | ||
| yield type if block_given? | ||
| end | ||
| end | ||
|
|
||
| # Creates a new scalar type with proto extensions. | ||
| # | ||
| # @param name [String] scalar type name | ||
| # @yield [ElasticGraph::SchemaDefinition::SchemaElements::ScalarType] | ||
| # @return [ElasticGraph::SchemaDefinition::SchemaElements::ScalarType] | ||
| def new_scalar_type(name) | ||
| super(name) do |type| | ||
| type.extend SchemaElements::ScalarTypeExtension | ||
| yield type if block_given? | ||
| end | ||
| end | ||
|
|
||
| # Creates a new union type with proto extensions. | ||
| # | ||
| # @param name [String] union type name | ||
| # @yield [ElasticGraph::SchemaDefinition::SchemaElements::UnionType] | ||
| # @return [ElasticGraph::SchemaDefinition::SchemaElements::UnionType] | ||
| def new_union_type(name) | ||
| super(name) do |type| | ||
| type.extend SchemaElements::ObjectInterfaceAndUnionExtension | ||
| yield type if block_given? | ||
| end | ||
| end | ||
|
|
||
| # Creates a new results object and extends it with proto generation APIs. | ||
| # | ||
| # @return [ElasticGraph::SchemaDefinition::Results] | ||
| def new_results | ||
| super.tap do |results| | ||
| results.extend ResultsExtension | ||
| end | ||
| end | ||
|
|
||
| # Creates a new schema artifact manager and extends it with proto artifact support. | ||
| # | ||
| # @return [ElasticGraph::SchemaDefinition::SchemaArtifactManager] | ||
| def new_schema_artifact_manager(...) | ||
| super.tap do |manager| | ||
| manager.extend SchemaArtifactManagerExtension | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One potential concern is that
stringis a much wider type thanDate. Something like 0.0001% of valid strings are valid ISO8601Datestrings.Have you given thought to how invalid values will get handled? With JSON schema we could specify validations for this kind of thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment to the proto in http://github.com/block/elasticgraph/pull/1306 but open to suggestions on other ways to do this.