Class: ElasticGraph::SchemaDefinition::Indexing::FieldType::Union Private

Inherits:
Object
  • Object
show all
Defined in:
elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/union.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.

Note:

In JSON schema, we model this with a oneOf, and a __typename field on each subtype.

Note:

Within the mapping, we have a single object type that has a set union of the properties of the subtypes (and also a __typename keyword field).

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#subtypes_by_nameHash<String, Object> (readonly)

Returns the subtypes of the union, keyed by name.

Returns:

  • (Hash<String, Object>)

    the subtypes of the union, keyed by name.



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
62
63
64
65
66
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/union.rb', line 26

class Union < ::Data.define(:subtypes_by_name)
  # @return [Hash<String, ::Object>] the JSON schema for this union type.
  def to_json_schema
    subtype_json_schemas = subtypes_by_name.keys.map { |name| {"$ref" => "#/$defs/#{name}"} }

    # A union type can represent multiple subtypes, referenced by the "anyOf" clause below.
    # We also add a requirement for the presence of __typename to indicate which type
    # is being referenced (this property is pre-defined on the type itself as a constant).
    #
    # Note: Although both "oneOf" and "anyOf" keywords are valid for combining schemas
    # to form a union, and validate equivalently when no object can satisfy multiple of the
    # subschemas (which is the case here given the __typename requirements are mutually
    # exclusive), we chose to use "oneOf" here because it works better with this library:
    # https://github.com/pwall567/json-kotlin-schema-codegen
    {
      "required" => %w[__typename],
      "oneOf" => subtype_json_schemas
    }
  end

  # @return [Hash<String, ::Object>] the datastore mapping for this union type.
  def to_mapping
    mapping_subfields = subtypes_by_name.values.map(&:subfields).reduce([], :union)

    Support::HashUtil.deep_merge(
      Field.normalized_mapping_hash_for(mapping_subfields),
      {"properties" => {"__typename" => {"type" => "keyword"}}}
    )
  end

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

  # @param customizations [Hash<String, ::Object>] JSON schema customizations
  # @return [Hash<String, ::Object>] formatted customizations.
  def format_field_json_schema_customizations(customizations)
    customizations
  end
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.



63
64
65
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/union.rb', line 63

def format_field_json_schema_customizations(customizations)
  customizations
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 union type.

Returns:

  • (Hash<String, ::Object>)

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



57
58
59
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/union.rb', line 57

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 union type.

Returns:

  • (Hash<String, ::Object>)

    the JSON schema for this union type.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/union.rb', line 28

def to_json_schema
  subtype_json_schemas = subtypes_by_name.keys.map { |name| {"$ref" => "#/$defs/#{name}"} }

  # A union type can represent multiple subtypes, referenced by the "anyOf" clause below.
  # We also add a requirement for the presence of __typename to indicate which type
  # is being referenced (this property is pre-defined on the type itself as a constant).
  #
  # Note: Although both "oneOf" and "anyOf" keywords are valid for combining schemas
  # to form a union, and validate equivalently when no object can satisfy multiple of the
  # subschemas (which is the case here given the __typename requirements are mutually
  # exclusive), we chose to use "oneOf" here because it works better with this library:
  # https://github.com/pwall567/json-kotlin-schema-codegen
  {
    "required" => %w[__typename],
    "oneOf" => subtype_json_schemas
  }
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 union type.

Returns:

  • (Hash<String, ::Object>)

    the datastore mapping for this union type.



47
48
49
50
51
52
53
54
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/indexing/field_type/union.rb', line 47

def to_mapping
  mapping_subfields = subtypes_by_name.values.map(&:subfields).reduce([], :union)

  Support::HashUtil.deep_merge(
    Field.normalized_mapping_hash_for(mapping_subfields),
    {"properties" => {"__typename" => {"type" => "keyword"}}}
  )
end