https://github.com/pathikrit/metarest
Scala macros to generate RESTful Models
https://github.com/pathikrit/metarest
rest-api scala-macros support-scala
Last synced: 15 days ago
JSON representation
Scala macros to generate RESTful Models
- Host: GitHub
- URL: https://github.com/pathikrit/metarest
- Owner: pathikrit
- Created: 2015-02-03T15:16:03.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-06-01T10:03:08.000Z (almost 8 years ago)
- Last Synced: 2025-03-26T20:21:19.687Z (about 1 month ago)
- Topics: rest-api, scala-macros, support-scala
- Language: Scala
- Homepage:
- Size: 72.3 KB
- Stars: 195
- Watchers: 9
- Forks: 11
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
MetaRest [](https://circleci.com/gh/pathikrit/metarest) [](https://bintray.com/pathikrit/maven/metarest/_latestVersion)
--------
Use Scala macros to generate your RESTy modelsLet's say you have the following `User` model in your business layer:
```scala
case class User(id: Int, name: String, email: String, registeredOn: DateTime)
```But, now you want to create well-formed models to describe the requests/responses of your HTTP REST APIs:
```scala
// Response to GET /users/$id (Retrieve an existing user)
case class UserGet(id: Int, name: String, email: String)// Request body of POST /users (Create a new user)
case class UserPost(name: String, email: String)//Request body of PATCH /users/$id (Edit name of an existing user)
case class UserPatch(name: Option[String])
```That is a lot of boilerplate! Keeping all these request models in sync with your business model and/or adding/removing fields quickly becomes tedious for more complicated models.
With MetaRest, all you need to do is:
```scala
import com.github.pathikrit.metarest._@Resource case class User(
@get id : Int,
@get @post @patch name : String,
@get @post email : String,
registeredOn : DateTime
)
```The above annotated code would generate code essentially looking like this:
```scala
object User {
case class Get(id: Int, name: String, email: String)
case class Post(name: String, email: String)
case class Patch(name: Option[String]) // Note: all fields in a PATCH are optional
}
```Now, you can have a well defined CRUD interface for your API:
```scala
trait UserRepo {
def getAll: List[User.Get]
def get(id: Int): User.Get
def create(request: User.Post): User.Get
def replace(id: Int, request: User.Put): User.Get
def update(id: Int, request: User.Patch): User.Get
def delete(id: Int): User.Get
}
```**sbt**
In your `build.sbt`, add the following entries:
```scala
resolvers += Resolver.bintrayRepo("pathikrit", "maven")libraryDependencies += "com.github.pathikrit" %% "metarest" % "2.0.0"
addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M8" cross CrossVersion.full)
```Although this library currently only supports Scala 2.11+, [older versions](https://github.com/pathikrit/metarest/tree/a883c674c67a31f9eddf70797328e864f185a714) of this library that used to support Scala 2.10.x are available [here](http://dl.bintray.com/pathikrit/maven/com/github/pathikrit).