All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.virtuslab.yaml.Yaml.scala Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package org.virtuslab

import org.virtuslab.yaml.internal.dump.present.PresenterImpl
import org.virtuslab.yaml.internal.dump.serialize.SerializerImpl
import org.virtuslab.yaml.internal.load.compose.ComposerImpl
import org.virtuslab.yaml.internal.load.parse.Parser
import org.virtuslab.yaml.internal.load.parse.ParserImpl
import org.virtuslab.yaml.internal.load.reader.Tokenizer

package object yaml {

  implicit class StringOps(val str: String) extends AnyVal {

    /**
   * Parse YAML from the given [[String]], returning either [[YamlError]] or [[T]].
   * 
   * According to the specification:
   * - [[Parser]] takes input string and produces sequence of events
   * - then [[Composer]] produces a representation graph from events
   * - finally [[YamlDecoder]] (construct phase from the YAML spec) constructs data type [[T]] from the YAML representation. 
   */
    def as[T](implicit
        c: YamlDecoder[T],
        settings: LoadSettings = LoadSettings.empty
    ): Either[YamlError, T] =
      for {
        events <- {
          val parser = ParserImpl(Tokenizer.make(str))
          parser.getEvents()
        }
        node <- ComposerImpl.fromEvents(events)
        t    <- node.as[T]
      } yield t

    def asNode: Either[YamlError, Node] =
      for {
        events <- {
          val parser = ParserImpl(Tokenizer.make(str))
          parser.getEvents()
        }
        node <- ComposerImpl.fromEvents(events)
      } yield node
  }

  implicit class AnyOps[T](val t: T) extends AnyVal {

    /**
   * Serialize a [[T]] into a YAML string.
   * 
   * According to the specification:
   * - [[YamlEncoder]] encode type [[T]] into [[Node]]
   * - [[Serializer]] serializes [[Node]] into sequence of [[Event]]s
   * - [[Presenter]] present [[Events]] as a character stream
   */
    def asYaml(implicit encoder: YamlEncoder[T]): String = {
      val node = encoder.asNode(t)
      node.asYaml
    }
  }

  implicit class NodeOps(val node: Node) extends AnyVal {
    def asYaml: String = {
      val events = SerializerImpl.toEvents(node)
      PresenterImpl.asString(events)
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy