struct KYAML::Any

Overview

KYAML::Any is a wrapper around all possible KYAML value types and can be used for traversing dynamic or unknown KYAML/YAML structures. Mirrors the YAML::Any interface for familiarity.

See https://github.com/crystal-lang/crystal/blob/master/src/yaml/any.cr

require "kyaml"

data = KYAML.parse <<-KYAML
         ---
         {
           foo: {
             bar: {
               baz: ["qux", "fox"],
             },
           },
         }
         KYAML
data["foo"]["bar"]["baz"][0].as_s # => "qux"
data["foo"]["bar"]["baz"].as_a    # => [KYAML::Any("qux"), KYAML::Any("fox")]

Note that methods used to traverse a KYAML structure (#[], #[]?, #each), always return a KYAML::Any to allow further traversal.

To extract the underlying scalar to String, Array, etc., use the as_ methods (eg #as_s, #as_a) which perform a type check against the raw underlying value. Invoking #as_s when the underlying value is not a String will raise and the value is not auto-converted. Nilable variants (eg #as_i?, #as_s?) return nil instead of raising when the type doesn't match.

Defined in:

kyaml/any.cr

Constructors

Instance Method Summary

Instance methods inherited from struct Struct

==(other : KYAML::Any) ==

Instance methods inherited from struct Value

==(other : KYAML::Any) ==

Instance methods inherited from class Object

to_kyaml(io : IO) : Nil
to_kyaml(builder : KYAML::Builder) : Nil
to_kyaml : String
to_kyaml

Constructor Detail

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) #

Deserializes a KYAML::Any from a YAML node tree.

This is the primary factory method for building a KYAML::Any from parsed YAML. It reuses stdlib YAML::ParseContext, YAML::Nodes, and YAML::Schema::Core for the heavy lifting.

  • Scalar: delegates to YAML::Schema::Core.parse_scalar(node). Deviation from YAML::Any in that KYAML treats Time and Bytes as quoted Strings.
  • Sequence: delegates to Array(KYAML::Any).new(ctx, node) from stdlib from_yaml.cr, which calls back here for each child element.
  • Mapping: delegates to Hash(String, KYAML::Any).new(ctx, node) which enforces String keys and uses YAML::Schema::Core.each for merge-key (aka <<) resolution.
  • Alias: resolves the anchor reference and calls back here.

[View source]
def self.new(raw : Type) #

Create a KYAML::Any to wrap the given Type.


[View source]
def self.new(raw : Int) #

Convert Int to Int64. Matches YAML::Any behavior.


[View source]
def self.new(raw : Float) #

Convert Float to Float64. Matches YAML::Any behavior.


[View source]

Instance Method Detail

def ==(other : KYAML::Any) : Bool #

Returns true if both self and other's raw object are equal


[View source]
def ==(other) : Bool #

Returns true if the raw object is equal to other.


[View source]
def [](index_or_key : Int | String) : KYAML::Any #

Assumes underlying value is Array or Hash and returns the element at the given index_or_key. Only accepts Int for Array. Only accepts String for Hash, since KYAML Hash keys are always String.

Raises otherwise.


[View source]
def []?(index_or_key : Int | String) : KYAML::Any | Nil #

Assumes underlying value is an Array or a Hash and returns the element at the given index_or_key, or nil if either the index is out of bounds, the key is missing, or key is not a String. Only accepts Int for Array. Only accepts String for Hash, since KYAML Hash keys are always String.

Raises if underlying value is not an Array or a Hash.


[View source]
def as_a : Array(KYAML::Any) #

Checks that the underlying value is Array and returns its value.

Otherwise raises.


[View source]
def as_a? : Array(KYAML::Any) | Nil #

Checks that the underlying value is Array and returns its value.

Otherwise returns nil.


[View source]
def as_bool : Bool #

Checks that the underlying value is Bool, and returns its value.

Otherwise raises.


[View source]
def as_bool? : Bool | Nil #

Checks that the underlying value is a Bool, and returns its value.

Otherwise returns nil.


[View source]
def as_f : Float64 #

Checks that the underlying value is Float64 (or Int64) and returns its value. Matches YAML::Any behavior, accepts Int and converts to Float for convenience.

Otherwise raises


[View source]
def as_f32? : Float32 | Nil #

Checks that the underlying value is Float64 (or Int64), and returns its value as Float32.

Otherwise returns nil.


[View source]
def as_f? : Float64 | Nil #

Checks that the underlying value is Float64 (or Int64) and returns its value.

Otherwise returns nil.


[View source]
def as_h : Hash(String, KYAML::Any) #

Checks that the underlying value is Hash and returns its value. Deviation from YAML::Any: KYAML Hash requires string keys.

Otherwise raises.


[View source]
def as_h? : Hash(String, KYAML::Any) | Nil #

Checks that the underlying value is Hash and returns its value. Deviation from YAML::Any: KYAML Hash requires string keys.

Otherwise returns nil.


[View source]
def as_i : Int32 #

Checks that the underlying value is Int64 and returns its value as Int32. Matches YAML::Any behavior.

Otherwise raises.


[View source]
def as_i64 : Int64 #

[View source]
def as_i64? : Int64 | Nil #

Checks that the underlying value is Int64, and returns its value.

Otherwise returns nil.


[View source]
def as_i? : Int32 | Nil #

Checks that the underlying value is Int64 and returns its value as Int32. Matches YAML::Any behavior.

Otherwise returns nil.


[View source]
def as_nil : Nil #

Checks that the underlying value is nil, and returns nil.

Otherwise raises.


[View source]
def as_s : String #

Checks that the underlying value is a String, and returns its value.

Otherwise raises.


[View source]
def as_s? : String | Nil #

Checks that the underlying value is a String, and returns its value.

Otherwise returns nil.


[View source]
def clone #

Returns a new KYAML::Any instance with the #raw value #cloneed


[View source]
def dig(index_or_key : Int | String, *subkeys) : KYAML::Any #

Traverses the depth of a structure and returns the value, otherwise raises.

TODO Probably need to add stricter conditionals here


[View source]
def dig?(index_or_key : Int | String, *subkeys) : KYAML::Any | Nil #

Traverses the depth of a structure and returns the value. Only accepts Int for Array. Only accepts String for Hash, since KYAML Hash keys are always String.

Returns nil if not found.


[View source]
def dup #

Returns a new KYAML::Any instance with the #raw value #duped


[View source]
def each(&) : Nil #

Iterates over underlying Array or Hash, yielding two values per iteration.

Array: yields index and element as (Int32, KYAML::Any) pairs. Use _ to discard index (eg. any.each { |_, elem| ... }) Hash: yields key and value as (String, KYAML::Any) pairs.

Note: The block's first parameter is typed Int32 | String and may require .as(...) in strict-typed call sites. Raises if underlying value is neither Array nor Hash.


[View source]
def hash(hasher) #

See Object#hash(hasher)


[View source]
def inspect(io : IO) : Nil #
Description copied from struct Struct

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"

[View source]
def raw : Type #

[View source]
def size : Int #

Assumes underlying value is Array or Hash and returns its size.

Raises otherwise.


[View source]
def to_json(builder : JSON::Builder) : Nil #

emits this value as JSON


[View source]
def to_json_object_key : String #

Forwards #to_json_object_key to raw, if it responds to that method. Otherwise Raises JSON::Error


[View source]
def to_kyaml(io : IO) : Nil #

emits this value as KYAML to the given IO


[View source]
def to_kyaml : String #

emits this value as KYAML and returns it as a string


[View source]
def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


[View source]