From 2eae16e53ac4353ed2c6b8447d1024e5d3b0dd86 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Sun, 19 Jul 2026 21:05:51 -0500 Subject: [PATCH] Centralize protobuf scalar initialization --- .../schema_definition/factory_extension.rb | 25 ++------------- .../schema_elements/scalar_type_extension.rb | 32 ++++++++++++++++--- .../schema_definition/factory_extension.rbs | 2 -- .../schema_elements/scalar_type_extension.rbs | 4 ++- .../schema_definition/api_extension_spec.rb | 6 ++-- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/factory_extension.rb b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/factory_extension.rb index 5548535aa..ef621415e 100644 --- a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/factory_extension.rb +++ b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/factory_extension.rb @@ -18,23 +18,6 @@ module ProtoIngestion module SchemaDefinition # Extension module applied to Factory to add proto support. module FactoryExtension - # Default protobuf types applied to ElasticGraph's built-in scalar types as they are constructed. - BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME = { - "Boolean" => "bool", - "Cursor" => "string", - "Date" => "string", - "DateTime" => "string", - "Float" => "double", - "ID" => "string", - "Int" => "int32", - "JsonSafeLong" => "int64", - "LocalTime" => "string", - "LongString" => "int64", - "String" => "string", - "TimeZone" => "string", - "Untyped" => "string" - }.freeze - # Creates a new enum type with proto extensions. # # @param name [String] enum type name @@ -92,13 +75,9 @@ def new_object_type(name) def new_scalar_type(name) super(name) do |type| extended_type = type.extend(SchemaElements::ScalarTypeExtension) # : ::ElasticGraph::SchemaDefinition::SchemaElements::ScalarType & SchemaElements::ScalarTypeExtension - - if (proto_type = BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME[name.to_s]) - extended_type.protobuf type: proto_type + extended_type.initialize_proto_extension do + yield extended_type if block_given? end - - yield extended_type if block_given? - extended_type.finalize_protobuf_configuration! end end diff --git a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb index 9a9308fde..f3dfaf898 100644 --- a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb +++ b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb @@ -14,6 +14,23 @@ module SchemaDefinition module SchemaElements # Extends ScalarType with proto field type conversion. module ScalarTypeExtension + # Default protobuf types applied to ElasticGraph's built-in scalar types as they are constructed. + BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME = { + "Boolean" => "bool", + "Cursor" => "string", + "Date" => "string", + "DateTime" => "string", + "Float" => "double", + "ID" => "string", + "Int" => "int32", + "JsonSafeLong" => "int64", + "LocalTime" => "string", + "LongString" => "int64", + "String" => "string", + "TimeZone" => "string", + "Untyped" => "string" + }.freeze + # Configured protobuf type (e.g. string, int64, bool). # @dynamic protobuf_type attr_reader :protobuf_type @@ -26,13 +43,20 @@ def protobuf(type:) @protobuf_type = type end - # Validates that a protobuf type has been configured on this scalar type. GraphQL-only - # scalar types are skipped because they are not part of ingestion. + # Applies any built-in protobuf type, yields for further configuration, and validates the result. # + # @yield additional scalar type configuration # @return [void] - # @raise [Errors::SchemaError] when missing - def finalize_protobuf_configuration! + # @raise [Errors::SchemaError] when a protobuf type is missing + def initialize_proto_extension + original_name = type_ref.with_reverted_override.name + if (proto_type = BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME[original_name]) + protobuf type: proto_type + end + + yield return if graphql_only? + proto_name nil end diff --git a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/factory_extension.rbs b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/factory_extension.rbs index 620a1664c..eb8374fef 100644 --- a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/factory_extension.rbs +++ b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/factory_extension.rbs @@ -2,8 +2,6 @@ module ElasticGraph module ProtoIngestion module SchemaDefinition module FactoryExtension: ::ElasticGraph::SchemaDefinition::Factory - BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME: ::Hash[::String, ::String] - def new_enum_type: (::String) ?{ (::ElasticGraph::SchemaDefinition::SchemaElements::EnumType & SchemaElements::EnumTypeExtension) -> void } -> (::ElasticGraph::SchemaDefinition::SchemaElements::EnumType & SchemaElements::EnumTypeExtension) def new_enum_value: (::String, ::String) ?{ (::ElasticGraph::SchemaDefinition::SchemaElements::EnumValue & SchemaElements::EnumValueExtension) -> void } -> (::ElasticGraph::SchemaDefinition::SchemaElements::EnumValue & SchemaElements::EnumValueExtension) def new_interface_type: (::String) ?{ (::ElasticGraph::SchemaDefinition::SchemaElements::InterfaceType & SchemaElements::ObjectInterfaceAndUnionExtension) -> void } -> (::ElasticGraph::SchemaDefinition::SchemaElements::InterfaceType & SchemaElements::ObjectInterfaceAndUnionExtension) diff --git a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rbs b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rbs index 553990072..f5032bd25 100644 --- a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rbs +++ b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rbs @@ -3,10 +3,12 @@ module ElasticGraph module SchemaDefinition module SchemaElements module ScalarTypeExtension: ::ElasticGraph::SchemaDefinition::SchemaElements::ScalarType + BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME: ::Hash[::String, ::String] + attr_reader protobuf_type: ::String? def protobuf: (type: ::String) -> void - def finalize_protobuf_configuration!: () -> void + def initialize_proto_extension: () { () -> void } -> void def proto_name: () -> ::String def proto_type_reference: (::String package_name) -> ::String def to_proto: (::String package_name) -> nil diff --git a/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/api_extension_spec.rb b/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/api_extension_spec.rb index 196308eaf..44f0d986c 100644 --- a/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/api_extension_spec.rb +++ b/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/api_extension_spec.rb @@ -35,7 +35,7 @@ module SchemaDefinition field_types = [] proto = define_proto_schema do |s| s.object_type "Widget" do |t| - FactoryExtension::BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME.each_key do |type_name| + SchemaElements::ScalarTypeExtension::BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME.each_key do |type_name| t.field type_name.downcase, type_name end field_types = t.graphql_fields_by_name.values.map { |field| field.type.name } @@ -43,8 +43,8 @@ module SchemaDefinition end end - expect(field_types).to match_array(FactoryExtension::BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME.keys) - FactoryExtension::BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME.each.with_index(1) do |(type_name, proto_type), field_number| + expect(field_types).to match_array(SchemaElements::ScalarTypeExtension::BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME.keys) + SchemaElements::ScalarTypeExtension::BUILT_IN_SCALAR_PROTO_TYPES_BY_NAME.each.with_index(1) do |(type_name, proto_type), field_number| expect(proto).to include("#{proto_type} #{type_name.downcase} = #{field_number};") end end