Class: ElasticGraph::ProtoIngestion::SchemaDefinition::Identifier

Inherits:
Object
  • Object
show all
Defined in:
elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/identifier.rb

Overview

Helpers for validating Protocol Buffers package names. Other emitted identifiers come from GraphQL names, whose lexical grammar matches the Protocol Buffers identifier grammar.

Constant Summary collapse

VALID_PACKAGE_SEGMENT =

Matches a single valid protobuf package segment. https://protobuf.dev/reference/protobuf/proto3-spec/#identifiers

/\A[A-Za-z_][A-Za-z0-9_]*\z/

Class Method Summary collapse

Class Method Details

.validate_package_name(name) ⇒ String

Validates a protobuf package identifier.

Parameters:

  • name (String)

Returns:

  • (String)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/identifier.rb', line 26

def validate_package_name(name)
  if !name.is_a?(String) || name.empty?
    raise Errors::SchemaError, "`package_name` must be a non-empty String"
  end

  segments = name.split(".", -1)

  if segments.empty? || segments.any? { |segment| !VALID_PACKAGE_SEGMENT.match?(segment) }
    raise Errors::SchemaError, "`package_name` must be a dot-separated list of protobuf identifiers " \
      "(each starting with a letter or underscore, containing only letters, digits, and underscores), " \
      "got: #{name.inspect}."
  end

  name
end