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)


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

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

#proto_name ⇒ String

Returns this enum type's name in protobuf schemas.

Returns:

  • (String)


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

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)


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

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)


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

def protobuf_field_comment
  nil
end

#protobuf_import ⇒ nil

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

Returns:

  • (nil)


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

def protobuf_import
  nil
end

#referenced_proto_types ⇒ Array

Returns the schema types referenced by this definition.

Returns:

  • (Array)


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

def referenced_proto_types
  []
end

#to_proto(schema, _package_name) ⇒ String

Renders this enum's protobuf definition.

Returns:

  • (String)


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

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.



23
24
25
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/schema_elements/enum_type_extension.rb', line 23

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