Description

Simple Piping / Chaining functionality

Package Information

Version0.9.0 (2017-Jan-26)
Homepage https://github.com/tmarsteel/dpipe
Repository https://github.com/tmarsteel/dpipe
LicenseLGPL-3.0
CopyrightCopyright © 2017, tobias.marstaller
Authorstobias.marstaller
Registered byTobias Marstaller
Dependencies none

Installation

To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Readme

dpipe

Simple Piping / Chaining functionality for the D language

Usage

dub install tmarsteel/dpipe

Examples

HTTP

import tmarsteel.pipe;
import vibe.d;

void main() {
  auto Router = new Router();
  // configure router

  auto requestHandler = new Pipe!(HttpRequest, HttpResponse)(router);
  requestHandler.attach(new CookieEncryptionMiddleware());
  // ... attach more middleware ...
  requestHandler.attach(new AuthenticationMiddleware());

  // use the pipe
  listenHTTP(serverSettings, requestHandler);
}

class AuthenticationMiddleware : Link!(HttpRequest, HttpResponse) {
  public HttpResponse process(HttpRequest request, PipeElement!(HttpRequest, HttpResponse) next) {
    if (this.isAuthenticated(request)) {
      return next(request);
    }
    else {
      return HttpResponse.unauthenticated();
    }
  }

  private boolean isAuthenticated(HttpRequest) {
    // ...
  }
}

class Router : PipeElement!(HttpRequest, HttpResponse) {
  public HttpResponse process(HttpRequest request) {
    // map request to Controller
    return controller.indexAction(request);
  }
}

class Controller {
  public HttpResponse indexAction(HttpRequest request) {
    return HttpResponse.ok("Hello World!");
  }
}

General

import std.stdio;
import tmarsteel.pipe;

void main()
{
	auto pipe = new Pipe!(string, string)(new AppendingElement("c"));
	pipe.attach(new AppendingElement("d"));
	writeln(pipe.run("foo-"));

	auto pipe2 = new Pipe!(string, string)((const string s) => s ~ "c");
	pipe2.attach((const string s, PipeElement!(string,string) n) => n(s) ~ "d");
	writeln(pipe2.run("foo-"));
}

class AppendingElement : PipeElement!(string, string), Link!(string, string) {
	private const string appending;

	public this(const string appending) {
		this.appending = appending;
	}

	public string process(const string input) {
		return input ~ appending;
	}

	public string process(const string input, PipeElement!(string, string) next) {
		return next.process(input ~ appending);
	}
}

prints

foo-dc
foo-cd

Available versions

0.9.0 ~master