File: param_formatter.rb

package info (click to toggle)
ruby-aws-sdk-core 3.104.3-3%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,444 kB
  • sloc: ruby: 11,201; makefile: 4
file content (70 lines) | stat: -rw-r--r-- 1,704 bytes parent folder | download | duplicates (3)
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
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
67
68
69
70
# frozen_string_literal: true

require 'pathname'

module Aws
  module Log
    # @api private
    class ParamFormatter

      # String longer than the max string size are truncated
      MAX_STRING_SIZE = 1000

      def initialize(options = {})
        @max_string_size = options[:max_string_size] || MAX_STRING_SIZE
      end

      def summarize(value)
        Hash === value ? summarize_hash(value) : summarize_value(value)
      end

      private

      def summarize_hash(hash)
        hash.keys.first.is_a?(String) ?
          summarize_string_hash(hash) :
          summarize_symbol_hash(hash)
      end

      def summarize_symbol_hash(hash)
        hash.map do |key,v|
          "#{key}:#{summarize_value(v)}"
        end.join(",")
      end

      def summarize_string_hash(hash)
        hash.map do |key,v|
          "#{key.inspect}=>#{summarize_value(v)}"
        end.join(",")
      end

      def summarize_string(str)
        if str.size > @max_string_size
          "#<String #{str[0...@max_string_size].inspect} ... (#{str.size} bytes)>"
        else
          str.inspect
        end
      end

      def summarize_value(value)
        case value
        when String   then summarize_string(value)
        when Hash     then '{' + summarize_hash(value) + '}'
        when Array    then summarize_array(value)
        when File     then summarize_file(value.path)
        when Pathname then summarize_file(value)
        else value.inspect
        end
      end

      def summarize_file(path)
        "#<File:#{path} (#{File.size(path)} bytes)>"
      end

      def summarize_array(array)
        "[" + array.map{|v| summarize_value(v) }.join(",") + "]"
      end

    end
  end
end