Class: ElasticGraph::SchemaDefinition::Indexing::FieldType::Enum Private

Inherits:
Data
  • Object
show all
Defined in:
elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/enum.rb,
elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/enum.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for the JSON schema and mapping of a SchemaElements::EnumType.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enum_value_namesArray<String> (readonly)

Returns list of names of values in this enum type.

Returns:

  • (Array<String>)

    list of names of values in this enum type.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/enum.rb', line 25

class Enum < ::Data
  # @return [Hash<String, ::Object>] the JSON schema for this enum type.
  def to_json_schema
    {"type" => "string", "enum" => enum_value_names}
  end

  # @return [Hash<String, ::Object>] the datastore mapping for this enum type.
  def to_mapping
    {"type" => "keyword"}
  end

  # @return [Hash<String, ::Object>] additional ElasticGraph metadata to put in the JSON schema for this enum type.
  def 
    {}
  end

  # @param customizations [Hash<String, ::Object>] JSON schema customizations
  # @return [Hash<String, ::Object>] formatted customizations.
  def format_field_json_schema_customizations(customizations)
    # Since an enum type already restricts the values to a small set of allowed values, we do not need to keep
    # other customizations (such as the `maxLength` field customization EG automatically applies to fields
    # indexed as a `keyword`--we don't allow enum values to exceed that length, anyway).
    #
    # It's desirable to restrict what customizations are applied because when a publisher uses the JSON schema
    # to generate code using a library such as https://github.com/pwall567/json-kotlin-schema-codegen, we found
    # that the presence of extra field customizations inhibits the library's ability to generate code in the way
    # we want (it causes the type of the enum to change since the JSON schema changes from a direct `$ref` to
    # being wrapped in an `allOf`).
    #
    # However, we still want to apply `enum` customizations--this allows a user to "narrow" the set of allowed
    # values for a field. For example, a `Currency` enum could contain every currency, and a user may want to
    # restrict a specific `currency` field to a subset of currencies (e.g. to just USD, CAD, and EUR).
    customizations.slice("enum")
  end

  # @dynamic initialize, enum_value_names
end

Instance Method Details

#format_field_json_schema_customizations(customizations) ⇒ Hash<String, ::Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns formatted customizations.

Parameters:

  • customizations (Hash<String, ::Object>)

    JSON schema customizations

Returns:

  • (Hash<String, ::Object>)

    formatted customizations.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/enum.rb', line 43

def format_field_json_schema_customizations(customizations)
  # Since an enum type already restricts the values to a small set of allowed values, we do not need to keep
  # other customizations (such as the `maxLength` field customization EG automatically applies to fields
  # indexed as a `keyword`--we don't allow enum values to exceed that length, anyway).
  #
  # It's desirable to restrict what customizations are applied because when a publisher uses the JSON schema
  # to generate code using a library such as https://github.com/pwall567/json-kotlin-schema-codegen, we found
  # that the presence of extra field customizations inhibits the library's ability to generate code in the way
  # we want (it causes the type of the enum to change since the JSON schema changes from a direct `$ref` to
  # being wrapped in an `allOf`).
  #
  # However, we still want to apply `enum` customizations--this allows a user to "narrow" the set of allowed
  # values for a field. For example, a `Currency` enum could contain every currency, and a user may want to
  # restrict a specific `currency` field to a subset of currencies (e.g. to just USD, CAD, and EUR).
  customizations.slice("enum")
end

#json_schema_field_metadata_by_field_nameHash<String, ::Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns additional ElasticGraph metadata to put in the JSON schema for this enum type.

Returns:

  • (Hash<String, ::Object>)

    additional ElasticGraph metadata to put in the JSON schema for this enum type.



37
38
39
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/enum.rb', line 37

def 
  {}
end

#to_json_schemaHash<String, ::Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the JSON schema for this enum type.

Returns:

  • (Hash<String, ::Object>)

    the JSON schema for this enum type.



27
28
29
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/enum.rb', line 27

def to_json_schema
  {"type" => "string", "enum" => enum_value_names}
end

#to_mappingHash<String, ::Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the datastore mapping for this enum type.

Returns:

  • (Hash<String, ::Object>)

    the datastore mapping for this enum type.



32
33
34
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/enum.rb', line 32

def to_mapping
  {"type" => "keyword"}
end