Module: LogStruct::Log::Shared::MergeAdditionalDataFields

Constant Summary collapse

RESERVED_KEYS =

Every line's own fields, and the request ID the formatter adds unless the line has one

T.let(
  [LogField::Source, LogField::Event, LogField::Level, LogField::Timestamp, LogField::RequestId].to_set(&:serialize).freeze,
  T::Set[Symbol]
)
NO_FIELD_KEYS =
T.let(Set.new.freeze, T::Set[Symbol])

Instance Method Summary collapse

Instance Method Details

#field_keys ⇒ Set<Symbol>

The keys of every field of the struct's type, set or not on this line, which additional_data can't set: it would replace the field, or stand in for one the type didn't write. LogStruct's log types list theirs. A custom type's serialize writes its own fields after merging additional_data, so they win.

Returns:

  • (Set<Symbol>)


30
31
32
# File 'lib/log_struct/shared/merge_additional_data_fields.rb', line 30

def field_keys
  NO_FIELD_KEYS
end

#merge_additional_data_fields(hash) ⇒ void

This method returns an undefined value.

Parameters:

  • hash (Hash{Symbol => T.untyped})


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/log_struct/shared/merge_additional_data_fields.rb', line 35

def merge_additional_data_fields(hash)
  ad = additional_data
  return unless ad
  ad.each_pair.with_index do |(key, value), index|
    break if index >= LogStruct.config.limits.max_hash_keys
    next if key.to_s.bytesize > LogStruct.config.limits.max_string_bytes

    sym_key = key.to_sym
    if RESERVED_KEYS.include?(sym_key) || field_keys.include?(sym_key)
      LogStruct.handle_exception(
        ArgumentError.new("additional_data attempted to overwrite reserved key: #{sym_key}"),
        source: Source::Internal
      )
      next
    end
    hash[sym_key] = value
  end
end