I’m writing a class that needs to parse simple YAML configuration files into corresponding sclang data structures and was wondering whether there’s a simple means of converting the Dictionaries returned by String.parseYAMLFile (in which all keys are Strings) into IdentityDictionaries in which all keys are Symbols. Dictionaries may be nested arbitrarily – the structure of the tree can’t be known in advance.
I’m quite clear on why String.parseYAMLFile treats everything as a String, and I’m fine with it being the user’s responsibility to ensure that keys used in the YAML files are unique, valid Symbols. I suspect that I might be missing something obvious that already exists in the class library for this purpose (some undocumented variation on “collect” maybe)? Any suggestions would be much appreciated. Thanks!
Thanks for this. Sorry if I was unclear in my initial post – iterating over the associations in the top-level Dictionary won’t convert any nested Dictionaries, unfortunately.
The YAML files I’m parsing may contain Dictionaries of Dictionaries, nested to arbitrary depth. Here’s a concrete example (this is part of a class I’m writing to make it easier to refer to subsets of s.inputBus and s.outputBus via versioned configuration files that specify what’s patched to what in my studio):
The config file format isn’t fixed at this point, so I’m hoping to convert the Dictionary in a way that doesn’t make any assumptions about the structure of the YAML file other than unique keys in any given dict.
Yes, that’s exactly it. In the absence of an already existing method, I knew the solution would be recursive, I was just having trouble visualizing the exact function. Thanks so much!
I haven’t tested interpret for any complex cases yet (just Integers, Floats, Strings, and Symbols so far), but will note that the quoting needed to specify Strings and Symbols in a YAML file looks like:
'"foo"' # String
"'bar'" # Symbol
\baz # Symbol
Of course String.parseYAMLFile can return other data structures depending on the structure of the underlying YAML – the above is only meant to deal with nested Dictionaries.