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

Option encoder #296

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Option encoder #296

wants to merge 4 commits into from

Conversation

lbialy
Copy link
Contributor

@lbialy lbialy commented May 10, 2024

Supersedes #127 (thanks @kpodsiad), closes #119

@lbialy lbialy requested a review from tgodzik May 10, 2024 14:49
Copy link
Member

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

LGTM, just one comment

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

assertNoDiff(some.asYaml, "field: some")
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a test going the other way? Or printing is not possible at all currently?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, sure it is!

@@ -18,6 +18,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)
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 some reason to encode null as scalar node with null tag? I know this should be question to myself, but just thinking loudly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are no Null nodes and I didn't check if null can be passed as a value to Scalar node. I wouldn't expect it to be tbh.

Copy link
Contributor

Choose a reason for hiding this comment

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

Node.ScalarNode("", Tag.nullTag) seems like a hack to represent something more complex that model was designed for :D It isn't obvious for me, but maybe it is ok because it is how spec defines it - https://yaml.org/spec/1.2.2/#10211-null.

Copy link
Contributor

Choose a reason for hiding this comment

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

ehh, I now saw the how Tag looks like. I think it's ok as it is tag which determines how value of scalar should be interpreted

@tgodzik
Copy link
Member

tgodzik commented May 29, 2024

@lbialy I think this can be merged?

@lbialy
Copy link
Contributor Author

lbialy commented May 29, 2024 via email

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

Successfully merging this pull request may close these issues.

Add YamlEncoder for Option
3 participants