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 import is rendered as import "PATH";, so a quote or newline in the path would produce invalid proto. protoc also requires the path to name a .proto file.

%r{\A[\w./-]+\.proto\z}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#protobuf_field_commentObject (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_importObject (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_typeObject (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.

Yields:

  • additional scalar type configuration

Raises:

  • (Errors::SchemaError)

    when a protobuf type is missing



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 (proto_options = BUILT_IN_SCALAR_PROTO_OPTIONS_BY_NAME[original_name])
    protobuf(**proto_options)
  end

  yield
  return if graphql_only?

  proto_name
  nil
end

#proto_nameString

Returns this scalar's name in protobuf schemas.

Returns:

  • (String)


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.

Returns:

  • (String)


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.

Parameters:

  • type (String)

    protobuf type name

  • import (String, nil) (defaults to: nil)

    proto file to import for an externally defined type

  • field_comment (String, nil) (defaults to: nil)

    comment rendered above each generated field of this type

Raises:

  • (Errors::SchemaError)

    when import is not a .proto file path



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_typesArray

Returns the schema types referenced by this definition.

Returns:

  • (Array)


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.

Returns:

  • (nil)


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