Module: ElasticGraph::ProtoIngestion::SchemaDefinition::SchemaElements::EnumTypeExtension

Defined in:
elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/enum_type_extension.rb

Overview

Extends EnumType with proto field type conversion.

Instance Method Summary collapse

Instance Method Details

#proto_enum_value_prefix ⇒ String

Returns the package-level prefix applied to this enum's protobuf values.

Returns:

  • (String)


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

def proto_enum_value_prefix
  @proto_enum_value_prefix ||= Support::Casing.to_upper_snake(name)
end

#proto_ingestion_metadata ⇒ RuntimeMetadata::Type

Returns names that removing the protobuf enum value prefix cannot recover.

Returns:

  • (RuntimeMetadata::Type)


97
98
99
100
101
102
103
104
105
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/enum_type_extension.rb', line 97

def 
  non_standard_enum_values_by_name = values_by_name.values.filter_map do |raw_value|
    value = raw_value # : ::ElasticGraph::SchemaDefinition::SchemaElements::EnumValue & EnumValueExtension
    proto_name = value.proto_name(proto_enum_value_prefix)
    [proto_name, value.name] unless ProtoConversions.enum_value_to_eg_name(proto_name, proto_enum_value_prefix) == value.name
  end.to_h

  RuntimeMetadata::Type.new(fields_by_name: {}, non_standard_enum_values_by_name: non_standard_enum_values_by_name, scalar: nil)
end

#proto_name ⇒ String

Returns this enum type's name in protobuf schemas.

Returns:

  • (String)


61
62
63
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/enum_type_extension.rb', line 61

def proto_name
  name
end

#proto_type_reference(package_name) ⇒ String

Returns the fully qualified name used to reference this enum from protobuf fields.

Returns:

  • (String)


68
69
70
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/enum_type_extension.rb', line 68

def proto_type_reference(package_name)
  ".#{package_name}.#{proto_name}"
end

#protobuf_field_comment ⇒ nil

Enum values are self-describing, so fields of this type get no format comment. Only scalar types document a format.

Returns:

  • (nil)


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

def protobuf_field_comment
  nil
end

#protobuf_import ⇒ nil

Enum types render their own protobuf definition, so they never require an import.

Returns:

  • (nil)


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

def protobuf_import
  nil
end

#referenced_proto_types ⇒ Array

Returns the schema types referenced by this definition.

Returns:

  • (Array)


54
55
56
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/enum_type_extension.rb', line 54

def referenced_proto_types
  []
end

#to_proto(schema, _package_name) ⇒ String

Renders this enum's protobuf definition.

Returns:

  • (String)


47
48
49
# File 'elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/enum_type_extension.rb', line 47

def to_proto(schema, _package_name)
  render_proto_enum(schema)
end

#value(value_name) ⇒ void

This method returns an undefined value.

Defines an enum value and immediately validates its protobuf name.



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

def value(value_name)
  super(value_name) do |new_value|
    new_proto_name = new_value.proto_name(proto_enum_value_prefix)

    if new_proto_name == proto_zero_value_name
      raise Errors::SchemaError, "Enum `#{name}` value `#{new_value.name}` maps to proto enum value name " \
        "`#{new_proto_name}`, which conflicts with the generated zero value `#{proto_zero_value_name}`."
    end

    if (duplicate = values_by_proto_name[new_proto_name])
      raise Errors::SchemaError, "Enum `#{name}` values `#{duplicate.name}` and `#{new_value.name}` " \
        "map to duplicate proto enum value name `#{new_proto_name}`."
    end

    yield new_value if block_given?
    values_by_proto_name[new_proto_name] = new_value
  end
end