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.crConstructors
-
.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
Deserializes a
KYAML::Anyfrom a YAML node tree. -
.new(raw : Type)
Create a
KYAML::Anyto wrap the givenType. -
.new(raw : Int)
Convert Int to Int64.
-
.new(raw : Float)
Convert Float to Float64.
Instance Method Summary
-
#==(other : KYAML::Any) : Bool
Returns
trueif bothselfand other's raw object are equal -
#==(other) : Bool
Returns
trueif the raw object is equal to other. - #[](index_or_key : Int | String) : KYAML::Any
- #[]?(index_or_key : Int | String) : KYAML::Any | Nil
-
#as_a : Array(KYAML::Any)
Checks that the underlying value is
Arrayand returns its value. -
#as_a? : Array(KYAML::Any) | Nil
Checks that the underlying value is
Arrayand returns its value. -
#as_bool : Bool
Checks that the underlying value is
Bool, and returns its value. -
#as_bool? : Bool | Nil
Checks that the underlying value is a
Bool, and returns its value. -
#as_f : Float64
Checks that the underlying value is
Float64(orInt64) and returns its value. -
#as_f32? : Float32 | Nil
Checks that the underlying value is
Float64(orInt64), and returns its value asFloat32. -
#as_f? : Float64 | Nil
Checks that the underlying value is
Float64(orInt64) and returns its value. -
#as_h : Hash(String, KYAML::Any)
Checks that the underlying value is
Hashand returns its value. -
#as_h? : Hash(String, KYAML::Any) | Nil
Checks that the underlying value is
Hashand returns its value. -
#as_i : Int32
Checks that the underlying value is
Int64and returns its value asInt32. - #as_i64 : Int64
-
#as_i64? : Int64 | Nil
Checks that the underlying value is
Int64, and returns its value. -
#as_i? : Int32 | Nil
Checks that the underlying value is
Int64and returns its value asInt32. -
#as_nil : Nil
Checks that the underlying value is
nil, and returnsnil. -
#as_s : String
Checks that the underlying value is a
String, and returns its value. -
#as_s? : String | Nil
Checks that the underlying value is a
String, and returns its value. - #clone
-
#dig(index_or_key : Int | String, *subkeys) : KYAML::Any
Traverses the depth of a structure and returns the value, otherwise raises.
-
#dig?(index_or_key : Int | String, *subkeys) : KYAML::Any | Nil
Traverses the depth of a structure and returns the value.
- #dup
- #each(&) : Nil
-
#hash(hasher)
See
Object#hash(hasher) -
#inspect(io : IO) : Nil
Appends this struct's name and instance variables names and values to the given IO.
- #raw : Type
- #size : Int
-
#to_json(builder : JSON::Builder) : Nil
emits this value as JSON
-
#to_json_object_key : String
Forwards
#to_json_object_keyto raw, if it responds to that method. -
#to_kyaml(io : IO) : Nil
emits this value as KYAML to the given IO
-
#to_kyaml : String
emits this value as KYAML and returns it as a string
-
#to_s(io : IO) : Nil
Same as
#inspect(io).
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) : Nilto_kyaml(builder : KYAML::Builder) : Nil
to_kyaml : String to_kyaml
Constructor Detail
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 stdlibfrom_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 usesYAML::Schema::Core.eachfor merge-key (aka<<) resolution. - Alias: resolves the anchor reference and calls back here.
Instance Method Detail
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.
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.
Checks that the underlying value is Array and returns its value.
Otherwise raises.
Checks that the underlying value is Array and returns its value.
Otherwise returns nil.
Checks that the underlying value is Bool, and returns its value.
Otherwise raises.
Checks that the underlying value is a Bool, and returns its value.
Otherwise returns nil.
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
Checks that the underlying value is Float64 (or Int64), and returns its value as Float32.
Otherwise returns nil.
Checks that the underlying value is Float64 (or Int64) and returns its value.
Otherwise returns nil.
Checks that the underlying value is Hash and returns its value.
Deviation from YAML::Any: KYAML Hash requires string keys.
Otherwise raises.
Checks that the underlying value is Hash and returns its value.
Deviation from YAML::Any: KYAML Hash requires string keys.
Otherwise returns nil.
Checks that the underlying value is Int64 and returns its value as Int32.
Matches YAML::Any behavior.
Otherwise raises.
Checks that the underlying value is Int64, and returns its value.
Otherwise returns nil.
Checks that the underlying value is Int64 and returns its value as Int32.
Matches YAML::Any behavior.
Otherwise returns nil.
Checks that the underlying value is a String, and returns its value.
Otherwise raises.
Checks that the underlying value is a String, and returns its value.
Otherwise returns nil.
Traverses the depth of a structure and returns the value, otherwise raises.
TODO Probably need to add stricter conditionals here
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.
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.
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)"
Forwards #to_json_object_key to raw, if it responds to that method.
Otherwise Raises JSON::Error