summaryrefslogtreecommitdiff
path: root/lib/gitlab/utils/safe_inline_hash.rb
blob: 644d87c6876c85db253aa8c8af5f8554c4fbf39a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

module Gitlab
  module Utils
    class SafeInlineHash
      # Validates the hash size using `Gitlab::Utils::DeepSize` before merging keys using `Gitlab::Utils::InlineHash`
      def initialize(hash, prefix: nil, connector: '.')
        @hash = hash
      end

      def self.merge_keys!(hash, prefix: nil, connector: '.')
        new(hash).merge_keys!(prefix: prefix, connector: connector)
      end

      def merge_keys!(prefix:, connector:)
        raise ArgumentError, 'The Hash is too big' unless valid?

        Gitlab::Utils::InlineHash.merge_keys(hash, prefix: prefix, connector: connector)
      end

      private

      attr_reader :hash

      def valid?
        Gitlab::Utils::DeepSize.new(hash).valid?
      end
    end
  end
end