Module: ElasticGraph::ProtoIngestion::SchemaDefinition::SchemaElements::ScalarTypeExtension
- Defined in:
- elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb
Overview
Extends ScalarType with proto field type conversion.
Constant Summary collapse
- BUILT_IN_SCALAR_PROTO_OPTIONS_BY_NAME =
Default protobuf options applied to ElasticGraph's built-in scalar types as they are constructed.
{ "Boolean" => {type: "bool"}, "Cursor" => {type: "string"}, "Date" => {type: "string", field_comment: %(Must be formatted as an ISO 8601 date, e.g. "2024-11-25".)}, "DateTime" => {type: "google.protobuf.Timestamp", import: "google/protobuf/timestamp.proto"}, "Float" => {type: "double"}, "ID" => {type: "string"}, "Int" => {type: "int32"}, "JsonSafeLong" => {type: "int64"}, "LocalTime" => {type: "string", field_comment: %(Must be formatted as an ISO 8601 local time, e.g. "14:23:12".)}, "LongString" => {type: "int64"}, "String" => {type: "string"}, "TimeZone" => {type: "string", field_comment: %(Must be an IANA time zone identifier, e.g. "America/Los_Angeles".)}, "Untyped" => {type: "string"} }.freeze
- VALID_PROTOBUF_IMPORT_PATH =
An
importis rendered asimport "PATH";, so a quote or newline in the path would produce invalid proto.protocalso requires the path to name a.protofile. %r{\A[\w./-]+\.proto\z}
Instance Attribute Summary collapse
-
#protobuf_field_comment ⇒ Object
readonly
Comment rendered above each generated proto field of this scalar type.
-
#protobuf_import ⇒ Object
readonly
Proto file to import for the configured protobuf type, if it is externally defined.
-
#protobuf_type ⇒ Object
readonly
Configured protobuf type (e.g. string, int64, bool).
Instance Method Summary collapse
-
#initialize_proto_extension { ... } ⇒ void
Applies any built-in protobuf type, yields for further configuration, and validates the result.
-
#proto_name ⇒ String
Returns this scalar's name in protobuf schemas.
-
#proto_type_reference(_package_name) ⇒ String
Returns this scalar's name when referenced by a protobuf field.
-
#protobuf(type:, import: nil, field_comment: nil) ⇒ void
Configures the protobuf type for this scalar type.
-
#referenced_proto_types ⇒ Array
Returns the schema types referenced by this definition.
-
#to_proto(_schema, _package_name) ⇒ nil
Scalars map to protobuf field types and do not render standalone definitions.
Instance Attribute Details
#protobuf_field_comment ⇒ Object (readonly)
Comment rendered above each generated proto field of this scalar type.
48 49 50 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 48 def protobuf_field_comment @protobuf_field_comment end |
#protobuf_import ⇒ Object (readonly)
Proto file to import for the configured protobuf type, if it is externally defined.
44 45 46 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 44 def protobuf_import @protobuf_import end |
#protobuf_type ⇒ Object (readonly)
Configured protobuf type (e.g. string, int64, bool).
40 41 42 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 40 def protobuf_type @protobuf_type end |
Instance Method Details
#initialize_proto_extension { ... } ⇒ void
This method returns an undefined value.
Applies any built-in protobuf type, yields for further configuration, and validates the result.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 75 def initialize_proto_extension original_name = type_ref.with_reverted_override.name if ( = BUILT_IN_SCALAR_PROTO_OPTIONS_BY_NAME[original_name]) protobuf(**) end yield return if graphql_only? proto_name nil end |
#proto_name ⇒ String
Returns this scalar's name in protobuf schemas.
105 106 107 108 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 105 def proto_name protobuf_type || raise(Errors::SchemaError, "Protobuf type not configured for scalar type `#{name}`. " \ 'To proceed, call `protobuf type: "TYPE"` on the scalar type definition.') end |
#proto_type_reference(_package_name) ⇒ String
Returns this scalar's name when referenced by a protobuf field.
113 114 115 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 113 def proto_type_reference(_package_name) proto_name end |
#protobuf(type:, import: nil, field_comment: nil) ⇒ void
This method returns an undefined value.
Configures the protobuf type for this scalar type. Each call replaces the full protobuf
configuration, so an override that omits import: or field_comment: clears the value
configured by a prior call.
59 60 61 62 63 64 65 66 67 68 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 59 def protobuf(type:, import: nil, field_comment: nil) if import && !VALID_PROTOBUF_IMPORT_PATH.match?(import) raise Errors::SchemaError, "`protobuf` import for `#{name}` must be the path of a `.proto` file, " \ "but got: #{import.inspect}." end @protobuf_type = type @protobuf_import = import @protobuf_field_comment = field_comment end |
#referenced_proto_types ⇒ Array
Returns the schema types referenced by this definition.
98 99 100 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 98 def referenced_proto_types [] end |
#to_proto(_schema, _package_name) ⇒ nil
Scalars map to protobuf field types and do not render standalone definitions.
91 92 93 |
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 91 def to_proto(_schema, _package_name) nil end |