Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting something in the path #14

Open
timgent opened this issue Sep 21, 2016 · 2 comments
Open

Extracting something in the path #14

timgent opened this issue Sep 21, 2016 · 2 comments

Comments

@timgent
Copy link

timgent commented Sep 21, 2016

I have a yaml object which has been parsed successfully and I'm interested in just a couple of the keys. I'd like to be able to specify the paths for the keys and cast them to strings. Is there an easy way to do this? Couldn't see anything in the docs

@jcazevedo
Copy link
Owner

MoultingYAML doesn't provide that facility. Currently, the best way to achieve that is to define a function on the lines of the following:

def getFromPath(yaml: YamlValue, path: List[String]): Option[String] = {
  path match {
    case Nil => yaml match {
      case YamlString(s) => Some(s)
      case _ => None
    }
    case h :: t => yaml match {
      case YamlObject(fields) => fields.get(YamlString(h)).flatMap(getFromPath(_, t))
      case _ => None
    }
  }
}

val yaml = """
k1:
  k2:
    k3: "test"
  k3: "2"
""".parseYaml

getFromPath(yaml, List("k1")) // returns None
getFromPath(yaml, List("k1", "k2", "k3")) // returns Some("test")
getFromPath(yaml, List("k1", "k3")) // returns Some("2")

However, I have on my backlog the idea to integrate a lens library (probably Monocle) with MoultingYAML, and that would enable an easier definition of extractors from a path.

@timgent
Copy link
Author

timgent commented Sep 23, 2016

Thanks @jcazevedo, that's great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants