Giter VIP home page Giter VIP logo

deriveables's Introduction

Deriveable types

This module implements deriveable types. The concept is simple, you supply converters to go from one or more types into another type, then you can define procedures with a nice type signature and tell the system to derive the procedure from the converters. This generates a new procedure which takes the base type, runs all the necessarry converters, and calls the initial typed procedure.

Example: In the Jester web framework you get access to the Request object in your routes. From this object you're going to extract the information about the request that you need and then perform the logic of the route. This means that routes typically contain quite a bit of parsing logic. With this library you can create procedures that only does the extraction, then procedures which only does the business logic, and simply tell this library to glue them together.

import jester, strutils, json, deriveables

type
  UserID = distinct int
  User = object
    name: string

# Define ways to get various things from a request, this would typically be
# done with parsing, verification and database access
proc getUserId(r: Request): UserId {.deriveable.} =
  return UserID(r.params["id"].parseInt)

proc getUser(u: UserID): User {.deriveable.} =
 # Do database lookup for the user ID and return a user object
 discard

# You could also attach the deriveable pragma to existing procs instead of
# the above pragma:
# deriveable(getUserId, getUser)

# Define our route bodies purely on our logical types, then derive a
# procedure from a set of given types
proc showUser(p: User): string {.derive: Request.} =
  "<html><body>This is the page for user " & p.name & "</body></html>"
  # If you want to automatically convert types to HTML via templates have
  # a look at PMunch/autotemplate

# Set up our routes, call our logical routes with the request object and
# the system will figure out how to derive the actual types
routes:
  get "/user/@id": resp showUser(request)

If a type conversion fails it will throw a DerivingError which just wraps the actual error. This means that it's easy to distinguish errors happening during the conversion and errors happening during the execution of a procedure itself.

This file is automatically generated from the documentation found in deriveables.nim. Use nim doc src/deriveables.nim to get the full documentation.

deriveables's People

Contributors

pmunch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

icodein

deriveables's Issues

Recommendation when dealing with a create request

Hey PMunch,

Thanks for taking the time to create this library and document it! One question I had from trying to play around with it is, how would you suggest using this library to handle creating an object? As an example, imagine that there was a create user request that would require you to parse a user from the Jester request object. How would you suggest doing that in a way that doesn't conflict with the getUser proc that uses the user id? If you try defining an additional proc that converts a request to a user, then you end up with it conflicting with the existing proc if I understand it correctly?

My current approach is to only use this for readonly workloads, and not for doing any writes - that logic goes in my routes, but I'm curious if you have any ideas. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.