Class: PG::AzureWorkloadIdentity::ConnectionInfo::KeyValueString::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/pg/azure_workload_identity/connection_info/key_value_string.rb

Overview

Tokenizes a libpq key=value connection string into a hash of symbol-keyed parameters.

The grammar mirrors conninfo_parse: whitespace separates pairs, whitespace is allowed around =, values may be single-quoted, and \\X unescapes to X in both quoted and unquoted values. A trailing backslash at EOF inside an unquoted value is silently dropped (matching libpq).

Instance Method Summary collapse

Constructor Details

#initialize(connection_string) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • connection_string (String)

    the connection string to parse.



94
95
96
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 94

def initialize(connection_string)
  @buffer = StringScanner.new(connection_string)
end

Instance Method Details

#parseHash{Symbol => String}

Parses the connection string passed to the constructor.

Returns:

  • (Hash{Symbol => String})

    the parsed parameters, in the order they appeared in the input. Later occurrences of the same key overwrite earlier ones.

Raises:

  • (Error)

    when a keyword is missing, = is missing after a keyword, or a quoted value is not terminated.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 105

def parse # rubocop:disable Metrics/MethodLength
  result = {} # : Hash[Symbol, String]

  skip_whitespace
  until @buffer.eos?
    key = parse_key
    skip_whitespace
    assert_followed_by_equals_sign key
    skip_whitespace
    value = parse_value
    skip_whitespace

    result[key] = value
  end

  @buffer.reset
  result
end