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

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_comment ⇒ Object (readonly)

Comment rendered above each generated proto field of this scalar type.



33
34
35
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 33

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.



29
30
31
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 29

def protobuf_import
  @protobuf_import
end

#protobuf_type ⇒ Object (readonly)

Configured protobuf type (e.g. string, int64, bool).



25
26
27
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 25

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 60

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_ingestion_metadata ⇒ RuntimeMetadata::Type

Returns the scalar's original name when it differs from the default for its protobuf type.

Returns:

  • (RuntimeMetadata::Type)


105
106
107
108
109
110
111
112
113
114
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 105

def 
  default_scalar = DEFAULT_SCALARS_BY_PROTO_TYPE[proto_name]
  scalar_name = type_ref.with_reverted_override.name

  RuntimeMetadata::Type.new(
    fields_by_name: {},
    non_standard_enum_values_by_name: {},
    scalar: (scalar_name == default_scalar) ? nil : scalar_name
  )
end

#proto_name ⇒ String

Returns this scalar's name in protobuf schemas.

Returns:

  • (String)


90
91
92
93
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 90

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)


98
99
100
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 98

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



44
45
46
47
48
49
50
51
52
53
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 44

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.

Returns:

  • (Array)


83
84
85
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 83

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)


76
77
78
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb', line 76

def to_proto(_schema, _package_name)
  nil
end