Skip to content

Commit

Permalink
Merge pull request #296 from VirtusLab/option-encoder-2
Browse files Browse the repository at this point in the history
Option encoder
  • Loading branch information
lbialy authored Jul 10, 2024
2 parents c27ba44 + e140444 commit eb4e95d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ object YamlEncoder extends YamlEncoderCrossCompanionCompat {
implicit def forBoolean: YamlEncoder[Boolean] = v => Node.ScalarNode(v.toString)
implicit def forString: YamlEncoder[String] = v => Node.ScalarNode(v)

implicit def forOption[T](implicit encoder: YamlEncoder[T]): YamlEncoder[Option[T]] = {
case Some(t) => encoder.asNode(t)
case None => Node.ScalarNode("", Tag.nullTag)
}

implicit def forSet[T](implicit encoder: YamlEncoder[T]): YamlEncoder[Set[T]] = (nodes) =>
Node.SequenceNode(nodes.map(encoder.asNode(_)).toSeq, Tag.seq)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import scala.annotation.tailrec
import scala.collection.mutable

import org.virtuslab.yaml.Range
import org.virtuslab.yaml.Tag
import org.virtuslab.yaml.internal.load.parse.EventKind
import org.virtuslab.yaml.internal.load.parse.EventKind._
import org.virtuslab.yaml.internal.load.parse.NodeEventMetadata

object PresenterImpl extends Presenter {
override def asString(events: Seq[EventKind]): String = {
Expand All @@ -28,10 +30,11 @@ object PresenterImpl extends Presenter {
insertSequencePadding()
pushAndIncreaseIndent(SequenceStart())
parseSequence(tail)
case Scalar(value, _, _) =>
case Scalar(value, _, NodeEventMetadata(_, tag)) =>
insertSequencePadding()
// todo escape string using doublequotes
sb.append(value)
if (tag.contains(Tag.nullTag)) sb.append("!!null")
else sb.append(value)
sb.append(newline)
tail
case DocumentStart(_) => parseNode(tail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.virtuslab.yaml.Node
import org.virtuslab.yaml.Range
import org.virtuslab.yaml.internal.load.parse.EventKind
import org.virtuslab.yaml.internal.load.parse.EventKind._
import org.virtuslab.yaml.internal.load.parse.NodeEventMetadata

object SerializerImpl extends Serializer {
override def toEvents(node: Node): Seq[EventKind] =
Expand All @@ -28,5 +29,6 @@ object SerializerImpl extends Serializer {
}

private def convertScalarNode(node: Node.ScalarNode): Seq[EventKind] =
Seq(Scalar(node.value))
Seq(Scalar(node.value, metadata = NodeEventMetadata(tag = node.tag)))

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.virtuslab.yaml

import org.virtuslab.yaml.*

class BaseYamlEncoderSuite extends munit.FunSuite:
class YamlEncoderSuite extends munit.FunSuite:

test("plain value") {
val data: String = "aezakmi"
Expand Down Expand Up @@ -125,6 +125,16 @@ class BaseYamlEncoderSuite extends munit.FunSuite:
assertEquals(data.asYaml, expected)
}

test("option") {
case class Foo(field: Option[String]) derives YamlCodec

val some = Foo(Some("some"))
val none = Foo(None)

assertNoDiff(some.asYaml, "field: some")
assertNoDiff(none.asYaml, "field: !!null")
}

test("complex kubernetes mapping") {
case class Web(build: String, ports: List[String], volumes: List[String]) derives YamlCodec
case class Redis(image: String) derives YamlCodec
Expand Down

0 comments on commit eb4e95d

Please sign in to comment.