Hi,
I’m trying to use YAML files as a read-only datastore. I have a YAML file with an array of objects. Let’s say Authors. And I have a corresponding Author struct. I’m reading the YAML, looping through each record and returning an Array(Author)
.
However, I noticed that YAML is deserializing the array as YAML::Any instead of an array of hash (like Ruby). So, I had to typecast to get it to work.
struct Author
property id, name, email
def initialize(@id : Int32, @name : String, @email : String)
end
end
require "yaml"
require "./data_provider"
class YamlDataProvider < DataProvider
def authors : Array(Author)
results = Array(Author).new
yaml = YAML.parse(File.read("../db/authors.yaml"))
yaml["authors"].as_a.each do |author|
results << Author.new(
author["id"].to_i.to_s,
author["name"].to_s,
author["email"].to_s
)
end
results
end
end
Is there a better way to achieve this? Something more idiomatic for Crystal? Am I missing something?
Thanks!