Class: ElasticGraph::Indexer::IndexingPreparers::Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/indexer/indexing_preparers/integer.rb

Class Method Summary collapse

Class Method Details

.prepare_for_indexing(value) ⇒ Object

Here we coerce an integer-valued float like ‘3.0` to a true integer (e.g. `3`). This is necessary because:

1. If a field is an integer in the datastore mapping, it does not tolerate it coming in
   as a float, even if it is integer-valued.
2. While we use JSON schema to validate event payloads before we get here, JSON schema
   cannot consistently enforce that we receive true integers for int fields.

As json-schema.org/understanding-json-schema/reference/numeric.html#integer explains:

> Warning > > The precise treatment of the “integer” type may depend on the implementation of your > JSON Schema validator. JavaScript (and thus also JSON) does not have distinct types > for integers and floating-point values. Therefore, JSON Schema can not use type alone > to distinguish between integers and non-integers. The JSON Schema specification > recommends, but does not require, that validators use the mathematical value to > determine whether a number is an integer, and not the type alone. Therefore, there > is some disagreement between validators on this point. For example, a JavaScript-based > validator may accept 1.0 as an integer, whereas the Python-based jsonschema does not.

Raises:

  • (IndexOperationError)


33
34
35
36
37
# File 'lib/elastic_graph/indexer/indexing_preparers/integer.rb', line 33

def self.prepare_for_indexing(value)
  integer = value.to_i
  return integer if value == integer
  raise IndexOperationError, "Cannot safely coerce `#{value.inspect}` to an integer"
end