• This repository has been archived on 11/Sep/2023
  • Stars
    star
    4
  • Rank 3,062,113 (Top 65 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated about 3 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
96
star
2

nand2tetris

Nand2Tetris course solutions
Scala
58
star
3

notes

My book notes, tutorials sketches and stuff
HTML
25
star
4

sbt-hepek

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

sharaf

Minimalistic Scala 3 web framework
Scala
19
star
6

writing-an-interpreter

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

kalem

Scalafix rules for generating Withers
Scala
6
star
8

hepek-examples

Self-contained examples of using hepek
Shell
6
star
9

PlayGuiceExample

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

squery

Simple SQL queries in Scala 3
Scala
5
star
11

sake-ba-source

Source code of sake.ba
Scala
5
star
12

cakum-pakum

Spring REST starter
Java
4
star
13

hepek-classycle

Classycle ressurection
Java
4
star
14

causality

Event driven CQRS example
Java
4
star
15

RxTags

Simple, reactive UIs in ScalaJS
Scala
4
star
16

Scalarizmi

Algorithms and data structures, in Scala
Scala
4
star
17

scalajs-router

ScalaJS frontend router
Scala
4
star
18

hepek-starter

Starter template for static blog with Hepek
Shell
4
star
19

hepek-starter.g8

Hepek giter8 template
Scala
3
star
20

tupson

Stupid simple Scala 3 library for writing and reading JSON
Scala
3
star
21

scala-spec-hepek

Scala specification written with hepek
Scala
3
star
22

cask-hepek

Combine Cask with Hepek
Scala
2
star
23

playful-scala

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

rpnhma

JavaScript
2
star
25

mimdex

Search. Memes.
JavaScript
2
star
26

fp4noobs

FP for beginners
Scala
2
star
27

mill-scala-hello.g8

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

hepek-kt

Hepek for Kotlin
Kotlin
2
star
29

play-hepek-example

Example of using Hepek with Play
Scala
2
star
30

spring-session-example

Example of using spring-session project
Java
1
star
31

Win32NamedPipe

Java
1
star
32

mill-powershell-completion

Basic TAB completion for Mill in PowerShell
PowerShell
1
star
33

sake-ba-blog-source

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

PlayBootstrap

Play, Bootstrap, forms
Scala
1
star
35

java-amber

Code from my presentation on Amber project
Java
1
star
36

sharaf-todo-backend

Shell
1
star
37

yugioh-abridged-calculator

yugioh abridged calculator
JavaScript
1
star
38

mill-hepek

Mill plugin for rendering Scala objects to files
Scala
1
star
39

sharaf-petclinic

A PetClinic web app based on sharaf, hepek, and squery
Scala
1
star
40

hepek-core

The core of hepek project
Java
1
star
41

capture-treasure-game

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

scala-guice

Guice examples with Scala
Scala
1
star
43

RxTags-Starter

RxTags starter template
Scala
1
star
44

spring-boot-mill

Shell
1
star