• This repository has been archived on 11/Sep/2023
  • Stars
    star
    4
  • Rank 3,304,323 (Top 66 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated about 4 years ago

Reviews

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

Repository Details

URL (de)construct. Withers.

Stone Maven Central Build Status

Handy Scala macros for everyday use: Route, Wither.

Scala 2.13 only!
ScalaJS 1 is supported.

@Route

Generates apply/unapply methods for extracting/constructing URLs. Here's why:

  • type safe URLs/routes
  • unlike Play SIRD and others, it can also construct a URL

In Play, Angular and other frameworks you'd write something like this:
/users/:id/:name ? minAge=:minAge & qs=:qs...

With @Route macro you write this:

@Route
class UsersRoute( /* first list contains path params */
    p1: "users",      // fixed
    val id: Long,     // variable
    val name: String
)(                /* second list contains query params (those after ?) */
    val minAge: Int,      // mandatory
    val opt: Option[Int], // optional
    val qs: Set[String]   // multi-valued
)

// construct a URL, type-safely
val route = UsersRoute(1, "Sake")(123, Some(456), Set("q1"))
val redirectUrl = route.urlData.url // /users/1/Sake?minAge=18&opt=456&qs=q1

// deconstruct a string URL to type-safe data
"users/1/Sake?minAge=123&qs=q1&qs=q2&opt=456" match {
  case UsersRoute(id, name, minAge, opt, qs) =>
    println(s"$id, $name, $minAge, $opt, $qs") // 1, Sake, 123, Some(456), Set(q1, q2)
  case _ => println("404 Not Found")
}

Match path segment regex

Regex is supported, just put it inside angle brackets:

@Route class RegexRoute(p1: "users", val name: "<[a-z]+>")

This would match a string /users/tom, but not /users/Tom.

Match multiple path segments

You can match on multi-segment path with a *:

@Route class StarRoute(p1: "files", val path: "*")

This would match a string /files/images/abc.jpg etc.
Basically, anything starting with /files...


@Wither

Generates with* methods. Here's why:

  • more readable than named args
  • autocomplete is nicer
  • additional withers for Option, List etc

If you have this:

@Wither
class MyClass(
  simple: Int,
  opt: Option[Int],
  list: List[Int]
)

you get to write:

val data = new ExampleData(1, Some(10), List(100))

data.withSimple(2)            // MyClass(2, Some(10), List(100))

data.withOpt(Some(11))        // MyClass(2, Some(11), List(100))
data.withOpt(12)              // MyClass(2, Some(12), List(100))

data.withList(List(101, 102)) // MyClass(7, None, List(101,102))
data.withList(103, 104)       // MyClass(7, None, List(103,104))

data.withSimple(2).withOpt(12).withList(103, 104) // MyClass(2, Some(12), List(103,104))

More Repositories

1

hepek

Typesafe HTML templates and static site generator in pure Scala
Scala
104
star
2

nand2tetris

Nand2Tetris course solutions
Scala
64
star
3

sharaf

Minimalistic Scala 3 web framework
Scala
31
star
4

notes

My book notes, tutorials sketches and stuff
HTML
27
star
5

sbt-hepek

Sbt plugin for rendering Scala objects to files. And more!
Scala
21
star
6

writing-an-interpreter

Writing a simple interpreter in ANTLR4 and by hand in Scala
Java
14
star
7

squery

Simple SQL queries in Scala 3
Scala
11
star
8

tupson

Stupid simple Scala 3 library for writing and reading JSON
Scala
6
star
9

kalem

Scalafix rules for generating Withers
Scala
6
star
10

hepek-examples

Self-contained examples of using hepek
Shell
6
star
11

PlayGuiceExample

PlayFramework, Scala, Guice, DI, Testing, ScalaTest
Scala
6
star
12

sake-ba-source

Source code of sake.ba
Scala
5
star
13

sharaf-petclinic

A PetClinic web app based on sharaf, hepek, and squery
Scala
4
star
14

cakum-pakum

Spring REST starter
Java
4
star
15

hepek-classycle

Classycle ressurection
Java
4
star
16

RxTags

Simple, reactive UIs in ScalaJS
Scala
4
star
17

Scalarizmi

Algorithms and data structures, in Scala
Scala
4
star
18

scalajs-router

ScalaJS frontend router
Scala
4
star
19

hepek-starter

Starter template for static blog with Hepek
Shell
4
star
20

causality

Event driven CQRS example
Java
4
star
21

scala-spec-hepek

Scala specification written with hepek
Scala
3
star
22

mimdex

Search. Memes.
JavaScript
2
star
23

playful-scala

Full-stack Scala with Play, ScalaJS, Hepek templates, Stone routes
Scala
2
star
24

hepek-cask-example

Combine Cask with Hepek
Shell
2
star
25

rpnhma

JavaScript
2
star
26

mill-hepek

Mill plugin for rendering Scala objects to files
Scala
2
star
27

fp4noobs

FP for beginners
Scala
2
star
28

mill-scala-hello.g8

Giter8 template for Scala hello world with Mill
Scala
2
star
29

hepek-play-example

Example of using Hepek with Play
Scala
2
star
30

hepek-kt

Hepek for Kotlin
Kotlin
2
star
31

hepek-zio-example

Scala
2
star
32

hepek-http4s-example

Example of using Hepek with Http4s
Scala
2
star
33

sharaf-fullstack.g8

Giter8 template for a Sharaf fullstack web app
Scala
2
star
34

mill-scala3-library-starter.g8

Giter8 template for a Scala 3 library with Mill
Shell
2
star
35

spring-session-example

Example of using spring-session project
Java
1
star
36

mill-powershell-completion

Basic TAB completion for Mill in PowerShell
PowerShell
1
star
37

PlayBootstrap

Play, Bootstrap, forms
Scala
1
star
38

Win32NamedPipe

Java
1
star
39

java-amber

Code from my presentation on Amber project
Java
1
star
40

sake-ba-blog-source

Source code of blog.sake.ba
Scala
1
star
41

hepek-core

The core of hepek project
Java
1
star
42

sharaf-todo-backend

Shell
1
star
43

capture-treasure-game

Self-playing "capture the treasure" game
Java
1
star
44

scala-guice

Guice examples with Scala
Scala
1
star
45

RxTags-Starter

RxTags starter template
Scala
1
star
46

sake92

1
star
47

spring-boot-mill

Shell
1
star
48

yugioh-abridged-calculator

yugioh abridged calculator
JavaScript
1
star