• Stars
    star
    195
  • Rank 199,374 (Top 4 %)
  • Language
    Scala
  • Created almost 10 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Scala macros to generate RESTful Models

MetaRest CircleCI Download

Use Scala macros to generate your RESTy models

Let's say you have the following User model in your business layer:

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:

// 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:

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:

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:

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:

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 of this library that used to support Scala 2.10.x are available here.

More Repositories

1

better-files

Simple, safe and intuitive Scala I/O
Scala
1,473
star
2

scalgos

algorithms in scala
Scala
434
star
3

sauron

Yet another Scala lens macro
Scala
168
star
4

mac-setup-script

script to setup my mac
Shell
161
star
5

newswall

Rotate and display latest newspapers on e-ink display
JavaScript
36
star
6

JFaceRecog

facial recognition in Java
Java
31
star
7

vocowl

A smart flashcard app
CoffeeScript
21
star
8

chatgpt-cli

Simple CLI for ChatGPT using node.js
JavaScript
16
star
9

node-thunder-driver

Nodejs driver for Dream Cheeky Thunder Missile Launcher
JavaScript
14
star
10

JSpeech2Text

A simple speech to text app in Java
Java
6
star
11

coffee-pearls

collection of small beautiful programs written in coffeescript
CoffeeScript
5
star
12

full-stack-scala-web-app

Working code for the "Full Stack Scala Web App" tech talk
Scala
4
star
13

Quora-Challenges

My solution in Java to the Quora Datacenter cooling challange problem
Java
4
star
14

Monty

a simple no-limit texas hold'em bot using monte carlo simulations
Scala
3
star
15

stamp

Scala library to format dates and times based on human-friendly examples
Scala
3
star
16

garfield

Scala
3
star
17

chrome_ai

Random collection of my chrome extensions
JavaScript
3
star
18

my-vestaboard

JavaScript
2
star
19

node-thunder-webui

a web interface for dream cheeky usb thunder missile launcher
JavaScript
2
star
20

BabelFish

Invoke other languages from Scala on the JVM
Scala
1
star
21

Kotha

A simple Java framework to write strongly typed remote APIs
Java
1
star
22

visionect-deploy

Code to Deploy Visionect Software
Dockerfile
1
star
23

bond_ladder_calculator

Python
1
star