All Projects → aloisdeniel → route_pattern_generator

aloisdeniel / route_pattern_generator

Licence: other
A Dart static code generator that produces matchers and builders from route uri patterns.

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to route pattern generator

node-match-path
Matches a URL against a path. Parameters, wildcards, RegExp.
Stars: ✭ 30 (-3.23%)
Mutual labels:  match, routing
glob
Pure Nim library for matching file paths against Unix style glob patterns.
Stars: ✭ 58 (+87.1%)
Mutual labels:  pattern, match
mswjs.io
Official website and documentation for the Mock Service Worker library.
Stars: ✭ 77 (+148.39%)
Mutual labels:  match, routing
html-comment-regex
Regular expression for matching HTML comments
Stars: ✭ 15 (-51.61%)
Mutual labels:  pattern, match
Bidi
Bidirectional URI routing
Stars: ✭ 941 (+2935.48%)
Mutual labels:  uri, routing
bash-glob
Bash-powered globbing for node.js. Alternative to node-glob. Does not work on Windows 9 and lower.
Stars: ✭ 13 (-58.06%)
Mutual labels:  pattern, match
Easy Tips
A little Tips in my Code Career with Go&PHP 🥳🥳🥳
Stars: ✭ 2,555 (+8141.94%)
Mutual labels:  pattern
cloudflare-worker-router
A super lightweight router (1.3K) with middleware support and ZERO dependencies for CloudFlare Workers.
Stars: ✭ 144 (+364.52%)
Mutual labels:  routing
Kommander Ios
A lightweight, pure-Swift library for manage the task execution in different threads. Through the definition a simple but powerful concept, Kommand.
Stars: ✭ 167 (+438.71%)
Mutual labels:  pattern
Ruby Possibly
A maybe monad
Stars: ✭ 149 (+380.65%)
Mutual labels:  pattern
dcompass
A high-performance programmable DNS component aiming at robustness, speed, and flexibility
Stars: ✭ 260 (+738.71%)
Mutual labels:  routing
rrx
⚛️ Minimal React router using higher order components
Stars: ✭ 69 (+122.58%)
Mutual labels:  routing
pinecone
Peer-to-peer overlay routing for the Matrix ecosystem
Stars: ✭ 361 (+1064.52%)
Mutual labels:  routing
Getx pattern
Design pattern designed to standardize your projects with GetX on Flutter.
Stars: ✭ 225 (+625.81%)
Mutual labels:  pattern
jurl
Fast and simple URL parsing for Java, with UTF-8 and path resolving support
Stars: ✭ 84 (+170.97%)
Mutual labels:  uri
Javascript Total
Сборник практических вопросов, задач разного уровня сложности, сниппетов (утилит), паттерны проектирования, а также полезные ссылки по JavaScript
Stars: ✭ 214 (+590.32%)
Mutual labels:  pattern
spec-pattern
Specification design pattern for JavaScript and TypeScript with bonus classes
Stars: ✭ 43 (+38.71%)
Mutual labels:  pattern
Jxpatternlock
An easy-to-use, powerful, customizable pattern lock view in swift. 图形解锁/手势解锁 / 手势密码 / 图案密码 / 九宫格密码
Stars: ✭ 165 (+432.26%)
Mutual labels:  pattern
nepomuk
A public transit router for GTFS feeds (currently only static) written in modern c++
Stars: ✭ 22 (-29.03%)
Mutual labels:  routing
receptacle
minimalistic implementation of the repository pattern
Stars: ✭ 18 (-41.94%)
Mutual labels:  pattern

route_pattern_generator

A Dart static code generator that produces matchers and builders from route URI patterns.

Quickstart

Define your patterns as constant strings annoted with @route.

import 'package:route_pattern/route_pattern.dart';
import 'package:flutter/widgets.dart';

part 'routes.g.dart';

@RoutePattern("/?tab&[int]scroll")
Route home(RouteSettings settings, HomeRouteArguments arguments) {
    // returns a Route
}

@RoutePattern("/article/:[int]id")
Route article(RouteSettings settings, ArticleRouteArguments arguments) {
    // returns a Route
}

Each constant will generate a Route with a build and match method, and a associated Argument class (an example of the generate sources is available in the sample).

// Build specific routes
final path = Routes.home.build(HomeRouteArguments(tab: "users"));
expect(path, "/?tab=users");

final path = Routes.article.build(ArticleRouteArguments(id: "12345"));
expect(path, "/article/12345");

// Match specific routes
final match = Routes.home.match("/?tab=users");
expect(match.isSuccess, true);
expect(match.arguments.tab, 'users');

final match = Routes.article.match("/article/12345");
expect(match.isSuccess, true);
expect(match.arguments.id, '12345');

// Or get the first matching route
final match = Routes.match("/article/12345");
if(match is MatchResult<ArticleRouteArguments>) {
    expect(match.arguments.id, '12345');
}

A Routes.onGenerateRoute method is also generated to use in your app or navigator. It will match the first route which settings name matches a pattern and call your declared function with corresponding arguments.

import 'package:sample/routes.dart';
import 'package:flutter/material.dart';

class ExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      onGenerateRoute: Routes.onGenerateRoute,
      // ...
    );
  }
}

To navigate to a given route, a Routes.push method is also available :

await Routes.push(context, ArticleRouteArguments(id: '12345'));

How to use

Install

There are a few separate packages you need to install:

dependencies:
  route_pattern:

dev_dependencies:
  route_pattern_generator: 
  build_runner: 

Pattern format

A route pattern is composed of static segments separated with /, required parameters as dynamic segments (starting with :), and optional parameters as query parameters (starting with ? and separated by &).

Typing parameters

By default, arguments are of type String, but a custom type surrounded with [ and ] can be added at the beginning of a required or optional parameter. This type must have static T parse(String value) and String toString() methods to serialize and deserialize arguments from path.

Example

/article/:[int]id/details?tab&[int]scroll

  • article : static segment
  • id : required dynamic segment of type int
  • details : static segment
  • tab : optionnal query parameter of type String
  • scroll : optionnal query parameter of type int

This example will match those URIs :

  • /article/26436/details
  • /article/1234/details?tab=second
  • /article/98904/details?tab=first&scroll=8

Run the generator

To run the generator, you must use build_runner cli:

flutter pub pub run build_runner watch
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].