Module: Ruby::Rego::Builtins::Yaml::Emitter

Defined in:
lib/ruby/rego/builtins/yaml/emitter.rb,
lib/ruby/rego/builtins/yaml/emitter/key_order.rb,
lib/ruby/rego/builtins/yaml/emitter/float_format.rb

Overview

Go strconv ‘g’ float formatting (FormatFloat(f, ‘g’, -1, 64)) for the YAML scalar emitter, delegating the shortest-digit rendering to the shared GoNumberFormat (the same Go-‘g’ renderer the arbitrary-precision Number uses). Keeps the finite/zero handling the YAML emitter needs. float_string stays public because ScalarResolver calls it.

Defined Under Namespace

Classes: MarshalError

Constant Summary collapse

PLAIN =
Psych::Nodes::Scalar::PLAIN
DOUBLE =
Psych::Nodes::Scalar::DOUBLE_QUOTED
LITERAL =
Psych::Nodes::Scalar::LITERAL
AUTO =

ANY lets libyaml auto-pick plain/single/double exactly as yaml.v2’s libyaml does — so the structural quote choice matches OPA for free.

Psych::Nodes::Scalar::ANY
BLOCK_SEQ =
Psych::Nodes::Sequence::BLOCK
BLOCK_MAP =
Psych::Nodes::Mapping::BLOCK
TIMESTAMP =

yaml.v2 quotes strings that would otherwise resolve to a timestamp or a base-60 number (the latter dropped from resolution but still quoted out).

/\A\d{4}-\d{1,2}-\d{1,2}([Tt ]\d{1,2}:\d{1,2}:\d{1,2}(\.\d+)?([Zz]|[+-]\d{1,2}(:\d{2})?)?)?\z/
BASE60 =
/\A[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+(\.[0-9_]*)?\z/

Class Method Summary collapse

Class Method Details

.emit(ruby) ⇒ String

Parameters:

  • ruby (Object)

    a JSON-compatible Ruby value

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby/rego/builtins/yaml/emitter.rb', line 41

def self.emit(ruby)
  document = Psych::Nodes::Document.new([], [], true)
  document.implicit_end = true
  document.children << build_node(ruby)
  stream = Psych::Nodes::Stream.new
  stream.children << document
  stream.to_yaml
rescue Psych::Exception => e
  raise MarshalError, e.message
end

.float_string(float) ⇒ String

Go strconv.FormatFloat(f, ‘g’, -1, 64): shortest digits, scientific when the decimal exponent is < -4 or >= 6 (handled by GoNumberFormat).

Returns:

  • (String)

Raises:



17
18
19
20
21
22
23
# File 'lib/ruby/rego/builtins/yaml/emitter/float_format.rb', line 17

def self.float_string(float)
  raise MarshalError, "non-finite number" unless float.finite?
  return float.to_s.start_with?("-") ? "-0" : "0" if float.zero?

  digits, point = GoNumberFormat.shortest_digits(float.to_s)
  GoNumberFormat.render(digits, point, float.negative?)
end