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

Add helper methods to Yaml{Encodr, Decoder, Codec} #303

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions core/shared/src/main/scala/org/virtuslab/yaml/YamlCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ package org.virtuslab.yaml
/**
* A type class that provides both-way conversion between [[Node]] and [[T]]
*/
trait YamlCodec[T] extends YamlDecoder[T] with YamlEncoder[T]
trait YamlCodec[T] extends YamlDecoder[T] with YamlEncoder[T] { self =>

object YamlCodec extends YamlCodecCompanionCrossCompat
def imap[T1](f: T => T1)(g: T1 => T): YamlCodec[T1] =
YamlCodec.from(self.map(f), self.contramap(g))

def iemap[T1](f: T => Either[ConstructError, T1])(g: T1 => T): YamlCodec[T1] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a better name for this shape of iso map? iemap is actively hostile (imap is too, tbh) if you're not familiar with ct and/or haskell

YamlCodec.from(self.flatMap(f), self.contramap(g))
}

object YamlCodec extends YamlCodecCompanionCrossCompat {

def apply[T](implicit self: YamlCodec[T]): YamlCodec[T] = self

def from[A](decoder: YamlDecoder[A], encoder: YamlEncoder[A]): YamlCodec[A] = new YamlCodec[A] {

override def construct(node: Node)(implicit settings: LoadSettings): Either[ConstructError, A] =
decoder.construct(node)

override def asNode(obj: A): Node =
encoder.asNode(obj)
}
}
16 changes: 15 additions & 1 deletion core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@ trait YamlDecoder[T] { self =>
): Either[ConstructError, T1] =
self.construct(node).map(f)
}

final def flatMap[T1](f: T => Either[ConstructError, T1]): YamlDecoder[T1] = new YamlDecoder[T1] {
override def construct(node: Node)(implicit
settings: LoadSettings
): Either[ConstructError, T1] =
self.construct(node).flatMap(f)
}
}

object YamlDecoder extends YamlDecoderCompanionCrossCompat {

def apply[T](implicit self: YamlDecoder[T]): YamlDecoder[T] = self

def from[T](pf: PartialFunction[Node, Either[ConstructError, T]])(implicit
classTag: ClassTag[T]
): YamlDecoder[T] = apply[T](pf)

def apply[T](
pf: PartialFunction[Node, Either[ConstructError, T]]
)(implicit classTag: ClassTag[T]): YamlDecoder[T] =
Expand Down Expand Up @@ -147,7 +161,7 @@ object YamlDecoder extends YamlDecoderCompanionCrossCompat {
}
}

implicit def forOption[T](implicit c: YamlDecoder[T]): YamlDecoder[Option[T]] = YamlDecoder {
implicit def forOption[T](implicit c: YamlDecoder[T]): YamlDecoder[Option[T]] = YamlDecoder.from {
node =>
if (node.tag == Tag.nullTag) Right(None)
else c.construct(node).map(Option(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ package org.virtuslab.yaml
/**
* A type class that provides a conversion from a given type [[T]] into [[Node]]
*/
trait YamlEncoder[T] {
trait YamlEncoder[T] { self =>
def asNode(obj: T): Node

final def contramap[T1](f: T1 => T): YamlEncoder[T1] = new YamlEncoder[T1] {
override def asNode(obj: T1): Node = self.asNode(f(obj))
}
}

object YamlEncoder extends YamlEncoderCrossCompanionCompat {

def apply[T](implicit self: YamlEncoder[T]): YamlEncoder[T] = self

implicit def forByte: YamlEncoder[Byte] = v => Node.ScalarNode(v.toString)
implicit def forChar: YamlEncoder[Char] = v => Node.ScalarNode(v.toString)
implicit def forShort: YamlEncoder[Short] = v => Node.ScalarNode(v.toString)
Expand Down