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

feat: 🚀 Add a ScalaDsl #355

Merged
merged 1 commit into from
Jan 19, 2023
Merged

feat: 🚀 Add a ScalaDsl #355

merged 1 commit into from
Jan 19, 2023

Conversation

gaeljw
Copy link
Collaborator

@gaeljw gaeljw commented Jan 8, 2023

Closes #123

I didn't implement the ideal DSL that I had in mind in the original issue but this is a first step which I believe is already nice to use.

// Pact JVM LambdaDsl
val dsl: DslPart = LambdaDsl
  .newJsonBody { rootObj =>
    rootObj.`object`(
      "keyA",
      o => {
        o.stringType("a1")
        o.stringType("a2")
        ()
      }
    )
    rootObj.array(
      "keyB",
      a => {
        a.integerType()
        ()
      }
    )
    ()
  }
  .build()

// Pact4s ScalaDsl
val dsl: DslPart = newJsonObject { rootObj =>
  rootObj.newObject("keyA") { o =>
    o.stringType("a1")
    o.stringType("a2")
  }
  rootObj.newArray("keyB") { a =>
    a.integerType()
  }
}

If we would like to go further, we'd need to duplicate every DSL method from Pact JVM which sounds painful in the long term. Note for future readers, in Scala 3 this would be slightly simpler thanks to Context Functions (https://dotty.epfl.ch/docs/reference/contextual/context-functions.html#example-builder-pattern).

@gaeljw
Copy link
Collaborator Author

gaeljw commented Jan 8, 2023

Any feedback is welcome @jbwheatley :)

@jbwheatley
Copy link
Owner

Hey Gael, thanks for this, looks good. Would you mind formatting the implicit classes so they are aligned with how they are done elsewhere in the project?

i.e.

object Foo {
  implicit class FooOps(val f: String) extends AnyVal {}
}

trait Foo {
  implicit def toFooOps(f: String): FooOps = new FooOps(f)
}

@gaeljw
Copy link
Collaborator Author

gaeljw commented Jan 19, 2023

@jbwheatley Done :)

@jbwheatley jbwheatley merged commit 90c10cf into jbwheatley:main Jan 19, 2023
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.

Provide a Scala Lambda DSL
2 participants