tmarsteel-dpipe 0.9.0

Simple Piping / Chaining functionality


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

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
Authors:
  • tobias.marstaller
Dependencies:
none
Versions:
0.9.0 2017-Jan-26
~master 2017-Jan-26
Show all 2 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 1 downloads this month

  • 33 downloads total

Score:
0.0
Short URL:
tmarsteel-dpipe.dub.pm