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

Use default arguments when field is not provided #215

Closed
scalway opened this issue Apr 8, 2023 · 2 comments · Fixed by #326
Closed

Use default arguments when field is not provided #215

scalway opened this issue Apr 8, 2023 · 2 comments · Fixed by #326
Assignees
Labels
feature New feature or request has-pr

Comments

@scalway
Copy link

scalway commented Apr 8, 2023

Upickle and some other json libraries uses default arguments when field is not provided. It'll highly improve readability of yaml when you don't need to provide optional fields, and will give Devs much more flexibility.

//> using scala "3.2.2"
//> using dep "org.virtuslab::scala-yaml:0.0.6"

import org.virtuslab.yaml.*

case class Foo(id:String, name:String = "optional") derives YamlCodec

val example1 = 
	"id: 0112\n" + 
	"name: Tottally not fake name\n"
	
val example2 = 
	"id: 0113\n"

println(">" + example1.as[Foo]) //> Right(Foo(0112,Tottally not fake name))
println(">" + example2.as[Foo]) //> Left(ConstructError(Key name doesn't exist in parsed document)) 
// expected                     //> Right(Foo(0113,optional))
@scalway scalway changed the title Use default arguments when field not provided Use default arguments when field is not provided Apr 8, 2023
@Flowdalic
Copy link
Contributor

I am looking for a similar feature, where defaults can be omitted from configuration files. However, I wonder if this should only be done if the type is Option. So instead of

case class Foo(id:String, name:String = "optional") derives YamlCodec

you have

case class Foo(id:String, name:Option[String] = None) derives YamlCodec

@Flowdalic
Copy link
Contributor

Ha, after wondering why I see code related to the functionality I am looking for, I've discovered that this was only recently added: 9054aff and not yet released.

@lbialy lbialy added the feature New feature or request label May 10, 2024
@lbialy lbialy self-assigned this Jul 21, 2024
@lbialy lbialy added the has-pr label Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request has-pr
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants